From 6fca0fbc431d43ec9fcf8524d5fdf1e88a6fa44a Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Sun, 13 Aug 2023 00:53:33 +0200 Subject: [PATCH] Zero-initialize AncillaryBuf allocations (#8) CMSG_FIRSTHDR() and CMSG_NXTHDR() expect the buffer to be zero-initialized. Also assert that these functions don't return NULL --- src/ancillary.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ancillary.rs b/src/ancillary.rs index 2a183a7..826edad 100644 --- a/src/ancillary.rs +++ b/src/ancillary.rs @@ -119,14 +119,19 @@ pub fn send_ancillary( } #[cfg(not(any(target_os="illumos", target_os="solaris")))] { - let mut header = &mut*CMSG_FIRSTHDR(&mut msg); + let header_ptr = CMSG_FIRSTHDR(&mut msg); + assert!(!header_ptr.is_null(), "CMSG_FIRSTHDR returned unexpected NULL pointer"); + #[allow(unused_mut)] + let mut header = &mut*header_ptr; #[cfg(any(target_os="linux", target_os="android"))] { if let Some(creds) = creds { header.cmsg_level = SOL_SOCKET; header.cmsg_type = SCM_CREDENTIALS; header.cmsg_len = CMSG_LEN(mem::size_of_val(&creds) as u32) as ControlLen; *(CMSG_DATA(header) as *mut c_void as *mut _) = creds; - header = &mut*CMSG_NXTHDR(&mut msg, header); + let header_ptr = CMSG_NXTHDR(&mut msg, header); + assert!(!header_ptr.is_null(), "CMSG_NXTHDR returned unexpected NULL pointer"); + header = &mut*header_ptr; } } @@ -194,7 +199,7 @@ impl AncillaryBuf { bytes as usize, mem::align_of::() ).unwrap(); - alloc::alloc(layout) + alloc::alloc_zeroed(layout) }, _ => panic!("capacity is too high"), },