Skip to content

fix(libSceUsbd): refuse a transfer that sceUsbdAllocTransfer did not return - #22965

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/usbd-allocated-transfers
Dec 3, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/usbd-allocated-transfers

Conversation

@milescoviello

@milescoviello milescoviello commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What

sceUsbdAllocTransfer returns a host calloc allocation and records it with its iso packet capacity. sceUsbdFreeTransfer frees it and drops the record. The calls that fill or read a transfer wrote and read its fields through the caller's pointer without looking at that record. A pointer that sceUsbdAllocTransfer didn't return, or one already freed, was therefore written as host memory: a stray pointer crashed the host, and a freed one corrupted the host heap.

Changes:

  • sceUsbdFillInterruptTransfer, sceUsbdFillBulkTransfer, sceUsbdFillIsoTransfer, sceUsbdFillControlTransfer, sceUsbdControlTransferGetData, sceUsbdControlTransferGetSetup, sceUsbdSetIsoPacketLengths and sceUsbdGetIsoPacketBuffer check the pointer against the allocated transfers before touching any field.
  • An unknown or freed transfer throws "the transfer was not returned by sceUsbdAllocTransfer or was already freed". That follows the existing messages for a device not returned by sceUsbdGetDeviceList and a handle not returned by sceUsbdOpen. A null transfer still throws "null transfer".
  • sceUsbdFillIsoTransfer and sceUsbdSetIsoPacketLengths already treated an unknown transfer as having no iso packets, so an iso fill of one already threw. The check now runs before the capacity check, in the same locked block.

Allocated transfers behave as before. sceUsbdSubmitTransfer and sceUsbdCancelTransfer are left alone, since #21530 and #21195 edit them.

Tested

Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release):

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

  • guest_usbd passes, 3 runs, on 09a93376e3 (this PR's head).

  • Negative control: the same tree with main's Export.cpp fails guest_usbd with an abort from a transfer sceUsbdAllocTransfer did not return was accepted.

  • Independent full-suite runs by @ycbuzi (report) of 09a93376e3 merged into main, WinLibs GCC 15.2.0: Windows Server 2025, 6765 of 6765 in each of 2 runs; Windows Server 2022, 2 and 3 failures of 6765 in 2 runs, 2 of which also fail on main there.

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

…return

The transfer fill and accessor calls wrote and read the transfer through
the caller's pointer. Transfers are host allocations here, so a pointer
that sceUsbdAllocTransfer did not return, or one sceUsbdFreeTransfer had
already freed, was written as host memory. Each call now checks the
pointer against the allocated transfers first and throws for any other,
as the library already does for unknown devices and handles.

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 09a93376e3:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_usbd_tests built cleanly.
  • CTest (guest_usbd):
    1/1 Test #5130: guest_usbd .......................   Passed    2.36 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceUsbd/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5130: guest_usbd .......................Subprocess aborted***Exception:   0.18 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceUsbd/Export.cpp: Verified RequireAllocatedTransfer correctly guards all 8 transfer filling/access functions (sceUsbdFillInterruptTransfer, sceUsbdFillBulkTransfer, sceUsbdFillIsoTransfer, sceUsbdFillControlTransfer, sceUsbdControlTransferGetData, sceUsbdControlTransferGetSetup, sceUsbdSetIsoPacketLengths, sceUsbdGetIsoPacketBuffer) with mutex lock and throws on null or unallocated transfers
    • core/libs/prx/libSceUsbd/Export.cpp: Checked mutex locking semantics in sceUsbdFillIsoTransfer, sceUsbdSetIsoPacketLengths, and sceUsbdGetIsoPacketBuffer to ensure state.mutex is held during RequireAllocatedTransfer and IsoCapacity queries without double-locking or TOCTOU races
    • docs/dev/debt/changes/fix-usbd-allocated-transfers.md: Verified technical debt fragment follows repository conventions, categorizes under Functional, cites the functions, and documents console behavior and closing condition
    • core/libs/tests/GuestUsbd.cpp: Verified test coverage exercises unallocated, freed, and null transfers across all modified functions, and verifies that legitimate allocated transfers continue to function properly

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 — no blocking findings

Nothing blocks this change; the prepared bundle and all three changed files were read, but no checks were run in this review.

What I ran

  • Read the prepared diff and source context at head 09a93376e32736204d52f21ad151ef32e92388eb using repo_read.py.
  • No tests or other checks were run.

Results

Check Result Note
Changed source review passed All three changed files read
Tests not run No test execution available

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • The author reports guest_usbd passed on Linux at the reviewed head, and reports Windows testing on a pre-squash commit; neither result was independently verified.
  • Console behavior and the reported convention and debt checks were not verified.
  • Files read: core/libs/prx/libSceUsbd/Export.cpp, core/libs/tests/GuestUsbd.cpp, and docs/dev/debt/changes/fix-usbd-allocated-transfers.md. No changed files were excluded; files.json was not available through the reader.

To get this to a good state

Nothing is required.

@ycbuzi

ycbuzi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Tested 09a93376e3 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, 2 tests failed out of 6765; 99% tests passed, 3 tests failed out of 6765. 2 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 6765; 100% tests passed, 0 tests failed out of 6765. run

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

@milescoviello

Copy link
Copy Markdown
Contributor Author

@ycbuzi thank you for the full-suite runs on both Windows Server images. I added them to Tested.

@oneandonlydean
oneandonlydean merged commit 67962e3 into boykopovar:main Dec 3, 2026
18 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.

5 participants