fix(libkernel): fail inet_ntop with EFAULT on an unmapped source or destination #15524

Merged
Gruzzberry merged 1 commit from upstream-pr/15524 into main 2026-11-17 10:38:41 +00:00
Gruzzberry commented 2026-11-03 17:44:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-03T17:44:24Z.


What

__inet_ntop and inet_ntop in libkernel read the address from the guest source pointer and wrote the text, and for AF_INET6 the zero padding up to size, through the guest destination pointer with raw memcpy and memset. A non-null pointer that is not mapped, such as 0x10 or an inaccessible page, faulted the host. They now read the address with GuestCopyIn_nid_no_patch (4 bytes for AF_INET, 16 for AF_INET6) and write the text and the padding with GuestCopyOut_nid_no_patch in 4096-byte chunks, and fail with a null result and EFAULT when either cannot be accessed.

The order of the checks is unchanged and matches FreeBSD 11.4's inet_ntop.c: an unknown family gives EAFNOSUPPORT before either pointer is touched, the source is read next, a result that does not fit gives ENOSPC before the destination is touched (so a bad destination with a too small size still gives ENOSPC), and then the destination is written. ENOSPC and EAFNOSUPPORT already matched the source and the console measurements pinned in GuestAddressConversion.cpp; nothing about them changes. A null source or destination still throws, as the existing tests and debt entries record, since the console dereferences them.

This is a host-safety fix, not console behaviour. inet_ntop runs entirely in user space on FreeBSD, so a console process faults on a bad pointer and sets no errno; EFAULT was not measured on a console. The entry is in docs/dev/debt/libkernel.md. Callers such as inet_ntoa_r in libSceLibcInternal and sceNetInetNtop in libSceNet keep their own handling of the result and of their own buffers; this change only makes this function safe.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on current main (rebased after the full suite; the 62 commits it picked up do not touch these files, and guest_addresses was rerun 3 times out of 3 on the rebased head).

  • guest_addresses: new CheckUnusablePointers, for both __inet_ntop and inet_ntop, covers a source at 0x10, on a PROT_NONE page and straddling a readable and an unreadable page (4 and 16 byte reads), for both families and sizes 0, 1 and 64 (EFAULT, before ENOSPC); a destination at 0x10, on a PROT_NONE page and on a read-only page, with room (EFAULT) and without (ENOSPC); an unknown family with each bad pointer (EAFNOSUPPORT); a result that ends exactly at an unreadable page (success) and one byte short of fitting (ENOSPC, nothing written); an AF_INET6 padding that ends exactly at the page (success) and one byte past it (EFAULT, the text already written). On main the test crashes; with this change it passes, three runs out of three. The existing console-measured cases still pass.
  • Full suite (3918 tests) on this branch: 19 failed in the parallel run (GPU agc_* timeouts and annotate_module under load); rerun alone, 16 pass and 3 fail. vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted fail because of the WindowsApps Python on this machine, and agc_driver_store_over_untracked, a GPU test, also fails with SignalSets.cpp reverted to main on another branch, so it does not depend on libkernel's socket code.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run.

Overlaps

tools/pr_overlap.py reports no conflicting file or export. The PRs it lists as sharing files without comparing them (they conflict with main themselves) only append to docs/dev/debt/libkernel.md. #12924 adds inet_netof, inet_lnaof, inet_makeaddr and inet_network to libSceLibcInternal and does not touch AddressConversion.cpp.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5141897588 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15524). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-03T17:44:24Z. --- ### What `__inet_ntop` and `inet_ntop` in libkernel read the address from the guest source pointer and wrote the text, and for `AF_INET6` the zero padding up to `size`, through the guest destination pointer with raw `memcpy` and `memset`. A non-null pointer that is not mapped, such as 0x10 or an inaccessible page, faulted the host. They now read the address with `GuestCopyIn_nid_no_patch` (4 bytes for `AF_INET`, 16 for `AF_INET6`) and write the text and the padding with `GuestCopyOut_nid_no_patch` in 4096-byte chunks, and fail with a null result and `EFAULT` when either cannot be accessed. The order of the checks is unchanged and matches FreeBSD 11.4's [`inet_ntop.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/inet/inet_ntop.c): an unknown family gives `EAFNOSUPPORT` before either pointer is touched, the source is read next, a result that does not fit gives `ENOSPC` before the destination is touched (so a bad destination with a too small `size` still gives `ENOSPC`), and then the destination is written. `ENOSPC` and `EAFNOSUPPORT` already matched the source and the console measurements pinned in `GuestAddressConversion.cpp`; nothing about them changes. A null source or destination still throws, as the existing tests and debt entries record, since the console dereferences them. This is a host-safety fix, not console behaviour. `inet_ntop` runs entirely in user space on FreeBSD, so a console process faults on a bad pointer and sets no errno; `EFAULT` was not measured on a console. The entry is in `docs/dev/debt/libkernel.md`. Callers such as `inet_ntoa_r` in libSceLibcInternal and `sceNetInetNtop` in libSceNet keep their own handling of the result and of their own buffers; this change only makes this function safe. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main` (rebased after the full suite; the 62 commits it picked up do not touch these files, and `guest_addresses` was rerun 3 times out of 3 on the rebased head). - `guest_addresses`: new `CheckUnusablePointers`, for both `__inet_ntop` and `inet_ntop`, covers a source at 0x10, on a `PROT_NONE` page and straddling a readable and an unreadable page (4 and 16 byte reads), for both families and sizes 0, 1 and 64 (`EFAULT`, before `ENOSPC`); a destination at 0x10, on a `PROT_NONE` page and on a read-only page, with room (`EFAULT`) and without (`ENOSPC`); an unknown family with each bad pointer (`EAFNOSUPPORT`); a result that ends exactly at an unreadable page (success) and one byte short of fitting (`ENOSPC`, nothing written); an `AF_INET6` padding that ends exactly at the page (success) and one byte past it (`EFAULT`, the text already written). On `main` the test crashes; with this change it passes, three runs out of three. The existing console-measured cases still pass. - Full suite (3918 tests) on this branch: 19 failed in the parallel run (GPU `agc_*` timeouts and `annotate_module` under load); rerun alone, 16 pass and 3 fail. `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted` fail because of the WindowsApps Python on this machine, and `agc_driver_store_over_untracked`, a GPU test, also fails with `SignalSets.cpp` reverted to `main` on another branch, so it does not depend on libkernel's socket code. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. ### Overlaps `tools/pr_overlap.py` reports no conflicting file or export. The PRs it lists as sharing files without comparing them (they conflict with `main` themselves) only append to `docs/dev/debt/libkernel.md`. #12924 adds `inet_netof`, `inet_lnaof`, `inet_makeaddr` and `inet_network` to libSceLibcInternal and does not touch `AddressConversion.cpp`. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-08 21:44:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T21:44:00Z.

Review: COMMENTED


Windows run of head d017b1e07b, merged onto main 095ff72367 in one tree with other open PRs that touch different files (each PR's test is its own file, so they don't interact). Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • guest_addresses with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's AddressConversion.cpp and this PR's test: 5 / 5 runs fail.

From reading: the source copy is 4 bytes for AF_INET and 16 for AF_INET6, and the AF_INET6 padding is written in 4 KiB chunks, so a large capacity doesn't need a large buffer. The debt entry records the EFAULT as a host-safety fallback, which matches FreeBSD's user-space inet_ntop.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5819793533 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15524#pullrequestreview-5819793533). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T21:44:00Z. **Review:** COMMENTED --- Windows run of head `d017b1e07b`, merged onto `main` `095ff72367` in one tree with other open PRs that touch different files (each PR's test is its own file, so they don't interact). Setup: Windows 11, WinLibs GCC 15.2 r7 (`x86_64-ucrt-posix-seh`, the CI toolchain), Release, `BUILD_TESTING=ON`. - `guest_addresses` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s `AddressConversion.cpp` and this PR's test: 5 / 5 runs fail. From reading: the source copy is 4 bytes for `AF_INET` and 16 for `AF_INET6`, and the `AF_INET6` padding is written in 4 KiB chunks, so a large `capacity` doesn't need a large buffer. The debt entry records the `EFAULT` as a host-safety fallback, which matches FreeBSD's user-space `inet_ntop`.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!15524
No description provided.