Skip to content

Commit

Permalink
Merge #1136
Browse files Browse the repository at this point in the history
1136: Properly initialize msghdr when using musl r=posborne a=yshui

Because of the use of MaybeUninit::uninit, the padding fields in msghdr,
which only present on musl builds, are not initialized. Causing garbage
data to be sent to the kernel. This change ensures the paddings are
always zeroed.

Co-authored-by: Yuxuan Shui <yshuiv7@gmail.com>
  • Loading branch information
bors[bot] and yshui committed Oct 15, 2019
2 parents a220ded + a510b47 commit 5d76d33
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -54,6 +54,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Fix length of abstract socket addresses
([#1120](https://github.com/nix-rust/nix/pull/1120))
- Fix initialization of msghdr in recvmsg/sendmsg when built with musl
([#1136](https://github.com/nix-rust/nix/pull/1136))

### Removed

Expand Down
4 changes: 2 additions & 2 deletions src/sys/socket/mod.rs
Expand Up @@ -857,7 +857,7 @@ pub fn sendmsg(fd: RawFd, iov: &[IoVec<&[u8]>], cmsgs: &[ControlMessage],
let mhdr = unsafe {
// Musl's msghdr has private fields, so this is the only way to
// initialize it.
let mut mhdr = mem::MaybeUninit::<msghdr>::uninit();
let mut mhdr = mem::MaybeUninit::<msghdr>::zeroed();
let p = mhdr.as_mut_ptr();
(*p).msg_name = name as *mut _;
(*p).msg_namelen = namelen;
Expand Down Expand Up @@ -910,7 +910,7 @@ pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
let mut mhdr = unsafe {
// Musl's msghdr has private fields, so this is the only way to
// initialize it.
let mut mhdr = mem::MaybeUninit::<msghdr>::uninit();
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_namelen = mem::size_of::<sockaddr_storage>() as socklen_t;
Expand Down

0 comments on commit 5d76d33

Please sign in to comment.