Skip to content

Commit 2cef18b

Browse files
bors[bot]eaufavor
andauthored
Merge #1414
1414: Fix corrupted sendmmsg() call r=asomers a=eaufavor Before this fix, the buffer that holds cmsgs may move due to the resize() call. That causes msg_hdr pointing to invalid memory, which ends up breaking the sendmmsg() call, resulting in EINVAL. This change fixes it by avoiding re-allocating the buffers. Co-authored-by: Yuchen Wu <[email protected]>
2 parents d6d5833 + 50fc2db commit 2cef18b

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
### Fixed
1414
- Allow `sockaddr_ll` size, as reported by the Linux kernel, to be smaller then it's definition
1515
(#[1395](https://github.com/nix-rust/nix/pull/1395))
16+
- Fix spurious errors using `sendmmsg` with multiple cmsgs
17+
(#[1414](https://github.com/nix-rust/nix/pull/1414))
1618

1719
### Removed
1820

src/sys/socket/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,23 +1102,22 @@ pub fn sendmmsg<'a, I, C>(
11021102

11031103
let mut output = Vec::<libc::mmsghdr>::with_capacity(reserve_items);
11041104

1105-
let mut cmsgs_buffer = vec![0u8; 0];
1105+
let mut cmsgs_buffers = Vec::<Vec<u8>>::with_capacity(reserve_items);
11061106

11071107
for d in iter {
1108-
let cmsgs_start = cmsgs_buffer.len();
1109-
let cmsgs_required_capacity: usize = d.cmsgs.as_ref().iter().map(|c| c.space()).sum();
1110-
let cmsgs_buffer_need_capacity = cmsgs_start + cmsgs_required_capacity;
1111-
cmsgs_buffer.resize(cmsgs_buffer_need_capacity, 0);
1108+
let capacity: usize = d.cmsgs.as_ref().iter().map(|c| c.space()).sum();
1109+
let mut cmsgs_buffer = vec![0u8; capacity];
11121110

11131111
output.push(libc::mmsghdr {
11141112
msg_hdr: pack_mhdr_to_send(
1115-
&mut cmsgs_buffer[cmsgs_start..],
1113+
&mut cmsgs_buffer,
11161114
&d.iov,
11171115
&d.cmsgs,
11181116
d.addr.as_ref()
11191117
),
11201118
msg_len: 0,
11211119
});
1120+
cmsgs_buffers.push(cmsgs_buffer);
11221121
};
11231122

11241123
let ret = unsafe { libc::sendmmsg(fd, output.as_mut_ptr(), output.len() as _, flags.bits() as _) };

0 commit comments

Comments
 (0)