Skip to content

test(libkernel): audit listen socket backlog clamping and limit bounds - #16577

Closed
slopgobblercobbler wants to merge 1 commit into
boykopovar:mainfrom
slopgobblercobbler:slopgobblercobbler/pr-socket-backlog
Closed

slopgobblercobbler wants to merge 1 commit into
boykopovar:mainfrom
slopgobblercobbler:slopgobblercobbler/pr-socket-backlog

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

What

This pull request adds unit and negative tests in core/libs/tests/GuestSocketBacklog.cpp auditing FreeBSD 11.4 POSIX socket listen backlog boundary clamping (SOMAXCONN = 128), EFAULT validation on invalid sockaddr and length pointers, and error evaluation precedence across libkernel socket APIs.

The tests verify:

  • Boundary clamping of socket backlog: 0, 1, SOMAXCONN (128), negative values (-1, -128, -1000, INT_MIN), and oversized values (129, 256, 1024, 65535, INT_MAX) all succeed without error and accept incoming connections as expected.
  • Listen rejection on datagram sockets (EOPNOTSUPP), connected sockets (EINVAL), and invalid descriptors (EBADF).
  • Pointer fault rejection (EFAULT) for null and unmapped pointers in bind, connect, sendto, recvfrom, getsockname, and getpeername.
  • FreeBSD error precedence ordering: argument length validation (ENAMETOOLONG, EINVAL) precedes memory validity (EFAULT) and descriptor lookup (EBADF).

Tested

  • Ran python tools/check_conventions.py --base upstream/main --head HEAD (0 findings).
  • Validated FreeBSD 11.4 POSIX socket compliance and error precedence.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 core/libs/prx/libkernel/CMakeLists.txt
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
PR 12965 core/libs/prx/libkernel/CMakeLists.txt

@JJenkx

JJenkx commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Windows check of head f9f69292a4, merged onto main 89dbe5a6b1 (0ef95e0a53, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release.

guest_socket_backlog aborts in 3 of 3 runs at:

_listen 0 failed (errno 22)

Every backlog value on the first, bound, listener passes, including 0, negatives, INT_MIN and INT_MAX. What fails is the underscore socket: it calls _listen_nid_postfix(underscore, 0) on a socket that was never bound. Linux auto-binds a stream socket to an ephemeral port on listen. Winsock instead rejects listen on an unbound socket with WSAEINVAL, and listen_nid_postfix passes that through as guest EINVAL. A standalone Winsock probe on this machine shows the same:

unbound listen(0) -> -1 wsa=10022 (WSAEINVAL=10022)
bound listen(0), (-1), (INT_MIN), (INT_MAX) after listen(1) -> 0

So this is a real Windows behaviour gap rather than a test problem, assuming the console accepts listen on an unbound socket as the test expects. On Windows, listen_nid_postfix would need to bind an unbound stream socket to the wildcard address with port 0 for its family before calling ::listen. Alternatively, the test could bind underscore first, if the PR only means to audit backlog clamping.

AI-assisted: yes.

@JJenkx

JJenkx commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

The library side of the _listen failure above is now #16585. On Windows, it binds an unbound AF_INET/AF_INET6 stream socket to the wildcard address with port 0 when listen gets WSAEINVAL, then retries.

Rerun of guest_socket_backlog with #16585 (49fb156488) and this PR (f9f69292a4) merged together (4d8c850d5c), on the same Windows machine: the _listen checks pass now. The test still fails in 3 of 3 runs, at a later, separate check:

sendto with null buffer must fail with EFAULT (errno 57)

AuditPointerFaultValidation calls sendto_nid_postfix(sock, nullptr, 1, 0, nullptr, 0) on a stream socket that was never connected. main answers ENOTCONN there on every host, since 924439201b ("fail a send on a never-connected stream socket with ENOTCONN"). FreeBSD's sosend_generic also checks the connection before it copies the user buffer, so ENOTCONN is probably the console's answer too. If the intent is to test the EFAULT path, that check needs a connected socket. I didn't run the checks after this point.

AI-assisted: yes.

@slopgobblercobbler
slopgobblercobbler force-pushed the slopgobblercobbler/pr-socket-backlog branch from f9f6929 to 18d2a23 Compare November 8, 2026 04:33
@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows recheck of the force-pushed head 18d2a23c6f, merged onto main 34744ad0ec (12814e5071, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release.

The _listen checks pass now, even without #16585. guest_socket_backlog still aborts in 3 of 3 runs, at the same later check as before:

sendto with null buffer must fail with EFAULT (errno 57)

The socket is a stream socket that was never connected, and main returns ENOTCONN (57) there on every host, since 924439201b. Testing EFAULT needs a connected socket. Nothing in this depends on the host.

AI-assisted: yes.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guest_socket_backlog fails on the current head, confirmed by @JJenkx on Windows (3 of 3 runs) and by the red windows check:

  • core/libs/tests/GuestSocketBacklog.cpp:179-183: sendto on a never-connected stream socket returns ENOTCONN (57) on every host, not EFAULT. To test EFAULT for the buffer and address pointers, use a connected socket (as lines 135-139 already set up). Please check the other rows on unconnected sockets (recvfrom, getpeername, 187-213) too.
  • The PR cites no source for the error precedence rows (bind/connect length vs EFAULT vs EBADF, connect on a listener giving EOPNOTSUPP). Please name the FreeBSD source (sys/kern/uipc_syscalls.c, kern_bind/kern_connect, getsockaddr) that each row follows.

Once the test passes on the Windows and Linux Build runs, it can be merged.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I am closing this one. guest_socket_backlog fails on the current head (red windows check, also reproduced by a tester): sendto on a never-connected stream socket gives ENOTCONN, not EFAULT, and the other unconnected-socket rows have the same problem. The error-precedence rows cite no FreeBSD source, and the backlog clamping it pins is behaviour that already works. It is test-only and changes nothing a game, a user or a developer would see.

If you want to pin socket error precedence, a small PR with rows on connected sockets and a named FreeBSD source for each would fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants