Skip to content

Commit ba0e5f1

Browse files
authored
Fix user after free in MacOS select() (#359)
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 <me@webbeef.org>
1 parent 862b0e2 commit ba0e5f1

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/platform/macos/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,11 +723,11 @@ fn select(
723723
MACH_PORT_NULL,
724724
) {
725725
MACH_RCV_TOO_LARGE => {
726+
let max_trailer_size =
727+
mem::size_of::<mach_sys::mach_msg_max_trailer_t>() as mach_sys::mach_msg_size_t;
728+
// the actual size gets written into msgh_size by the kernel
729+
let mut actual_size = (*message).header.msgh_size + max_trailer_size;
726730
loop {
727-
// the actual size gets written into msgh_size by the kernel
728-
let max_trailer_size = mem::size_of::<mach_sys::mach_msg_max_trailer_t>()
729-
as mach_sys::mach_msg_size_t;
730-
let actual_size = (*message).header.msgh_size + max_trailer_size;
731731
allocated_buffer = Some(libc::malloc(actual_size as size_t));
732732
setup_receive_buffer(
733733
slice::from_raw_parts_mut(
@@ -748,6 +748,7 @@ fn select(
748748
) {
749749
MACH_MSG_SUCCESS => break,
750750
MACH_RCV_TOO_LARGE => {
751+
actual_size = (*message).header.msgh_size + max_trailer_size;
751752
libc::free(allocated_buffer.unwrap() as *mut _);
752753
continue;
753754
},

0 commit comments

Comments
 (0)