Skip to content

fix(libkernel): refuse a stale equeue handle - #21726

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
AasishDairelSahayaGrinspan:fix/libkernel-equeue-stale-handle
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
AasishDairelSahayaGrinspan:fix/libkernel-equeue-stale-handle

Conversation

@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor

What

EqueuePin_nid_postfix matched only the descriptor half of an equeue handle, (id << 32) | descriptor. After sceKernelDeleteEqueue and a recycled descriptor number, a stale handle waited on, triggered or deleted events of the new queue instead of failing. Firmware 13.42's libkernel_sys creates a named object, returns (id << 32) | descriptor, and deletes the named object before closing the descriptor, so a stale handle cannot match a live queue on the console (technical debt entry, disassembly).

The pin now refuses a handle whose id half does not match the queue registered at its descriptor. Every caller — sceKernelAddUserEvent, sceKernelTriggerUserEvent, sceKernelDeleteUserEvent, sceKernelWaitEqueue, sceKernelDeleteEqueue, sceKernelAddFileEvent and sceKernelDeleteFileEvent — answers EBADF for a stale handle, while a live handle keeps working normally.

Tested

Linux (Omarchy 4.0.2), host build Release with BUILD_TESTING=ON:

  • guest_equeue_events — Passed, 0.66–0.75 s
  • guest_kqueue — Passed, 5.17 s
  • guest_kqueue_cleared_race — Passed, 1.41–1.79 s

The new case creates a queue, deletes it, creates another that recycles the descriptor, then requires EBADF from all seven stale-handle calls and normal operation on the fresh queue. Negative control: with the fix stashed the case aborts, so it genuinely covers the bug.

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

An equeue handle is (id << 32) | descriptor, and EqueuePin_nid_postfix
resolved only the descriptor half, so once a deleted queue's descriptor
number was recycled by a new queue a stale handle resolved to that other
queue: Add event, Trigger event, Delete event, sceKernelWaitEqueue and
the file event calls then added, triggered, deleted or waited on events
of the wrong queue instead of answering SCE_KERNEL_ERROR_EBADF.
sceKernelDeleteEqueue already compared owner->Handle() with eq, showing
the check belongs in the pin. EqueuePin_nid_postfix now returns null
when the queue registered at the descriptor does not match the whole
handle, which covers every caller (also the AgcDriver and VideoOut
event queues) without special-casing any handle value.

Evidence: the fw 13.42 libkernel_sys entry in docs/dev/debt/libkernel.md
records that the console creates a kqueue plus a named object, returns
(id << 32) | descriptor and deletes the named object before closing the
descriptor, so a stale handle cannot match a live queue there; the
changed entry now states the pinned stale-handle result.

Tests: guest_equeue_events gains
VerifyStaleHandleRefusedAfterDescriptorReuse, which creates and deletes
a queue, lets the descriptor recycle into a fresh queue and requires
EBADF from the stale handle on add, trigger and delete user event,
wait, delete queue and add/delete file event, then checks the fresh
queue still works. Without the fix the test aborts; with it
guest_equeue_events passes in 0.7 s, guest_kqueue in 5.2 s and
guest_kqueue_cleared_race in 1.8 s. check_conventions passes;
pr_overlap --branch reports no overlap with open pull requests.

