Skip to content

Commit

Permalink
Clear any messages from RecvAncillaryBuffer at start of recv
Browse files Browse the repository at this point in the history
This should prevent leaks of file descriptors if the `RecvAncillaryBuffer`
is re-used for multiple reads.
  • Loading branch information
ids1024 committed May 24, 2023
1 parent ae94409 commit 68717e5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/backend/libc/net/msghdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub(crate) fn with_recv_msghdr<R>(
control: &mut RecvAncillaryBuffer<'_>,
f: impl FnOnce(&mut c::msghdr) -> io::Result<R>,
) -> io::Result<R> {
control.clear();

let namelen = size_of::<c::sockaddr_storage>() as c::socklen_t;
let mut msghdr = {
let mut h: c::msghdr = unsafe { zeroed() };
Expand Down
2 changes: 2 additions & 0 deletions src/backend/linux_raw/net/msghdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub(crate) fn with_recv_msghdr<R>(
control: &mut RecvAncillaryBuffer<'_>,
f: impl FnOnce(&mut c::msghdr) -> io::Result<R>,
) -> io::Result<R> {
control.clear();

let namelen = size_of::<c::sockaddr_storage>() as c::c_int;
let mut msghdr = c::msghdr {
msg_name: name.as_mut_ptr().cast(),
Expand Down
7 changes: 6 additions & 1 deletion src/net/send_recv/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ impl<'buf> RecvAncillaryBuffer<'buf> {
self.read = 0;
}

/// Delete all messages from the buffer.
pub(crate) fn clear(&mut self) {
self.drain().for_each(drop);
}

/// Drain all messages from the buffer.
pub fn drain(&mut self) -> AncillaryDrain<'_> {
AncillaryDrain {
Expand All @@ -260,7 +265,7 @@ impl<'buf> RecvAncillaryBuffer<'buf> {

impl Drop for RecvAncillaryBuffer<'_> {
fn drop(&mut self) {
self.drain().for_each(drop);
self.clear();
}
}

Expand Down

0 comments on commit 68717e5

Please sign in to comment.