Skip to content

sendmsg: Fix padding for ControlMessages, and zero it rather than leave it uninitialised #874

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

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 32 additions & 14 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ unsafe fn copy_bytes<'a, 'b, T: ?Sized>(src: &T, dst: &'a mut &'b mut [u8]) {
mem::swap(dst, &mut remainder);
}

/// Pad byte slice dst with `len` zeroes, and update the slice to point to
/// the remainder.
fn pad_bytes(len: usize, dst: &mut &mut [u8]) {
let mut tmpbuf = &mut [][..];
mem::swap(&mut tmpbuf, dst);
let (padding, mut remainder) = tmpbuf.split_at_mut(len);
for i in padding {
*i = 0;
}
mem::swap(dst, &mut remainder);
}

cfg_if! {
// Darwin and DragonFly BSD always align struct cmsghdr to 32-bit only.
if #[cfg(any(target_os = "dragonfly", target_os = "ios", target_os = "macos"))] {
Expand Down Expand Up @@ -559,13 +571,12 @@ impl<'a> ControlMessage<'a> {

let padlen = cmsg_align(mem::size_of_val(&cmsg)) -
mem::size_of_val(&cmsg);

let mut tmpbuf = &mut [][..];
mem::swap(&mut tmpbuf, buf);
let (_padding, mut remainder) = tmpbuf.split_at_mut(padlen);
mem::swap(buf, &mut remainder);
pad_bytes(padlen, buf);

copy_bytes(fds, buf);

let padlen = self.space() - self.len();
pad_bytes(padlen, buf);
},
ControlMessage::ScmTimestamp(t) => {
let cmsg = cmsghdr {
Expand All @@ -578,17 +589,24 @@ impl<'a> ControlMessage<'a> {

let padlen = cmsg_align(mem::size_of_val(&cmsg)) -
mem::size_of_val(&cmsg);

let mut tmpbuf = &mut [][..];
mem::swap(&mut tmpbuf, buf);
let (_padding, mut remainder) = tmpbuf.split_at_mut(padlen);
mem::swap(buf, &mut remainder);
pad_bytes(padlen, buf);

copy_bytes(t, buf);

let padlen = self.space() - self.len();
pad_bytes(padlen, buf);
},
ControlMessage::Unknown(UnknownCmsg(orig_cmsg, bytes)) => {
copy_bytes(orig_cmsg, buf);

let padlen = cmsg_align(mem::size_of_val(&orig_cmsg)) -
mem::size_of_val(&orig_cmsg);
pad_bytes(padlen, buf);

copy_bytes(bytes, buf);

let padlen = self.space() - self.len();
pad_bytes(padlen, buf);
}
}
}
Expand All @@ -601,24 +619,24 @@ impl<'a> ControlMessage<'a> {
///
/// Allocates if cmsgs is nonempty.
pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'a>], flags: MsgFlags, addr: Option<&'a SockAddr>) -> Result<usize> {
let mut len = 0;
let mut capacity = 0;
for cmsg in cmsgs {
len += cmsg.len();
capacity += cmsg.space();
}
// Note that the resulting vector claims to have length == capacity,
// so it's presently uninitialized.
let mut cmsg_buffer = unsafe {
let mut vec = Vec::<u8>::with_capacity(len);
vec.set_len(len);
let mut vec = Vec::<u8>::with_capacity(capacity);
vec.set_len(capacity);
vec
};
{
let end = unsafe{cmsg_buffer.as_ptr().offset(capacity as isize)};
let mut ptr = &mut cmsg_buffer[..];
for cmsg in cmsgs {
unsafe { cmsg.encode_into(&mut ptr) };
}
assert_eq!(ptr.as_ptr(), end);
}

let (name, namelen) = match addr {
Expand Down