fix(libkernel): accept socket option values longer than an int #4658

Merged
milescoviello merged 1 commit from upstream-pr/4658 into main 2026-10-13 03:53:12 +00:00
milescoviello commented 2026-10-12 18:05:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T18:05:49Z.


What

setsockopt() on a guest socket failed with EINVAL unless the option length was exactly sizeof(int), so an integer option passed in a wider variable (int64_t, size_t, long) was rejected and silently left unchanged: SO_BROADCAST stayed off and SO_RCVBUF/SO_SNDBUF kept their size.

FreeBSD 11.4 reads these options through sooptcopyin() with sizeof(int) as both the wanted and the minimum length, and only a shorter value fails (sys/kern/uipc_socket.c):

	 * If the user gives us more than we wanted, we ignore it, but if we
	 * don't get the minimum length the caller wants, we return EINVAL.
	...
	if ((valsize = sopt->sopt_valsize) < minlen)
		return EINVAL;
	if (valsize > len)
		sopt->sopt_valsize = valsize = len;

sosetopt() reads SO_BROADCAST, SO_REUSEADDR, SO_SNDBUF and SO_RCVBUF with sooptcopyin(sopt, &optval, sizeof optval, sizeof optval). A longer value now sets the option from its first int; a shorter one still fails with EINVAL. libSceNet sockets reached through this export already accept lengths of 4 or more.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main 2a5eca11. guest_udp now sets SO_BROADCAST from an 8-byte 0 and then an 8-byte 1 and reads each value back, and checks that a 2-byte value still fails with EINVAL.

  • Red: with main's Udp.cpp the test aborts (setsockopt with the 8-byte value returns -1, errno 22).
  • Green: guest_udp passes; all guest_* tests except guest_video_out_*: 306/306 passed in eight consecutive runs (one earlier run had a single failure in another test that I could not identify or reproduce).

Not tested on Windows.

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 in TechnicalDebt
  • 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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4833215895 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4658). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T18:05:49Z. --- ### What `setsockopt()` on a guest socket failed with `EINVAL` unless the option length was exactly `sizeof(int)`, so an integer option passed in a wider variable (`int64_t`, `size_t`, `long`) was rejected and silently left unchanged: `SO_BROADCAST` stayed off and `SO_RCVBUF`/`SO_SNDBUF` kept their size. FreeBSD 11.4 reads these options through `sooptcopyin()` with `sizeof(int)` as both the wanted and the minimum length, and only a shorter value fails (`sys/kern/uipc_socket.c`): ```c * If the user gives us more than we wanted, we ignore it, but if we * don't get the minimum length the caller wants, we return EINVAL. ... if ((valsize = sopt->sopt_valsize) < minlen) return EINVAL; if (valsize > len) sopt->sopt_valsize = valsize = len; ``` `sosetopt()` reads `SO_BROADCAST`, `SO_REUSEADDR`, `SO_SNDBUF` and `SO_RCVBUF` with `sooptcopyin(sopt, &optval, sizeof optval, sizeof optval)`. A longer value now sets the option from its first `int`; a shorter one still fails with `EINVAL`. libSceNet sockets reached through this export already accept lengths of 4 or more. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` 2a5eca11. `guest_udp` now sets `SO_BROADCAST` from an 8-byte 0 and then an 8-byte 1 and reads each value back, and checks that a 2-byte value still fails with `EINVAL`. - Red: with main's `Udp.cpp` the test aborts (`setsockopt` with the 8-byte value returns -1, `errno` 22). - Green: `guest_udp` passes; all `guest_*` tests except `guest_video_out_*`: 306/306 passed in eight consecutive runs (one earlier run had a single failure in another test that I could not identify or reproduce). Not tested on Windows. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-12 18:13:20 +00:00 (Migrated from github.com)

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


Windows test of head eb9da23e6 merged onto current main (2a5eca11c, clean merge): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -j2.

guest_udp (the test this PR changes: SO_BROADCAST from an 8-byte 0 and 1, read back, and a 2-byte value still failing with EINVAL) builds and passes. Only that target was rebuilt for this run; the other socket tests I ran with it (guest_tcp, guest_socket_*, guest_poll, guest_dup, guest_sce_net, 21 of 21 pass) come from earlier builds of the same tree, so they do not exercise this change.

Not checked: that guest_udp fails without the change on Windows (the PR reports it on Linux), the Winsock SO_RCVBUF/SO_SNDBUF path with a longer value beyond what guest_udp covers, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129957795 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4658#issuecomment-6129957795). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T18:13:20Z. --- Windows test of head `eb9da23e6` merged onto current `main` (`2a5eca11c`, clean merge): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `-j2`. `guest_udp` (the test this PR changes: `SO_BROADCAST` from an 8-byte 0 and 1, read back, and a 2-byte value still failing with `EINVAL`) builds and passes. Only that target was rebuilt for this run; the other socket tests I ran with it (`guest_tcp`, `guest_socket_*`, `guest_poll`, `guest_dup`, `guest_sce_net`, 21 of 21 pass) come from earlier builds of the same tree, so they do not exercise this change. Not checked: that `guest_udp` fails without the change on Windows (the PR reports it on Linux), the Winsock `SO_RCVBUF`/`SO_SNDBUF` path with a longer value beyond what `guest_udp` covers, Linux, a title run. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-12 22:18:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T22:18:19Z.


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
#4796 core/libs/prx/libkernel/Socket/src/Udp.cpp L597-601
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133646280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4658#issuecomment-6133646280). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T22:18:19Z. --- <!-- 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 | |---|---|---|---| | #4796 | `core/libs/prx/libkernel/Socket/src/Udp.cpp` [L597-601](https://github.com/boykopovar/AnyPS5/blob/6124bf063f6348e4451ad0b9c67f82acec5bc1c2/core/libs/prx/libkernel/Socket/src/Udp.cpp#L597-L601) | | |
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!4658
No description provided.