Repository navigation
Fix the maximum path length allowable on MacOS, NetBSD, and DragonFlyBSD for Unix Domain Sockets - #161149
Fix the maximum path length allowable on MacOS, NetBSD, and DragonFlyBSD for Unix Domain Sockets#161149asder8215 wants to merge 7 commits into
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
93ceaa8 to
6cb2f0b
Compare
This comment has been minimized.
This comment has been minimized.
6cb2f0b to
cca95af
Compare
This comment has been minimized.
This comment has been minimized.
cca95af to
77555af
Compare
There was a problem hiding this comment.
I'm wondering if we can add some unit tests (not sure we actually would run those in CI, but better than nothing) that assert we got the right value? E.g., we would pass paths with the expected max length + 1 character + null byte and confirm the kernel used the path we expected, without the extra character. For platforms with a mandatory null byte we should be able to confirm in unit tests that the kernel indeed expects it, I think?
77555af to
aca8c5d
Compare
|
@Mark-Simulacrum I added a test based around using This should confirm whether the kernel |
aca8c5d to
5569949
Compare
|
@rustbot ready |
|
Can you reply to my comments in the GitHub UI? I think you addressed them only partially with the push? @bors try jobs=various,distbsd,aarch64-apple-1,dist--apple |
This comment has been minimized.
This comment has been minimized.
Fix the maximum path length allowable on MacOS, NetBSD, and DragonFlyBSD for Unix Domain Sockets try-job: *various* try-job: dist*bsd try-job: aarch64-apple-1 try-job: dist-*-apple
Are you talking about the commit message? Or the review comments? I can update the commit message for former, but the latter on my end it shows that I've replied to them all. |
|
#161149 (comment) seems to be without reply. #161149 (comment) also looks partially unaddressed. In order to properly review and update the code here, building a mental model for what the limits are and how platforms behave is useful. Right now the pieces I've pulled from your comments are (some of these are maybe incorrect):
But that isn't reflected coherently in a single place in the code or in the PR description as far as I can tell. The newly added comment on the max path length partially reflects this... but it's missing discussion of why the +1 and whether null bytes matter or not. The gap on the static assert (that maybe will fail CI, not sure if we have that target in the runners I launched?) touches on that as well. It sounds to me like if we want to be as close to the limits as possible, we need to have a constant or function that goes through the checks needed -- I think this gets part or all of the way there from an implementation perspective, but it's just as important to build that clear narrative as getting the raw behavior right. |
|
Since this was mostly reviewed by Trevor already, I hope you don't mind me reassigning this PR to him. r? @tgross35 |
|
|
This comment has been minimized.
This comment has been minimized.
fcb1b8a to
6912b12
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
6912b12 to
0063fe1
Compare
This comment has been minimized.
This comment has been minimized.
|
Hi @xingxue-ibm and @psumbera. I was wondering if either of you know about the max length of a unix domain socket address path that can be contained within AIX/Solaris' Context is that it looks like MacOS/NetBSD/DragonFlyBSD supports binding/connecting to a socket with up to 253 valid characters. Their |
|
For Solaris, I can confirm that 1023 bytes is the maximum pathname length, excluding the terminating NUL. I checked the current Solaris 11.4 ON sources: although So Rust can use a Solaris buffer of |
|
@psumbera I was taking a look at the link associated with if (so->so_family == AF_UNIX) {
so->so_mode |= SM_FDPASSING | SM_OPTDATA;
if (sti->sti_addr_size == -1) {
/* MAXPATHLEN + soun_family + nul termination */
sti->sti_addr_size = (t_scalar_t)(MAXPATHLEN +
sizeof (short) + 1);
}This would be 1024 + 2 + 1 = 1027, right? If the |
| // 1023 bytes with a max path component byte of 255. | ||
| #[cfg(any(target_os = "illumos", target_os = "solaris"))] | ||
| #[test] | ||
| fn test_solaris_datagram_max_path() { |
There was a problem hiding this comment.
@tgross35 whenever you can, could you run bors try on this Illumos/Solaris test to verify that this test passes?
This comment has been minimized.
This comment has been minimized.
…BSD for Unix Domain sockets
…ddr::default, SocketAddr::set_path, SocketAddr:set_len, SocketAddr::sun_family_from_addr), use safer methods (e.g. copy_from_slice over ptr::copy_nonoverlapping), and updated a few tests in unix/net/tests
…_with_ancillary_from
… API to utilize SockaddrBuf as a medium to pass as a mutable or const input to C socket interop calls
…ask construction and access to certain fields of the sockaddr_un through SockaddrBuf::abstract_path and SockaddrBuf::set_abstract_path
…mentations as well as minor changes or refactoring unix/net/listener.rs + unix/net/ancillar.rs
3320bf2 to
efacabd
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
…anch containing the correct max socket address size/path length On Solaris/Illumos, their Unix Domain sockets are allowed to be binded/connected to a path of up to PATH_MAX - 1 non-nul bytes. A caveat to keep in mind is that the max length of a path component on these platforms is still 255 non-nul bytes; a separate test has been made exclusively for Solaris/Illumos to test the succesful binding of a socket to a PATH_MAX - 1 path length
efacabd to
b7b9b71
Compare
On AIX, the maximum Unix domain socket pathname length is 1023 bytes including the terminating NUL byte, matching In the Rust |
|
Okay, so I think this refactor/redesign for @tgross35 lmk if there's anything else I should refactor or change here. Also, I'm not sure if this PR requires a libs discussion/nominated label since it does change behavior for MacOS/NetBSD/DragonFlyBSD/Solaris, so just in case: @rustbot label +I-libs-nominated |
View all comments
This PR fixes the issue described in #160684. In doing so, it also refactors the
SockAddrAPI to support a max-sizedsockaddr_unstruct in a unix platform-agnostic way.For example, on MacOS, NetBSD, and DragonFlyBSD the maximum path length for a Unix Domain sockets on those platforms is actually 253 bytes. We can't rely on the length of the
sun_pathfield inlibc::sockaddr_un:As it's acknowledged within each of the aforementioned BSD platform that the
104bytes number is a gag number and people can actually malloc a larger size thansizeof(libc::sockaddr_un)with a path name up to 253 bytes (excluding nul). I assume thesun_pathfield is more akin to a flexible array member in C.To clarify, the max valid characters a socket path can have on certain unix-based platforms are as follows:
sun_pathis 104 bytes, including +1 for the nul byte)sun_patharray inlibc::sockaddr_un)I was also looking at this hacker news forum as well, and according to the user wahern, the
bind/connectmax path for Unix domain sockets on Solaris/AIX is 1023. Currently, we received confirmation from @psumbera here that Solaris/Illumos does accept a path of up to 1023 non-nul bytes for Unix Domain sockets. Unfortunately, I couldn't confirm this behavior for AIX because the source code is closed-source and I don't have access to these OSes. If someone has access to one of these OSes and can confirm this, that could be a future PR to adjust the Path limits for AIX.All other platforms still considers their maximum socket path size based on the prescribed length of the
sun_pathfield inlibc::sockaddr_un.Implementation Details
Since we can't use
libc::sockaddr_unto reliably contain the max socket path for Darwin, NetBSD, and DragonFlyBSD, this PR changes from usinglibc::sockaddr_unto a constant-sized zeroed buffer that captures the max size of a Unix domain socket (what their maximum 'sockaddr_un' could be) on each platform (i.e[0; SOCK_MAX_SIZE]).To clarify some things about
SOCK_MAX_SIZE, which is defined as follows:*Note:
libc::SOCK_MAXADDRLENis 255 on MacOS/DragonFly, it includes a byte for thesun_len, a byte forsun_family, and 253 bytes forsun_path.We add +1 for the maximum size of a socket struct in MacOS, DragonFlyBSD, and NetBSD just as insurance to include the nul byte when we're given a maximum valid path size of 253 bytes. While the length of the path is technically encoded into the
sun_lenof thesockaddr_unstruct in BSD-based platforms (where they can subtract the valuesun_lenholds by the offset tosun_path), not all the BSD platforms that allow a path up to 253 bytes may approach copying or accessing the path in this manner, and could check for if the nul byte exists at the end ofsun_path/provided path. Platforms that utilizelibc::sockaddr_unalready account for the nul byte within the prescribed length of theirsun_pathfield, so that furthers the point that we want make sure a nul byte exists even when we reach the max 253 bytes for our path.On a separate note, one difference to point out between the definition of
libc::sockaddr_unstruct in BSD-based platforms and Linux is that Linux'slibc::sockaddr_undoes not have asun_len, it only contains au16sun_family, and constant sizedsun_pathfield. Since this wasn't done prior to this PR, we ensure that the area that should contain thesun_lenfield contains the size of the entire socket (i.e.sun_len,sun_family, andsun_path). This is in consideration without the trailing nul byte from the path field, which makes sense because if our socket struct has a path of 253 bytes, it is impossible forsun_lento account for that as it is limited between 0-255 since it is au8field.Our
SocketAddrAPI now looks as follows (credit to @tgross35 for most of this API design):SockaddrBufacts as an internal helper struct that has its own API to access + mutate itslenfield, implicitsun_pathandsun_familyfields within the buffer, and is able to pass itself as mutable or const inputs to libc syscalls withinUnixDatagram,UnixListener,UnixStream, and a couple of the socket ancillary functions. The whole goal with theSockaddrBufAPI is to mask the[0; SOCK_MAX_SIZE]buffer field and have other parts of Rust's Unix Domain socket library utilize methods that returns reasonable/understandable outputs or it's clear to know what you're modifying (e.g.SockaddrBuf::set_pathto set thesun_pathfield of the[0; SOCK_MAX_SIZE]buffer).Only one new function is introduced on
SockAddr, which isSockAddr::from_path; this function does essentially the same thing aspub(super) fn sockaddr_unthat we had before (the error checking and construction ofSockAddris done inSockAddr::from_path, but the mutation of thesun_pathfield in[0; SOCK_MAX_SIZE]is done throughSockaddrBuf). A lot of other refactoring with methods and functions inSockAddrhas been done to keep everything done mostly throughSockaddrBuf. The internalSockAddr::from_partsfunction was removed since it wasn't necessary with being able to create a default blankSockaddrBufstruct and being able to modify that struct through theSockaddrBuf's API.Other changes made
long_pathupdated to also show that on MacOS/DragonFlyBSD/NetBSD would trigger theInvalidInputcase from giving it aPathlonger than 253 bytes. Previously, this test was failing on apple platforms because the long path provided was a fixed string with 108 characters and that was never going to trigger theInvalidInputcase on apple platforms. The os error it returned was that socket path did not exist (as the test does not create the socket viabind), not that the file path was too long.test_unix_datagram_max_paththat confirms on all platforms that it'll take a path with the maximum number of valid character (e.g. 107 on Linux, 103 on OpenBSD/FreeBSD, and 253 on DragonFlyBSD/Darwin/NetBSD)Unix*::bind/Unix*::connect/Unix*::peer_addr/Unix*::local_addr/UnixDatagram::send_to, etc. to adjust toSocketaddrBuf's API.sock_addr_without_trailing_nulandsock_addr_pathname_fills_sun_pathtests. It was there in place to test for behavior inSockAddr::from_part, but now thatSockAddr::from_parthas been removed and a good chunk ofSockAddr's API revolves aroundSockAddrBuf's API, which is tested through the current tests we have, these tests are no longer necessary.A couple other resources that substantiate information about the maximum unix domain socket address path is 253 bytes:
cc @tgross35