Repository navigation
Report actual IP socket address lengths - #666
Conversation
| } | ||
| None => { | ||
| if !a4.is_null() { | ||
| a5.write(0); |
There was a problem hiding this comment.
this store doesn't seem necessary according to the spec
There was a problem hiding this comment.
The zero store is unnecessary under POSIX when no source address is provided, so it is removed along with the fixture's length/family assertion for an unnamed sender. Named-source length checks are unchanged.
| _ => {} | ||
| } | ||
| out_len.write(ss.len()); | ||
| _ => ss.len(), |
There was a problem hiding this comment.
shouldn't we panic here rather than accepting silently?
There was a problem hiding this comment.
Unsupported address families now panic instead of reporting a length without encoding an address. The supported IP and Unix branches are unchanged by this revision.
| }); | ||
| } | ||
| } | ||
| ss.len() |
There was a problem hiding this comment.
should return len here, I believe
There was a problem hiding this comment.
The local len is the truncated copy count, not the address length. POSIX says, “The address_len argument points to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the socket address.” The native C checks in tests/unit/socket_unix_name.c pass with zero and short buffers while requiring the full returned length, so ss.len() is retained.
There was a problem hiding this comment.
But then it should be something like ::std::mem::size_of_val(&l) as u32. I think ss.len() doesn't make sense
There was a problem hiding this comment.
On Linux, native and translated return 19 for the named socket and 2 for the unnamed one; size_of_val(&l) gives 8 because l is a reference, while size_of::<sockaddr_un>() gives 110. POSIX says, “The address_len argument points to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the socket address.” The updated tests/unit/socket_unix_name.c checks portable named and unnamed lengths and prints them for native-versus-translated comparison; do you still want size_of_val?
9040656 to
fe9b526
Compare
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
fe9b526 to
f8f307d
Compare
Summary
On Linux, successful refcount IP socket queries report the storage container's size instead of the address length. This gives callers an incorrect length even with a full-sized output buffer.
Derive the length from the concrete IPv4 or IPv6 libc value already being encoded. The shared writeback reports 16 or 28 bytes for
getsockname,getpeernameandrecvfrom, independently of caller capacity. Address bytes and variable Unix lengths are unchanged. Unsupported address families now panic instead of silently reporting a length. The shim length change has no effect on macOS, because nix already readssa_lenthere.When no sender address is available, address outputs remain unspecified. At the base, a NULL source address aborted translated programs (on Linux for IP and named Unix senders; on macOS also for unnamed Unix senders). It now returns normally and leaves an unused length untouched.
This builds on #659: its native-sized serialization buffers and bounded IP copies in
rules/socket/shim.rsare preserved.Testing
Linux Release and Debug socket lit runs each pass 3 tests. Compile-only lit runs of the 2 new fixtures pass in both builds, and both generated refcount programs pass execution in each build. Native/unsafe validation of the two new fixtures remains for upstream CI. macOS is covered by the upstream
run-tests.ymlmatrix.Clang-format, Rust formatting and clippy checks pass.
Test results and fixture coverage
The executed lit tests are
tests/unit/socket_ip_name.c,tests/unit/socket_unix_name.c(now checking portable named and unnamed lengths and printing them for native-versus-translated comparison) andtests/unit/socket_transparent_union.c.tests/unit/socket_ip_addrlen.candtests/unit/socket_recvfrom_name.cfail during native execution before the translated programs run. Their complete generated refcount programs are executed separately.tests/unit/socket_ip_addrlen.ccovers IPv4 lengths, IPv6 getsockname length, zero-capacity IPv4 name queries, IPv4 peer and datagram-source bytes, and NULL source addresses with both NULL and non-NULL lengths.tests/unit/socket_recvfrom_name.ccovers named and unnamed Unix senders, including NULL source addresses with both NULL and non-NULL lengths. Both generated expectations are included. The existingtests/unit/socket_ip_name.cexercises #659's short-buffer copies alongside the existing Unix and transparent-union socket tests.To ask for a change, comment or review in plain words.
Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.