Skip to content

fix(libc): size CopyGuestString's buffer by the bytes it reads - #596

Open
Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/20678
Open

Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/20678

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#20678, originally opened by milescoviello.

Source commit: c90b8d8aa603b66958d61696a71fe0d140cfc063. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

CopyGuestString_nid_no_patch resized its buffer to the whole maximum before reading the first byte. So the cost of a call grew with the cap rather than the string. A cap that couldn't be allocated also returned 12 (ENOMEM) even when the string was a few bytes long, so a caller had to pick a cap it could afford to allocate on every call.

The buffer now grows one page chunk at a time, by exactly the bytes about to be copied. The loop, the page-sized reads, the terminator search, the copied text and the return codes for every cap that could be allocated before (0, 14 for unreadable memory, 63 for no terminator within the cap) are unchanged. A short string with a large cap now costs one small allocation, and a cap like SIZE_MAX / 2 works, reading only up to the terminator the way strlen would.

There were no direct tests, so this adds GuestCopyGuestString.cpp, registered through a tests/cmake fragment gated on BUILD_TESTING the way libc_runtime_assertion_tests.cmake is.

Tested

11-21, 1ed2a4caa8 (rebased on main 19729fa854): the buffer resize and the final assign in CopyGuestString_nid_no_patch also return 12 when they throw std::length_error, not only std::bad_alloc, so neither exception leaves a C API through callers that translate 12 (MarcZX10's point on #23418, which carries this helper). In practice it can't be thrown: the buffer grows a page at a time and the copy stops at the first unreadable page. Linux, GCC 15.2, Ninja Release: guest_copy_guest_string passes in each of 3 runs. Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release): it passes in each of 3 runs.

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

11-24, c90b8d8aa6: guard-page cases from Helldiverr's run. Helldiverr's Windows run (Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, this head's parent 1ed2a4caa8 merged onto main fa86dfcd3b): guest_copy_guest_string passes in 3 of 3 runs, main's General.cpp fails it, and 4 of 5 mutations are caught. The survivor: removing the page alignment still passed, because every test source was readable well past its terminator. c90b8d8aa6 adds cases with a string at the end of a readable page that has an inaccessible page after it: the unbounded and exact caps copy it, and the not-terminated text returns 63 at a cap that ends at the inaccessible page and 14 at one that reaches it.

  • Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged onto main adacf0bd83: guest_copy_guest_string passes in 3 of 3 runs, and without the page alignment it aborts.

  • Windows 11 VM (WinLibs GCC 16.2.0, Release), the same merge: passes in 3 of 3 runs.

  • Not verified on Windows: in Helldiverr's run, a bad_alloc forced inside libc.prx (by the negative control and one mutation) stopped the process as an undeliverable exception, so the 12 returns are measured on Linux only.

  • Helldiverr's Windows re-check at c90b8d8aa6, merged onto main 95702cd8d8: guest_copy_guest_string passes in 3 of 3 runs, and without the page alignment it fails at the first guard-page case.

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of guest strings with large length limits by growing storage as needed, reducing unnecessary memory use.
    • Preserved existing results for invalid memory, allocation failures, and strings that lack a terminator within the specified limit.

milescoviello and others added 3 commits November 21, 2026 04:34
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_error

The buffer resize and the final assign caught only std::bad_alloc. A
std::length_error now returns 12 as well, so neither exception leaves a
C API through the callers that translate 12.

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

Every source the test used was readable well past its terminator, so
reading a whole host page from an unaligned start still passed. Put a
string at the end of a readable page with an inaccessible page after it,
and check the unbounded and exact caps, and the not-terminated cases that
stop at or reach the inaccessible page. From Helldiverr's Windows probe
on boykopovar#20678.

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

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Guest string copy

Layer / File(s) Summary
Incremental guest-string copy
core/libs/prx/libc/src/General.cpp
The function grows its buffer in page-bounded chunks, stopping when it finds a null terminator or reaches the maximum. It returns 12 for allocation and length errors.
Copy behavior tests and registration
core/libs/tests/GuestCopyGuestString.cpp, core/libs/prx/libc/tests/cmake/guest_copy_guest_string.cmake
The tests check return codes, output, and reads at guarded page boundaries. CMake builds and registers the executable when BUILD_TESTING is enabled.

Change: Bug fix

Merge Risk: Minimal · up to c90b8

This change lets guest-string copies with very large caps work for short strings without huge upfront allocations, and it keeps the existing return codes. New boundary tests cover the main edge cases. No merge-blocking risk was found.

Security architecture risk: Minimal · up to c90b8

libc can now copy a short guest string without allocating the caller’s entire maximum. The caller’s read limit and the existing memory-read path remain in place. Some calls that previously failed during allocation can now complete, but the reviewed change does not establish a new privilege, destination, or material loss of failure containment.

Security review details

Security Blast Radius

  • inferred — The changed resource behavior is reachable through existing guest-facing callers, including libSceRtc and libSceNpCommon. The change lets some previously allocation-limited calls proceed, without adding a production caller or changing the caller-supplied bounds.

Trust Boundaries and Controls

  • observed — Guest source bytes still pass through CopyGuestBytes, which invokes the existing GuestArena host-read preparation hook and uses the platform-specific read path. The allocation change does not replace either mechanism or turn the caller’s cap into permission to read beyond it.

Resilience and Maintainability Implications

  • observed — Scratch storage is local to the call. Read failure, cap exhaustion, and handled allocation or length failures return without publishing a partial destination string; the next call starts with fresh scratch storage.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 3
  • Waived: $0.75 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5ea40e40-13d5-4d5c-bfdf-17913715971f
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and c90b8d8.

Files selected for processing: 3

  • core/libs/prx/libc/src/General.cpp
  • core/libs/prx/libc/tests/cmake/guest_copy_guest_string.cmake
  • core/libs/tests/GuestCopyGuestString.cpp

Limit details: You’ve used all 10 included reviews currently available.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 946 core/libs/prx/libc/tests/cmake/guest_copy_guest_string.cmake
core/libs/tests/GuestCopyGuestString.cpp

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.

2 participants