Skip to content

Commit

Permalink
Fix user after free in MacOS select() (#359)
Browse files Browse the repository at this point in the history
Update to the loop handling large messages logic in order to not reuse the `message` after it's backing buffer was deallocated.

Signed-off-by: webbeef <[email protected]>
  • Loading branch information
webbeef authored Sep 20, 2024
1 parent 862b0e2 commit ba0e5f1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/platform/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,11 @@ fn select(
MACH_PORT_NULL,
) {
MACH_RCV_TOO_LARGE => {
let max_trailer_size =
mem::size_of::<mach_sys::mach_msg_max_trailer_t>() as mach_sys::mach_msg_size_t;
// the actual size gets written into msgh_size by the kernel
let mut actual_size = (*message).header.msgh_size + max_trailer_size;
loop {
// the actual size gets written into msgh_size by the kernel
let max_trailer_size = mem::size_of::<mach_sys::mach_msg_max_trailer_t>()
as mach_sys::mach_msg_size_t;
let actual_size = (*message).header.msgh_size + max_trailer_size;
allocated_buffer = Some(libc::malloc(actual_size as size_t));
setup_receive_buffer(
slice::from_raw_parts_mut(
Expand All @@ -748,6 +748,7 @@ fn select(
) {
MACH_MSG_SUCCESS => break,
MACH_RCV_TOO_LARGE => {
actual_size = (*message).header.msgh_size + max_trailer_size;
libc::free(allocated_buffer.unwrap() as *mut _);
continue;
},
Expand Down

0 comments on commit ba0e5f1

Please sign in to comment.