@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 9018a0ee7c:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_equeue_event_tests built cleanly.
  • CTest (guest_equeue_events):
    1/1 Test #5319: guest_equeue_events ..............   Passed    0.70 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Equeue/Equeue.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5319: guest_equeue_events ..............Subprocess aborted***Exception:   0.37 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libkernel/Equeue/Equeue.cpp:691-695 verifies that EqueuePin_nid_postfix compares owner->Handle() against the full 64-bit KernelEqueue handle eq after looking up the low 32-bit descriptor, returning nullptr on mismatch.
    • core/libs/tests/GuestEqueueEvents.cpp:530-558 verifies that deleting an equeue and creating a new one with the recycled descriptor refuses the stale handle with SCE_KERNEL_ERROR_EBADF across all seven equeue operations (sceKernelAddUserEvent, sceKernelTriggerUserEvent, sceKernelDeleteUserEvent, sceKernelWaitEqueue, sceKernelDeleteEqueue, sceKernelAddFileEvent, sceKernelDeleteFileEvent) while preserving normal operation on the fresh handle.
    • Checked concurrency and lifetime safety of owner->Handle() comparison against eq; m_handle is immutable after construction in KernelEqueuePrivate.
    • Checked LP64/LLP64 portability of the 32-bit descriptor cast static_caststd::uint32_t(fresh) == static_caststd::uint32_t(stale) and 64-bit KernelEqueue equality comparison.
    • docs/dev/debt/changes/fix-libkernel-equeue-stale-handle.md properly updates the technical debt documentation for sceKernelCreateEqueue and sceKernelGetKqueueFromEqueue in compliance with AGENTS.md.

