fix(libkernel_sys): reject parent-directory components in symlink targets #6200

Merged
valenciana-nc merged 3 commits from upstream-pr/6200 into main 2026-10-19 02:22:00 +00:00
valenciana-nc commented 2026-10-15 02:37:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-15T02:37:39Z.


What

symlink_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp rejected absolute symlink targets, but it stored relative targets verbatim without checking for .. components. The guest path resolver (ResolveHostPath in core/libs/prx/libc/src/General.cpp) accepts a symlink component as-is, and the host OS follows it when the link is later opened. So two calls escape the guest sandbox:

symlink("../../../../..", "e");
sceKernelOpen("e/etc/passwd", O_RDONLY, 0);  // reads the HOST /etc/passwd

The fix rejects any .. component in the target. This is deliberately conservative: a/../b is also rejected even when it would stay inside the root.

Per maintainer review, the absolute-target check now rejects any target with a root name or root directory (has_root_name() || has_root_directory()) instead of only a leading /. Windows targets such as C:/ or \\server\share are therefore rejected too. Guest tests (CheckSymlinkTargetRestrictions in core/libs/tests/GuestKernelSys.cpp) cover:

  • Rejected targets: .., ../x, a/../b, a/../../x and /abs, plus C:/ and a UNC path on Windows.
  • Accepted targets: sub/dir, a..b and ..., created as real links. The test reads each target back through readlink_nid_postfix and removes the link.

The test now uses readlink because MinGW's std::filesystem::symlink_status does not report symlinks, so the earlier is_symlink checks failed on Windows. Left in place, the accepted links also made the test's final std::filesystem::remove_all throw on Windows.

Tested

Each build is this branch merged onto main a373e85e5, Release, BUILD_TESTING=ON.

  • Windows 11 (26300) with symbolic links enabled, WinLibs GCC 15.2.0 r7:
    • The 463 guest_* tests registered under core/libs: all pass, the same as main. guest_video_out_wqhd_detection_no_vulkan is skipped on both.
    • guest_kernel_sys runs the accepted-link cases.
    • With Export.cpp from main, guest_kernel_sys fails at the .. rejection check.
  • Linux, Ubuntu 24.04 container (Docker Desktop, WSL2 kernel 6.6), GCC 14.2:
    • 417 of 419 guest_* tests pass, including guest_kernel_sys. The two failures, guest_system_load_exec_standard_error and guest_named_shm_concurrency, fail on main in the same container too; it runs as root.
    • With Export.cpp from main, guest_kernel_sys fails at the .. rejection check.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4867385047 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200). Originally posted by [valenciana-nc](https://github.com/valenciana-nc) on 2026-10-15T02:37:39Z. --- ### What `symlink_nid_postfix` in `core/libs/prx/libkernel_sys/Export.cpp` rejected absolute symlink targets, but it stored relative targets verbatim without checking for `..` components. The guest path resolver (`ResolveHostPath` in `core/libs/prx/libc/src/General.cpp`) accepts a symlink component as-is, and the host OS follows it when the link is later opened. So two calls escape the guest sandbox: ``` symlink("../../../../..", "e"); sceKernelOpen("e/etc/passwd", O_RDONLY, 0); // reads the HOST /etc/passwd ``` The fix rejects any `..` component in the target. This is deliberately conservative: `a/../b` is also rejected even when it would stay inside the root. Per maintainer review, the absolute-target check now rejects any target with a root name or root directory (`has_root_name() || has_root_directory()`) instead of only a leading `/`. Windows targets such as `C:/` or `\\server\share` are therefore rejected too. Guest tests (`CheckSymlinkTargetRestrictions` in `core/libs/tests/GuestKernelSys.cpp`) cover: - **Rejected targets:** `..`, `../x`, `a/../b`, `a/../../x` and `/abs`, plus `C:/` and a UNC path on Windows. - **Accepted targets:** `sub/dir`, `a..b` and `...`, created as real links. The test reads each target back through `readlink_nid_postfix` and removes the link. The test now uses `readlink` because MinGW's `std::filesystem::symlink_status` does not report symlinks, so the earlier `is_symlink` checks failed on Windows. Left in place, the accepted links also made the test's final `std::filesystem::remove_all` throw on Windows. ### Tested Each build is this branch merged onto `main` `a373e85e5`, Release, `BUILD_TESTING=ON`. - **Windows 11 (26300) with symbolic links enabled, WinLibs GCC 15.2.0 r7:** - The 463 `guest_*` tests registered under `core/libs`: all pass, the same as `main`. `guest_video_out_wqhd_detection_no_vulkan` is skipped on both. - `guest_kernel_sys` runs the accepted-link cases. - With `Export.cpp` from `main`, `guest_kernel_sys` fails at the `..` rejection check. - **Linux, Ubuntu 24.04 container (Docker Desktop, WSL2 kernel 6.6), GCC 14.2:** - 417 of 419 `guest_*` tests pass, including `guest_kernel_sys`. The two failures, `guest_system_load_exec_standard_error` and `guest_named_shm_concurrency`, fail on `main` in the same container too; it runs as root. - With `Export.cpp` from `main`, `guest_kernel_sys` fails at the `..` rejection check. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [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`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [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
github-actions[bot] commented 2026-10-15 02:37:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T02:37:58Z.


Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6183133145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200#issuecomment-6183133145). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T02:37:58Z. --- Thank you for your contribution! Authors with no pull requests merged into `main` can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.
github-actions[bot] commented 2026-10-17 23:27:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T23:27:08Z.


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 5021 core/libs/prx/libkernel_sys/Export.cpp
PR 6044 core/libs/tests/GuestKernelSys.cpp
PR 7039 core/libs/tests/GuestKernelSys.cpp
PR 7201 core/libs/prx/libkernel_sys/Export.cpp
PR 7332 core/libs/prx/libkernel_sys/Export.cpp
core/libs/tests/GuestKernelSys.cpp
PR 7658 core/libs/prx/libkernel_sys/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243059378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200#issuecomment-6243059378). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T23:27:08Z. --- <!-- pr-overlap --> 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 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel_sys/Export.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKernelSys.cpp` | | [PR 7039](https://www.github.com/boykopovar/AnyPS5/pull/7039) | `core/libs/tests/GuestKernelSys.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel_sys/Export.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel_sys/Export.cpp`<br>`core/libs/tests/GuestKernelSys.cpp` | | [PR 7658](https://www.github.com/boykopovar/AnyPS5/pull/7658) | `core/libs/prx/libkernel_sys/Export.cpp` |
oneandonlydean commented 2026-10-17 23:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T23:57:55Z.


Thanks for finding this. The escape is real on current main: ResolveHostPath lets the host follow a relative symlink out of the guest root, and this PR closes the .. route. The PR merges cleanly into current main, and the changed Export.cpp compiles there. Two things before it can go in:

1. Windows root names still get through. On Windows, a target such as C:/Users, C:\Windows or \\server\share has a root name, so std::filesystem::path treats it as absolute. It doesn't start with / and has no .. component, so it passes both the existing check and the new one, and CreateSymbolicLinkW gets an absolute host target. That is the same escape:

symlink("C:/", "e");
sceKernelOpen("e/Windows/win.ini", O_RDONLY, 0);  // would read the host file

(I haven't run this on Windows; it follows from how std::filesystem parses root names. On Linux, C:/x is just a relative name inside the root, so only Windows is affected.)

Rejecting any target with a root name or a root directory covers /, C:, C:/ and UNC paths in one place, for example:

const std::filesystem::path contents(target);
if (contents.has_root_name() || contents.has_root_directory()) {
    NotImplemented_nid_no_patch("symlink: absolute targets");
    return -1;
}
for (const auto& part : contents) {
    if (part == "..") {
        NotImplemented_nid_no_patch("symlink: parent-directory targets");
        return -1;
    }
}

That can replace the existing *target == '/' check.

2. Please add a test. The PR has no test yet. A guest test that calls symlink_nid_postfix should cover:

  • rejected: .., ../x, a/../b, a/../../x, /abs, and under _WIN32 also C:/ and \\server\share
  • accepted, with the link created: sub/dir, a..b, ...

Registering it in the existing libkernel_sys tests is fine. The test must fail on current main for the .. cases.

Keeping the conservative behaviour (rejecting a/../b even when it would stay inside the root) is fine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243432163 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200#issuecomment-6243432163). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T23:57:55Z. --- Thanks for finding this. The escape is real on current `main`: `ResolveHostPath` lets the host follow a relative symlink out of the guest root, and this PR closes the `..` route. The PR merges cleanly into current `main`, and the changed `Export.cpp` compiles there. Two things before it can go in: **1. Windows root names still get through.** On Windows, a target such as `C:/Users`, `C:\Windows` or `\\server\share` has a root name, so `std::filesystem::path` treats it as absolute. It doesn't start with `/` and has no `..` component, so it passes both the existing check and the new one, and `CreateSymbolicLinkW` gets an absolute host target. That is the same escape: ``` symlink("C:/", "e"); sceKernelOpen("e/Windows/win.ini", O_RDONLY, 0); // would read the host file ``` (I haven't run this on Windows; it follows from how `std::filesystem` parses root names. On Linux, `C:/x` is just a relative name inside the root, so only Windows is affected.) Rejecting any target with a root name or a root directory covers `/`, `C:`, `C:/` and UNC paths in one place, for example: ```cpp const std::filesystem::path contents(target); if (contents.has_root_name() || contents.has_root_directory()) { NotImplemented_nid_no_patch("symlink: absolute targets"); return -1; } for (const auto& part : contents) { if (part == "..") { NotImplemented_nid_no_patch("symlink: parent-directory targets"); return -1; } } ``` That can replace the existing `*target == '/'` check. **2. Please add a test.** The PR has no test yet. A guest test that calls `symlink_nid_postfix` should cover: - rejected: `..`, `../x`, `a/../b`, `a/../../x`, `/abs`, and under `_WIN32` also `C:/` and `\\server\share` - accepted, with the link created: `sub/dir`, `a..b`, `...` Registering it in the existing libkernel_sys tests is fine. The test must fail on current `main` for the `..` cases. Keeping the conservative behaviour (rejecting `a/../b` even when it would stay inside the root) is fine.
valenciana-nc commented 2026-10-18 21:22:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-18T21:22:18Z.


Both points addressed in 84d77798c:

  1. The absolute-target check is now your suggested has_root_name() || has_root_directory() form, replacing the leading-/ check, so C:/, \\server\share and other root-named targets are rejected too. The later contents declaration was folded into it. Conservative .. rejection (including a/../b) kept.

  2. Added CheckSymlinkTargetRestrictions to core/libs/tests/GuestKernelSys.cpp: rejects .., ../x, a/../b, a/../../x, /abs (plus C:/ and UNC paths under _WIN32); accepts and creates real links for sub/dir, a..b, .... Verified the test aborts on unpatched main at the .. check and that the full guest_kernel_sys_tests suite passes with the fix on Linux. check_conventions.py passes. (Built outside CMake since submodules are uninitialized here — details in the PR body.)

This change was written with AI assistance, as disclosed in the PR body.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6255304184 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200#issuecomment-6255304184). Originally posted by [valenciana-nc](https://github.com/valenciana-nc) on 2026-10-18T21:22:18Z. --- Both points addressed in 84d77798c: 1. The absolute-target check is now your suggested `has_root_name() || has_root_directory()` form, replacing the leading-`/` check, so `C:/`, `\\server\share` and other root-named targets are rejected too. The later `contents` declaration was folded into it. Conservative `..` rejection (including `a/../b`) kept. 2. Added `CheckSymlinkTargetRestrictions` to `core/libs/tests/GuestKernelSys.cpp`: rejects `..`, `../x`, `a/../b`, `a/../../x`, `/abs` (plus `C:/` and UNC paths under `_WIN32`); accepts and creates real links for `sub/dir`, `a..b`, `...`. Verified the test aborts on unpatched `main` at the `..` check and that the full `guest_kernel_sys_tests` suite passes with the fix on Linux. `check_conventions.py` passes. (Built outside CMake since submodules are uninitialized here — details in the PR body.) This change was written with AI assistance, as disclosed in the PR body.
oneandonlydean commented 2026-10-19 01:22:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-19T01:22:05Z.


Thanks for the update. The checks themselves work on Windows: in our full CI the C:/ and UNC cases pass. The new test still fails on Windows, but for a test-only reason:

Kernel sys check failed at line 111 (guest errno 2)

(at the merge with current main). Line 111 is Require(std::filesystem::is_symlink(std::filesystem::symlink_status(nested))); in CheckSymlinkTargetRestrictions. The Windows CI build uses MinGW GCC with libstdc++, and libstdc++'s std::filesystem doesn't recognise symlinks on Windows: symlink_status reports the link as a plain file or directory, and create_symlink fails. That's why the existing Windows block in this test checks FILE_ATTRIBUTE_REPARSE_POINT and readlink_nid_postfix, and only uses is_symlink under #else. Here, symlink_nid_postfix really does create the link (it returns 0), and only the check fails.

Suggested fix: check what was stored through readlink, which works on both platforms and also tests the target text, e.g.

char buffer[16];
Require(readlink_nid_postfix(nested.c_str(), buffer, sizeof(buffer)) == 7 && std::memcmp(buffer, "sub/dir", 7) == 0);

and the same for a..b (4 bytes) and ... (3 bytes) in place of the three is_symlink checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6257647398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200#issuecomment-6257647398). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-19T01:22:05Z. --- Thanks for the update. The checks themselves work on Windows: in our full CI the `C:/` and UNC cases pass. The new test still fails on Windows, but for a test-only reason: ``` Kernel sys check failed at line 111 (guest errno 2) ``` (at the merge with current main). Line 111 is `Require(std::filesystem::is_symlink(std::filesystem::symlink_status(nested)));` in `CheckSymlinkTargetRestrictions`. The Windows CI build uses MinGW GCC with libstdc++, and libstdc++'s `std::filesystem` doesn't recognise symlinks on Windows: `symlink_status` reports the link as a plain file or directory, and `create_symlink` fails. That's why the existing Windows block in this test checks `FILE_ATTRIBUTE_REPARSE_POINT` and `readlink_nid_postfix`, and only uses `is_symlink` under `#else`. Here, `symlink_nid_postfix` really does create the link (it returns 0), and only the check fails. Suggested fix: check what was stored through `readlink`, which works on both platforms and also tests the target text, e.g. ```cpp char buffer[16]; Require(readlink_nid_postfix(nested.c_str(), buffer, sizeof(buffer)) == 7 && std::memcmp(buffer, "sub/dir", 7) == 0); ``` and the same for `a..b` (4 bytes) and `...` (3 bytes) in place of the three `is_symlink` checks.
valenciana-nc commented 2026-10-19 01:37:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-19T01:37:04Z.


I ran the test in the CMake builds on Windows and Linux; before, it had only been built outside CMake on Linux.

On Windows the accepted-target cases failed, because MinGW's std::filesystem::symlink_status does not report symlinks. 1a200a1e1 fixes the test:

  • It reads each accepted target back through readlink_nid_postfix.
  • It removes the accepted links afterwards. Left in place, they made the test's final remove_all throw on Windows.

Both platforms pass now. With Export.cpp from main, the test fails at the .. check on both.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6257803344 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6200#issuecomment-6257803344). Originally posted by [valenciana-nc](https://github.com/valenciana-nc) on 2026-10-19T01:37:04Z. --- I ran the test in the CMake builds on Windows and Linux; before, it had only been built outside CMake on Linux. On Windows the accepted-target cases failed, because MinGW's `std::filesystem::symlink_status` does not report symlinks. 1a200a1e1 fixes the test: - It reads each accepted target back through `readlink_nid_postfix`. - It removes the accepted links afterwards. Left in place, they made the test's final `remove_all` throw on Windows. Both platforms pass now. With `Export.cpp` from `main`, the test fails at the `..` check on both.
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!6200
No description provided.