Skip to content

Commit

Permalink
Merge pull request #103 from SubconsciousCompute/main
Browse files Browse the repository at this point in the history
fix: `use-after-free` in `try_load_macos_table` leading to segfault
  • Loading branch information
mulark committed Aug 6, 2023
2 parents 1cd9111 + daf7ead commit 57f5f5a
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/macos/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,12 @@ fn try_load_macos_entry_point() -> Result<SMBiosEntryPoint32, Error> {
));
}

if !data_ref.is_null() {
CFRelease(data_ref.as_void_ptr());
}

let data_ptr = CFDataGetBytePtr(data_ref);
let data_length = CFDataGetLength(data_ref);
let mut entry_point = Vec::with_capacity(data_length as usize);

std::ptr::copy(data_ptr, entry_point.as_mut_ptr(), data_length as usize);
entry_point.set_len(data_length as usize);
let entry_point = std::slice::from_raw_parts(data_ptr, data_length as usize).to_vec();

CFRelease(data_ref.as_void_ptr());

SMBiosEntryPoint32::try_from(entry_point)
}
Expand All @@ -102,16 +98,12 @@ fn try_load_macos_table() -> Result<Vec<u8>, Error> {
return Err(Error::new(ErrorKind::NotFound, "SMBIOS is unreachable"));
}

if !data_ref.is_null() {
CFRelease(data_ref.as_void_ptr());
}

let data_ptr = CFDataGetBytePtr(data_ref);
let data_length = CFDataGetLength(data_ref);
let mut table: Vec<u8> = Vec::with_capacity(data_length as usize);

std::ptr::copy(data_ptr, table.as_mut_ptr(), data_length as usize);
table.set_len(data_length as usize);
let table = std::slice::from_raw_parts(data_ptr, data_length as usize).to_vec();

CFRelease(data_ref.as_void_ptr());

Ok(table)
}
Expand Down

0 comments on commit 57f5f5a

Please sign in to comment.