Skip to content

fix(libSceHttp): safely probe the outputs of the URI builders - #20805

Open
Tanju42 wants to merge 2 commits into
boykopovar:mainfrom
Tanju42:stack/libhttp-probe-uri-outputs
Open

Tanju42 wants to merge 2 commits into
boykopovar:mainfrom
Tanju42:stack/libhttp-probe-uri-outputs

Conversation

@Tanju42

@Tanju42 Tanju42 commented Nov 15, 2026 •

Copy link
Copy Markdown

What

sceHttpUriBuild, sceHttpUriEscape and sceHttpUriMerge wrote requiredSize/require through direct stores and the output buffer through raw std::memcpy — an unmapped guest output was a raw host write. Route every one of these writes through GuestCopyOut_nid_no_patch, in the order the library already wrote them; an unwritable output returns SCE_HTTP_ERROR_INVALID_VALUE (0x804311FE) and leaves errno alone, following sceHttpsGetSslError in the same library. The merge's non-opaque path keeps #12056's zero-padded copy of the relative URI, including the forced terminator at the last byte. Console behavior on unwritable outputs is unmeasured; recorded as a docs/dev/debt/changes fragment.

Tested

Ubuntu 24.04 cloud VM, 8 cores, CPU-only (lavapipe), no GPU. Fresh full build tree (configure + 5500+ targets) of this stack: full ctest green and -R guest_http green. No game titles run. core/libs/tests/GuestHttp.cpp gains Require lines for sceHttpUriBuild, sceHttpUriEscape and sceHttpUriMerge: unmapped output or require pointers return SCE_HTTP_ERROR_INVALID_VALUE with errno preserved, and a valid call right after a failed one still succeeds. python3 tools/check_conventions.py --base origin/main clean. pr_overlap.py --branch output verbatim below.

Independent Windows verification by a contributor: head 0306eb9a23 merged alone into main 56c87adbdc on Windows 11 26300 (i7-12700K, WinLibs GCC 15.2.0, Release) — the tests this PR adds/extends, guest_http and guest_http_uri_locale, pass 2/2 over three runs, and the PR adds no build failure beyond main's own UploadCacheReadOnlyFlush.cpp setenv break (fixed by #19899) and the Defender AGC-test quarantine case documented in BUILD.md. Not run on a PS5. Full report.

Checklist

  • Based on current main; no other open PR implements the same functions — sits on fix(libSceHttp): fill a short sceHttpUriEscape buffer as a PS5 does #12056's head 3b10a94b24 (main 228cd64b53); no open PR touches the six sceHttpUri* exports
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file — no new stubs
  • No notes, investigation .md files or images added to the repository (attach them to this PR) — only the mandated debt fragment under docs/dev/debt/changes/
  • New third-party code is a submodule built from source — none
  • Generic behaviour, not specific to one title
  • Depends on: fix(libSceHttp): fill a short sceHttpUriEscape buffer as a PS5 does #12056
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (no GPU code)
  • AI-assisted: yes

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 9834 core/libs/tests/GuestHttp.cpp
docs/dev/debt/libSceHttp.md
PR 11134 core/libs/tests/GuestHttp.cpp
docs/dev/debt/libSceHttp.md
PR 14212 docs/dev/debt/libSceHttp.md
PR 18515 docs/dev/debt/libSceHttp.md
PR 19769 docs/dev/debt/libSceHttp.md

Manual comparison of that list: #9834 and #11134 edit core/libs/tests/GuestHttp.cpp only in their transport/CA areas — neither diff line touches the URI or escape tests (git diff origin/main...pr9834 -- core/libs/tests/GuestHttp.cpp contains no URI/escape lines); #14212 is itself a debt-file formatting PR; #18515 and #19769 share only docs/dev/debt/libSceHttp.md entry lines — #19769's GuestCopy pass covers scalar and struct outputs and does not overlap any sceHttpUri* export.

When prepare was too small for the escaped text and its terminator,
sceHttpUriEscape stored the full size in require and left the output
untouched before returning SCE_HTTP_ERROR_OUT_OF_MEMORY.

A PS5 does the reverse. In the console oracle row
nh_escape_buffer_too_small (an 8-byte output, prepare 4, "hello world")
it returned 0x80431022, wrote "hell" without a terminator and did not
touch require:
https://github.com/mattias800/prosper/blob/9a9a4bd1b8474e3b930f79bea32c845229ed103c/prosper/tests/data/console_oracle/nethttp.golden.tsv#L37
The other escape rows of that file (lines 31 to 36) already matched.

