Reject out-of-range POSIX socket descriptors before socket operations can alias another socket
Current master follow-up (2026-11-12)
This update records the remaining behavior on the audited master. The historical report and earlier delivered work are retained below; current code paths are Rust rather than the retired Python helpers.
Problem
net_fd_valid and other socket paths were hardened, but net_socket_error still sends an i64 descriptor directly to a narrower POSIX descriptor operation.
Reproduction and evidence
Audited canonical master 69a67c6e0fb700f37ee6114f5a66d37ee13771a5 (0.0.1-alpha-dev). Matching checkout-built compiler/std were used for compiler probes on Linux amd64 with Rust 1.89.0 and LLVM 21.1.8. Artifact inspection, mocked/projection checks and foreign-target object emission are identified below and are not native execution on those operating systems.
import("std::net::socket_base")::{net_socket_udp_v4,net_close};
import("std::net::poll")::{net_socket_error};
fun main()->i32{
var fd:i64=net_socket_udp_v4();
if(fd<0){return 1;}
println("real={} wide={} negative={}",net_socket_error(fd),net_socket_error(fd+4294967296),net_socket_error(fd-4294967296));
net_close(fd);
return 0;
}
target/debug/wavec --std-root "$PWD/std" build repro.wave -O0 --run --out-dir out-O0
target/debug/wavec --std-root "$PWD/std" build repro.wave -O2 --run --out-dir out-O2
Linux O0/O2: real=0 wide=0 negative=0. fd+2^32 and fd-2^32 alias the live UDP socket instead of being rejected. Only a local socket and getsockopt were used; no external traffic.
Scope
Reuse the established POSIX descriptor validation before reading SO_ERROR, preserving full-width Windows SOCKET semantics.
Starting points: std/net/poll.wave:86.
Completion criteria
- [ ] Truncating-positive and truncating-negative values are rejected before the syscall.
- [ ] The valid socket and its pending error remain untouched by rejected calls.
- [ ] Valid and platform-specific error results remain unchanged.
This is a bounded newcomer task with the starting point and focused negative/positive checks above.
Related: #807, #1069. Related broad trackers or completed implementations are context, not claims that their entire scope is unfinished.
Original report (historical)
Problem
The portable socket layer accepts i64 descriptors, but net_fd_valid checks only fd >= 0. POSIX socket syscalls consume narrower descriptors, so an oversized value can operate on an unrelated live socket. The readiness-only fix #807 and descriptor byte-I/O fix #936 do not cover these socket entry points.
Starting points: socket validation and operations, socket options.
Reproduction
Audited canonical master 540728c2eb6cc12f5c69c3fb3fdb96e07ba059a4 on Linux amd64 with a checkout-built Wave 0.0.1-alpha-dev, matching checkout std, Rust 1.89.0 and LLVM 21.1.8. Executed at O0 and O2, using only a local UDP socket:
import("std::net::socket_base")::{net_socket_udp_v4,net_fd_valid,net_close};
import("std::net::socketopt")::{net_set_nonblock,net_get_nonblock};
fun main() -> i32 {
var fd: i64 = net_socket_udp_v4();
if (fd < 0) { return 1; }
var invalid: i64 = fd + 4294967296;
println("valid={} set={}", net_fd_valid(invalid), net_set_nonblock(invalid, 1));
println("real nonblock={}", net_get_nonblock(fd));
net_close(fd);
return 0;
}
target/debug/wavec --std-root "$PWD/std" build repro.wave -O0 -o repro followed by ./repro prints valid=1 set=0 and real nonblock=1. Repeat at O2. The invalid handle changed the original socket.
Scope
Validate representability before portable socket ownership, byte/vector I/O, options and shutdown/close operations reach POSIX providers. Reuse the existing descriptor-validation conventions. Do not impose an i32 range on Windows SOCKET values or change deliberately disabled entries in generic poll. This is bounded validation work; no socket API redesign is required.
Completion criteria
- [ ] Values such as
2^32 + live_fdand truncating-negative descriptors fail without changing, reading, writing or closing the live socket. - [ ] Cover ownership constructors, a nonempty I/O operation, options and close/shutdown, preserving zero-length contracts.
- [ ] Valid POSIX descriptors and full-width Windows socket handling retain their behavior; O0/O2 regressions exercise the boundary.