fix(libSceLibreSsl): copy cipher, key, rand, X509V3 and PEM guest pointers safely #22476

Open
domx4q wants to merge 1 commit from upstream-pr/22476 into main
domx4q commented 2026-11-17 17:29:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by domx4q on 2026-11-17T17:29:31Z.


What

Six libSceLibreSsl exports no longer dereference guest pointers. I checked the host-allocated-object exception first, and none of the six applies. The objects are all guest memory:

  • EVP_PKEY and RSA come from the guest malloc.
  • SSL_CTX is a guest structure; SslCtx.cpp already reads it through GuestCopyIn.
  • EVP_CIPHER has a public layout, and EvpCipherCtx.cpp already copies it in through LoadCipher.
  • The extension, the RAND_file_name buffer and the PEM_read_RSAPrivateKey output pointer are guest memory supplied by the caller.

Changes:

  • SSL_CTX_set_cipher_list (src/CipherList.cpp) reads the context's internal pointer, its cipher list and its by-id list through GuestCopyIn_nid_no_patch. Before the lists are rebuilt, it checks with GuestWritable_nid_no_patch that both list fields are writable, so a read-only context fails before the old lists are freed. It writes the new lists back through GuestCopyOut_nid_no_patch.
  • EVP_CIPHER_block_size (src/EvpCipher.cpp) and EVP_PKEY_id (src/PkeyId.cpp) copy the block size and the key type in.
  • RAND_file_name (src/Runtime.cpp) builds the path on the host and writes at most the requested bytes with one GuestCopyOut_nid_no_patch. Nothing is sized from the guest length.
  • PEM_read_RSAPrivateKey (src/PemX509.cpp) checks that the output pointer is readable and writable before it reads the key. It then copies the old key in and the new key out.
  • X509V3_EXT_print (src/X509V3Print.cpp) copies in the extension, its object (with the OID bytes when it has no NID) and its value, and prints from host copies. The OID and value bytes are read in 4 KiB chunks, and the host buffer only grows by bytes that were already read. A huge length on a short value fails with the normal guest error instead of causing a large host allocation.

errno is preserved, as the helpers in SslCtx.cpp and EvpCipherCtx.cpp already do. An unreadable or unwritable pointer now throws through NotImplementedAt_nid_no_patch, which is how this library already rejects them. It is no longer dereferenced. Results for valid inputs are byte-for-byte the same, and the existing tests for all six functions pass unchanged.

tools/guest_pointer_audit.py libSceLibreSsl no longer lists any of the six functions. tools/progress.py --module libSceLibreSsl shows 302 done on both origin/main and this branch. The debt entries for these functions describe layouts, sources and null handling, not unchecked guest access, so there is no debt fragment. No listed function was a false positive.

Not changed: the rules string of SSL_CTX_set_cipher_list, which the audit does not list. LibreSslObjectNid_nid_no_patch and the extension value decoders are also unchanged; X509V3_EXT_print now hands them only host copies.

Overlap: python3 tools/pr_overlap.py --names SSL_CTX_set_cipher_list EVP_CIPHER_block_size EVP_PKEY_id RAND_file_name X509V3_EXT_print PEM_read_RSAPrivateKey lists only #17299 and #17303. Those PRs name libSceLibreSsl3 parent-export aliases and change no file in core/libs/prx/libSceLibreSsl/. python3 tools/pr_overlap.py --branch fix/libressl-misc-guest-pointers reports no overlap with open pull requests.

Tested

Arch Linux, GCC, Release. All 96 libSceLibreSsl tests passed (ctest --test-dir build -R 'guest_libressl_|guest_libre_ssl|guest_digest_extents|libressl' -E libressl3), and ctest -R '^public_headers' passed (65/65).

New cases pass an unmapped pointer (0x10) or read-only memory to each export, and each call must throw with errno preserved:

  • tests/CipherList.cpp: unmapped context, unmapped internal state, read-only internal state. The cipher list must stay unset.
  • GuestLibreSslEvpCipher.cpp: unmapped cipher.
  • GuestLibreSslPkeyId.cpp: unmapped key.
  • GuestLibreSslRuntime.cpp: unmapped buffer with sizes 1, 13 and SIZE_MAX, and a read-only buffer.
  • GuestLibreSslPemPrivateKey.cpp: unmapped and read-only output pointer, with no error queued.
  • GuestLibreSslX509V3Print.cpp: unmapped extension, object and value. The OID and the value are also placed just before a guard page: they must print exactly as before, and an INT_MAX length must be rejected without changing the allocations.

Mutation checks:

  • With the six source files reset to origin/main, guest_libressl_cipher_list, guest_libressl_evp_cipher, guest_libressl_pkey_id, guest_libressl_runtime, guest_libressl_x509v3_print and guest_libressl_pem_private_key crash with SegFault.
  • With only the writability check of the by-id list removed from SSL_CTX_set_cipher_list, guest_libressl_cipher_list fails at line 370.

python3 tools/check_conventions.py --base origin/main and python3 tools/debt_fold.py --check passed.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5361765549 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22476). Originally posted by [domx4q](https://github.com/domx4q) on 2026-11-17T17:29:31Z. --- ### What Six libSceLibreSsl exports no longer dereference guest pointers. I checked the host-allocated-object exception first, and none of the six applies. The objects are all guest memory: - `EVP_PKEY` and `RSA` come from the guest `malloc`. - `SSL_CTX` is a guest structure; `SslCtx.cpp` already reads it through `GuestCopyIn`. - `EVP_CIPHER` has a public layout, and `EvpCipherCtx.cpp` already copies it in through `LoadCipher`. - The extension, the `RAND_file_name` buffer and the `PEM_read_RSAPrivateKey` output pointer are guest memory supplied by the caller. Changes: - `SSL_CTX_set_cipher_list` (`src/CipherList.cpp`) reads the context's internal pointer, its cipher list and its by-id list through `GuestCopyIn_nid_no_patch`. Before the lists are rebuilt, it checks with `GuestWritable_nid_no_patch` that both list fields are writable, so a read-only context fails before the old lists are freed. It writes the new lists back through `GuestCopyOut_nid_no_patch`. - `EVP_CIPHER_block_size` (`src/EvpCipher.cpp`) and `EVP_PKEY_id` (`src/PkeyId.cpp`) copy the block size and the key type in. - `RAND_file_name` (`src/Runtime.cpp`) builds the path on the host and writes at most the requested bytes with one `GuestCopyOut_nid_no_patch`. Nothing is sized from the guest length. - `PEM_read_RSAPrivateKey` (`src/PemX509.cpp`) checks that the output pointer is readable and writable before it reads the key. It then copies the old key in and the new key out. - `X509V3_EXT_print` (`src/X509V3Print.cpp`) copies in the extension, its object (with the OID bytes when it has no NID) and its value, and prints from host copies. The OID and value bytes are read in 4 KiB chunks, and the host buffer only grows by bytes that were already read. A huge length on a short value fails with the normal guest error instead of causing a large host allocation. errno is preserved, as the helpers in `SslCtx.cpp` and `EvpCipherCtx.cpp` already do. An unreadable or unwritable pointer now throws through `NotImplementedAt_nid_no_patch`, which is how this library already rejects them. It is no longer dereferenced. Results for valid inputs are byte-for-byte the same, and the existing tests for all six functions pass unchanged. `tools/guest_pointer_audit.py libSceLibreSsl` no longer lists any of the six functions. `tools/progress.py --module libSceLibreSsl` shows 302 done on both `origin/main` and this branch. The debt entries for these functions describe layouts, sources and null handling, not unchecked guest access, so there is no debt fragment. No listed function was a false positive. Not changed: the `rules` string of `SSL_CTX_set_cipher_list`, which the audit does not list. `LibreSslObjectNid_nid_no_patch` and the extension value decoders are also unchanged; `X509V3_EXT_print` now hands them only host copies. Overlap: `python3 tools/pr_overlap.py --names SSL_CTX_set_cipher_list EVP_CIPHER_block_size EVP_PKEY_id RAND_file_name X509V3_EXT_print PEM_read_RSAPrivateKey` lists only #17299 and #17303. Those PRs name libSceLibreSsl3 parent-export aliases and change no file in `core/libs/prx/libSceLibreSsl/`. `python3 tools/pr_overlap.py --branch fix/libressl-misc-guest-pointers` reports no overlap with open pull requests. ### Tested Arch Linux, GCC, Release. All 96 libSceLibreSsl tests passed (`ctest --test-dir build -R 'guest_libressl_|guest_libre_ssl|guest_digest_extents|libressl' -E libressl3`), and `ctest -R '^public_headers'` passed (65/65). New cases pass an unmapped pointer (0x10) or read-only memory to each export, and each call must throw with errno preserved: - `tests/CipherList.cpp`: unmapped context, unmapped internal state, read-only internal state. The cipher list must stay unset. - `GuestLibreSslEvpCipher.cpp`: unmapped cipher. - `GuestLibreSslPkeyId.cpp`: unmapped key. - `GuestLibreSslRuntime.cpp`: unmapped buffer with sizes 1, 13 and `SIZE_MAX`, and a read-only buffer. - `GuestLibreSslPemPrivateKey.cpp`: unmapped and read-only output pointer, with no error queued. - `GuestLibreSslX509V3Print.cpp`: unmapped extension, object and value. The OID and the value are also placed just before a guard page: they must print exactly as before, and an `INT_MAX` length must be rejected without changing the allocations. Mutation checks: - With the six source files reset to `origin/main`, `guest_libressl_cipher_list`, `guest_libressl_evp_cipher`, `guest_libressl_pkey_id`, `guest_libressl_runtime`, `guest_libressl_x509v3_print` and `guest_libressl_pem_private_key` crash with SegFault. - With only the writability check of the by-id list removed from `SSL_CTX_set_cipher_list`, `guest_libressl_cipher_list` fails at line 370. `python3 tools/check_conventions.py --base origin/main` and `python3 tools/debt_fold.py --check` passed. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (Claude Code)
PrivacyOnion commented 2026-11-17 17:33:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T17:33:44Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against f2cf79de26:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl_cipher_list_tests, guest_libressl_evp_cipher_tests, guest_libressl_pem_private_key_tests, guest_libressl_pkey_id_tests, guest_libressl_runtime_tests, guest_libressl_x509v3_print_tests built cleanly.
  • CTest (guest_libressl_pem_private_key, guest_libressl_runtime, guest_libressl_pkey_id, guest_libressl_evp_cipher, guest_libressl_x509v3_print, guest_libressl_cipher_list, guest_libressl_cipher_list_allocator):
    1/7 Test #4260: guest_libressl_pkey_id .................   Passed    0.04 sec
    2/7 Test #4259: guest_libressl_runtime .................   Passed    0.04 sec
    3/7 Test  #715: guest_libressl_pem_private_key .........   Passed    0.10 sec
    4/7 Test #4277: guest_libressl_evp_cipher ..............   Passed    0.04 sec
    5/7 Test #4282: guest_libressl_x509v3_print ............   Passed    0.06 sec
    6/7 Test #4292: guest_libressl_cipher_list_allocator ...   Passed    0.06 sec
    7/7 Test #4291: guest_libressl_cipher_list .............   Passed    0.44 sec
    100% tests passed, 0 tests failed out of 7
    
  • Negative control: with core/libs/prx/libSceLibreSsl/src/CipherList.cpp, core/libs/prx/libSceLibreSsl/src/EvpCipher.cpp, core/libs/prx/libSceLibreSsl/src/PemX509.cpp, core/libs/prx/libSceLibreSsl/src/PkeyId.cpp, core/libs/prx/libSceLibreSsl/src/Runtime.cpp, core/libs/prx/libSceLibreSsl/src/X509V3Print.cpp reverted to main and the new tests kept, the suite fails:
    1/7 Test #4291: guest_libressl_cipher_list .............***Exception: SegFault  0.56 sec
    2/7 Test #4292: guest_libressl_cipher_list_allocator ...   Passed    0.02 sec
    3/7 Test  #715: guest_libressl_pem_private_key .........***Exception: SegFault  0.65 sec
    4/7 Test #4282: guest_libressl_x509v3_print ............***Exception: SegFault  0.47 sec
    5/7 Test #4277: guest_libressl_evp_cipher ..............***Exception: SegFault  0.58 sec
    6/7 Test #4259: guest_libressl_runtime .................***Exception: SegFault  0.49 sec
    7/7 Test #4260: guest_libressl_pkey_id .................***Exception: SegFault  0.61 sec
    14% tests passed, 6 tests failed out of 7
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified SSL_CTX_set_cipher_list_nid_postfix correctly validates guest memory readability and writability before updating context pointers.
    • Verified EVP_CIPHER_block_size_nid_postfix and EVP_PKEY_id_nid_postfix safely copy guest memory via GuestCopyIn_nid_no_patch.
    • Verified PEM_read_RSAPrivateKey_nid_postfix checks guest rsa pointer readability/writability and correctly reads the old pointer before freeing.
    • Verified RAND_file_name_nid_postfix uses GuestCopyOut_nid_no_patch with bounded sizes without guest buffer overrun.
    • Verified X509V3_EXT_print_nid_postfix safely retrieves guest Extension and ASN1 structures in bounded chunks without out-of-bounds guest access.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5943449349 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22476#pullrequestreview-5943449349). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T17:33:44Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `f2cf79de26`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libressl_cipher_list_tests`, `guest_libressl_evp_cipher_tests`, `guest_libressl_pem_private_key_tests`, `guest_libressl_pkey_id_tests`, `guest_libressl_runtime_tests`, `guest_libressl_x509v3_print_tests` built cleanly. - **CTest** (`guest_libressl_pem_private_key`, `guest_libressl_runtime`, `guest_libressl_pkey_id`, `guest_libressl_evp_cipher`, `guest_libressl_x509v3_print`, `guest_libressl_cipher_list`, `guest_libressl_cipher_list_allocator`): ``` 1/7 Test #4260: guest_libressl_pkey_id ................. Passed 0.04 sec 2/7 Test #4259: guest_libressl_runtime ................. Passed 0.04 sec 3/7 Test #715: guest_libressl_pem_private_key ......... Passed 0.10 sec 4/7 Test #4277: guest_libressl_evp_cipher .............. Passed 0.04 sec 5/7 Test #4282: guest_libressl_x509v3_print ............ Passed 0.06 sec 6/7 Test #4292: guest_libressl_cipher_list_allocator ... Passed 0.06 sec 7/7 Test #4291: guest_libressl_cipher_list ............. Passed 0.44 sec 100% tests passed, 0 tests failed out of 7 ``` - **Negative control**: with `core/libs/prx/libSceLibreSsl/src/CipherList.cpp`, `core/libs/prx/libSceLibreSsl/src/EvpCipher.cpp`, `core/libs/prx/libSceLibreSsl/src/PemX509.cpp`, `core/libs/prx/libSceLibreSsl/src/PkeyId.cpp`, `core/libs/prx/libSceLibreSsl/src/Runtime.cpp`, `core/libs/prx/libSceLibreSsl/src/X509V3Print.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/7 Test #4291: guest_libressl_cipher_list .............***Exception: SegFault 0.56 sec 2/7 Test #4292: guest_libressl_cipher_list_allocator ... Passed 0.02 sec 3/7 Test #715: guest_libressl_pem_private_key .........***Exception: SegFault 0.65 sec 4/7 Test #4282: guest_libressl_x509v3_print ............***Exception: SegFault 0.47 sec 5/7 Test #4277: guest_libressl_evp_cipher ..............***Exception: SegFault 0.58 sec 6/7 Test #4259: guest_libressl_runtime .................***Exception: SegFault 0.49 sec 7/7 Test #4260: guest_libressl_pkey_id .................***Exception: SegFault 0.61 sec 14% tests passed, 6 tests failed out of 7 ``` Restoring the change makes it pass again. - **Code reading**: - Verified SSL_CTX_set_cipher_list_nid_postfix correctly validates guest memory readability and writability before updating context pointers. - Verified EVP_CIPHER_block_size_nid_postfix and EVP_PKEY_id_nid_postfix safely copy guest memory via GuestCopyIn_nid_no_patch. - Verified PEM_read_RSAPrivateKey_nid_postfix checks guest rsa pointer readability/writability and correctly reads the old pointer before freeing. - Verified RAND_file_name_nid_postfix uses GuestCopyOut_nid_no_patch with bounded sizes without guest buffer overrun. - Verified X509V3_EXT_print_nid_postfix safely retrieves guest Extension and ASN1 structures in bounded chunks without out-of-bounds guest access. Looks Good To Me
MarcZX10 commented 2026-11-23 03:45:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:45:04Z.

Review: APPROVED


Reviewed — looks good

The guest pointer handling changes at head f2cf79de26f2b5e76a1577338c295d3f17a77512 appear sound based on the changed implementation and tests read; author-reported test results were not independently run.

What I ran

Read the prepared review artifacts and targeted repository source at the pinned head. No build or tests were run.

Results

Check Result Note
Changed code review passed Reviewed six implementation files
Tests not run Results in description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests, builds, and runtime behavior were not independently verified. Read all changed implementation files and the changed test files’ added lines as listed in review/README.md; no changed files were excluded. Read review/description.md, review/commits.md, review/manifest.json, and review/diff.patch.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016326126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22476#pullrequestreview-6016326126). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T03:45:04Z. **Review:** APPROVED --- # Reviewed — looks good The guest pointer handling changes at head `f2cf79de26f2b5e76a1577338c295d3f17a77512` appear sound based on the changed implementation and tests read; author-reported test results were not independently run. ## What I ran Read the prepared review artifacts and targeted repository source at the pinned head. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Reviewed six implementation files | | Tests | not run | Results in description are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Tests, builds, and runtime behavior were not independently verified. Read all changed implementation files and the changed test files’ added lines as listed in `review/README.md`; no changed files were excluded. Read `review/description.md`, `review/commits.md`, `review/manifest.json`, and `review/diff.patch`. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 11:24:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:24:06Z.

Review: COMMENTED


Built the submitted LibreSSL implementations and ran the six changed fixtures on Linux. Cipher list, EVP cipher, PEM private key, PKEY ID, runtime, and X509V3 print all passed with the pinned project libraries. The cases exercise the guest pointer checks and normal API behavior.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6168781783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22476#pullrequestreview-6168781783). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:24:06Z. **Review:** COMMENTED --- Built the submitted LibreSSL implementations and ran the six changed fixtures on Linux. Cipher list, EVP cipher, PEM private key, PKEY ID, runtime, and X509V3 print all passed with the pinned project libraries. The cases exercise the guest pointer checks and normal API behavior. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/22476:upstream-pr/22476'
git switch 'upstream-pr/22476'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/22476'
git switch 'upstream-pr/22476'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/22476'
git switch 'upstream-pr/22476'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/22476'
git switch 'main'
git merge --squash 'upstream-pr/22476'
git switch 'main'
git merge --ff-only 'upstream-pr/22476'
git switch 'main'
git merge 'upstream-pr/22476'
git push origin 'main'
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!22476
No description provided.