Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't deref uninit addrs in pack_mhdr_to_receive #1992

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ impl private::SockaddrLikePriv for () {
fn as_mut_ptr(&mut self) -> *mut libc::sockaddr {
ptr::null_mut()
}

fn as_mut_ptr_raw(_: *mut Self) -> *mut libc::sockaddr {
ptr::null_mut()
}
}

/// `()` can be used in place of a real Sockaddr when no address is expected,
Expand Down Expand Up @@ -1682,7 +1686,14 @@ mod private {
/// It is best to use this method only with foreign functions that do
/// not change the sockaddr type.
fn as_mut_ptr(&mut self) -> *mut libc::sockaddr {
self as *mut Self as *mut libc::sockaddr
(self as *mut Self).cast::<libc::sockaddr>()
}

fn as_mut_ptr_raw(this: *mut Self) -> *mut libc::sockaddr
where
Self: Sized,
{
this as *mut libc::sockaddr
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,9 @@ unsafe fn read_mhdr<'a, 'i, S>(
/// headers are not used
///
/// Buffers must remain valid for the whole lifetime of msghdr
///
/// `address` must be non-null, properly-aligned, and point to a space suitable
/// for an `S`. It does not have to be initialized.
unsafe fn pack_mhdr_to_receive<S>(
iov_buffer: *const IoSliceMut,
iov_buffer_len: usize,
Expand All @@ -1960,7 +1963,7 @@ unsafe fn pack_mhdr_to_receive<S>(
// initialize it.
let mut mhdr = mem::MaybeUninit::<msghdr>::zeroed();
let p = mhdr.as_mut_ptr();
(*p).msg_name = (*address).as_mut_ptr() as *mut c_void;
(*p).msg_name = S::as_mut_ptr_raw(address) as *mut c_void;
(*p).msg_namelen = S::size();
(*p).msg_iov = iov_buffer as *mut iovec;
(*p).msg_iovlen = iov_buffer_len as _;
Expand Down