From 6c225b53699708ec5ba01f4d32c29596aed2b36c Mon Sep 17 00:00:00 2001 From: s1341 Date: Thu, 4 Jan 2024 08:57:17 +0200 Subject: [PATCH 1/2] rangedetails: don't return dangling pointers --- frida-gum/src/memory_range.rs | 2 ++ frida-gum/src/range_details.rs | 54 +++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/frida-gum/src/memory_range.rs b/frida-gum/src/memory_range.rs index 4408613..e9e1dc8 100644 --- a/frida-gum/src/memory_range.rs +++ b/frida-gum/src/memory_range.rs @@ -43,6 +43,8 @@ pub struct ScanResult { pub address: usize, pub size: usize, } + +#[derive(Clone)] pub struct MemoryRange { pub(crate) memory_range: gum_sys::GumMemoryRange, } diff --git a/frida-gum/src/range_details.rs b/frida-gum/src/range_details.rs index e2b23b3..7e5838c 100644 --- a/frida-gum/src/range_details.rs +++ b/frida-gum/src/range_details.rs @@ -23,7 +23,7 @@ use { use alloc::boxed::Box; /// The memory protection of an unassociated page. -#[derive(FromPrimitive)] +#[derive(Clone, FromPrimitive)] #[repr(u32)] pub enum PageProtection { NoAccess = gum_sys::_GumPageProtection_GUM_PAGE_NO_ACCESS as u32, @@ -40,32 +40,43 @@ pub enum PageProtection { } /// The file association to a page. +#[derive(Clone)] pub struct FileMapping<'a> { - file_mapping: *const gum_sys::GumFileMapping, + path: &'a str, + size: u64, + offset: u64, phantom: PhantomData<&'a gum_sys::GumFileMapping>, } impl<'a> FileMapping<'a> { - pub(crate) fn from_raw(file: *const gum_sys::GumFileMapping) -> Self { - Self { - file_mapping: file, - phantom: PhantomData, + pub(crate) fn from_raw(file: *const gum_sys::GumFileMapping) -> Option { + if file.is_null() { + None + } else { + Some(unsafe { + Self { + path: CStr::from_ptr((*file).path).to_str().unwrap(), + size: (*file).size, + offset: (*file).offset, + phantom: PhantomData, + } + }) } } /// The path of the file mapping on disk. pub fn path(&self) -> &str { - unsafe { CStr::from_ptr((*self.file_mapping).path).to_str().unwrap() } + self.path } /// The offset into the file mapping. pub fn offset(&self) -> u64 { - unsafe { (*self.file_mapping).offset } + self.offset } /// The size of the mapping. pub fn size(&self) -> u64 { - unsafe { (*self.file_mapping).size as u64 } + self.size } } @@ -104,15 +115,21 @@ unsafe extern "C" fn enumerate_ranges_stub( /// Details a range of virtual memory. pub struct RangeDetails<'a> { - range_details: *const gum_sys::GumRangeDetails, + range: MemoryRange, + protection: PageProtection, + file: Option>, phantom: PhantomData<&'a gum_sys::GumRangeDetails>, } impl<'a> RangeDetails<'a> { pub(crate) fn from_raw(range_details: *const gum_sys::GumRangeDetails) -> Self { - Self { - range_details, - phantom: PhantomData, + unsafe { + Self { + range: MemoryRange::from_raw((*range_details).range), + protection: num::FromPrimitive::from_u32((*range_details).protection).unwrap(), + file: FileMapping::from_raw((*range_details).file), + phantom: PhantomData, + } } } @@ -154,21 +171,16 @@ impl<'a> RangeDetails<'a> { /// The range of memory that is detailed. pub fn memory_range(&self) -> MemoryRange { - unsafe { MemoryRange::from_raw((*self.range_details).range) } + self.range.clone() } /// The page protection of the range. pub fn protection(&self) -> PageProtection { - let protection = unsafe { (*self.range_details).protection }; - num::FromPrimitive::from_u32(protection).unwrap() + self.protection.clone() } /// The associated file mapping, if present. pub fn file_mapping(&self) -> Option { - if self.range_details.is_null() { - None - } else { - Some(unsafe { FileMapping::from_raw((*self.range_details).file) }) - } + self.file.clone() } } From 9bfa5f154be974efa5c9773c87334cb06ebb9230 Mon Sep 17 00:00:00 2001 From: s1341 Date: Thu, 4 Jan 2024 09:02:29 +0200 Subject: [PATCH 2/2] size is a usize --- frida-gum/src/range_details.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frida-gum/src/range_details.rs b/frida-gum/src/range_details.rs index 7e5838c..5fdea78 100644 --- a/frida-gum/src/range_details.rs +++ b/frida-gum/src/range_details.rs @@ -43,7 +43,7 @@ pub enum PageProtection { #[derive(Clone)] pub struct FileMapping<'a> { path: &'a str, - size: u64, + size: usize, offset: u64, phantom: PhantomData<&'a gum_sys::GumFileMapping>, } @@ -56,7 +56,7 @@ impl<'a> FileMapping<'a> { Some(unsafe { Self { path: CStr::from_ptr((*file).path).to_str().unwrap(), - size: (*file).size, + size: (*file).size as usize, offset: (*file).offset, phantom: PhantomData, } @@ -75,8 +75,8 @@ impl<'a> FileMapping<'a> { } /// The size of the mapping. - pub fn size(&self) -> u64 { - self.size + pub fn size(&self) -> usize { + self.size as usize } }