fix(libkernel): sched_getparam reads out-of-range priorities, sched_setparam EPERM outside 256-767, own pid on the main thread #11865

Merged
ionut27001 merged 1 commit from upstream-pr/11865 into main 2026-10-29 19:34:54 +00:00
ionut27001 commented 2026-10-27 10:01:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-27T10:01:34Z.


What

sched_getparam no longer throws for a priority outside sched_get_priority_min to sched_get_priority_max, sched_setparam returns EPERM there instead of throwing, and the own pid works from the process's main thread:

  • sched_getparam returns the thread's priority as it is (a thread put at 900 or 959 by scePthreadSetprio reads back 900 or 959, p83 lines 217 and 221).
  • sched_setparam on a SCHED_FIFO or SCHED_RR thread with a priority outside 256-767 fails with EPERM and leaves the priority unchanged. Measured on a SCHED_FIFO thread with 255 and 768; SCHED_RR was not probed and follows FreeBSD's ksched range check.
  • sched_getparam, sched_getscheduler and sched_setparam with the own pid (getpid()) act on the calling thread when it is the process's initial thread (IsInitialPthread_nid_no_patch, which pthread_main_np already uses), the same as pid 0. From any other thread the own pid still throws, as on main: in a payload process on a PS5 (firmware 12.70) it read the priority of the thread that ran the payload's main (p84 line 20); whether that is the process's first thread is unmeasured. This is not modelled and stays in the debt entry. On macOS the guest's main thread runs on a second host thread, which is not the initial thread, so the own pid throws there too; the test's own-pid check is compiled out on macOS.
  • sched_setparam on a thread whose policy is neither SCHED_FIFO nor SCHED_RR still throws.

Evidence, probe p83_libkernel (a payload process on a PS5, firmware 12.70), result.log:

  • lines 204-208: sched_get_priority_min/max are 256/767 for policies 1 and 3 and EINVAL for 0, 2, 4.
  • lines 217 and 221: with the helper thread's SCE priority at 900 and 959, sched_getparam(0) returns 0 with 900 and 959.
  • lines 224-227: on the helper (policy 1), sched_setparam(0) with 255 and 768 returns -1 EPERM and sched_getparam(0) then reads 700; with 256 and 767 it returns 0 and reads back 256 and 767.

Evidence, probe p84_libkernel (a payload process on a PS5, firmware 12.70), result.log:

  • lines 14-16: the probe's main thread is moved to 600; from it, pid 0 and getpid() both read 600, sched_getscheduler 1.
  • lines 18-20: a helper moved to 650 reads 650 for pid 0 and 600, the priority of the thread that ran the payload's main, for getpid() through sched_getparam; sched_getscheduler(getpid()) is 1, the policy of both threads.
  • lines 21-22: the main thread, while the helper is at 650, reads 600 for both pids.

Test: GuestSchedParams.cpp gains CheckOutOfRange (read of 900 and 959; EPERM for 255 and 768 with the priority unchanged) and CheckOwnPid (the main thread at 600 reads 600 and its own policy through getpid(), then restores its priority through getpid()), only values the probes answered.

Tested

Windows 11, WinLibs GCC 15.2.0 (Release, Ninja), commit e679177c1b on main 431a5156b9:

  • ctest -R ^guest_sched_params$ passed twice.
  • With Pthread/Posix/Common.hpp and Pthread/Posix/Thread.cpp restored from main and rebuilt, the same test aborts (sched_getparam throws for priority 900).
  • Linux and macOS were not built or run.

