fix(libkernel): return EFAULT from getlogin_r and SetLogin for invalid memory pointers #13577

Merged
pushkarreddyy merged 6 commits from upstream-pr/13577 into main 2026-10-31 04:08:36 +00:00
pushkarreddyy commented 2026-10-30 18:42:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-30T18:42:22Z.


What

Fixes #13567.
getlogin_r_nid_postfix and SetLogin_nid_no_patch directly read from and wrote to guest memory pointers without guest copy safety checks. When called with non-null unwritable output memory or an unreadable name pointer, the host crashed with a segmentation fault (SIGSEGV).

This change:

  • Uses GuestCopyOut_nid_no_patch in getlogin_r_nid_postfix to safely write the account name to guest output memory, returning 14 (EFAULT) when unwritable.
  • Uses CopyGuestString_nid_no_patch in SetLogin_nid_no_patch to safely copy the login name from guest memory, returning 14 (EFAULT) on bad memory.
  • Adds regression unit tests in core/libs/tests/GuestGetlogin.cpp verifying invalid output pointer handling before and after session login is set.
  • Updates documentation in docs/dev/debt/libkernel.md.

Tested

  • Ran python tools/check_conventions.py --base origin/main.
  • Added unit tests in core/libs/tests/GuestGetlogin.cpp verifying that unwritable pointers return 14 without modifying guest memory.

Checklist

  • What
  • Tested
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5089382817 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13577). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-30T18:42:22Z. --- ### What Fixes #13567. `getlogin_r_nid_postfix` and `SetLogin_nid_no_patch` directly read from and wrote to guest memory pointers without guest copy safety checks. When called with non-null unwritable output memory or an unreadable name pointer, the host crashed with a segmentation fault (`SIGSEGV`). This change: - Uses `GuestCopyOut_nid_no_patch` in `getlogin_r_nid_postfix` to safely write the account name to guest output memory, returning `14` (`EFAULT`) when unwritable. - Uses `CopyGuestString_nid_no_patch` in `SetLogin_nid_no_patch` to safely copy the login name from guest memory, returning `14` (`EFAULT`) on bad memory. - Adds regression unit tests in `core/libs/tests/GuestGetlogin.cpp` verifying invalid output pointer handling before and after session login is set. - Updates documentation in `docs/dev/debt/libkernel.md`. ### Tested - Ran `python tools/check_conventions.py --base origin/main`. - Added unit tests in `core/libs/tests/GuestGetlogin.cpp` verifying that unwritable pointers return `14` without modifying guest memory. ### Checklist - [x] What - [x] Tested - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
jotitadev commented 2026-10-30 21:31:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T21:31:39Z.

Review: COMMENTED


Linux run of head 330e1c1d69, merged onto main 6472c708ae (clean merge). Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON, Ninja.

guest_getlogin fails 3/3 on this head, at two separate checks:

  1. Line 91, getlogin_r_nid_postfix(reinterpret_cast<char*>(0x10), 1) == 14. It returns 34. The size check (length > size -> 34) runs before GuestCopyOut, and any host account name needs more than 1 byte, so this one doesn't depend on the host OS. The next line (0x10 with the full 4096-byte size) already covers EFAULT.
  2. With line 91 removed, line 93, *__error_nid_postfix() == 12345, fails: errno is 14 after the 0x10 call. On Linux, GuestCopyOut_nid_no_patch uses process_vm_writev, which sets the host errno to EFAULT, and that is the same errno the guest reads. The existing code already saves and restores errno around ReadLogin for this reason; the new GuestCopyOut calls are outside that window. The Windows path (VirtualQuery + memcpy) doesn't touch errno.

Fix I verified: drop the size-1 0x10 check and restore errno after each GuestCopyOut in getlogin_r_nid_postfix:

             if (sessionLogin->size() + 1 > static_cast<std::size_t>(size)) return 34;
-            if (!GuestCopyOut_nid_no_patch(output, sessionLogin->c_str(), sessionLogin->size() + 1)) return 14;
-            return 0;
+            const int saved = errno;
+            const bool copied = GuestCopyOut_nid_no_patch(output, sessionLogin->c_str(), sessionLogin->size() + 1);
+            errno = saved;
+            return copied ? 0 : 14;
 ...
     if (length > static_cast<std::size_t>(size)) return 34;
-    if (!GuestCopyOut_nid_no_patch(output, buffer, length)) return 14;
-    return 0;
+    const bool copied = GuestCopyOut_nid_no_patch(output, buffer, length);
+    errno = saved;
+    return copied ? 0 : 14;

With that, guest_getlogin passes 3/3. Reverting only Login.cpp to main (fixed test kept) segfaults 3/3, so the test does catch the original bug. public_headers passes.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5718882433 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13577#pullrequestreview-5718882433). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T21:31:39Z. **Review:** COMMENTED --- Linux run of head `330e1c1d69`, merged onto `main` `6472c708ae` (clean merge). Ubuntu 26.04, i7-8700, GCC 15.2, Release, `BUILD_TESTING=ON`, Ninja. `guest_getlogin` fails 3/3 on this head, at two separate checks: 1. Line 91, `getlogin_r_nid_postfix(reinterpret_cast<char*>(0x10), 1) == 14`. It returns 34. The size check (`length > size` -> 34) runs before `GuestCopyOut`, and any host account name needs more than 1 byte, so this one doesn't depend on the host OS. The next line (`0x10` with the full 4096-byte size) already covers EFAULT. 2. With line 91 removed, line 93, `*__error_nid_postfix() == 12345`, fails: errno is 14 after the `0x10` call. On Linux, `GuestCopyOut_nid_no_patch` uses `process_vm_writev`, which sets the host errno to EFAULT, and that is the same errno the guest reads. The existing code already saves and restores errno around `ReadLogin` for this reason; the new `GuestCopyOut` calls are outside that window. The Windows path (`VirtualQuery` + `memcpy`) doesn't touch errno. Fix I verified: drop the size-1 `0x10` check and restore `errno` after each `GuestCopyOut` in `getlogin_r_nid_postfix`: ```diff if (sessionLogin->size() + 1 > static_cast<std::size_t>(size)) return 34; - if (!GuestCopyOut_nid_no_patch(output, sessionLogin->c_str(), sessionLogin->size() + 1)) return 14; - return 0; + const int saved = errno; + const bool copied = GuestCopyOut_nid_no_patch(output, sessionLogin->c_str(), sessionLogin->size() + 1); + errno = saved; + return copied ? 0 : 14; ... if (length > static_cast<std::size_t>(size)) return 34; - if (!GuestCopyOut_nid_no_patch(output, buffer, length)) return 14; - return 0; + const bool copied = GuestCopyOut_nid_no_patch(output, buffer, length); + errno = saved; + return copied ? 0 : 14; ``` With that, `guest_getlogin` passes 3/3. Reverting only `Login.cpp` to `main` (fixed test kept) segfaults 3/3, so the test does catch the original bug. `public_headers` passes. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!13577
No description provided.