Skip to content

fix(libSceLibreSsl): copy DES and X509_EXTENSION guest buffers safely - #24401

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
domx4q:fix/libressl-des-extension-guest-pointers
Dec 4, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
domx4q:fix/libressl-des-extension-guest-pointers

Conversation

@domx4q

@domx4q domx4q commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

Six libSceLibreSsl exports in Export.cpp no longer read or write guest buffers directly. All of them take caller-supplied guest memory (DES blocks, keys and key schedules, and an X509_EXTENSION the title holds), so none is a host-handle false positive.

  • DES_ecb_encrypt copies the 8-byte input block and the 8 key bytes of the schedule in through GuestCopyIn_nid_no_patch, runs mbedtls on host copies and writes the output block with one GuestCopyOut_nid_no_patch. In-place calls (input == output) still work.
  • DES_set_key copies the key in and writes the zero-filled 128-byte schedule with one GuestCopyOut_nid_no_patch, as before.
  • DES_set_odd_parity copies the key in, sets parity on the host copy and writes it back. The audit does not list it (it calls mbedtls, not memcpy), but it dereferenced the guest key the same way.
  • X509_EXTENSION_get_critical, X509_EXTENSION_get_data and X509_EXTENSION_get_object read their field through GuestCopyIn_nid_no_patch instead of memcpy.

errno is preserved, as CRYPTO_THREADID_set_numeric in the same file already does. An unreadable or unwritable pointer throws through NotImplementedAt_nid_no_patch, which is how this library rejects them elsewhere (SslCtx.cpp, Bio.cpp). Null arguments keep their existing std::invalid_argument / null-return handling, and results for valid inputs are byte-for-byte unchanged; the existing DES known-answer and extension tests pass unchanged.

tools/guest_pointer_audit.py libSceLibreSsl no longer lists the five audited functions. tools/progress.py --module libSceLibreSsl reports 269/402 on both main and this branch. No debt entry describes the old behaviour, so there is no debt fragment. No listed function was a false positive. Not changed in Export.cpp: OCSP_basic_verify, SSL_CTX_use_certificate_ASN1, SSL_get_peer_cert_chain, SSL_get_peer_certificate and d2i_PKCS12_bio (separate topics), and SSL_get1_session (edited by #23139 and #23336).

Overlap: python3 tools/pr_overlap.py --names DES_ecb_encrypt DES_set_key DES_set_odd_parity X509_EXTENSION_get_critical X509_EXTENSION_get_data X509_EXTENSION_get_object lists only #17299 and #17303, which name libSceLibreSsl3 parent-export aliases and change no file in core/libs/prx/libSceLibreSsl/. python3 tools/pr_overlap.py --branch fix/libressl-des-extension-guest-pointers (re-run on 2026-11-20 against main 0a025bd) could not compare #11146, #11466, #13285, #13547, #15278, #15279 and #15280 (they conflict with main) on Export.cpp; none of their diffs adds or removes a line that mentions the DES or X509_EXTENSION functions.

Tested

Arch Linux, GCC, Release, re-run on 2026-11-20 after rebasing onto main 0a025bd. All libSceLibreSsl tests and public_headers passed (ctest --test-dir build -R 'guest_libressl_|guest_libre_ssl|guest_digest_extents|libressl_|public_headers', 163/163).

New cases pass an unmapped pointer (0x10) and require a throw with errno preserved:

  • GuestLibreSslDes.cpp: unmapped input, schedule and output for DES_ecb_encrypt (the output block must stay untouched), unmapped key and schedule for DES_set_key (the schedule must stay unchanged), unmapped key for DES_set_odd_parity.
  • GuestLibreSslX509Extension.cpp: unmapped extension for each of the three getters.

Mutation check: with Export.cpp reset to origin/main, guest_libressl_des and guest_libressl_x509_extension crash with SegFault; with the change both pass.

python3 tools/check_conventions.py --base origin/main 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)

