Skip to content
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

Recvmmsg can return fewer than specified number of messages #1325

Closed
gmilleramilar opened this issue Oct 21, 2020 · 2 comments
Closed

Recvmmsg can return fewer than specified number of messages #1325

gmilleramilar opened this issue Oct 21, 2020 · 2 comments

Comments

@gmilleramilar
Copy link

The recvmmsg system call can result in fewer than the specified number of message buffers being filled. For example if the system call is interrupted after some data has been received. However the implementation of nix::sys::socket::recvmmsg() does not allow for this and unconditionally attempts to process all of the message buffers, and crashes with a failed assertion.

Fortunately, I believe the fix is simple.

diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 5e5fb8d..98203bf 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -1219,7 +1219,7 @@ pub fn recvmmsg<'a, I>(
     let r = Errno::result(ret)?;

     Ok(output
-        .into_iter()
+        .into_iter().take(ret as usize)
         .zip(addresses.iter().map(|addr| unsafe{addr.assume_init()}))
         .zip(results.into_iter())
         .map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| {
philipstears added a commit to philipstears/nix that referenced this issue Nov 24, 2020
bors bot added a commit that referenced this issue Nov 28, 2020
1341: Fix recvmmsg(2) implementation r=asomers a=codeslinger

There were two problems discovered with the `recvmmsg(2)` implementation
that this changeset attempts to fix:

1. As mentioned in /issues/1325, `recvmmsg(2)` can return fewer
   messages than requested, and
2. Passing the return value of `recvmmsg(2)` as the number of bytes in
   the messages received is incorrect.

This changeset incorporates the proposed fix from /issues/1325,
as well as passing the correct value (`mmsghdr.msg_len`) for the number
of bytes in a given message.

Co-authored-by: Toby DiPasquale <[email protected]>
asomers pushed a commit to asomers/nix that referenced this issue Nov 29, 2020
There were two problems discovered with the `recvmmsg(2)` implementation
that this changeset attempts to fix:

1. As mentioned in nix-rust/issues/1325, `recvmmsg(2)` can return fewer
   messages than requested, and
2. Passing the return value of `recvmmsg(2)` as the number of bytes in
   the messages received is incorrect.

This changeset incorporates the proposed fix from nix-rust/issues/1325,
as well as passing the correct value (`mmsghdr.msg_len`) for the number
of bytes in a given message.
@codeslinger
Copy link
Contributor

Probably can close this one now, right?

@asomers
Copy link
Member

asomers commented Nov 30, 2020

Fixed by #1341

@asomers asomers closed this as completed Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants