getsockname ignores caller limits and can panic on truncated output
perfloop-oss/cpp2rust · Extent mismatch
https://perfloop.ai/t/oss/case_k95q5x47pv
Verdict
VERIFIED · settled 2026-11-19 · merged as Cpp2Rust/cpp2rust#659
What happened: The check fails before this change and passes with it.
Hypothesis
Refcount getsockname can change bytes beyond the caller's advertised buffer limit, or panic when native code would return a truncated address normally. This affects valid short-buffer calls, including a zero-byte length query. Application frequency and downstream impact remain unmeasured.
Sockaddr::encode never reads the input out_len. Its IPv4 and IPv6 branches reinterpret the output as a whole SockaddrIn or SockaddrIn6 and call Ptr::write. Successful whole-value writes cover 16 or 28 bytes regardless of the advertised limit. A small physical allocation can instead panic before any write, because Ptr::write uses with_mut_reinterpreted to read the complete old value. The claim is not that every invocation writes a whole address. f12 reaches encode only after a successful native query; its syscall-error branch is separate.
The revised checkProperty(prop_gtdb5pvh0k) run at e2b679de9fa36c3b015b4604759ab0ff53003fa4 used cargo test --manifest-path libcc2rs/Cargo.toml --test perfloop_sockaddr_writeback limits_ -- --nocapture --test-threads=1. Four full-size IPv4/IPv6 byte controls passed in each of two tests. Each test then failed on its first case, an unbound IPv4 datagram socket with capacity zero. With 128 physical bytes, native left all bytes at 0xa5 while encode replaced the first 16 bytes. With one physical byte, native succeeded unchanged while encode panicked reading a 16-byte range. Both emitted replays are stored on that node. Neither 516-case grid completed. Returned lengths were deliberately excluded from the byte assertions because hyp_g17v2mnp5b owns the independent length defect.
A single bounded byte-write operation could remove the full-destination requirement across the supported families. Unix output itself is a separate question: adding its missing branch alone would not make IP truncation correct. The smallest Case check is the two saved runtime replays, followed by one native-C/refcount short-buffer fixture. Normal completion, the correct written prefix and an untouched suffix would confirm the repair. If the unchanged encoder already obeys those properties on the supported witnesses, the mechanism is rejected. Retain full-size byte controls; no performance saving or memory-unsafe Rust execution is claimed.
The upstream refresh still lists the inspected https://github.com/Cpp2Rust/cpp2rust/pull/579 and https://github.com/Cpp2Rust/cpp2rust/pull/561. Their actual implementations change descriptor/stream ownership and empty-initializer rule matching respectively, not caller-capacity handling in address encoding. Prior searches for Sockaddr, getsockname and socket writeback found no covering repair; a search miss alone is not a novelty proof.
Change to test: Read the caller's capacity before writeback and update only the allowed address prefix without requiring a complete typed destination value. Preserve the untouched suffix and report the actual full address length separately.
Where it lives
perfloop-oss/cpp2rust · rules/socket/tgt_refcount.rs
Evidence
The check fails before this change and passes with it: `On Linux, successful refcount getsockname calls on unbound IPv4/IPv6 datagram sockets and sockets bound to IPv4 loopback (IPv4 or IPv4-mapped IPv6) complete without panicking, copy the native address prefix within the caller's capacity, and preserve all remaining destination bytes, including zero-capacity and physically short buffers.`
Checks: 3 of 3 passed. Verification: no defect found.