DES_ecb_encrypt, DES_set_key and DES_set_odd_parity copy the block, key
and schedule bytes in through GuestCopyIn, run mbedtls on host copies
and write the result with one GuestCopyOut. X509_EXTENSION_get_critical,
X509_EXTENSION_get_data and X509_EXTENSION_get_object read their field
through GuestCopyIn instead of memcpy. Unreadable or unwritable guest
pointers throw through NotImplementedAt_nid_no_patch with errno
preserved; null arguments keep their existing handling.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_threads_tests built cleanly.
  • CTest (guest_libc_threads):
    1/1 Test #1298: guest_libc_threads ...............   Passed    0.32 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Threads.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1298: guest_libc_threads ...............***Exception: SegFault  0.84 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked _Mtx_init_nid_postfix in core/libs/prx/libc/src/Threads.cpp: verifies unwritable pointer early, allocates mutex, and safely frees mutex if the second GuestCopyOut fails before throwing std::invalid_argument
    • Checked _Cnd_init_nid_postfix in core/libs/prx/libc/src/Threads.cpp: verifies unwritable pointer early, allocates condition, and safely deletes condition if the second GuestCopyOut fails before throwing std::invalid_argument
    • Checked _Cnd_destroy_nid_postfix in core/libs/prx/libc/src/Threads.cpp: reads handle via GuestCopyIn, safely ignores null handles, resolves valid condition, and clears guest pointer via GuestCopyOut before deleting the condition
    • Checked _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix in core/libs/prx/libc/src/Threads.cpp: clears mutex via GuestCopyOut with proper std::invalid_argument exception on unwritable memory
    • Checked test coverage in core/libs/tests/GuestLibcThreads.cpp: ReadOnlyHandleSlot and UnusableHandlePointers test both unmapped and read-only memory on Linux and Windows without leaks or crashes

Looks Good To Me

@github-actions github-actions Bot added the libc label Nov 20, 2026
@domx4q
domx4q force-pushed the fix/libressl-des-extension-guest-pointers branch from b9eab21 to d411a34 Compare November 20, 2026 20:17
@domx4q

domx4q commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

A batch push at 16:38 UTC+1 put the wrong commit on this branch (b9eab21, the _Mtx_init/_Cnd_init change that is #24466). I restored the branch to the commit this PR is about: d411a34, fix(libSceLibreSsl): copy DES and X509_EXTENSION guest buffers safely (3 files, same content as before the bad push, clean on current main). The earlier approval was given on the wrong diff, so please take another look when you have time.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the prx label Nov 20, 2026

@MarcZX10 MarcZX10 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.

Reviewed — looks good

The change safely copies guest buffers in the six described exports; the review covered all three changed files at head d411a34351c1f87cf95e1ee27a3c372defb3b3cd.

What I ran

No commands were run. I read the prepared diff and the full text of each changed file through repo_read.py; no repository test run was performed.

Results

Check Result Note
Changed-file review passed All three changed files read
Tests not run Results in the description are author-reported
Negative control not run Mutation check is author-reported

Findings

Blocking

None.

Non-blocking

None.

Sources

The reviewed change is pinned to d411a34351c1f87cf95e1ee27a3c372defb3b3cd. Changed files read: core/libs/prx/libSceLibreSsl/Export.cpp, core/libs/tests/GuestLibreSslDes.cpp, and core/libs/tests/GuestLibreSslX509Extension.cpp.

Not verified

Build and test results, mutation checks, conventions checks, and behavior on a physical console were not independently verified. The description reports 163 passing tests and a passing conventions check.

To get this to a good state

Nothing is required.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 11146 core/libs/prx/libSceLibreSsl/Export.cpp
PR 23336 core/libs/prx/libSceLibreSsl/Export.cpp

@maskym

maskym commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Windows result for head d411a34351, merged alone into main 2b09db6f11 (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. Build clean.

The tests this PR adds or extends, guest_libressl_des, guest_libressl_x509_extension, pass 2/2, three runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@Helldiverr

Copy link
Copy Markdown
Contributor

Windows test run of d411a34 merged onto current main: guest_libressl_des and guest_libressl_x509_extension pass 3/3. main's Export.cpp with the new tests crashes both with a SegFault. Of 7 source mutations, 3 are caught and 2 are equivalent. The other 2 reach code no test covers: a write-back to a read-only key in DES_set_odd_parity, and the zero tail of the DES_set_key schedule. Small pins, which I added locally only, catch both.

Existing runs. maskym ran Windows at d411a34 (guest_libressl_des, guest_libressl_x509_extension, pass). PrivacyOnion posted a Linux run at b9eab21, an earlier head, but it reports guest_libc_threads and Threads.cpp, not this PR's tests. MarcZX10 read the code at d411a34 without running it. The PR body reports Arch Linux runs.

What this run adds. A Windows negative control against main's source, source mutations, and pins for the two survivors that can be observed. The tests passing on Windows was already known.

Code.

  • CopyGuestIn and CopyGuestOut (core/libs/prx/libSceLibreSsl/Export.cpp:38-50) throw through NotImplementedAt_nid_no_patch when a copy fails. Ignoring a failed read (m1) is caught by both tests. Ignoring a failed write (m2) is caught by the unmapped output and schedule cases in guest_libressl_des.
  • Both errno restores (:41, :48) are redundant. CopyGuestBytes and GuestCopyOut_nid_no_patch each hold a RestoreErrno (core/libs/prx/libc/src/General.cpp:97, :961 on main 61ab0ba), so dropping either one (m3, m4) passes. The errno == 71 checks in RequireRejected still guard against a future change in libc.
  • DES_ecb_encrypt (:230-244) reads the block and the key before it writes anything, and writes the output only after mbedtls succeeds (:243). Writing the output before the key is read (m7) is caught by Require(output == untouched) (core/libs/tests/GuestLibreSslDes.cpp:91). In-place calls are covered by CheckVector (GuestLibreSslDes.cpp:46).
  • DES_set_odd_parity (:254-260) is tested only with an unmapped key (GuestLibreSslDes.cpp:95), so the read already fails there. Replacing the checked write-back with a bare GuestCopyOut_nid_no_patch (m5) passes: a read-only key would come back silently unchanged.
  • DES_set_key (:246-252) writes all 128 schedule bytes, as main did with its memset. No test looks past the first 8 bytes. CheckVector fills the schedule with 0xaa (GuestLibreSslDes.cpp:39) but checks only the cipher results, so writing just the 8 key bytes (m6) passes.

PR body. "Writes the zero-filled 128-byte schedule … as before" is right, but no test checks it (see Code). The other claims match the code and the tests.

Suggestion. Lines after GuestLibreSslDes.cpp:95, before return 0;. I added them locally only:

    schedule.fill(0xff);
    Require(DES_set_key_nid_postfix(weak.data(), schedule.data()) == 0);
    Require(std::equal(weak.begin(), weak.end(), schedule.begin()));
    Require(std::all_of(schedule.begin() + 8, schedule.end(), [](unsigned char value) { return value == 0; }));
    static constexpr unsigned char readOnlyKey[8] {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
    RequireRejected([] { DES_set_odd_parity_nid_postfix(const_cast<unsigned char*>(readOnlyKey)); });

The first lines check the whole schedule over a dirty buffer. The last two pass a key in read-only static data, which reads fine but cannot be written back. With the pins, the merge passes 3/3, and the pinned test aborts on m5 and on m6.

Negative control and mutations. Each row is one change to Export.cpp on the merge, run once with the head tests.

change runs result
main's Export.cpp 1 SegFault in both tests, caught
m1: ignore a failed GuestCopyIn_nid_no_patch in CopyGuestIn (:42) 1 both tests abort (guest_libressl_x509_extension at "unmapped extension critical flag"), caught
m2: ignore a failed GuestCopyOut_nid_no_patch in CopyGuestOut (:49) 1 guest_libressl_des aborts, caught
m3: drop errno = savedErrno in CopyGuestIn (:41) 1 passes; equivalent
m4: drop errno = savedErrno in CopyGuestOut (:48) 1 passes; equivalent
m5: DES_set_odd_parity ignores a failed write-back (:259) 1 passes; pin aborts
m6: DES_set_key writes only MBEDTLS_DES_KEY_SIZE bytes (:250) 1 passes; pin aborts
m7: DES_ecb_encrypt writes the input block to output before reading the key (:234-235) 1 guest_libressl_des aborts, caught

Tests.

test main merge 1 merge 2 merge 3
guest_libressl_des Passed Passed Passed Passed
guest_libressl_x509_extension Passed Passed Passed Passed

On main, the tests are main's own GuestLibreSslDes.cpp and GuestLibreSslX509Extension.cpp, without the unmapped-pointer cases.

On the merge, tools/check_conventions.py --base upstream/main passes with no findings, and tools/debt_fold.py --check exits 0.

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head d411a34351, merged onto main 61ab0babb5 as 460bb001ea

AI-assisted: yes

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran guest_libressl_des and guest_libressl_x509_extension on Linux with this head integrated. Both passed, including the new checked guest buffer cases and the existing valid-input controls.

AI-assisted: yes.

@Plyxen Plyxen 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.

I read the diff at d411a34351. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), applied onto main at c1593e4e1dd together with other open PRs (so not an isolated build).

  • guest_libressl_des and guest_libressl_x509_extension: pass on both.
  • Control on Windows (the same tree and tests, with the sources of this PR and of the other PRs in the batch put back to main's): both crash, on reads of addresses 0x10 and 0x18.

Not checked: a title.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit e488c40 into boykopovar:main Dec 4, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants