fix(libkernel): accept an address buffer without a length pointer in recvfrom #7507

Closed
Bonjourcestmatt wants to merge 3 commits from upstream-pr/7507 into main
Bonjourcestmatt commented 2026-10-18 10:10:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-18T10:10:24Z.


What

recvfrom with an address buffer but no length pointer now succeeds and returns the received bytes, instead of failing with EFAULT (14) after the descriptor lookup. FreeBSD 11.4 sys_recvfrom() (sys/kern/uipc_syscalls.c, release/11.4.0) only does the copyin of the length when fromlenaddr is non-NULL; otherwise it sets msg.msg_namelen = 0 and still passes msg.msg_name = uap->from. kern_recvit() then copies MIN(len, fromsa->sa_len) bytes of the source address, which is zero bytes, so nothing is written and the receive succeeds. The address is only written back when the length pointer is present.

Overlap: no open PR changes recvfrom_nid_postfix. tools/pr_overlap.py lists PRs touching Udp.cpp and GuestSocketPair.cpp; I read the diffs of #7332, #6044, #6222, #7201 and #6977: only #7332 mentions recvfrom_nid_postfix, in a test line that passes a null address and a null length, which still behaves the same.

Tested

  • Windows 11, WinLibs GCC 15.2.0 UCRT, Release: guest_socket_pair_tests has a new check (a Unix stream recvfrom with an address buffer and a null length pointer returns the data). It fails on main without the fix (exit code 0xc0000409) and passes with it; all socket and sce_net ctest tests pass and the full non-GPU suite passes (758 of 758, skipped ones included); python tools/check_conventions.py --base origin/main passes.
  • Not run: Linux and macOS, no console. Only the FreeBSD source was used as reference; the console's behaviour is unverified.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4908209976 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7507). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-18T10:10:24Z. --- ### What `recvfrom` with an address buffer but no length pointer now succeeds and returns the received bytes, instead of failing with `EFAULT` (14) after the descriptor lookup. FreeBSD 11.4 `sys_recvfrom()` (`sys/kern/uipc_syscalls.c`, release/11.4.0) only does the `copyin` of the length when `fromlenaddr` is non-NULL; otherwise it sets `msg.msg_namelen = 0` and still passes `msg.msg_name = uap->from`. `kern_recvit()` then copies `MIN(len, fromsa->sa_len)` bytes of the source address, which is zero bytes, so nothing is written and the receive succeeds. The address is only written back when the length pointer is present. Overlap: no open PR changes `recvfrom_nid_postfix`. `tools/pr_overlap.py` lists PRs touching `Udp.cpp` and `GuestSocketPair.cpp`; I read the diffs of #7332, #6044, #6222, #7201 and #6977: only #7332 mentions `recvfrom_nid_postfix`, in a test line that passes a null address and a null length, which still behaves the same. ### Tested - Windows 11, WinLibs GCC 15.2.0 UCRT, Release: `guest_socket_pair_tests` has a new check (a Unix stream `recvfrom` with an address buffer and a null length pointer returns the data). It fails on `main` without the fix (exit code 0xc0000409) and passes with it; all socket and `sce_net` ctest tests pass and the full non-GPU suite passes (758 of 758, skipped ones included); `python tools/check_conventions.py --base origin/main` passes. - Not run: Linux and macOS, no console. Only the FreeBSD source was used as reference; the console's behaviour is unverified. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
wachi35676 commented 2026-10-18 12:20:03 +00:00 (Migrated from github.com)

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


Checked on Ubuntu 24.04 (GCC 13.3) as well as Windows 11 (MinGW GCC 15.2), with this PR merged onto main 9f25d0bf5. guest_socket_pair with the new lines aborts with main's Udp.cpp and passes with this PR on both. The socket/sce_net set passes (Linux 26/26, Windows 38/38).

This matches release/11.4.0: without a length pointer, sys_recvfrom sets msg_namelen = 0 (L1220), and kern_recvit then copies 0 bytes (L1099).

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249839205 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7507#issuecomment-6249839205). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-18T12:20:03Z. --- Checked on Ubuntu 24.04 (GCC 13.3) as well as Windows 11 (MinGW GCC 15.2), with this PR merged onto `main` `9f25d0bf5`. `guest_socket_pair` with the new lines aborts with `main`'s `Udp.cpp` and passes with this PR on both. The socket/sce_net set passes (Linux 26/26, Windows 38/38). This matches release/11.4.0: without a length pointer, `sys_recvfrom` sets `msg_namelen = 0` ([L1220](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1220)), and `kern_recvit` then copies 0 bytes ([L1099](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1099)). AI-assisted: yes (Claude Code); results above were run on this machine.
ycarogabriel-lgtm commented 2026-10-19 00:13:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycarogabriel-lgtm on 2026-10-19T00:13:28Z.


