Skip to content

Reject null bytes in Unix socket pathnames - #682

Open
omrzk wants to merge 2 commits into
rust-lang:masterfrom
omrzk:fix/unix-address-conversion-681
Open

omrzk wants to merge 2 commits into
rust-lang:masterfrom
omrzk:fix/unix-address-conversion-681

Conversation

@omrzk

@omrzk omrzk commented Nov 2, 2026 •

Copy link
Copy Markdown

SockAddr::unix accepts pathname bytes containing NUL, which operating systems can truncate and the standard library rejects. Reject those pathnames with InvalidInput in both constructor backends. Preserve the existing leading-NUL address behavior, including Linux abstract names containing additional zero bytes.

Retain a fallible as_unix conversion for addresses initialized from raw storage: a full-size, null-terminated sockaddr_un can still include padding NULs in the pathname exposed by as_pathname. The new raw-storage fixture covers that conversion boundary.

Fixes #681.

Validation: constructor rejection fails on the previous PR head and passes after the change; ordinary/non-UTF-8 paths, empty/unnamed addresses and abstract names are preserved. Default and all-feature suites pass in debug and release (11 unit tests; 47 default or 82 all-feature integration tests; 7 or 8 doc tests). Formatting, all-target/all-feature Clippy with warnings denied, docs and all-target/all-feature Windows MSVC and FreeBSD compile checks pass. Runtime tests were run on Linux; the Windows/FreeBSD checks were compilation only.

@Thomasdezeeuw

Copy link
Copy Markdown
Contributor

I don't think is the right approach, we should look into disallowing the creation of address with internal null bytes. Most OS, being C based, don't deal well with internal null bytes.

@omrzk omrzk changed the title Avoid panicking when a Unix address cannot convert to std Reject null bytes in Unix socket pathnames Nov 2, 2026
@omrzk

omrzk commented Nov 2, 2026

Copy link
Copy Markdown
Author

Updated the existing branch to reject NUL bytes in pathname addresses in both constructor backends. Leading-NUL addresses retain their existing behavior, including interior zeros in Linux abstract names.

I kept the fallible as_unix conversion because raw initializers can still provide a full-size null-terminated sockaddr_un whose padding is included by as_pathname; a separate raw-storage regression covers that case. The constructor regression fails on the previous PR head and passes with this change. Final test and cross-compilation results are recorded in the updated description.

Comment thread src/sys/unix.rs
Comment on lines 969 to +974
/// Returns this address as Unix `SocketAddr` if it is an `AF_UNIX` pathname
/// address, otherwise returns `None`.
/// address that can be represented by the standard library, otherwise returns
/// `None`.
pub fn as_unix(&self) -> Option<std::os::unix::net::SocketAddr> {
let path = self.as_pathname()?;
// SAFETY: we can represent this as a valid pathname, then so can the
// standard library.
Some(std::os::unix::net::SocketAddr::from_pathname(path).unwrap())
std::os::unix::net::SocketAddr::from_pathname(path).ok()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see where you changed whether this conversion can fail.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The return type was already Option; the change is replacing Some(SocketAddr::from_pathname(path).unwrap()) with SocketAddr::from_pathname(path).ok(). A standard-library rejection now returns None instead of panicking.

The constructor checks cover normal pathname creation. socket_address_unix_padded_raw_pathname covers the remaining raw-storage path: it supplies a full-size, NUL-padded sockaddr_un, checks that as_pathname() still exposes the padding, and verifies that as_unix() returns None. That is why I kept the fallible conversion after adding constructor validation.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

SockAddr::as_unix panics on a path with an interior null byte

3 participants