Looks Good To Me

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

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 9293 core/libs/prx/libkernel/Equeue/Equeue.cpp
PR 9297 core/libs/prx/libkernel/Equeue/Equeue.cpp
PR 9302 core/libs/prx/libkernel/Equeue/Equeue.cpp

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 9018a0ee7c, merged alone into main 56c87adbdc (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

guest_equeue_events, the test this PR adds or extends, passes 3 of 3 runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@ycbuzi

ycbuzi commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Tested 9018a0ee7c merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 4 tests failed out of 6200; 99% tests passed, 4 tests failed out of 6200. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 6200; 100% tests passed, 0 tests failed out of 6200. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 9018a0ee7c, applied with git apply on main fd4aeed483 together with the other 26 PRs of one batch (#24498, #24665, #17297, #21499, #22534, #12900, #17843, #22051, #22075, #23087, #23206, #23222, #23227, #13147, #17482, #21935, #17477, #18272, #22858, #22929, #23028, #23295, #23916, #24179, #24706, #12619). 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_equeue_events (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 (6525 tests) on the RTX 3080 has 8 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. Traced to other PRs of the batch, each by reverting it and testing again: #18272 (guest_audio_out), #12900 (guest_user_service_initial_user_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): guest_content_search. The 2125 agc_/video tests on the iGPU have 2 failing: agc_driver_stencil_plane_memory, agc_driver_store_over_untracked (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)

@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

Nothing blocks this change; the review found no verified defects in the inspected code.

What I ran

Reviewed head 9018a0ee7ccfd282ec31fb02e4e8dc275a7de2d5 using repo_read.py. No build or tests were run by this reviewer; the test runs and negative control in the description are author-reported.

Results

Check Result Note
Source review passed All changed files and added lines reviewed
Build and tests not run Reported test runs were not independently verified
Negative control not run Author reports the test aborts with the fix stashed

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and test results, including the reported negative control, were not independently run. Console behavior and the firmware disassembly claims were not independently confirmed. All files marked reviewed were read: core/libs/prx/libkernel/Equeue/Equeue.cpp, core/libs/tests/GuestEqueueEvents.cpp, and docs/dev/debt/changes/fix-libkernel-equeue-stale-handle.md. No changed files were excluded.

To get this to a good state

Nothing is required.

@Helldiverr

Copy link
Copy Markdown
Contributor

On Windows, merged onto main 7a0de821ab, guest_equeue_events and the three kqueue neighbour tests pass in 3 of 3 runs. With main's Equeue.cpp, the new stale-handle case aborts. Both mutations that disable the new check in EqueuePin_nid_postfix are caught, and a third shows that sceKernelDeleteEqueue's own handle compare is now redundant.

Existing runs:

  • The author ran guest_equeue_events, guest_kqueue and guest_kqueue_cleared_race on Linux. With the fix stashed, the new case aborted.
  • PrivacyOnion ran guest_equeue_events on Linux, with a negative control that put back main's Equeue.cpp, and the test aborted.
  • maskym ran guest_equeue_events on Windows 11, merged onto 56c87adbdc, and it passed 3 of 3.
  • ycbuzi ran the whole suite on GitHub-hosted Windows Server 2022 and 2025 runners, and nothing failed that does not also fail on main.
  • kiwe147 applied the head together with 26 other PRs onto fd4aeed483 on Windows 11, and guest_equeue_events passed.
  • MarcZX10 read the code.

What this run adds.

  • A Windows negative control and mutations of the new check.
  • The kqueue neighbour tests on Windows, with the PR merged alone onto 7a0de821ab.

Code.

  • The check is in EqueuePin_nid_postfix (core/libs/prx/libkernel/Equeue/Equeue.cpp:691-694 at the head). It compares the whole 64-bit handle with the handle of the queue registered at its descriptor. That handle is built as (id << 32) | descriptor (:738), with ids starting at 1 (:78).

  • Because the check is in the pin, callers outside libkernel get it too. At main:

    With this change, a stale handle with a recycled descriptor takes those paths in both libraries instead of registering on the new queue. The What section and the debt entry name only the libkernel calls, so the two libraries could be added there. This run did not run the AGC or VideoOut tests. kiwe147's batch ran the agc_ and video tests, but on a combined tree.

  • sceKernelDeleteEqueue keeps its own owner->Handle() != eq test (Equeue.cpp:751-752 at the head), and the pin now does the same compare. Mutation q3 below drops that compare, and the test still passes. if (!owner) would be enough there.

  • sceKernelGetKqueueFromEqueue (libkernel_sys/Export.Discovery.cpp:848-855 at main) takes no pin. It writes the handle's low 32 bits, so a stale handle whose descriptor was recycled still returns 0 and the live queue's descriptor. The fragment's heading covers this function, but its sentence on the calls that answer EBADF leaves it out. It also does not say what the console's function does with a stale handle. Its test, CheckKqueueFromEqueue (GuestKernelSys.cpp:1228-1240 at main), uses a live handle only.

  • The diff adds no code comments and no title-specific values. Debt goes through its own fragment, docs/dev/debt/changes/fix-libkernel-equeue-stale-handle.md.

  • tools/check_conventions.py --base 7a0de821ab exits 0 with one warning on that fragment: [debt-length] Technical debt entry over 1000 characters ... 1458 characters. The entry is already 1036 characters at main (docs/dev/debt/libkernel.md:183), and the new sentence adds the rest.

  • tools/debt_fold.py --check exits 1 on the merge and on main 7a0de821ab with the same message. Two libSceRudp fragments, fix-rudp-unconnected-read-write.md and fix-rudp-net-received-checks.md, target one entry. This PR does not touch them.

Negative control and mutation. All of these ran on the merge, using guest_equeue_events.

Change Result
Equeue.cpp from main caught: FATAL: abort(), exit 0xc0000409
q1: the three-line check in EqueuePin_nid_postfix removed caught: exit 0xc0000409
q2: the condition replaced with static_cast<std::uint32_t>(owner->Handle() >> 32) == 0, which no live queue meets caught: exit 0xc0000409
q3: sceKernelDeleteEqueue's || owner->Handle() != eq removed survives (passes), as expected with the pin doing the compare

Tests. ctest --test-dir build -R "^guest_(equeue_events|kqueue|kqueue_cleared_race|kqueue_user_state)$" -V

Tree Run 1 Run 2 Run 3
main 7a0de821ab 4/4 - -
merge ef735799c6 4/4 4/4 4/4

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 9018a0ee7c, merged onto main 7a0de821ab as ef735799c6

main has since moved 87 commits to 2711791787. The head still merges cleanly, none of those commits change the code or lines cited here, and the two libSceRudp fragments now fold. With this PR merged onto 52e74b8d79, tools/debt_fold.py --check exits 0 and check_conventions.py gives the same single debt-length warning.

AI-assisted: yes

@oneandonlydean
oneandonlydean merged commit 80e2b7a into boykopovar:main Nov 27, 2026
11 checks passed
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.

8 participants