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

OpenBSD fix long socket addresses #118349

Closed
wants to merge 5 commits into from
Closed

Conversation

notgull
Copy link

@notgull notgull commented Nov 27, 2023

There is an OpenBSD bug where the "len" returned by functions like "getsockname" is too long and makes it so the resulting address contains zero bytes. While this isn't a problem for C code, where strings are null terminated anyways, it's a problem for Rust.

This commit fixes this issue by adding a check that truncates the address length to the first zero when OpenBSD is detected. If there are no zeroes, it just uses the original length provided by the system.

Closes #116523

@rustbot
Copy link
Collaborator

rustbot commented Nov 27, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 27, 2023
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@semarie
Copy link
Contributor

semarie commented Nov 28, 2023

@notgull could you please adjust the comment saying it is an OpenBSD bug, as it is the intented behaviour for OpenBSD ?
Thanks.

There is an OpenBSD bug where the "len" returned by functions like
"getsockname" is too long and makes it so the resulting address contains
zero bytes. While this isn't a problem for C code, where strings are
null terminated anyways, it's a problem for Rust.

This commit fixes this issue by adding a check that truncates the
address length to the first zero when OpenBSD is detected. If there are
no zeroes, it just uses the original length provided by the system.

Signed-off-by: John Nunley <dev@notgull.net>
Signed-off-by: John Nunley <dev@notgull.net>
@rust-log-analyzer

This comment has been minimized.

@joshtriplett
Copy link
Member

r? libs

.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
}

if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is now run even when len == 0. That might be the right behavior, but it seems like a change in behavior that should go to a separate PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added back the original behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the current PR actually adds back the original behavior, but thinking more on this it's not clear to me what the original intent behind placing the family check in an else was. I'm not able to find when this code was originally introduced quickly (maybe in #51553 but that seems surprising to me...), but I think my preference would be to check the family before the length (i.e., not guard it with any length comparisons). That seems like the most intuitive behavior for this function and I'd expect that it should work, if we see tests fail etc. we can revert and add some comments describing why.

(The current PR is adding a len == 0 check before returning an error is looking at the updated length after it may have been overwritten, though only in the OpenBSD case could it get overwritten to a zero I think).

library/std/src/os/unix/net/addr.rs Outdated Show resolved Hide resolved
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 18, 2024
Signed-off-by: John Nunley <dev@notgull.net>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 18, 2024
@rust-log-analyzer

This comment has been minimized.

Signed-off-by: John Nunley <dev@notgull.net>
@rust-log-analyzer

This comment has been minimized.

Signed-off-by: John Nunley <dev@notgull.net>
@Mark-Simulacrum
Copy link
Member

Please also keep commits squashed.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 24, 2024
@notgull
Copy link
Author

notgull commented Apr 10, 2024

Closing as I do not have the time to rebase this

@notgull notgull closed this Apr 10, 2024
semarie added a commit to semarie/rust that referenced this pull request Apr 11, 2024
Original diff from @notgull in rust-lang#118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and 
not the len of the content. Figure out the length for ourselves.
see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes rust-lang#116523
@semarie
Copy link
Contributor

semarie commented Apr 11, 2024

@notgull thanks for your work.
I took your changes and opened a new PR for integrating them in rust.

semarie added a commit to semarie/rust that referenced this pull request Apr 11, 2024
Original diff from @notgull in rust-lang#118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and 
not the len of the content. Figure out the length for ourselves.
see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes rust-lang#116523
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 14, 2024
…Mark-Simulacrum

OpenBSD fix long socket addresses

Original diff from `@notgull` in rust-lang#118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and  not the len of the content. Figure out the length for ourselves. see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes rust-lang#116523
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 14, 2024
…Mark-Simulacrum

OpenBSD fix long socket addresses

Original diff from ``@notgull`` in rust-lang#118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and  not the len of the content. Figure out the length for ourselves. see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes rust-lang#116523
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 14, 2024
Rollup merge of rust-lang#123779 - semarie:notgull-openbsd-socket, r=Mark-Simulacrum

OpenBSD fix long socket addresses

Original diff from ``@notgull`` in rust-lang#118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and  not the len of the content. Figure out the length for ourselves. see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes rust-lang#116523
TheBearodactyl pushed a commit to TheBearodactyl/confusing-rust that referenced this pull request Apr 15, 2024
Original diff from @notgull in rust-lang#118349, small changes from me.

on OpenBSD, getsockname(2) returns the actual size of the socket address, and 
not the len of the content. Figure out the length for ourselves.
see https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2

Fixes rust-lang#116523
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-unix Operating system: Unix-like S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenBSD socket address is padded with zeroes
6 participants