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

sendmsg fails with EINVAL when sending ScmRights over unconnected socket #884

Closed
swsnr opened this issue Oct 19, 2023 · 3 comments · Fixed by #885
Closed

sendmsg fails with EINVAL when sending ScmRights over unconnected socket #884

swsnr opened this issue Oct 19, 2023 · 3 comments · Fixed by #885

Comments

@swsnr
Copy link

swsnr commented Oct 19, 2023

I can send a file descriptor over a connected unix socket just fine with the following code:

let target = UnixDatagram::unbound().unwrap();
let fd = write_and_seal("rustix_send_fd_connected_socket");

target.connect(JOURNALD_PATH).unwrap();
let fds = &[fd.as_fd()];
let scm_rights = rustix::net::SendAncillaryMessage::ScmRights(fds);
let mut buffer = vec![0; scm_rights.size()];
let mut buffer = SendAncillaryBuffer::new(&mut buffer);
assert!(buffer.push(scm_rights), "Failed to push ScmRights message");
sendmsg(&target, &[], &mut buffer, SendFlags::NOSIGNAL).unwrap();

I can also send an FD over an unconnected socket with libc, see https://github.com/swsnr/rustix-sendfd/blob/86540a6d37ee8670c77f467ddde3d7430d22ccb8/src/lib.rs#L74 (too long to paste here).

However, doing that in rustix with the following code fails with called Result::unwrap() on an Err value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" } at sendmsg_unix:

let target = UnixDatagram::unbound().unwrap();
let fd = write_and_seal("rustix_send_fd_unconnected_socket");

let fds = &[fd.as_fd()];
let scm_rights = rustix::net::SendAncillaryMessage::ScmRights(fds);
let mut buffer = vec![0; scm_rights.size()];
let mut buffer = SendAncillaryBuffer::new(&mut buffer);
assert!(buffer.push(scm_rights), "Failed to push ScmRights message");
let sockaddr = SocketAddrUnix::new(JOURNALD_PATH).unwrap();
sendmsg_unix(&target, &sockaddr, &[], &mut buffer, SendFlags::NOSIGNAL).unwrap();

What did I do wrong here?

See https://github.com/swsnr/rustix-sendfd for a repo with three tests which reproduce this, and the corresponding workflow for the actual failure at https://github.com/swsnr/rustix-sendfd/actions/runs/6572703065/job/17854300669#step:4:33

sunfishcode added a commit that referenced this issue Oct 19, 2023
When encoding the address for `sendmsg_unix`, use the `unix` field of
`SocketAddrUnix`, since the `unix` field is the `sockaddr_un` that the
OS will read.

Fixes #884.
@sunfishcode
Copy link
Member

Thanks for the report! This is a rustix bug; I've now filed #885 with a fix.

sunfishcode added a commit that referenced this issue Oct 19, 2023
When encoding the address for `sendmsg_unix`, use the `unix` field of
`SocketAddrUnix`, since the `unix` field is the `sockaddr_un` that the
OS will read.

Fixes #884.
sunfishcode added a commit that referenced this issue Oct 19, 2023
When encoding the address for `sendmsg_unix`, use the `unix` field of
`SocketAddrUnix`, since the `unix` field is the `sockaddr_un` that the
OS will read.

Fixes #884.
sunfishcode added a commit that referenced this issue Oct 19, 2023
When encoding the address for `sendmsg_unix`, use the `unix` field of
`SocketAddrUnix`, since the `unix` field is the `sockaddr_un` that the
OS will read.

Fixes #884.
@swsnr
Copy link
Author

swsnr commented Oct 19, 2023

Thank you very much 🙏

sunfishcode added a commit that referenced this issue Oct 19, 2023
When encoding the address for `sendmsg_unix`, use the `unix` field of
`SocketAddrUnix`, since the `unix` field is the `sockaddr_un` that the
OS will read.

Fixes #884.
sunfishcode added a commit that referenced this issue Oct 19, 2023
When encoding the address for `sendmsg_unix`, use the `unix` field of
`SocketAddrUnix`, since the `unix` field is the `sockaddr_un` that the
OS will read.

Fixes #884.
@sunfishcode
Copy link
Member

This is now released in rustix 0.38.20.

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

Successfully merging a pull request may close this issue.

2 participants