Skip to content

Commit

Permalink
OpenBSD fix long socket addresses
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
notgull committed Nov 27, 2023
1 parent ac9b308 commit d6d32c3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion library/std/src/os/unix/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ impl SocketAddr {
// When there is a datagram from unnamed unix socket
// linux returns zero bytes of address
len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
} else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
} else if cfg!(target_os = "openbsd") {
// OpenBSD has a bug where the socket name's length
// is more than what the buffer actually contains.
// Figure out the length for ourselves.
// https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
let sun_path: &[u8] = unsafe { crate::mem::transmute::<&[i8], &[u8]>(&addr.sun_path) };
len = crate::sys::memchr::memchr(0, sun_path)
.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 {
return Err(io::const_io_error!(
io::ErrorKind::InvalidInput,
"file descriptor did not correspond to a Unix socket",
Expand Down

0 comments on commit d6d32c3

Please sign in to comment.