Skip to content

fix(libkernel): fail a kern.proc.env read into a buffer with EINVAL, as a PS5 does - #24480

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-sysctl-proc-env-read-einval
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-sysctl-proc-env-read-einval

Conversation

@brajankw

Copy link
Copy Markdown
Contributor

What

sysctl {CTL_KERN, KERN_PROC, KERN_PROC_ENV, pid} with a buffer returned 0 bytes. A payload process on a PS5 (firmware 11.60) got EINVAL for every such read in xXPBSXx's probes in #22788 (third probe, lines 9 to 16 and 25 to 28 of its output): its own pid, pid 1, -1 and 0x7fffffff, with a 4096- and a 24576-byte buffer, the length left at the buffer size. The size query gave 0 for its own pid and ESRCH for -1, so the check comes before the pid lookup.

  • A read into a buffer now fails with EINVAL before the pid check and leaves the buffer and the length alone. A write keeps EPERM and the size query keeps its answers.
  • The test also uses 16- and 1-byte buffers, beyond the two measured sizes.
  • PS5VR (PPSA99177) reads this node for its own pid at start-up (6e1df42). It was not run here.
  • Tests: CheckEnvSysctl in GuestSystemConfiguration.cpp reads into a buffer for the own pid, the calling thread's id, another pid and -1.
  • Debt: the pending fragment fix-libkernel-sysctl-proc-thread-id-pathname-env-vmmap.md already changes the kern.proc.env entry, so it is edited in place.

Tested

Linux (Debian 13, GCC 14.2.0, Release). Head 9638f548ef, the single commit on main c8f72e905c.

  • Negative control: with only the test change, on main 84e16538d4, guest_system_configuration aborts.
  • On 9638f548ef, ctest over guest_system_configuration, guest_kernel_sysctl_name_bounds, guest_kernel_sysctl_memory_nodes, guest_sce_net and guest_getrlimit: all pass.
  • Full ctest (6393 tests, -j 12) on this branch and my other eleven branches of this round, merged onto main 84e16538d4: 6378 passed. The 15 failures also fail on main in this environment:
    • nine need a .git directory, network access or a passwd entry for the user: conventions_git_errors, test_script_registration, web_library_assets, title_run, title_run_identity, guest_kernel_sys_login, guest_net_ctl, guest_getlogin and guest_getlogin_no_session;
    • five agc_driver GPU cases fail the same way on main: prepared_shaders with its indirect, uncached and debug forms, and storage_file_write;
    • agc_driver_image_table_packed_guards is unstable on main as well (2 of 10 runs passed on its own on main 3f4693be43).
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch fix/libkernel-sysctl-proc-env-read-einval: no overlap with open pull requests.
  • Not tested on Windows or macOS. No title.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

…as a PS5 does

A payload process on a PS5 (firmware 11.60) got EINVAL from every read
of {CTL_KERN, KERN_PROC, KERN_PROC_ENV, pid} into a 4096- or 24576-byte
buffer, for its own pid, pid 1, -1 and 0x7fffffff, while the size query
gave 0 for its own pid and ESRCH for -1 (boykopovar#22788). The EINVAL for -1 puts
the check before the pid lookup. The read returned 0 bytes here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 9638f548ef:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_system_configuration_tests built cleanly.
  • CTest (guest_system_configuration):
    1/1 Test #5837: guest_system_configuration .......   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/System/src/SystemConfiguration.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5837: guest_system_configuration .......Subprocess aborted***Exception:   0.62 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • SystemConfiguration.cpp: verified that sysctl returns EINVAL (errno 22) when oldValue is non-null for KERN_PROC_ENV (nameLength != 4 || oldValue != nullptr), leaving buffer and length unmodified
    • SystemConfiguration.cpp: confirmed that write operations continue to be rejected with EPERM (errno 1) via the existing newValue check before the oldValue check
    • SystemConfiguration.cpp: confirmed that size queries (oldValue == nullptr) proceed past this check to return length 0 for the calling process, or ESRCH (errno 3) for other processes
    • GuestSystemConfiguration.cpp: verified that tests cover the own PID, calling thread ID, foreign PID, and -1 with both buffer size 16 and 1 byte, checking errno 22 and unchanged buffer/length
    • Technical debt documentation: confirmed the changes fragment docs/dev/debt/changes/fix-libkernel-sysctl-proc-thread-id-pathname-env-vmmap.md accurately reflects the measured PS5 11.60 behavior

Looks Good To Me

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — looks good

The change matches the described kern.proc.env behavior: size queries are preserved, while reads with a buffer fail with EINVAL before process ID validation.

What I ran

Reviewed head 9638f548ef39dfaaf00d1752614021910141113e. I read the prepared description, commits, manifest, diff, and all three changed files in full or around the changed lines. No tests or builds were run during this review.

Results

Check Result Note
Diff and changed code passed Behavior and tests inspected
Tests during review not run Author-reported results not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports targeted tests and a Linux full ctest run passing, but these were not run during this review. PS5 behavior and Windows/macOS behavior were not verified. files.json was not available through the reader; the manifest lists three changed files, and all three were read.

To get this to a good state

Nothing is required.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 9638f548ef, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24576, #24603, #14196, #14527, #17718, #18671, #22753, #22792, #23503, #24245, #24553, #24714, #21236, #21765, #21987, #22030, #22107, #22397, #22571, #23158, #23816, #24067, #18401, #19101, #19398, #19657, #21563, #24584, #24628). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_system_configuration (1 test, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6523 tests) on the RTX 3080 has 7 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked, agc_driver_store_over_untracked, agc_storage_write_back_host_retile. Traced to other PRs of the batch, each by reverting it and testing again: #19101 (guest_video_out_window_margins_bounds); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): agc_driver_image_gather_lod. The 2125 agc_/video tests on the iGPU have 5 failing: agc_driver_image_gather_lod, agc_driver_stencil_plane_memory, agc_driver_store_over_untracked, agc_storage_write_back_host_retile, guest_video_out_window_margins_bounds (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

This was referenced Nov 21, 2026
@Helldiverr

Copy link
Copy Markdown
Contributor

On Windows the merge passes guest_system_configuration and 10 neighbouring sysctl, net and rlimit tests in all 3 runs. The PR's test on main aborts (the negative control), and all 4 mutations of the new check fail it. A probe of the reads the PS5 measurement in #22788 covers (own pid, pid 1, -1 and 0x7fffffff with 4096- and 24576-byte buffers) gets EINVAL with the length and buffer unchanged in every case, matching that measurement.

Existing runs:

  • The author: Linux (Debian 13, GCC 14.2), with a negative control. The PR body says it was not tested on Windows.
  • PrivacyOnion: Linux (Ubuntu 24.04, GCC 13.3) at 9638f548ef, with a negative control, and approved.
  • MarcZX10: approved at 9638f548ef after reading the code.
  • kiwe147: Windows at 9638f548ef, applied on fd4aeed483 together with 29 other PRs, with no control.

What this run adds.

Code.

  • sysctl_nid_postfix, kern.proc.env branch (core/libs/prx/libkernel/System/src/SystemConfiguration.cpp:550-564):
    • A write fails with EPERM (:551-554).
    • A name that is not 4 elements long, or any read with a buffer, fails with EINVAL (:555-558). This is the one line the PR changes.
    • The pid is looked up only after that (:559-562), so reads for other pids give EINVAL, not ESRCH. That matches the PS5 readings for pid -1 and 0x7fffffff (cases 2.2.2, 2.4.2 and 4.2.3-4.2.4 in the comment cited by the fragment).
    • A size query still reaches CopyOut(nullptr, 0, ...) (:563).
  • The test (core/libs/tests/GuestSystemConfiguration.cpp:1070-1078) reads into a 16-byte and a 1-byte buffer for the own pid, the thread id, a foreign pid and -1. It checks errno 22, the length unchanged and the buffer untouched (Untouched, :131). The PR removes the earlier check that such a read succeeds.
  • The debt change edits docs/dev/debt/changes/fix-libkernel-sysctl-proc-thread-id-pathname-env-vmmap.md, a pending fragment already on main (added in 94aa6c0be2). tools/debt_fold.py refuses two fragments that change the same entry, so editing that fragment is the way to change the [kern.proc.env] text. The new text cites the measurement and records that pid 1 gave size 0 on the console, where this code gives ESRCH.
  • No comments, no made-up success and no title-specific case. tools/check_conventions.py passes.

Not covered by the measurement. The comment behind the fragment says its probes only read sysctl. These cases are therefore not measured on a PS5:

  • A write combined with a read buffer. :1062 expects EPERM for this, because the write check (:551) comes first. The "write check after the read check" mutation below changes only this case.
  • A buffer with a length of 0, or 1 or 16 bytes. The PS5 readings used 4096 and 24576 bytes. Here every non-null buffer fails with EINVAL, whatever its length.
  • Pid 0 and a thread id. Both give EINVAL for a read. For a size query, pid 0 gives ESRCH and the thread id gives size 0.

Negative control and mutation.

  • Negative control: the PR's core/libs/tests/GuestSystemConfiguration.cpp on main sources. guest_system_configuration aborts with exit code 0xc0000409, the code std::abort gives on Windows. Require (:22) prints no line. On main, the read into the buffer returns 0, which fails the new loop.
  • Mutations (lines in SystemConfiguration.cpp), each with the probe, rebuilt and run once:
Mutation Change guest_system_configuration probe
Read check after the pid check oldValue test moved below :559-562 aborts 6 failures: pid 1, -1 and 0x7fffffff reads give ESRCH
Read check before the write check oldValue test moved above :551 aborts (:1062 gets EINVAL, expects EPERM) 0 failures
Length cleared *oldLength = 0 before failing at :555 aborts 8 failures: the length is no longer kept
Wrong errno 14 (EFAULT) instead of 22 at :556 aborts 8 failures
  • The restored merge passes again.

Probe. A temporary block at the start of CheckEnvSysctl (:1053), not part of the PR. It ran 3 times on the merge with identical output each time, and 0 failures. Failures count only for the cases the PS5 measured.

  • Reads: own pid, thread id, pid 1, -1, 0x7fffffff and 0, each with 4096, 24576, 1 and 0 bytes. All 24 give rc=-1, errno=22, the length unchanged and the buffer untouched.
  • Size queries: own pid and thread id give 0 with errno unchanged. Pid 1, -1, 0x7fffffff and 0 give ESRCH with the length unchanged. The PS5 gave size 0 for pid 1 (case 2.3.1); the fragment records this.
  • 3-element name with a buffer: EINVAL.

Tests. The pattern guest_system_configuration|guest_kernel_sysctl|guest_sce_net|guest_getrlimit selects 12 tests. guest_sce_net_kernel_forwards is one of them, but its target (guest_net_kernel_forwards_tests) was not in this run's build. It shows Not Run on main and on the merge alike, and is left out below.

Run Result (11 tests)
main 03064c281f 11/11
PR test on main (negative control) 10/11, guest_system_configuration aborts
merge, run 1 11/11
merge, run 2 11/11
merge, run 3 11/11
merge + probe, 3 runs 11/11 each, probe failures=0
merge, restored after mutations 11/11
tools/check_conventions.py --base 03064c281f passed
tools/debt_fold.py --check passed

The 11 tests are:

  • guest_system_configuration
  • guest_kernel_sysctl_name_bounds
  • guest_kernel_sysctl_memory_nodes
  • guest_getrlimit
  • guest_sce_net, guest_sce_net_keepalive, guest_sce_net_linger
  • guest_sce_net_listen_ipv4, guest_sce_net_listen_ipv6
  • guest_sce_net_reuseport_ipv4, guest_sce_net_reuseport_ipv6

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 9638f548ef, merged onto main 03064c281f as 702715fb42

main has since moved 78 commits to bd3a694f2b, none of them in the files this PR touches, and the head still merges cleanly.

AI-assisted: yes

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.

6 participants