Ran this on macOS, since it was not run there: Apple M1, macOS 26.6.2 (25G83), x86-64 build under Rosetta 2, AppleClang 21. Merged into the macOS host branch of #7332 (57bccfbfc, main at d71853cf2) together with #7121, #7475, #7490, #7507 and #7612; Release build.

  • guest_socket_pair passes.
  • The suite without the GPU tests passes, apart from a guest_kqueue case from #7121 that is unrelated to this change.

AI-assisted: yes (Claude Code). I ran the build and tests on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256922042 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7507#issuecomment-6256922042). Originally posted by [ycarogabriel-lgtm](https://github.com/ycarogabriel-lgtm) on 2026-10-19T00:13:28Z. --- Ran this on macOS, since it was not run there: Apple M1, macOS 26.6.2 (25G83), x86-64 build under Rosetta 2, AppleClang 21. Merged into the macOS host branch of #7332 (`57bccfbfc`, `main` at `d71853cf2`) together with #7121, #7475, #7490, #7507 and #7612; `Release` build. - `guest_socket_pair` passes. - The suite without the GPU tests passes, apart from a `guest_kqueue` case from #7121 that is unrelated to this change. AI-assisted: yes (Claude Code). I ran the build and tests on my machine.
ahmadmuj commented 2026-10-25 09:53:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T09:53:31Z.

Review: COMMENTED


Checked head 911c6765cb merged onto main 16025845e5 (merges clean; socket files are the same on main acbfd7c7fe). Linux, WSL2 Ubuntu 24.04, GCC 13.3, Release, guest_socket_pair only.

The merged tree crashes on a zero-length recvfrom with an address and no length pointer. #8492 landed after this PR was opened and added an early return in recvfrom_nid_postfix that still does:

if (address) *addressLength = 0;

Before this PR the EFAULT guard kept that safe. With the guard gone it writes through null. It's reached whenever ReceiveAnsweredHere returns 0: length 0 on a connected stream, length 0 on a datagram socket with nothing queued, or on Windows any length after shutdown(SHUT_RD). The new test uses length 8, so it takes the host path and misses this.

Repro, one line after your new check in GuestSocketPair.cpp:

Require(recvfrom_nid_postfix(pair[1], withoutLength, 0, 0, from, nullptr) == 0);
  • merged tree: guest_socket_pair SegFault
  • merged tree + if (address && addressLength) *addressLength = 0; on that line: passes

FreeBSD returns 0 there too: msg_namelen = 0 when fromlenaddr is NULL (L1220) and the length copyout is skipped (L1188). So a rebase plus that guard plus the zero-length test line should cover it. The host-path change itself looks right to me.

AI-assisted: yes (Claude Code); the build and test above ran on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642608267 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7507#pullrequestreview-5642608267). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T09:53:31Z. **Review:** COMMENTED --- Checked head `911c6765cb` merged onto main `16025845e5` (merges clean; socket files are the same on main `acbfd7c7fe`). Linux, WSL2 Ubuntu 24.04, GCC 13.3, Release, `guest_socket_pair` only. The merged tree crashes on a zero-length `recvfrom` with an address and no length pointer. #8492 landed after this PR was opened and added an early return in `recvfrom_nid_postfix` that still does: ```cpp if (address) *addressLength = 0; ``` Before this PR the EFAULT guard kept that safe. With the guard gone it writes through null. It's reached whenever `ReceiveAnsweredHere` returns 0: length 0 on a connected stream, length 0 on a datagram socket with nothing queued, or on Windows any length after `shutdown(SHUT_RD)`. The new test uses length 8, so it takes the host path and misses this. Repro, one line after your new check in `GuestSocketPair.cpp`: ```cpp Require(recvfrom_nid_postfix(pair[1], withoutLength, 0, 0, from, nullptr) == 0); ``` - merged tree: `guest_socket_pair` SegFault - merged tree + `if (address && addressLength) *addressLength = 0;` on that line: passes FreeBSD returns 0 there too: `msg_namelen = 0` when `fromlenaddr` is NULL ([L1220](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1220)) and the length copyout is skipped ([L1188](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1188)). So a rebase plus that guard plus the zero-length test line should cover it. The host-path change itself looks right to me. AI-assisted: yes (Claude Code); the build and test above ran on this machine.

Pull request closed

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!7507
No description provided.