| 1 |
// |
| 2 |
// Look for a value in the virtual memory of a process, and physical memory, then prints memory page details |
| 3 |
// This shows how to go from virtual address to mapping, and from mapping to physical address. |
| 4 |
// |
| 5 |
// This requires CAP_SYS_ADMIN privilege, or root |
| 6 |
// |
| 7 |
// Sample output: |
| 8 |
// |
| 9 |
// Virtual address of `variable`: 0x7ffd2de4708f |
| 10 |
// Found memory mapping |
| 11 |
// MemoryMap { address: (140725373272064, 140725373407232), perms: "rw-p", offset: 0, dev: (0, 0), inode: 0, pathname: Stack } |
| 12 |
// Found page |
| 13 |
// virt_mem: 0x7ffd2de47000, pfn: 0x107b06, phys_addr: 0x107b06000, flags: UPTODATE | LRU | MMAP | ANON | SWAPBACKED |
| 14 |
// |
| 15 |
|
| 16 |
use procfs::process::Process; |
| 17 |
use procfs::KPageFlags; |
| 18 |
|
| 19 |
fn main() { |
| 20 |
if !rustix::process::geteuid().is_root() { |
| 21 |
// KpageFlags::new().unwrap() will panic either way |
| 22 |
panic!("ERROR: Access to /proc/kpageflags requires root, re-run with sudo"); |
| 23 |
} |
| 24 |
|
| 25 |
let page_size = procfs::page_size(); |
| 26 |
|
| 27 |
// We will inspect this process's own memory |
| 28 |
let process = Process::myself().expect("Unable to load myself!"); |
| 29 |
let mut kpageflags = KPageFlags::new().expect("Can't open /proc/kpageflags"); |
| 30 |
|
| 31 |
let mut pagemap = process.pagemap().unwrap(); |
| 32 |
|
| 33 |
// The memory maps are read now, so the value we look for must already exist in RAM when we it this line |
| 34 |
// In this case it works, because the variables already exist in the executable |
| 35 |
// You probably want to put this right above the "for memory_map" loop |
| 36 |
let mem_map = process.maps().unwrap(); |
| 37 |
|
| 38 |
// We allocate memory for a value. This is a trick to get a semi random value |
| 39 |
// The goal is to find this value in physical memory |
| 40 |
let chrono = std::time::Instant::now(); |
| 41 |
let variable: u8 = chrono.elapsed().as_nanos() as u8; |
| 42 |
|
| 43 |
// We could do the same with a constant, the compiler will place this value in a different memory mapping with different properties |
| 44 |
//let constant = 42u8; |
| 45 |
|
| 46 |
// `ptr` is the virtual address we are looking for |
| 47 |
let ptr = &variable as *const u8; |
| 48 |
println!("Virtual address of `variable`: {:p}", ptr); |
| 49 |
|
| 50 |
for memory_map in mem_map { |
| 51 |
let mem_start = memory_map.address.0; |
| 52 |
let mem_end = memory_map.address.1; |
| 53 |
|
| 54 |
if (ptr as u64) < mem_start || (ptr as u64) >= mem_end { |
| 55 |
// pointer is not in this memory mapping |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
// found the memory mapping where the value is stored |
| 60 |
println!("Found memory mapping\n{:?}", memory_map); |
| 61 |
|
| 62 |
// memory is split into pages (usually 4 kiB) |
| 63 |
let index_start = (mem_start / page_size) as usize; |
| 64 |
let index_end = (mem_end / page_size) as usize; |
| 65 |
|
| 66 |
for index in index_start..index_end { |
| 67 |
// we search for the exact page inside the memory mapping |
| 68 |
let virt_mem = index * page_size as usize; |
| 69 |
|
| 70 |
// ptr must be reside between this page and the next one |
| 71 |
if (ptr as usize) < virt_mem || (ptr as usize) >= virt_mem + page_size as usize { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
// we found the exact page where the value resides |
| 76 |
let page_info = pagemap.get_info(index).unwrap(); |
| 77 |
match page_info { |
| 78 |
procfs::process::PageInfo::MemoryPage(memory_page) => { |
| 79 |
let pfn = memory_page.get_page_frame_number(); |
| 80 |
let phys_addr = pfn.0 * page_size; |
| 81 |
|
| 82 |
let physical_page_info = kpageflags.get_info(pfn).expect("Can't get kpageflags info"); |
| 83 |
|
| 84 |
println!( |
| 85 |
"Found page\nvirt_mem: 0x{:x}, pfn: 0x{:x}, phys_addr: 0x{:x}, flags: {:?}", |
| 86 |
virt_mem, pfn, phys_addr, physical_page_info |
| 87 |
); |
| 88 |
} |
| 89 |
procfs::process::PageInfo::SwapPage(swap_page_flags) => { |
| 90 |
let swap_type = swap_page_flags.get_swap_type(); |
| 91 |
let swap_offset = swap_page_flags.get_swap_offset(); |
| 92 |
|
| 93 |
println!( |
| 94 |
"Found page\nvirt_mem: 0x{:x}, swap_type: {:}, swap_offset: 0x{:x}, flags: {:?}", |
| 95 |
virt_mem, swap_type, swap_offset, swap_page_flags |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |