Skip to content

fix(libSceLibreSsl3): copy the sk_free stack guest record safely - #24399

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
domx4q:fix/libressl3-stack-free-guest-pointers
Dec 3, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
domx4q:fix/libressl3-stack-free-guest-pointers

Conversation

@domx4q

@domx4q domx4q commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

sk_free (src/Stack.cpp) no longer reads stack->data in place. Stacks are guest memory: sk_new allocates them with the guest malloc, and sk_push/sk_num in the same file already copy the header through GuestCopyIn. It is not a host-handle false positive.

sk_free now copies the stack header in through the file's existing CopyGuestIn helper before freeing the element array and the stack. As sk_push in this file already does, an unreadable stack throws std::invalid_argument with errno preserved, and nothing is freed. A null stack is still a no-op, and valid stacks are freed exactly as before.

tools/guest_pointer_audit.py libSceLibreSsl3 no longer lists sk_free. tools/progress.py --module libSceLibreSsl3 gives 550/667 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.

Overlap: python3 tools/pr_overlap.py --names sk_free reports no open pull request. python3 tools/pr_overlap.py --branch fix/libressl3-stack-free-guest-pointers (re-run on 2026-11-20 against main 0a025bd) reports no overlap with open pull requests.

Tested

Arch Linux, GCC, Release, re-run on 2026-11-20 after rebasing onto main 0a025bd. ctest --test-dir build -R 'libressl3|public_headers': 159/159 passed.

FreeNull in core/libs/prx/libSceLibreSsl3/tests/Stack.cpp now also passes unmapped stacks (0x1 and 0x10): each call must throw std::invalid_argument, keep errno and free nothing.

Mutation check: with src/Stack.cpp reset to origin/main, guest_libressl3_stack crashes with SegFault; with the change it passes.

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)

sk_free now copies the stack header in through GuestCopyIn before
freeing its element array, like sk_push in the same file. An unreadable
stack throws std::invalid_argument with errno preserved and frees
nothing.

@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 5b9c11f294:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_stack_tests built cleanly.
  • CTest (guest_libressl3_stack):
    1/1 Test #4596: guest_libressl3_stack ............   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/Stack.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4596: guest_libressl3_stack ............***Exception: SegFault  0.48 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sk_free_nid_postfix correctly checks for nullptr before any operations and returns safely as a no-op
    • Verified CopyGuestIn(&guestStack, stack, sizeof(guestStack)) safely validates guest memory and throws std::invalid_argument on unreadable guest pointer
    • Verified errno preservation pattern across CopyGuestIn matches other functions in core/libs/prx/libSceLibreSsl3/src/Stack.cpp
    • Verified deallocation order of guestStack.data followed by stack using free_nid_postfix matches upstream OpenBSD sk_free semantics
    • Verified unit tests in core/libs/prx/libSceLibreSsl3/tests/Stack.cpp rigorously test nullptr no-op and bad pointers (0x1, 0x10) ensuring std::invalid_argument is thrown, errno is preserved, and no memory is freed

Looks Good To Me

@github-actions github-actions Bot added the prx label Nov 20, 2026
This was referenced Nov 21, 2026
@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 5b9c11f294, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24470, #24601, #17439, #17546, #21117, #21261, #21741, #21995, #22067, #22398, #23111, #23346, #23864, #24377, #24475, #24556, #14106, #14109, #19966, #20050, #20161, #20622, #21237, #22215, #22764, #22884, #22938, #23745, #24460). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
agc_stack_walk, guest_libressl3_stack, guest_libressl_stack and 3 more (6 tests, matched by name to the test file this PR changes) all pass all pass

Whole batch: the full ctest -j 12 (6525 tests) on the RTX 3080 has 5 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Traced to other PRs of the batch, each by reverting it and testing again: #21117 (guest_pad); none is in this PR's area. The 2125 agc_/video tests on the iGPU have 1 failing: agc_driver_stencil_plane_memory (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@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

Nothing blocks this change; the targeted code supports the stated safe-copy behavior, and the added test checks invalid pointers, preserved errno, and no frees.

What I ran

Read the prepared diff and changed source and test files at head 5b9c11f2943769cf7b1edcb9cbcd88f8c9518f07. Used repo_read.py search and read operations for the guest-copy implementation and allocator free helper. No test commands were run.

Results

Check Result Note
Changed source and test review passed Both changed files read
Related copy and free helpers passed Targeted implementation read
Test execution not run Author reports 159/159 passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

No test suite, build, or mutation check was run during this review. Author-reported test and mutation results were not independently verified. Read files: core/libs/prx/libSceLibreSsl3/src/Stack.cpp, core/libs/prx/libSceLibreSsl3/tests/Stack.cpp, and targeted ranges of core/libs/prx/libc/src/General.cpp and core/libs/prx/libc/src/FileAndHeap.cpp. Excluded files: none; both files listed as reviewed in the manifest were read.

To get this to a good state

Nothing is required.

@Helldiverr

Copy link
Copy Markdown
Contributor

On Windows, head 5b9c11f294 merges onto main dad6acd1b8 without a textual conflict, but the merge does not compile: error: 'CopyGuestIn' was not declared in this scope at Stack.cpp:130. With that one call renamed to GuestCopyIn_nid_no_patch, the 3 stack tests pass 4/4 times, the PR's test against main's Stack.cpp crashes with a read of 0x9, and 4 of 4 mutations are caught.

Existing runs:

  • PrivacyOnion ran guest_libressl3_stack on Linux at the head, with a negative control (main's Stack.cpp with the new tests gives a SegFault), and approved.
  • MarcZX10 reviewed the head and approved.
  • kiwe147 ran it on Windows in a 30-PR batch applied to main fd4aeed483, where the 6 stack tests pass.

All three ran before #26161 merged, so none of them could see the break below.

What this run adds. A Windows run of this PR alone on main dad6acd1b8, which includes #26161. It covers the build break, a negative control and four mutations.

Code.

  • refactor(libressl3): drop errno saves around guest copies #26161 (refactor(libressl3): drop errno saves around guest copies, commit 6048bc0600) removed the file-local CopyGuestIn, GuestWritable and CopyGuestOut helpers from Stack.cpp. At the head the CopyGuestIn helper is still at Stack.cpp:71-76, and the new call at Stack.cpp:151 uses it. Git merges the two cleanly because the hunks do not overlap, but main's Stack.cpp no longer declares the helper. sk_push on main now calls GuestCopyIn_nid_no_patch directly (Stack.cpp:95 at dad6acd1b8). Making the same change on line 151 is enough:
    if (!GuestCopyIn_nid_no_patch(&guestStack, stack, sizeof(guestStack))) throw std::invalid_argument("sk_free: unreadable stack");
    With that change, the test's errno == E2BIG check at tests/Stack.cpp:124 still passes. That matches refactor(libressl3): drop errno saves around guest copies #26161's premise that GuestCopyIn_nid_no_patch preserves errno without a wrapper.
  • Other than this, the change follows sk_push: copy the record in, throw std::invalid_argument on an unreadable stack, and free data and then the stack.
  • py -3 tools/check_conventions.py --base dad6acd1b8 on the merge prints All conventions checks passed. tools/debt_fold.py --check exits 1, but only because of two libSceRudp fragments that conflict on main dad6acd1b8 itself. This PR adds no fragment.

Negative control and mutation. All of these ran on the merge with the one-line rename above. The mutations were applied to Stack.cpp.

  • Negative control: main's Stack.cpp with the PR's tests crashes with 0xc0000005, read of 0x0000000000000009. That is stack->data read through stack == 1, because data is at offset 8 of LibreSslStack.
  • mA: an unreadable stack returns instead of throwing. Caught at tests/Stack.cpp:124.
  • mB: free_nid_postfix(guestStack.data) is dropped. Caught at line 91 (RoundTrip expects two frees and no allocations left).
  • mC: errno = EFAULT is set before the throw. Caught at line 124.
  • mD: it throws std::runtime_error instead of std::invalid_argument. Caught: the exception is not handled and the test aborts.

Tests.

tests main dad6acd1b8 head merged as 00ff42c90c with the rename (61a8fa17a4)
guest_libressl3_stack pass (old test) does not build pass 4/4
guest_libressl3_x509_extension_stack pass does not build pass 4/4
guest_libressl_stack pass does not build pass 4/4
guest_libressl3_stack, negative control crash, read of 0x9
guest_libressl3_stack, mutations 4 of 4 caught

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 5b9c11f294, merged onto main dad6acd1b8 as 00ff42c90c; the one-line rename on top of that is 61a8fa17a4, a local commit only.

main has since moved 60 commits to 7a0de821ab, none of them in libSceLibreSsl3, and the head still merges without a textual conflict; that merge still calls CopyGuestIn at Stack.cpp:130.

AI-assisted: yes

@domx4q

domx4q commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@Helldiverr Thanks for catching the silent break. b590668 makes sk_free call GuestCopyIn_nid_no_patch directly, as sk_push does on main after #26161, so the merge no longer depends on the removed CopyGuestIn helper.

Tested on Linux (GCC 16, Release), with the head merged onto origin/main 7a0de821ab:

test merge
guest_libressl3_stack pass
guest_libressl3_x509_extension_stack pass
guest_libressl_stack pass
guest_libressl3_stack with main's Stack.cpp (negative control) SegFault

tools/check_conventions.py passes, and the PR still touches only its 2 files.

AI-assisted: yes (Claude Code)

@Helldiverr

Copy link
Copy Markdown
Contributor

Thanks, and thanks for the Linux run with the negative control. With b590668854 merged onto main 17220db6a3, Stack.cpp and tests/Stack.cpp are byte for byte the same as the tree from our earlier Windows run with the one-line rename (61a8fa17a4). That tree passed the 3 stack tests 4/4 times, the negative control crashed with a read of 0x9, and all 4 mutations were caught. So those results carry over to this head.

  • The merged Stack.cpp has no call to the removed CopyGuestIn, and sk_free uses GuestCopyIn_nid_no_patch the same way sk_push does.
  • The head itself still carries the old file-local helpers from its base, but they go away on merge, because refactor(libressl3): drop errno saves around guest copies #26161 removed them on main. A rebase would make the head match what gets merged, but it is not needed for the build.

AI-assisted: yes

@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 changed stack cleanup now reads its guest header through the guest-copy API and preserves errno on unreadable input; I found no blocking defect in the reviewed code.

What I ran

Reviewed head b5906688542dfafff425944dbf44c1a4601997f4. No commands or tests were run in this review; repo_read.py was unavailable as a command tool, so I used the provided pinned-head reader interface. No merged-preview check was run.

Files read: core/libs/prx/libSceLibreSsl3/src/Stack.cpp, core/libs/prx/libSceLibreSsl3/tests/Stack.cpp, core/libs/prx/libc/src/General.cpp, core/libs/prx/libSceLibreSsl3/src/SslCtxCiphersuites.cpp, and core/libs/prx/libSceLibreSsl3/include/Api.hpp. Both changed files listed in the manifest were read; no files were excluded by the manifest.

Results

Check Result Note
Source review passed Changed code and relevant callers inspected
Automated checks not run No tests or build run in this review
Negative control not run Mutation check only reported by the author

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports 159/159 tests and a mutation check passing, but I did not run these checks. Runtime behavior on console hardware and other host operating systems was not verified.

To get this to a good state

Nothing is required.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants