Skip to content

fix(libc): return EFAULT from gethostname for an unwritable buffer - #13794

Closed
DeveshB-1 wants to merge 1 commit into
boykopovar:mainfrom
DeveshB-1:fix/gethostname-efault
Closed

DeveshB-1 wants to merge 1 commit into
boykopovar:mainfrom
DeveshB-1:fix/gethostname-efault

Conversation

@DeveshB-1

Copy link
Copy Markdown
Contributor

What

Safely copy the hostname in gethostname using GuestCopyOut_nid_no_patch so that an unwritable output buffer returns -1 with errno 14 (EFAULT) instead of crashing the host process with SIGSEGV.

Tested

Linux x86_64 (AMD Ryzen 7 7730U):

  • guest_hostname passes 3 of 3 (ctest --test-dir build -R '^guest_hostname$' --repeat until-fail:3 -V).
  • Negative control: on unmodified main, passing an unwritable pointer to gethostname crashes deterministically with SIGSEGV (***Exception: SegFault 0.08 sec). With this fix applied, it safely returns -1 with errno 14 (EFAULT).
  • python3 tools/check_conventions.py --base origin/main reports zero findings.
  • python3 tools/pr_overlap.py --branch fix/gethostname-efault reports no conflicting open pull requests implementing the same functions.

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
  • 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

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

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 7447 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9087 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 11172 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 12849 docs/dev/debt/libc.md
PR 15973 docs/dev/debt/libc.md
PR 16791 docs/dev/debt/libc.md

@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 exact-head change uses the existing checked guest-memory copy helper to return EFAULT for an unwritable buffer, and the reported regression test distinguishes this from the crash on the base behavior.

What I ran

Reviewed head e65a480a7840a82578cef06f035107b76cbb1043 against PR base de83e8f27015b91327029755f1ea5b60b74d940b. GitHub reports the PR open and mergeable. Environment: Debian 12 x86-64, Python 3.11.2, GCC 12.2.0. No project build or local C++ test was run.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13794
git diff --check de83e8f27015b91327029755f1ea5b60b74d940b...e65a480a7840a82578cef06f035107b76cbb1043
python3 tools/pr_abi_audit.py --repo /marc/AnyPS5 --base de83e8f27015b91327029755f1ea5b60b74d940b --head e65a480a7840a82578cef06f035107b76cbb1043 --json
python3 tools/pr_test_quality.py --root /tmp/anyps5-review-13794 --json core/libs/tests/GuestHostname.cpp
gh pr checks 13794 --repo boykopovar/AnyPS5

Results

Check Result
Diff and conventions Passed.
Export ABI audit Passed with no findings; the existing gethostname_nid_postfix declaration and definition remain consistent.
Test registration The heuristic warned that GuestHostname.cpp might be unregistered; exact-head CMake explicitly builds and registers guest_hostname. False positive.
Hosted CI Windows, conventions, library-portability, intake, labels, eligibility, report, Ubuntu relinker, Windows relinker, Ubuntu register-context, and relinker-asan passed. The Windows job compiled the libraries but its changed-test selector reported no guest test source changed. Linux was skipped; macOS relinker, register-context, and write-watch jobs were still queued.
Author-reported focused test The PR description reports Linux x86-64 guest_hostname passing 3/3 and a negative control that crashes on unmodified main. It does not state the tested SHA; I did not independently reproduce it.
Local build / C++ tests Not run; initial review checks do not build.
Unedited output excerpts
All conventions checks passed.
{
  "status": "passed",
  "symbols": [
    {
      "name": "gethostname_nid_postfix",
      "computed_nid": "-nvxBWa0iDs",
      "declarations": [
        "core/libs/prx/libc/include/Api.hpp"
      ],
      "definitions": [
        "core/libs/prx/libc/src/Hostname.cpp"
      ],
      "explicit_aliases": [],
      "version": "not encoded by these source declarations; not verified"
    }
  ],
  "findings": [],
  "limitations": [
    "This lexical audit does not parse C++ fully, prove calling-convention or binary ABI correctness, verify runtime version tables, or replace compilation.",
    "An absent source declaration or explicit alias can be intentional for assembly exports, conditional platform code, or generated interfaces; inspect findings before treating them as defects."
  ]
}
No guest core/libs test sources changed
No guest core/libs test sources changed; the libs build still ran
windows	pass	7m27s	https://github.com/boykopovar/AnyPS5/actions/runs/41178585218/job/124471183544
linux	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/41178585218/job/124471184488
relinker (macos-latest, clang++)	pending	0	https://github.com/boykopovar/AnyPS5/actions/runs/41178585218/job/124471183560
write-watch (macos-15)	pending	0	https://github.com/boykopovar/AnyPS5/actions/runs/41178585218/job/124471183687