Checklist

  • Based on current main (rebased onto it); 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 (only a docs line conflict with #11849; whichever lands second rebases)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5032020644 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11865). Originally posted by [ionut27001](https://github.com/ionut27001) on 2026-10-27T10:01:34Z. --- ### What `sched_getparam` no longer throws for a priority outside `sched_get_priority_min` to `sched_get_priority_max`, `sched_setparam` returns `EPERM` there instead of throwing, and the own pid works from the process's main thread: - `sched_getparam` returns the thread's priority as it is (a thread put at 900 or 959 by `scePthreadSetprio` reads back 900 or 959, p83 lines 217 and 221). - `sched_setparam` on a `SCHED_FIFO` or `SCHED_RR` thread with a priority outside 256-767 fails with `EPERM` and leaves the priority unchanged. Measured on a `SCHED_FIFO` thread with 255 and 768; `SCHED_RR` was not probed and follows FreeBSD's `ksched` range check. - `sched_getparam`, `sched_getscheduler` and `sched_setparam` with the own pid (`getpid()`) act on the calling thread when it is the process's initial thread (`IsInitialPthread_nid_no_patch`, which `pthread_main_np` already uses), the same as pid 0. From any other thread the own pid still throws, as on `main`: in a payload process on a PS5 (firmware 12.70) it read the priority of the thread that ran the payload's main (p84 line 20); whether that is the process's first thread is unmeasured. This is not modelled and stays in the debt entry. On macOS the guest's main thread runs on a second host thread, which is not the initial thread, so the own pid throws there too; the test's own-pid check is compiled out on macOS. - `sched_setparam` on a thread whose policy is neither `SCHED_FIFO` nor `SCHED_RR` still throws. Evidence, probe `p83_libkernel` (a payload process on a PS5, firmware 12.70), `result.log`: - lines 204-208: `sched_get_priority_min`/`max` are 256/767 for policies 1 and 3 and `EINVAL` for 0, 2, 4. - lines 217 and 221: with the helper thread's SCE priority at 900 and 959, `sched_getparam(0)` returns 0 with 900 and 959. - lines 224-227: on the helper (policy 1), `sched_setparam(0)` with 255 and 768 returns -1 `EPERM` and `sched_getparam(0)` then reads 700; with 256 and 767 it returns 0 and reads back 256 and 767. Evidence, probe `p84_libkernel` (a payload process on a PS5, firmware 12.70), `result.log`: - lines 14-16: the probe's main thread is moved to 600; from it, pid 0 and `getpid()` both read 600, `sched_getscheduler` 1. - lines 18-20: a helper moved to 650 reads 650 for pid 0 and 600, the priority of the thread that ran the payload's main, for `getpid()` through `sched_getparam`; `sched_getscheduler(getpid())` is 1, the policy of both threads. - lines 21-22: the main thread, while the helper is at 650, reads 600 for both pids. Test: `GuestSchedParams.cpp` gains `CheckOutOfRange` (read of 900 and 959; `EPERM` for 255 and 768 with the priority unchanged) and `CheckOwnPid` (the main thread at 600 reads 600 and its own policy through `getpid()`, then restores its priority through `getpid()`), only values the probes answered. ### Tested Windows 11, WinLibs GCC 15.2.0 (Release, Ninja), commit e679177c1b on main 431a5156b9: - `ctest -R ^guest_sched_params$` passed twice. - With `Pthread/Posix/Common.hpp` and `Pthread/Posix/Thread.cpp` restored from main and rebuilt, the same test aborts (`sched_getparam` throws for priority 900). - Linux and macOS were not built or run. ### Checklist - [x] Based on current `main` (rebased onto it); 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (only a docs line conflict with #11849; whichever lands second rebases) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 10:25:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T10:25:57Z.

Review: DISMISSED


I independently verified the lifetime race described in the existing inline review comment on the current head c566f667bccdf894153b82fa6a83b3c84dc95ca4. SchedTarget loads the raw main-thread pointer before ReadLiveSched acquires the live-thread registry lock. If that thread exits and its address is reused before validation, the read can accept the replacement thread. sched_setparam also releases the validation lock before calling scePthreadSetprio, leaving the same stale-address window for a write.

This can report or mutate the wrong guest thread, so it blocks approval. Please address the existing request by keeping identity/lifetime stable through selection and use, and add deterministic teardown/address-reuse coverage for reads and writes. I am not repeating the inline comment's detailed implementation suggestions.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5665555487 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11865#pullrequestreview-5665555487). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T10:25:57Z. **Review:** DISMISSED --- I independently verified the lifetime race described in the existing inline review comment on the current head `c566f667bccdf894153b82fa6a83b3c84dc95ca4`. `SchedTarget` loads the raw main-thread pointer before `ReadLiveSched` acquires the live-thread registry lock. If that thread exits and its address is reused before validation, the read can accept the replacement thread. `sched_setparam` also releases the validation lock before calling `scePthreadSetprio`, leaving the same stale-address window for a write. This can report or mutate the wrong guest thread, so it blocks approval. Please address the existing request by keeping identity/lifetime stable through selection and use, and add deterministic teardown/address-reuse coverage for reads and writes. I am not repeating the inline comment's detailed implementation suggestions.
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!11865
No description provided.