fix(libSceLibreSsl): copy SSL_CTX guest pointers safely #22128

Merged
domx4q merged 1 commit from upstream-pr/22128 into main 2026-11-17 06:48:13 +00:00
domx4q commented 2026-11-17 06:18:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by domx4q on 2026-11-17T06:18:43Z.


What

fix(libSceLibreSsl): copy SSL_CTX guest pointers safely

The flagged exports in core/libs/prx/libSceLibreSsl/src/SslCtx.cpp now reach the guest SSL_CTX and its internal state through GuestCopyIn, GuestCopyOut and GuestWritable, using errno-preserving wrappers like those in src/Cipher.cpp. SSL_CTX_new is still a stub, so every SSL_CTX these functions receive is guest memory, not a host-allocated handle.

  • SSL_CTX_ctrl reads ctx->internal through a copy. Each command then reads, exchanges or updates its field through a copy. The 48-byte ticket keys (parg) are copied in or out as a whole, and setting them first checks that all three internal key fields are writable. SSL_CTX_CTRL_GET_EXTRA_CHAIN_CERTS reads extraCerts and writes it to parg through copies.
  • SSL_CTX_check_private_key reads internal and internal->cert through copies.
  • SSL_CTX_set_verify checks that the callback field is writable, then writes verifyMode and defaultVerifyCallback through copies.
  • SSL_CTX_set_session_id_context copies sidCtx in and checks that the destination is writable. It then writes sidCtxLength and the bytes through copies.
  • SSL_CTX_get_ex_data reads internal and exData through copies.

If a guest pointer cannot be read or written, the call throws through NotImplementedAt_nid_no_patch, keeps errno and leaves guest memory unchanged. It no longer dereferences the pointer on the host. Results for valid inputs do not change. python3 tools/guest_pointer_audit.py libSceLibreSsl no longer lists anything in src/SslCtx.cpp.

Not changed: the other exports in SslCtx.cpp (SSL_CTX_callback_ctrl, SSL_CTX_set_msg_callback, the getters and others) still go through the reference-returning Context/Internal helpers, which the audit does not flag. That is a follow-up.

No debt fragment. The existing SslCtx.cpp debt entries describe LibreSSL behaviour, not unchecked guest access, and that behaviour does not change.

Tested

Arch Linux, GCC, Release (-DBUILD_TESTING=ON).

  • guest_libressl_ssl_ctx_fields gains TestGuestPointers, which covers:

    • Calls to all five exports with an unmapped context (0x10).
    • Calls with a context whose internal pointer is unmapped.
    • Calls with unmapped parg (extra chain certificates, get and set ticket keys) and an unmapped sidCtx.
    • A read-only internal reached through a writable context, and a fully read-only context. Getters must still work. Setters, Options, ticket key set and set_verify must be rejected.
    • A context that straddles into a read-only page at sidCtx. set_session_id_context must be rejected without writing sidCtxLength.

    Each rejection must throw std::runtime_error with errno unchanged, and the bytes must stay the same.

  • ctest -R libressl -E libressl3: 91/91 passed. ctest -R public_headers: 65/65 passed.

  • Mutation checks, each run against the new test:

    • SegFault: direct ctx->internal in SSL_CTX_ctrl, a direct readAhead exchange, a direct *parg write for extra chain certificates, direct memcpy for get and set ticket keys, direct ctx->internal->cert in check_private_key, a direct memcpy of sidCtx, direct ->exData in get_ex_data.
    • Abort: removing the writability check in set_verify, removing the writability check in set_session_id_context (straddling-page test).
  • python3 tools/check_conventions.py --base origin/main passes. python3 tools/debt_fold.py --check fails on main already (docs-nkwebkit-drop-readded-count-lines.md). This PR did not cause it.

Overlap

python3 tools/pr_overlap.py --names SSL_CTX_ctrl SSL_CTX_check_private_key SSL_CTX_set_verify SSL_CTX_set_session_id_context SSL_CTX_get_ex_data lists #17299 and #17303 (LibreSsl3 parent-export alias naming). Neither touches core/libs/prx/libSceLibreSsl/ (checked with gh pr diff --name-only).

python3 tools/pr_overlap.py --branch fix/libressl-ssl-ctx-guest-pointers: no overlap with open pull requests (1747 probed).

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:5351912119 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22128). Originally posted by [domx4q](https://github.com/domx4q) on 2026-11-17T06:18:43Z. --- ### What `fix(libSceLibreSsl): copy SSL_CTX guest pointers safely` The flagged exports in `core/libs/prx/libSceLibreSsl/src/SslCtx.cpp` now reach the guest `SSL_CTX` and its internal state through `GuestCopyIn`, `GuestCopyOut` and `GuestWritable`, using errno-preserving wrappers like those in `src/Cipher.cpp`. `SSL_CTX_new` is still a stub, so every `SSL_CTX` these functions receive is guest memory, not a host-allocated handle. - `SSL_CTX_ctrl` reads `ctx->internal` through a copy. Each command then reads, exchanges or updates its field through a copy. The 48-byte ticket keys (`parg`) are copied in or out as a whole, and setting them first checks that all three internal key fields are writable. `SSL_CTX_CTRL_GET_EXTRA_CHAIN_CERTS` reads `extraCerts` and writes it to `parg` through copies. - `SSL_CTX_check_private_key` reads `internal` and `internal->cert` through copies. - `SSL_CTX_set_verify` checks that the callback field is writable, then writes `verifyMode` and `defaultVerifyCallback` through copies. - `SSL_CTX_set_session_id_context` copies `sidCtx` in and checks that the destination is writable. It then writes `sidCtxLength` and the bytes through copies. - `SSL_CTX_get_ex_data` reads `internal` and `exData` through copies. If a guest pointer cannot be read or written, the call throws through `NotImplementedAt_nid_no_patch`, keeps `errno` and leaves guest memory unchanged. It no longer dereferences the pointer on the host. Results for valid inputs do not change. `python3 tools/guest_pointer_audit.py libSceLibreSsl` no longer lists anything in `src/SslCtx.cpp`. Not changed: the other exports in `SslCtx.cpp` (`SSL_CTX_callback_ctrl`, `SSL_CTX_set_msg_callback`, the getters and others) still go through the reference-returning `Context`/`Internal` helpers, which the audit does not flag. That is a follow-up. No debt fragment. The existing `SslCtx.cpp` debt entries describe LibreSSL behaviour, not unchecked guest access, and that behaviour does not change. ### Tested Arch Linux, GCC, Release (`-DBUILD_TESTING=ON`). - `guest_libressl_ssl_ctx_fields` gains `TestGuestPointers`, which covers: - Calls to all five exports with an unmapped context (`0x10`). - Calls with a context whose `internal` pointer is unmapped. - Calls with unmapped `parg` (extra chain certificates, get and set ticket keys) and an unmapped `sidCtx`. - A read-only `internal` reached through a writable context, and a fully read-only context. Getters must still work. Setters, `Options`, ticket key set and `set_verify` must be rejected. - A context that straddles into a read-only page at `sidCtx`. `set_session_id_context` must be rejected without writing `sidCtxLength`. Each rejection must throw `std::runtime_error` with `errno` unchanged, and the bytes must stay the same. - `ctest -R libressl -E libressl3`: 91/91 passed. `ctest -R public_headers`: 65/65 passed. - Mutation checks, each run against the new test: - SegFault: direct `ctx->internal` in `SSL_CTX_ctrl`, a direct `readAhead` exchange, a direct `*parg` write for extra chain certificates, direct `memcpy` for get and set ticket keys, direct `ctx->internal->cert` in `check_private_key`, a direct `memcpy` of `sidCtx`, direct `->exData` in `get_ex_data`. - Abort: removing the writability check in `set_verify`, removing the writability check in `set_session_id_context` (straddling-page test). - `python3 tools/check_conventions.py --base origin/main` passes. `python3 tools/debt_fold.py --check` fails on `main` already (`docs-nkwebkit-drop-readded-count-lines.md`). This PR did not cause it. ### Overlap `python3 tools/pr_overlap.py --names SSL_CTX_ctrl SSL_CTX_check_private_key SSL_CTX_set_verify SSL_CTX_set_session_id_context SSL_CTX_get_ex_data` lists #17299 and #17303 (LibreSsl3 parent-export alias naming). Neither touches `core/libs/prx/libSceLibreSsl/` (checked with `gh pr diff --name-only`). `python3 tools/pr_overlap.py --branch fix/libressl-ssl-ctx-guest-pointers`: no overlap with open pull requests (1747 probed). ### 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 06:26:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T06:26:14Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl_ssl_ctx_fields_tests built cleanly.
  • CTest (guest_libressl_ssl_ctx_fields):
    1/1 Test #4246: guest_libressl_ssl_ctx_fields ....   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl/src/SslCtx.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4246: guest_libressl_ssl_ctx_fields ....***Exception: SegFault  0.40 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified SSL_CTX_ctrl ticket key handling in core/libs/prx/libSceLibreSsl/src/SslCtx.cpp:388-403: checked size validation (larg == 48), errno preservation, and exact 48-byte copy-in/out across all three 16-byte key fields (tlsextTickKeyName, tlsextTickHmacKey, tlsextTickAesKey).
    • Verified FieldOffset and FieldBytes template implementations against probe object layout, ensuring correct byte-offset calculation and LP64/LLP64 memory alignment for member pointers.
    • Verified SSL_CTX_set_session_id_context in SslCtx.cpp:468-480 for buffer overflow guards (length <= sizeof(LibreSslCtx::sidCtx)), null-check semantics, and pre-write guest memory writability checks via RequireGuestWritablePreservingErrno.
    • Verified SSL_CTX_set_verify in SslCtx.cpp:455-462: checked null context handling via GuestInternal, writability checks on defaultVerifyCallback, and field updates to verifyMode and defaultVerifyCallback.
    • Verified SSL_CTX_get_ex_data and SSL_CTX_check_private_key in SslCtx.cpp:440-450, 483-488: checked safe guest pointer dereferencing through ReadField and proper error propagation via PutSslError(NoCertificateAssigned).
    • Verified AGENTS.md compliance: no unauthorized comments introduced, no special-casing of game IDs, proper errno preservation across GuestCopyIn/Out/Writable invocations, and thorough mprotect/VirtualProtect test coverage in GuestLibreSslSslCtxFields.cpp.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5933967751 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22128#pullrequestreview-5933967751). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T06:26:14Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `efa8e2d84b`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libressl_ssl_ctx_fields_tests` built cleanly. - **CTest** (`guest_libressl_ssl_ctx_fields`): ``` 1/1 Test #4246: guest_libressl_ssl_ctx_fields .... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibreSsl/src/SslCtx.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4246: guest_libressl_ssl_ctx_fields ....***Exception: SegFault 0.40 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified SSL_CTX_ctrl ticket key handling in core/libs/prx/libSceLibreSsl/src/SslCtx.cpp:388-403: checked size validation (larg == 48), errno preservation, and exact 48-byte copy-in/out across all three 16-byte key fields (tlsextTickKeyName, tlsextTickHmacKey, tlsextTickAesKey). - Verified FieldOffset and FieldBytes template implementations against probe object layout, ensuring correct byte-offset calculation and LP64/LLP64 memory alignment for member pointers. - Verified SSL_CTX_set_session_id_context in SslCtx.cpp:468-480 for buffer overflow guards (length <= sizeof(LibreSslCtx::sidCtx)), null-check semantics, and pre-write guest memory writability checks via RequireGuestWritablePreservingErrno. - Verified SSL_CTX_set_verify in SslCtx.cpp:455-462: checked null context handling via GuestInternal, writability checks on defaultVerifyCallback, and field updates to verifyMode and defaultVerifyCallback. - Verified SSL_CTX_get_ex_data and SSL_CTX_check_private_key in SslCtx.cpp:440-450, 483-488: checked safe guest pointer dereferencing through ReadField and proper error propagation via PutSslError(NoCertificateAssigned). - Verified AGENTS.md compliance: no unauthorized comments introduced, no special-casing of game IDs, proper errno preservation across GuestCopyIn/Out/Writable invocations, and thorough mprotect/VirtualProtect test coverage in GuestLibreSslSslCtxFields.cpp. Looks Good To Me
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!22128
No description provided.