Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mach_vm_read_overwrite to read image infos for dyld_images #403

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions samply/src/mac/proc_maps.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::mem::MaybeUninit;
use std::ops::{Deref, Range};
use std::{mem, ptr};

Expand All @@ -21,7 +22,7 @@ use mach::thread_act::{thread_get_state, thread_resume, thread_suspend};
use mach::thread_status::thread_state_flavor_t;
use mach::thread_status::thread_state_t;
use mach::traps::mach_task_self;
use mach::vm::{mach_vm_deallocate, mach_vm_read, mach_vm_remap};
use mach::vm::{mach_vm_deallocate, mach_vm_read, mach_vm_read_overwrite, mach_vm_remap};
use mach::vm_inherit::VM_INHERIT_SHARE;
use mach::vm_page_size::{mach_vm_trunc_page, vm_page_size};
use mach::vm_prot::{vm_prot_t, VM_PROT_NONE, VM_PROT_READ};
Expand Down Expand Up @@ -158,6 +159,7 @@ impl DyldInfoManager {

let new_image_info = enumerate_dyld_images(
&mut self.memory,
self.task,
info_array_addr,
info_array_count,
dyld_image_load_addr,
Expand Down Expand Up @@ -211,6 +213,7 @@ fn with_suspended_task<T>(

fn enumerate_dyld_images(
memory: &mut ForeignMemory,
task: mach_port_t,
info_array_addr: u64,
info_array_count: u32,
dyld_image_load_addr: u64,
Expand All @@ -227,8 +230,17 @@ fn enumerate_dyld_images(
let (base_avma, image_file_path) = {
let info_array_elem_addr =
info_array_addr + image_index as u64 * mem::size_of::<dyld_image_info>() as u64;
let image_info: &dyld_image_info =
unsafe { memory.get_type_ref_at_address(info_array_elem_addr) }?;
let mut image_info: MaybeUninit<dyld_image_info> = MaybeUninit::uninit();
let mut size = mem::size_of::<dyld_image_info>() as u64;
Maaarcocr marked this conversation as resolved.
Show resolved Hide resolved
unsafe {
mach_vm_read_overwrite(task, info_array_elem_addr, size, &mut image_info as *mut MaybeUninit<dyld_image_info> as u64, &mut size).into_result()?;
Maaarcocr marked this conversation as resolved.
Show resolved Hide resolved
}

if size != mem::size_of::<dyld_image_info>() as u64 {
Maaarcocr marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check that the result of mach_vm_read_overwrite was KERN_SUCCESS, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

into_result does it

return Err(kernel_error::KernelError::InvalidValue);
}

let image_info = unsafe { image_info.assume_init() };
(
image_info.imageLoadAddress as usize as u64,
image_info.imageFilePath as usize as u64,
Expand Down
Loading