Skip to content

fix(libSceLibreSsl): read the X509_VERIFY_PARAM_set1_host name through checked copies - #21634

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/libressl-x509-set1-host-guest-copy
Nov 16, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/libressl-x509-set1-host-guest-copy

Conversation

@milescoviello

Copy link
Copy Markdown
Contributor

What

X509_VERIFY_PARAM_set1_host measured the guest name with std::strlen when the length was 0, scanned it with std::memchr for an embedded NUL, and duplicated it with strndup, all in place. So an unreadable name crashed the host.

The name is now read through GuestCopyIn_nid_no_patch. A new ReadHostName does it in chunks that never cross a 4 KiB boundary:

  • With a length of 0, it reads up to the terminator, as strlen did. A name that ends right before an unreadable page still works.
  • With a counted length, it reads that many bytes and stops at the first NUL. That NUL is still refused (return 0) before the host list changes, as memchr did.

The duplicate is taken from the host bytes with the same strndup, so the list still owns guest-heap strings that X509_STORE_free releases. An unreadable name throws std::invalid_argument, as null verify parameters already do, and leaves the list as it was. errno is kept.

What stays the same: a null or empty name clears the list and returns 1; a set replaces the list; the counted form needs no terminator.

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

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

…h checked copies

X509_VERIFY_PARAM_set1_host measured the guest name with strlen, scanned
it with memchr and duplicated it with strndup, all in place. It now reads
the name through GuestCopyIn in page-bounded chunks, up to the terminator
or the counted length, keeps the embedded-NUL refusal and errno, and
duplicates the host copy. An unreadable name throws, as null verify
parameters do, and leaves the host list as it was.

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

@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 1cf2124a59:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libressl_x509_store_tests built cleanly.
  • CTest (guest_libressl_x509_store, guest_libressl_x509_store_allocator):
    1/2 Test #4179: guest_libressl_x509_store .............   Passed    0.04 sec
    2/2 Test #4180: guest_libressl_x509_store_allocator ...   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceLibreSsl/src/X509Store.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4180: guest_libressl_x509_store_allocator ...   Passed    0.03 sec
    2/2 Test #4179: guest_libressl_x509_store .............***Exception: SegFault  0.51 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ReadHostName in core/libs/prx/libSceLibreSsl/src/X509Store.cpp:169-187 bounds each GuestCopyIn_nid_no_patch call to Page - address % Page (at most 4096 bytes) and length - host.size() when counted, preventing page-boundary overreads and buffer overflows.
    • Verified ReadHostName saves and restores errno around GuestCopyIn_nid_no_patch (X509Store.cpp:177-179) and throws std::invalid_argument on unreadable memory before X509_VERIFY_PARAM_set1_host_nid_postfix modifies id->hosts.
    • Verified NUL handling in ReadHostName (X509Store.cpp:181-183): when length == 0 (!counted), stops at the first NUL and returns true; when length > 0 (counted), returns false on any embedded NUL so X509_VERIFY_PARAM_set1_host_nid_postfix returns 0 without clearing id->hosts.
    • Verified empty and null name handling in X509_VERIFY_PARAM_set1_host_nid_postfix (X509Store.cpp:277-287): name == nullptr or an empty NUL-terminated string leaves host.empty() == true, frees id->hosts, sets id->hosts = nullptr, and returns 1.
    • Verified LP64/LLP64 type safety (std::min<std::size_t>, std::uintptr_t, std::size_t), debt fragment docs/dev/debt/changes/fix-libressl-x509-set1-host-guest-copy.md, and multi-page/unreadable/embedded-NUL test coverage in core/libs/prx/libSceLibreSsl/tests/X509Store.cpp:205-220.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit d2fe3f5 into boykopovar:main Nov 16, 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.

3 participants