fix(libSceNpCommon): copy at most the length limit in sceNpStrncpyToBuf #14318
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14318"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T09:59:13Z.
What
sceNpStrncpyToBufcan copy more thanmaxLengthcharacters and read the source pastmaxLength.CopyToBufferchecks the destination size against the string length, then callsstrncpy(dst, src, size - 1). When the source has no terminator withinmaxLength,strncpykeeps reading the source until it finds a NUL or fills the destination, so the copy can exceed the length the caller passed.The fix copies exactly
lengthbytes and zero-fills the rest of the destination up tosize - 1, then writes the terminator through the same zero-fill. For every input whose source ends withinmaxLength, the output bytes are identical to the previous code. That coverssceNpStrcpyToBuftoo, which calls the same helper.Commits:
565e2dc60b: the copy helper incore/libs/prx/libSceNpCommon/Strings.cpp.6739179eed:TestHexAndCopiesincore/libs/tests/GuestNpCommonRuntime.cppcopies a source whose first terminator comes after the limit and expects the first four characters.Tested
Linux, archive build of
upstream/main5c9837bfe7plus these commits, built with the validation helper in/marc/AnyPS5/.git/info/tools/run_tests.sh. That helper patches the local CPU startup gate in the throwaway copy only.ctest -R '^guest_np_common_runtime$' --output-on-failure: 1 of 1 passed, on the fixed tree (BUILD-EXIT 0).strncpy(dst, src, size - 1)). The new check aborts witha source without a terminator within the limit is cut at the limit, andctestreports 0 of 1 passed.python3 tools/check_conventions.py --base upstream/mainandgit diff --check upstream/main: clean.tools/pr_overlap.py --branchfails closed on five open PRs that touchGuestNpCommonRuntime.cpporStrings.cpp: #12818, #12852, #13130, #13270 and #13277. Compared by hand: each PR's test and strings hunks were split per file and checked withpatch --dry-run -F0againstmainand against this branch's files. They apply to this branch wherever they apply tomain. #12852 fails onmaintoo, so that is base drift. #12818 changes the XML code inStrings.cpp, notCopyToBuffer. No patch namesCopyToBuffer; the only matches forstrncpy,TestHexAndCopiesorunterminatedare in #12852's own test inTestObjects. That is evidence, not proof.tools/pr_abi_audit.py. Run by hand from the main checkout on this head,tools/pr_abi_audit.py --base upstream/main --head 6739179eed --jsonreports"status": "passed". The test-quality helper, also run by hand, reports onepossible-unregistered-cpp-testfinding forGuestNpCommonRuntime.cpp. That is a false positive: the test is registered asadd_test_executable(guest_np_common_runtime_tests tests/GuestNpCommonRuntime.cpp)incore/libs/CMakeLists.txt, relative tocore/libs, and it ran in the archive build.Not run: Windows and macOS builds, a title or console run, and the null-argument paths, which this change does not touch.
Checklist
mainNotImplemented_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)Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T01:08:31Z.
Review: COMMENTED
Read against main:
sceNpStrncpyToBufpasseslength = strnlen(src, maxLength)(Strings.cpp:478), butstrncpy(dst, src, size - 1)copied until the source's terminator, pastmaxLength. With the new test's input ("ABCDEF", limit 4, size 8), main writes"ABCDEF"and the PR writes"ABCD".memcpy(length)+memset(dst + length, 0, size - length)stays withinsize, keeps the old zero padding, and leavessceNpStrcpyToBuf(length = strlen) byte-identical. Looks correct. I haven't built or run it yet.Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T01:16:02Z.
Review: APPROVED
Follow-up to my earlier comment: I've now run it.
Windows 11, WinLibs GCC 15.2.0 r7, Release. Head
6739179eedmerged into upstream/main93dbd0f82a:guest_np_common_runtimepasses.Strings.cppaborts witha source without a terminator within the limit is cut at the limit(0xc0000409), so the new check covers the change.With the read-through above (
length = strnlen(src, maxLength), the copy and zero fill stay withinsize,sceNpStrcpyToBufoutput unchanged), this looks good to merge.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T06:21:12Z.
Review: COMMENTED
Reviewed — no blocking findings
CopyToBuffernow copies exactlylengthbytes and zero-fills the rest of the destination. Before,strncpycopied up tosize - 1bytes, which could go past the caller's length limit when the source was longer than the limit. Both callers pass a length that stops at the first NUL, so the change affects only that case. The new test covers it. No hosted job has run this head: it has no Windows check-run at all.What I ran
6739179eedd24b1a8bb5f1f8262281ac485574c3; base26fa1212b82f76dd49cf12d65e445d6ff3e0844c. 2 files:libSceNpCommon/Strings.cpp(the function body) andtests/GuestNpCommonRuntime.cpp(one new check).Strings.cpp:CopyToBuffer(dst, size, src, std::strlen(src))andCopyToBuffer(dst, size, src, strnlen(src, maxLength)). Solengthnever includes a NUL. Checked the bounds: the size check keepslength + 1 <= size, anddst + length + (size - length)isdst + size.sceNpStrncpyToBuf(copy, 8, "ABCDEF\0", 4)must give"ABCD".GuestNpCommonRuntimeexists anywhere for it.Results
strlenandstrnlenlength + 1 <= size; the fill ends atsizelength, as the title saysGuestNpCommonRuntime)Read-only check output
Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. ABI audit: passed (26 symbols). Stack check: passed. Hosted:
gh api repos/boykopovar/AnyPS5/commits/6739179eedd24b1a8bb5f1f8262281ac485574c3/check-runs.Negative control
Not run here. A negative control needs a local build, and code from this head is not run here.
Findings
Blocking
None.
Non-blocking
GuestNpCommonRuntime.cpphas no result anywhere. A run at this head would settle it.Sources
github.com/boykopovar/AnyPS5@6739179eed/core/libs/prx/libSceNpCommon/Strings.cpp(lines 191-196; callers at 432 and 436)github.com/boykopovar/AnyPS5@6739179eed/core/libs/tests/GuestNpCommonRuntime.cpp(TestHexAndCopies)Not verified
sceNpStrncpyToBuffor a source longer than the limit.To get this to a good state