Skip to content

fix(libSceUserService): stage user group names before guest copy-out - #22315

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
THEMANnt:fix/user-group-name-output-20261117
Nov 17, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
THEMANnt:fix/user-group-name-output-20261117

Conversation

@THEMANnt

@THEMANnt THEMANnt commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Read stored user-group names and format fallback names into a host buffer, then publish the complete fixed-size record through GuestCopyOut. Keep file closure and the existing fallback behavior for failed or empty reads.

The getter requires a 129-byte destination. Its existing checks reject smaller sizes with USER_SERVICE_ERROR_BUFFER_TOO_SHORT and larger sizes with USER_SERVICE_ERROR_INVALID_ARGUMENT before file I/O or guest writes.

Tested

Windows 11, WinLibs GCC 15.2.0, Ninja, Release. Built guest_user_service_group_io_tests; guest_user_service_group_io passed three consecutive runs. New cases cover unmapped and read-only output buffers, stored-name and fallback paths, preserved errno, and descriptor closure.

Follow-up regression coverage checks sizes 0, 1, 16, 128, 130 and SIZE_MAX, unchanged buffer canaries and errno, no file I/O on rejected sizes, and null-pointer ordering. The updated guest_user_service_group_io passed on Windows with WinLibs GCC 15.2.0.

Negative control: with the new tests retained and the implementation unchanged on main e2ca7c394a, the test fails with a segmentation fault. Restoring the fix passes. git diff --check and tools/check_conventions.py --base upstream/main passed. The required origin/main comparison also ran; its errors concern upstream commit subjects since the older fork main. No title, Linux, or macOS validation.

Export overlap lookup found no competing implementation. Manually compared the seven conflicting shared-file PRs (#9345, #9828, #12900, #14829, #14851, #20162, #21521); they do not change this getter.

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

@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 607654eb51:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_user_service_group_io_tests built cleanly.
  • CTest (guest_user_service_group_io):
    1/1 Test #5029: guest_user_service_group_io ......   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceUserService/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5029: guest_user_service_group_io ......***Exception: SegFault  2.32 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sceUserServiceGetUserGroupName parameter checks: size parameter is ignored by GuestCopyOut_nid_no_patch which copies sizeof(output) (129 bytes) unconditionally into guest name buffer
    • Inspected buffer overflow vulnerability: guest caller specifying size < 129 (or smaller buffer with size parameter) will suffer a buffer overrun via unchecked GuestCopyOut of 129 bytes
    • Checked error handling for nullptr and invalid groupIndex in Export.cpp: UserGroupSlotExists(groupIndex) properly rejects invalid indices
    • Verified errno preservation across GuestCopyOut_nid_no_patch calls
    • Examined docs/dev/debt/changes/user-group-name-output.md technical debt fragment format and conventions
  • Concerns:
    • core/libs/prx/libSceUserService/Export.cpp:1159: sceUserServiceGetUserGroupName accepts a size parameter (std::size_t size) representing the capacity of the guest destination buffer name, but passes sizeof(output) (129 bytes) to GuestCopyOut_nid_no_patch(name, output, sizeof(output)) without checking size < sizeof(output). If a caller passes a buffer smaller than 129 bytes (or specifies size < 129), GuestCopyOut_nid_no_patch overwrites 129 bytes into the guest memory, causing a guest buffer overflow / memory corruption. Either size must be validated (e.g. returning USER_SERVICE_ERROR_BUFFER_TOO_SHORT or USER_SERVICE_ERROR_INVALID_ARGUMENT if size < 129, similar to sceUserServiceGetUserName), or copy at most std::min(size, sizeof(output)).

Requesting changes for the points above.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I read the new copy-out and it ignores the caller's size: it always writes 129 bytes. The old fallback was bounded by size, so a guest that passes a smaller buffer now gets an overrun. PrivacyOnion's review makes the same point.

Please bound the copy by size:

  • n = min(size, sizeof(output)); with n == 0 return INVALID_ARGUMENT.
  • If n < sizeof(output), set output[n - 1] to 0 so the name stays terminated.
  • Copy n bytes with GuestCopyOut_nid_no_patch.
  • Add a test with a size-16 buffer next to the existing size-129 cases.

The rest looks right to me: the staging, the errno restore and the INVALID_ARGUMENT on a bad pointer.

@THEMANnt

Copy link
Copy Markdown
Contributor Author

@PrivacyOnion @oneandonlydean The size checks above the changed hunk already require exactly 129 bytes. size < 129 returns USER_SERVICE_ERROR_BUFFER_TOO_SHORT, and size > 129 returns USER_SERVICE_ERROR_INVALID_ARGUMENT, before file I/O or the copy-out.

I added explicit regression coverage in d2491d3 for sizes 0, 1, 16, 128, 130 and SIZE_MAX. These calls preserve the destination canaries and errno and perform no file I/O. The Windows guest_user_service_group_io CTest and conventions check pass.

The implementation keeps its existing exact-size contract. Could you recheck the retained validation above the new copy-out?

@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 d2491d383e:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_user_service_group_io_tests built cleanly.
  • CTest (guest_user_service_group_io):
    1/1 Test #5030: guest_user_service_group_io ......   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceUserService/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5030: guest_user_service_group_io ......***Exception: SegFault  1.49 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sceUserServiceGetUserGroupName parameter checks: pre-existing validation requires size == 129, returning USER_SERVICE_ERROR_BUFFER_TOO_SHORT for size < 129 and USER_SERVICE_ERROR_INVALID_ARGUMENT for size > 129 before any file I/O or guest write.
    • Verified copy-out safety: output is staged into a 129-byte host buffer initialized to zero, populated via read or fallback formatting, and safely copied out via GuestCopyOut_nid_no_patch with errno preserved across the call.
    • Verified resource cleanup: descriptor opened via sceKernelOpen is closed on both read success and failure before copy-out or exit.
    • Verified AGENTS.md conventions: technical debt fragment docs/dev/debt/changes/user-group-name-output.md adheres to the required format and labels, and no forbidden code comments were introduced.
    • Verified test coverage in GuestUserServiceGroupIo.cpp: explicit regression coverage exercises boundary sizes (0, 1, 16, 128, 130, SIZE_MAX), nullptr handling, invalid group indices, unmapped/read-only memory, and descriptor leak prevention.

Looks Good To Me

@oneandonlydean

Copy link
Copy Markdown
Collaborator

You are right: the size checks at the top of sceUserServiceGetUserGroupName return early for any size other than 129, so the 129-byte copy-out cannot overrun. Our request for a size bound was mistaken. The new tests for the short and long sizes are a good addition. I passed it on d2491d3.

@oneandonlydean
oneandonlydean merged commit f7666f3 into boykopovar:main Nov 17, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants