summaryrefslogtreecommitdiff
path: root/src/sys/socket/mod.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-04-08 03:32:06 +0000
committerGitHub <noreply@github.com>2021-04-08 03:32:06 +0000
commit2cef18bcf1dffe1662c62424543a348ddbd839ea (patch)
tree7b13fa4d35fa94b73b959fb6cec441de152bc731 /src/sys/socket/mod.rs
parentd6d58338869326420b30aac16efc3b53a8375d8e (diff)
parent50fc2dbdfac25b228d7f7907a5ef471ac226b190 (diff)
downloadnix-2cef18bcf1dffe1662c62424543a348ddbd839ea.zip
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 <yuchen@cloudflare.com>
Diffstat (limited to 'src/sys/socket/mod.rs')
-rw-r--r--src/sys/socket/mod.rs11
1 files changed, 5 insertions, 6 deletions
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 4301ee1f..726ff8e1 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -1102,23 +1102,22 @@ pub fn sendmmsg<'a, I, C>(
let mut output = Vec::<libc::mmsghdr>::with_capacity(reserve_items);
- let mut cmsgs_buffer = vec![0u8; 0];
+ let mut cmsgs_buffers = Vec::<Vec<u8>>::with_capacity(reserve_items);
for d in iter {
- let cmsgs_start = cmsgs_buffer.len();
- let cmsgs_required_capacity: usize = d.cmsgs.as_ref().iter().map(|c| c.space()).sum();
- let cmsgs_buffer_need_capacity = cmsgs_start + cmsgs_required_capacity;
- cmsgs_buffer.resize(cmsgs_buffer_need_capacity, 0);
+ let capacity: usize = d.cmsgs.as_ref().iter().map(|c| c.space()).sum();
+ let mut cmsgs_buffer = vec![0u8; capacity];
output.push(libc::mmsghdr {
msg_hdr: pack_mhdr_to_send(
- &mut cmsgs_buffer[cmsgs_start..],
+ &mut cmsgs_buffer,
&d.iov,
&d.cmsgs,
d.addr.as_ref()
),
msg_len: 0,
});
+ cmsgs_buffers.push(cmsgs_buffer);
};
let ret = unsafe { libc::sendmmsg(fd, output.as_mut_ptr(), output.len() as _, flags.bits() as _) };