Repository navigation
fix(libc): probe vasprintf destination pointer before writing - #26638
Conversation
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 8bb35191d8:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_allocating_strings_testsbuilt cleanly. - CTest (
guest_allocating_strings):1/1 Test #1210: guest_allocating_strings ......... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libc/src/Formatting.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #1210: guest_allocating_strings .........***Exception: SegFault 0.48 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified vasprintf_nid_postfix destination probe uses GuestCopyOut_nid_no_patch with sizeof(nullOutput) (8 bytes / pointer width) matching the guest pointer size
- Verified errno save and restore around GuestCopyOut_nid_no_patch to guarantee errno=22 (EINVAL) is set consistently on probe failure
- Verified post-allocation GuestCopyOut_nid_no_patch failure path frees output via ApplicationHeapFree_nid_no_patch to prevent guest heap leakage
- Verified input ordering: destination is probed and set to nullptr before checking format and args pointers (errno 22)
- Verified AGENTS.md compliance: no comments added, no hardcoded title workarounds, conventional commit format in PR title
- Verified core/libs/tests/GuestAllocatingStrings.cpp asserts return code -1, errno 22, and allocation count stability when destination is unmapped
Looks Good To Me
|
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.
|
|
Works on Windows at head Existing runs. 1 review and 1 comment. No inline comments.
What this run adds.
Code. Line numbers are from the head. The merge is clean.
Negative control and mutation. Each row is the merge plus the probe, with one change to
Probes. One extra test executable,
Tests.
Host.
AI-assisted: yes |
What
vasprintf_nid_postfixincore/libs/prx/libc/src/Formatting.cppwrote through the guest-suppliedchar** destinationas a raw host pointer write at two points — the initial*destination = nullptrand the post-allocation*destination = output— with only a null check ondestinationitself. Every other failure path in the function is error-coded (null destination/format/args →-1/errno 22; heap exhaustion →-1/errno 12), but an unguarded write to a non-null, unmapped guest pointer faults the host withSIGSEGV(exit 139) instead of failing gracefully.Both writes are now routed through
GuestCopyOut_nid_no_patch(the same fault-safe copy helper used by thefopen_s_nid_postfixsibling incore/libs/prx/libc/src/FileAndHeap.cpp, and byGuestCopyOutcallers acrossGeneral.cpp/Backtrace.cpp/Radix64.cpp/Time.cpp):GuestCopyOut(destination, &nullOutput, sizeof(char*))), mirroringfopen_s_nid_postfix; on an inaccessible destination it setserrno = 22and returns-1before any formatting or heap allocation, saving/restoringerrnoaround the probe.ApplicationHeapFree_nid_no_patch) and the function returns-1/errno 22, closing the otherwise-unreachable guest-heap leak — the analogue offopen_sclosing its stream on a later write failure.Existing contracts are unchanged: null destination/format/args →
-1/errno 22; heap exhaustion →-1/errno 12; the success path is identical.asprintfanddprintf/vdprintfroute throughvasprintfand inherit the guard.The
-1/errno 22choice for an inaccessible out-pointer is the codebase-internal sibling-precedent convention (matchingfopen_s_nid_postfix); it is not presented as measured console firmware behaviour.Tested
vasprintf_nid_postfixwith an unmapped guest destinationCallVasprintf(reinterpret_cast<char**>(0x10), "value=%d", 42)faults the host —[libc] fatal SIGSEGV (code 1) at address 0x0000000000000010, exit 139;ctest -R "^guest_allocating_strings"→SEGFAULT.-1witherrno == 22and performs zero heap allocations (proving abort-before-format);ctest --test-dir build -R "^guest_allocating_strings"→guest_allocating_strings Passed.guest_allocating_strings,guest_formatting,guest_allocator_failures,guest_libc_internal_format— 4/4 passed, 0 skipped.tools/check_conventions.pypasses;tools/pr_overlap.pyreports a feature-vs-fix false positive (file sharing with the original libc feature PRs), dismissed after hunk-level review — none of the overlapping PRs probe thevasprintfdestination.Regression test extends the existing harness
core/libs/tests/GuestAllocatingStrings.cpp(no new CMake target). Changed files:core/libs/prx/libc/src/Formatting.cpp,core/libs/tests/GuestAllocatingStrings.cpp(+17/−2).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's [technical debt] file.mdfiles or images added to the repository (attach them to this PR)