A buffer that is too short now gets the first prepare bytes of the
escaped text, and require is written only on success, the way
sceHttpUriUnescape already treats a short buffer. What the console
writes of a %XX sequence that reaches the end of the buffer was not
measured; the debt entry says so and lists the input bytes the oracle
covers, in place of the claim that no non-ASCII input was measured.

guest_http checks the measured row and that a buffer one byte short
leaves require alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DXPnhogDW4zpeNCBro4Ctt
Co-Authored-By: Hermes (Qwen3.8-Flash) <noreply@nousresearch.com>
@Tanju42
Tanju42 force-pushed the stack/libhttp-probe-uri-outputs branch from 94003c2 to 0306eb9 Compare November 15, 2026 16:18
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

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

Pull request Shared files not compared Same functions (from the diff)
PR 9834 core/libs/tests/GuestHttp.cpp
docs/dev/debt/libSceHttp.md
PR 11134 core/libs/tests/GuestHttp.cpp
docs/dev/debt/libSceHttp.md
PR 26370 core/libs/prx/libSceHttp/src/Uri.cpp

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 0306eb9a23, merged alone into main 56c87adbdc (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. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

The tests this PR adds or extends, guest_http, guest_http_uri_locale, 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.

@malekaifa

Copy link
Copy Markdown
Contributor

Independent recheck on 2026-11-19 found one convention blocker at the exact PR head 0306eb9a23: tools/check_conventions.py reports docs/dev/debt/changes/fix-libhttp-probe-uri-builder-outputs.md:5 has a broken relative link: ../../../core/libs/prx/libSceHttp/src/Uri.cpp resolves under docs/core; from docs/dev/debt/changes/ it needs four .. components to reach the repository root. Please correct this before merge.

Separately, the stacked test head e0b4e7c5e214 (this PR head plus the test-only follow-up) built with no work and ctest -R '^guest_http$' passed 1/1; the child PR #1 conventions check passes.

@Tanju42

Tanju42 commented Nov 22, 2026

Copy link
Copy Markdown
Author

Thanks for the recheck. The link is correct by design and the blocker does not reproduce against current main — details below.

Debt fragments in docs/dev/debt/changes/ are not resolved from their on-disk directory. tools/check_conventions.py resolves them as if they were the consolidated debt file they fold into (docs/dev/debt/<library>.md, heading-derived):

https://github.com/boykopovar/AnyPS5/blob/dfaa13cdcd/tools/check_conventions.py#L705-L714

def link_bases(path, source):
    bases = {}
    base = f"{DEBT_DIR}fragment.md" if is_debt_fragment(path) else path
    for number, line in enumerate(source.split("\n"), 1):
        if is_debt_fragment(path) and line.startswith("# "):
            library = line[2:].strip()
            base = DEBT if library == PurePosixPath(DEBT).stem else f"{DEBT_DIR}{library}.md"
        bases[number] = base
    return bases

Our fragment's heading is # libSceHttp, so line 5 resolves from docs/dev/debt/libSceHttp.md: ../../../core/libs/prx/libSceHttp/src/Uri.cpp → repo-root core/libs/prx/libSceHttp/src/Uri.cpp. With four .. the link would escape above the repo root and break the consolidated docs/dev/debt/libSceHttp.md after the fold.

Fresh verification today (2026-11-22) from a current-main worktree at dfaa13cdcd:

git worktree add -f ~/main-wt origin/main
cd ~/main-wt && python3 -I tools/check_conventions.py --base HEAD --head 0306eb9a23
All conventions checks passed.

CI conventions is also green at the exact head 0306eb9a23 (alongside pr and labels). My guess is the recheck resolved the link from the fragment's real directory (or from an older checker version) — resolving from docs/dev/debt/ is the sanctioned behaviour because fragments get consolidated there. Holding the fragment as-is; happy to reconsider if a checker change makes the 4-level form required.

@malekaifa

Copy link
Copy Markdown
Contributor

Confirmed — your explanation is correct, and I’m retracting my earlier conventions blocker.

I reproduced the exact check from a clean detached current-main worktree at c94713f7c7e3a1ef79ab45ba8edcc8decaea4073:

python -I tools/check_conventions.py --base HEAD --head 0306eb9a23407d4a80e21dbbe04cc34fb693323f

Result: All conventions checks passed.

The debt-fragment link is intentionally resolved using the heading-derived consolidated debt base (docs/dev/debt/libSceHttp.md), not the fragment's physical directory. My earlier comment incorrectly evaluated the relative link from docs/dev/debt/changes/, so the requested extra .. was wrong.

No parent change is requested from me on this point. The stacked child remains based on this exact parent head.

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 — no blocking findings

The change routes the URI builder outputs through guest-safe copies and preserves the documented short-buffer behavior; no blocking defect was verified.

What I ran

Reviewed head 0306eb9a23407d4a80e21dbbe04cc34fb693323f. I read the prepared description, commits, manifest, diff, and all four changed files. I inspected the touched helpers and the GuestCopyOut_nid_no_patch implementation with repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and changed files passed All four changed files read
Tests and build not run No test execution performed
Negative control not run No assertion was deliberately broken

Findings

Blocking

None.

Non-blocking

None.

Sources

The review covered Uri.cpp at the reviewed head, GuestHttp.cpp at the reviewed head, and General.cpp at the reviewed head.

Not verified

Builds, tests, and runtime behavior were not independently verified. The review bundle included full text for every changed file; no changed files were excluded.

To get this to a good state

Nothing is required. Tests and build were not run in this review.

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

@Tanju42 current head 0306eb9a23407d4a80e21dbbe04cc34fb693323f is still stacked on #12056 and preserves its unresolved short-buffer %XX behavior in ProbeTextRaw(value, out, prepare). Firmware 13.42 evidence and @oneandonlydean's Nov. 1 request show that a short sceHttpUriEscape buffer must stop before a %XX unit that would cross the end (a b, prepare 2 => a, not a%). Please let #12056 fix that measured boundary, then restack this PR and retain the guest-pointer safety changes/tests on the corrected behavior. My earlier debt-link conventions objection remains retracted; this is the remaining blocker. AI-assisted: yes

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

Superseding my earlier change request: the conventions-link concern was retracted after reproducing the repository checker, and I have now validated this exact head against current main.

Reviewed exact PR head 0306eb9a23407d4a80e21dbbe04cc34fb693323f. I merged it in an isolated worktree onto current main c3aec4c2f341247e2ed47ae9365e2eff8e880c75. The merge is conflict-free even though main has since added broader guest-input probing in Uri.cpp and GuestHttp.cpp; the merged source retains both that newer input safety and this PR's ProbeText/ProbeSize output safety.

Machine main, Windows 11, MSYS2 UCRT64 GCC 16.2.0, Release: current-main + this PR built guest_http_tests, and ctest -R ^guest_http$ --output-on-failure passed 1/1. The merged tree also passes current conventions and git diff --check.

No blocking findings remain from my review.

AI-assisted review: yes

@malekaifa

Copy link
Copy Markdown
Contributor

I checked the short-buffer path against the firmware 13.42 boundary measurements. ProbeTextRaw(value, out, prepare) currently copies exactly prepare bytes, so sceHttpUriEscape("a b") with capacities 2 and 3 can leave a% and a%2, splitting the %20 escape. The measured behavior copies the longest prefix ending at a complete unit: capacities 1?3 write a, 4 writes a%20, 5 writes a%20b, and a full 6-byte buffer includes the terminator. On the short-buffer error, the output is not terminated and require stays unchanged.

I have a local F regression case covering capacities 0?6 and the whole-unit calculation on prime-f/fix-http-uri-escape-whole-units-20261125 (commit 2d6220a11415). Its current implementation uses memcpy, so it should not be cherry-picked over this PR's guest-pointer probing. The safe integration is to calculate the complete-unit byte count, then call ProbeTextRaw(value, out, written) when written != 0, preserving the existing ERROR_INVALID_VALUE result if the guest write probe fails, and return ERROR_OUT_OF_MEMORY for the short buffer.

The older #12056 implementation has the same partial-%XX issue. I am not opening a duplicate PR while #12056 and this URI-output PR remain open.

@Tanju42

Tanju42 commented Nov 27, 2026

Copy link
Copy Markdown
Author

Confirmed against the boundary measurements — the clamp itself belongs in the dependency, though.

The prepare-byte fill in ProbeTextRaw(value, out, prepare) (the behavior that can leave a% / a%2) comes from #12056's head 3b10a94b24; this PR adds the output probing and the short-buffer error path around it — the three behaviors you listed (short-buffer error, no terminator, require unchanged) are already satisfied by our head. Implementing the unit clamp inside this PR would fork the escape short-buffer semantics from the open dependency, so the fix belongs there: every % in escaped output is the start of a 3-char unit (a literal % escapes to %25), so the complete-unit cap is exactly computable at the same site where #12056 already implements the fill.

Could you post your regression case on #12056 — @Aspct3434 was already pinged there (#12056 (comment))? The moment the dependency head carries the unit-clamp fix, this PR restacks onto it right away.

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

I ran guest_http and guest_http_uri_guest_pointers with the patch applied on Linux. Both passed, covering the URI builders' valid results and guarded output destinations.

AI-assisted: yes.

This branch has not been deployed

No deployments
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.

6 participants