fix(libkernel): reject socket addresses longer than sockaddr_in or sockaddr_in6 with EINVAL #9108
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9108"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-21T12:32:14Z.
What
bindandconnectaccepted a socket address longer thansockaddr_inorsockaddr_in6. FreeBSD 11.4'sgetsockaddrsetssa_lento the length argument (sys/kern/uipc_syscalls.c), and every bind and connect path for TCP and UDP compares it with the size of the socket family's address and answersEINVALwhen they differ:tcp_usr_bind,tcp_usr_connect,tcp6_usr_bindandtcp6_usr_connect(sys/netinet/tcp_usrreq.c),in_pcbbind_setupandin_pcbconnect_setup(sys/netinet/in_pcb.c, reached fromudp_bindandudp_connect), andin6_pcbbindandin6_pcbladdr(sys/netinet6/in6_pcb.c, reached fromudp6_bindandudp6_connect).udp6_send(sys/netinet6/udp6_usrreq.c) makes the same test before anything else whensendtogives an address.bindandconnecton anAF_INETorAF_INET6socket now fail withEINVALwhen the length is above 16 or 28 bytes.connectmakes the test after the listening-socket check (EOPNOTSUPPinsoconnectatcomes first) and before the port-0 check, asin_pcbconnect_setupdoes. The one path FreeBSD leaves unchecked is kept: on anAF_INET6datagram socket withIPV6_V6ONLYclear,udp6_bindandudp6_connecthand an IPv4-mapped address to the IPv4 code as a freshsockaddr_in, so the length is not tested there.sendtoon anAF_INET6datagram socket fails a longer address withEINVALtoo, including an IPv4-mapped one.sendtoon anAF_INETdatagram socket is left alone, becauseudp_output(sys/netinet/udp_usrreq.c) tests the length only when the socket has no local address or port or the destination isINADDR_ANYorINADDR_BROADCAST. Streamsendtois also left alone: FreeBSD checks the length only during the implied connect of an unconnected socket. The debt entry forbind,connectandsendtoindocs/dev/debt/libkernel.mdnow names just these twosendtocases.Test changes:
guest_socket_address_lengthbinds and connects with every length from 17 (AF_INET) or 29 (AF_INET6) up to 255, on stream and datagram sockets, and expectsEINVAL. The same sockets still bind and connect with the exact length. On anAF_INET6datagram socket,sendtowith every longer length fails andsendtowith the exact length sends.bindwithIPV6_V6ONLYset and binds with it cleared.sendtowith that address still fails.Tested
Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release,
-DBUILD_TESTING=ON, onmain11bb53a347.guest_socket_address_lengthwith the new cases fails (exit 0xc0000409):bind with an address longer than sockaddr_in or sockaddr_in6 must fail with EINVAL (22), because a 17-byteAF_INETbind succeeds. With this PR it passes.-R "socket" -E "^agc_": 27 of 27 pass when run serially.python tools/check_conventions.py --base origin/mainreports no errors.getsockopt(IPV6_V6ONLY), which both hosts have, so Linux CI should give the same result.Overlap:
tools/pr_overlap.pyreports textual conflicts inUdp.cppwith #8499 (port-0sendtofrom an unbound IPv4 datagram socket) and #8682 (wildcard local address after a UDP association is dissolved). Both add code next to the new helper and thebindandsendtolines, and neither checks address length. #8392 (address arguments checked before the descriptor) moves the short-length,ENAMETOOLONGandEFAULTchecks ahead of the descriptor lookup and merges cleanly with this PR. The PRs it could not compare that touchUdp.cpp(#5021, #6977, #7088, #7201, #8483, #8670) do not change these checks. The rest touch only other lines ofdocs/dev/debt/libkernel.md. #9105 (connect to port 0 on a connected datagram socket) also changesconnectinUdp.cpp, and the two branches merge without conflict.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)