fix(libkernel): check socket address arguments before the descriptor #8392

Merged
xXPBSXx merged 1 commit from upstream-pr/8392 into main 2026-10-21 18:05:37 +00:00
xXPBSXx commented 2026-10-20 12:03:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T12:03:52Z.


What

AddressLengthError (the length and null checks of Address(), i.e. FreeBSD getsockaddr) now runs before Lookup() in bind, connect and sendto (only when an address is given). Valid arguments on a closed descriptor still fail with EBADF; the family and short-AF_INET checks stay after the descriptor lookup.
Fixes #8391
Follow-up to #8154 (socket address length). setsockopt is not changed here; the pointer and length checks of setsockopt/getsockopt are in #7511. git merge-tree against the open socket PRs #7490, #7850, #7475 and #8312 is clean; pr_overlap was not run.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main d50ec76da0 + this commit: guest_socket_address_length fails without the change to Udp.cpp (bind(-1) with a 1-byte address answers 9) and passes with it; 28 other socket and sceNet tests pass (29 of 29); python tools/check_conventions.py --base upstream/main passes. Not tested: Linux/macOS, a console, a title run.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935438330 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T12:03:52Z. --- ### What `AddressLengthError` (the length and null checks of `Address()`, i.e. FreeBSD `getsockaddr`) now runs before `Lookup()` in `bind`, `connect` and `sendto` (only when an address is given). Valid arguments on a closed descriptor still fail with EBADF; the family and short-AF_INET checks stay after the descriptor lookup. Fixes #8391 Follow-up to #8154 (socket address length). `setsockopt` is not changed here; the pointer and length checks of `setsockopt`/`getsockopt` are in #7511. `git merge-tree` against the open socket PRs #7490, #7850, #7475 and #8312 is clean; `pr_overlap` was not run. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main d50ec76da0 + this commit: `guest_socket_address_length` fails without the change to `Udp.cpp` (`bind(-1)` with a 1-byte address answers 9) and passes with it; 28 other socket and sceNet tests pass (29 of 29); `python tools/check_conventions.py --base upstream/main` passes. Not tested: Linux/macOS, a console, a title run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 12:17:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:17:06Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 9169 core/libs/tests/GuestSocketAddressLength.cpp L58, L70

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 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290641478 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392#issuecomment-6290641478). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T12:17:06Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 9169](https://www.github.com/boykopovar/AnyPS5/pull/9169) | `core/libs/tests/GuestSocketAddressLength.cpp` [L58](https://github.com/boykopovar/AnyPS5/blob/95d17687aed6bad81082d9bbef01cb1d9c0a384c/core/libs/tests/GuestSocketAddressLength.cpp#L58), [L70](https://github.com/boykopovar/AnyPS5/blob/95d17687aed6bad81082d9bbef01cb1d9c0a384c/core/libs/tests/GuestSocketAddressLength.cpp#L70) | | | 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 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` |
xXPBSXx commented 2026-10-20 12:43:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T12:43:36Z.


Overlap note: #7511 (validate the pointers of getsockopt and setsockopt like FreeBSD) adds the same null-value and negative-length checks for setsockopt. git merge-tree of the two heads is clean, but the setsockopt part of this PR then repeats those checks; this PR only moves them in front of the descriptor lookup, which is what kern_setsockopt does (uipc_syscalls.c:1414-1417). If #7511 lands first, I can rebase this to the descriptor-order change only. I should have listed #7511 in the description; pr_overlap was not run before opening.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291115563 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392#issuecomment-6291115563). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T12:43:36Z. --- Overlap note: #7511 (validate the pointers of getsockopt and setsockopt like FreeBSD) adds the same null-value and negative-length checks for `setsockopt`. `git merge-tree` of the two heads is clean, but the `setsockopt` part of this PR then repeats those checks; this PR only moves them in front of the descriptor lookup, which is what `kern_setsockopt` does (`uipc_syscalls.c:1414-1417`). If #7511 lands first, I can rebase this to the descriptor-order change only. I should have listed #7511 in the description; `pr_overlap` was not run before opening.
oneandonlydean commented 2026-10-20 14:26:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T14:26:18Z.


Thanks, the ordering matches FreeBSD's getsockaddr before getsock. One thing in the description: it says setsockopt now rejects a null value with a non-zero length and a negative length before the descriptor, but the diff doesn't change setsockopt, and it still looks the descriptor up first. Could you either drop that sentence or add the change with a test?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293183729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392#issuecomment-6293183729). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T14:26:18Z. --- Thanks, the ordering matches FreeBSD's `getsockaddr` before `getsock`. One thing in the description: it says `setsockopt` now rejects a null value with a non-zero length and a negative length before the descriptor, but the diff doesn't change `setsockopt`, and it still looks the descriptor up first. Could you either drop that sentence or add the change with a test?
xXPBSXx commented 2026-10-20 14:54:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T14:54:12Z.


Thanks, you are right: the diff changes only the address checks of bind, connect and sendto; setsockopt still looks the descriptor up first. The setsockopt sentence came from an earlier version of this change that I dropped because #7511 covers the pointer and length checks of setsockopt/getsockopt. I removed it from the description, the title and the linked issue. My earlier comment here about the setsockopt part overlapping #7511 was therefore also wrong: there is no overlap in setsockopt.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293794575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392#issuecomment-6293794575). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T14:54:12Z. --- Thanks, you are right: the diff changes only the address checks of `bind`, `connect` and `sendto`; `setsockopt` still looks the descriptor up first. The `setsockopt` sentence came from an earlier version of this change that I dropped because #7511 covers the pointer and length checks of `setsockopt`/`getsockopt`. I removed it from the description, the title and the linked issue. My earlier comment here about the `setsockopt` part overlapping #7511 was therefore also wrong: there is no overlap in `setsockopt`.
xXPBSXx commented 2026-10-21 03:40:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T03:40:15Z.


Linux data point for ce8f7803ea (WSL2, Ubuntu 24.04, GCC 13.3, Release, -j4; a virtualised kernel, not bare-metal Linux): the socket and UDP tests pass on Linux (14 of 14). This replaces the "Linux not tested" line in the description for this head. Not tested: macOS.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6307453366 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392#issuecomment-6307453366). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T03:40:15Z. --- Linux data point for `ce8f7803ea` (WSL2, Ubuntu 24.04, GCC 13.3, Release, `-j4`; a virtualised kernel, not bare-metal Linux): the socket and UDP tests pass on Linux (14 of 14). This replaces the "Linux not tested" line in the description for this head. Not tested: macOS. AI-assisted (Claude Code).
adriadam10 commented 2026-10-21 18:06:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T18:06:30Z.


Checked the order of bind, connect and sendto against FreeBSD 11.4 (getsockaddr before getsock), the new test pins the length/null errors before EBADF and fails on main, the family checks stay after the lookup, and Windows plus WSL2 runs are in the thread; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323041693 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8392#issuecomment-6323041693). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T18:06:30Z. --- Checked the order of `bind`, `connect` and `sendto` against FreeBSD 11.4 (`getsockaddr` before `getsock`), the new test pins the length/null errors before EBADF and fails on `main`, the family checks stay after the lookup, and Windows plus WSL2 runs are in the thread; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8392
No description provided.