git diff --check produced no output and exited 0.

Negative control

Assertion Change used as the control Result
An unwritable output buffer returns -1/EFAULT rather than crashing The PR description reports running the same invalid-pointer case against unmodified main and the PR change Author reports main crashed with SIGSEGV and the change returned -1 with errno 14; not independently reproduced, and the tested SHA was not stated.

Findings

Blocking

None found in the reviewed source or available checks.

Non-blocking

The Linux focused test and negative control are author-reported without a tested SHA. Windows CI compiled the changed library, but its test-selection step did not run the changed guest test. The macOS jobs were pending at the final refresh, and Linux product CI was skipped.

Sources

Not verified

No local project build, local C++ run, macOS test, Linux hosted product test, or console behavior was verified. Author-reported Linux results are not independently reproduced.

@blawesom

Copy link
Copy Markdown
Contributor

Verified head 206d4b3e70ce3fb0bbf23e38009d64569ab8ced2 on Linux x86-64 (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja 1.13.2, Release, BUILD_TESTING=ON, Ryzen 7 9800X3D).

What I checked

  • Static review of the diff against merge base de83e8f27015b91327029755f1ea5b60b74d940b (3 files): gethostname_nid_postfix now routes the copy through GuestCopyOut_nid_no_patch and returns -1 with errno 14 (EFAULT) when the helper rejects the buffer, preserving the existing null-query (returns 0) and ENAMETOOLONG (63) paths and the saved-errno restore on success. The helper (core/libs/prx/libc/src/General.cpp:823) rejects null/overflowing ranges and verifies writability via GuestArena::HostWrite plus process_vm_writev (EFAULT -> false), so no host dereference of an unchecked guest pointer remains on this path. The regression test in core/libs/tests/GuestHostname.cpp asserts -1/14 for an unmapped pointer (0x10) while leaving errno untouched on either side, which distinguishes it from the base crash.
  • Debt entry in docs/dev/debt/libc.md now states the host-safety-fallback sentence ("an unwritable buffer returns -1 with errno 14 (EFAULT) as a host-safety fallback; console behaviour for unwritable memory has not been measured"), matching the resolved pattern from fix(libkernel): return EFAULT from cpuset and pthread affinity for invalid memory #13908.

Test runs

Run Command Result
Focused test x3 ctest --test-dir build -R "^guest_hostname$" --output-on-failure Passed 3/3 (0.01-0.18 s each)
Conventions python3 tools/check_conventions.py --base origin/main All conventions checks passed
Build ninja guest_hostname_tests (full configure, not relinker-only, since the test lives in core/libs/prx/libc) Clean

Negative control

Reverted only the GuestCopyOut_nid_no_patch guard back to the raw std::memcpy in a scratch build (no commit): guest_hostname fails with ***Exception: SegFault (0.15 s). Restored the file, rebuilt: Passed. No edits left behind (git status clean).

Findings

None. The change is generic, uses the existing checked copy helper, keeps the FreeBSD-following behavior otherwise, and the debt file documents the unmeasured console behavior. No AGC/shader surface involved, so no GPU limitation applies to this PR.

@adriadam10 adriadam10 self-assigned this Oct 31, 2026
@bunbeans2-arch

Copy link
Copy Markdown
Contributor

A Windows run with GCC 16: Windows 11 Pro 10.0.22631, i9-9900K, MSYS2 UCRT64 GCC 16.1.0, Release, this PR (206d4b3e70) merged into main 0a148be.

  • With this PR: guest_hostname passed 5 of 5.
  • Same tree with only Hostname.cpp put back to main (this PR's tests kept): it crashed with an access violation (0xc0000005) inside libc.prx in 5 of 5 runs.
  • main 0a148be alone (its own tests and source): the same test passed 3 of 3.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 3, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@DeveshB-1 the code looks good to me and the tests are solid (blawesom's Linux run and bunbeans2-arch's Windows run both show the fault without the fix). It needs one thing: a rebase onto main, since the PR is conflicting and labelled needs rebase. Please push the rebased head so it can be reviewed on it.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I am closing this for now. The branch conflicts with main and there has been no push since 31 Oct, so it needs a rebase and a fresh run of the hostname test. Feel free to reopen or push a rebased branch at any time and we will look at it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants