fix(libSceLibreSsl3): copy ASN1_get_object and d2i_ASN1_INTEGER guest pointers safely #22286

Merged
Gruzzberry merged 1 commit from upstream-pr/22286 into main 2026-11-17 12:51:56 +00:00
Gruzzberry commented 2026-11-17 11:17:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-17T11:17:28Z.


What

ASN1_get_object and d2i_ASN1_INTEGER in IntegerDecoder.cpp dereferenced guest pointers directly (*input, *output, *tag, *tagClass, *length and the input buffer). They now read and write them through GuestCopyIn_nid_no_patch / GuestCopyOut_nid_no_patch, and d2i_ASN1_INTEGER copies its input buffer once per call. An unreadable or unwritable pointer throws std::invalid_argument, as the null checks already do. Behaviour for valid pointers is unchanged.

Tested

  • g++ -std=c++20 -fsyntax-only on IntegerDecoder.cpp with the repo include dirs: no errors. Not linked or run; submodules are not initialised.
  • python3 tools/check_conventions.py --base upstream/main: passed.
  • Duplicate check: a script comparing against every open PR's changed files reported CLEAN, before and after the change.
  • tools/guest_pointer_audit.py libSceLibreSsl3 flagged these lines before the change.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5355816994 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22286). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-17T11:17:28Z. --- ### What `ASN1_get_object` and `d2i_ASN1_INTEGER` in `IntegerDecoder.cpp` dereferenced guest pointers directly (`*input`, `*output`, `*tag`, `*tagClass`, `*length` and the input buffer). They now read and write them through `GuestCopyIn_nid_no_patch` / `GuestCopyOut_nid_no_patch`, and `d2i_ASN1_INTEGER` copies its input buffer once per call. An unreadable or unwritable pointer throws `std::invalid_argument`, as the null checks already do. Behaviour for valid pointers is unchanged. ### Tested - `g++ -std=c++20 -fsyntax-only` on `IntegerDecoder.cpp` with the repo include dirs: no errors. Not linked or run; submodules are not initialised. - `python3 tools/check_conventions.py --base upstream/main`: passed. - Duplicate check: a script comparing against every open PR's changed files reported CLEAN, before and after the change. - `tools/guest_pointer_audit.py libSceLibreSsl3` flagged these lines before the change. ### Checklist - [x] Based on current `main`; 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) and the licence notice of code adapted from upstream (see AGENTS.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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-17 11:33:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T11:33:57Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this; the points below are optional.

What I ran

  • Read the full head text of IntegerDecoder.cpp (254 lines) at fb39cc82ca445de9c664cf7f2890025880817a86, and the diff.
  • repo_read.py search GuestCopyIn_nid_no_patch, to check the helper's call pattern against the repository.
  • No compiler, linker or tests in this review.
  • Files read: core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp. Files reviewed: the same one. Excluded by rule: none.

Results

Check Result Note
Read of the changed file passed Full text, 254 lines
Compile or link not run No compiler run here
Author's syntax check not run Author-reported only
Author's convention and audit scripts not run Author-reported only

Findings

Blocking

None.

Non-blocking

  1. core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:198: d2i_ASN1_INTEGER now allocates length bytes and copies all of them from guest memory before parsing. The old code only touched the bytes the ASN.1 header covers. A guest-supplied length much larger than the object allocates that much memory. A length that runs past readable guest memory now throws invalid_argument where the old code returned the integer. So the description's claim that "behaviour for valid pointers is unchanged" does not hold for these inputs. Smallest fix: parse the header in bounded reads, or cap the copy and state in a comment-free way that length must be readable.
  2. core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:249–250: if GuestStore(input, ...) at line 250 throws, the integer and data allocations (lines 230 and 241) are never freed. *output was already set to integer at line 249, so the caller gets an exception with output pointing at a leaked object. Smallest fix: check that input is writable before allocating, or free on that path.
  3. core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:209: the old pointer is now read through GuestLoad, but FreeInteger (unchanged, lines 139–152) still dereferences the object it points to directly. The fix guards the pointer read, not the object. If GuestStore at line 210 throws after the free, *output is left pointing at freed memory.

Not verified

  • Whether GuestCopyIn_nid_no_patch returns false, rather than faulting, on unreadable or unmapped guest memory. I checked the call sites only, not the helper.
  • Whether a std::length_error or std::bad_alloc from the new vector at line 198 is caught at the extern "C" boundary.
  • Whether the object that *output points to (point 3) lives in guest memory.
  • Compile, link, and the author's checks (check_conventions.py, duplicate check, guest_pointer_audit.py). None were run here.
  • Runtime behaviour of libSceLibreSsl3 in the emulator.

To get this to a good state

Nothing is required. The non-blocking points above are optional.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5937667214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22286#pullrequestreview-5937667214). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-17T11:33:57Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this; the points below are optional. ## What I ran - Read the full head text of `IntegerDecoder.cpp` (254 lines) at `fb39cc82ca445de9c664cf7f2890025880817a86`, and the diff. - `repo_read.py search GuestCopyIn_nid_no_patch`, to check the helper's call pattern against the repository. - No compiler, linker or tests in this review. - Files read: `core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp`. Files reviewed: the same one. Excluded by rule: none. ## Results | Check | Result | Note | |---|---|---| | Read of the changed file | passed | Full text, 254 lines | | Compile or link | not run | No compiler run here | | Author's syntax check | not run | Author-reported only | | Author's convention and audit scripts | not run | Author-reported only | ## Findings ### Blocking None. ### Non-blocking 1. `core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:198`: `d2i_ASN1_INTEGER` now allocates `length` bytes and copies all of them from guest memory before parsing. The old code only touched the bytes the ASN.1 header covers. A guest-supplied `length` much larger than the object allocates that much memory. A `length` that runs past readable guest memory now throws `invalid_argument` where the old code returned the integer. So the description's claim that "behaviour for valid pointers is unchanged" does not hold for these inputs. Smallest fix: parse the header in bounded reads, or cap the copy and state in a comment-free way that `length` must be readable. 2. `core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:249`–`250`: if `GuestStore(input, ...)` at line 250 throws, the `integer` and `data` allocations (lines 230 and 241) are never freed. `*output` was already set to `integer` at line 249, so the caller gets an exception with `output` pointing at a leaked object. Smallest fix: check that `input` is writable before allocating, or free on that path. 3. `core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:209`: the old pointer is now read through `GuestLoad`, but `FreeInteger` (unchanged, lines 139–152) still dereferences the object it points to directly. The fix guards the pointer read, not the object. If `GuestStore` at line 210 throws after the free, `*output` is left pointing at freed memory. ## Not verified - Whether `GuestCopyIn_nid_no_patch` returns false, rather than faulting, on unreadable or unmapped guest memory. I checked the call sites only, not the helper. - Whether a `std::length_error` or `std::bad_alloc` from the new vector at line 198 is caught at the `extern "C"` boundary. - Whether the object that `*output` points to (point 3) lives in guest memory. - Compile, link, and the author's checks (`check_conventions.py`, duplicate check, `guest_pointer_audit.py`). None were run here. - Runtime behaviour of `libSceLibreSsl3` in the emulator. ## To get this to a good state Nothing is required. The non-blocking points above are optional.
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!22286
No description provided.