fix(libkernel): fail unlink and rmdir of a path that ends in a dot component with EINVAL #8797

Merged
xXPBSXx merged 5 commits from upstream-pr/8797 into main 2026-10-22 18:40:45 +00:00
xXPBSXx commented 2026-10-21 00:57:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T00:57:03Z.


What

FreeBSD 11.4 refuses "." and ".." as the last component of a delete lookup with EINVAL before any other check (lookup(), sys/kern/vfs_lookup.c:750-754, 823-826). unlink/sceKernelUnlink answered EPERM, rmdir/sceKernelRmdir answered ENOTEMPTY for "..", and unlinkat without AT_REMOVEDIR answered EPERM. They now run the RenameDot check from #8174 first, so a missing or non-directory parent still reports ENOENT/ENOTDIR. remove (libc) is not touched. The unlinkat(base, ".", 0) expectation in GuestKernelSys.cpp changes from EPERM to EINVAL for the same source line; I did not measure a console.
Fixes #8796

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main 57fdffd915 + this commit:

  • guest_kernel_sys (line 549, guest errno 1) and guest_filesystem (line 243, guest errno 1) fail with the three source files taken back and the new cases in place; both pass with the change.
  • Tests that call these functions pass (11): guest_filesystem, guest_kernel_sync, guest_kernel_sys, guest_kqueue, guest_path_name_length, guest_poll, guest_posix_scalar_io, guest_rename_dot, guest_select, guest_select_wait, guest_truncate_extend.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux and macOS, a console, a title run.

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 #8796
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4944702666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T00:57:03Z. --- ### What FreeBSD 11.4 refuses "." and ".." as the last component of a delete lookup with `EINVAL` before any other check (`lookup()`, sys/kern/vfs_lookup.c:750-754, 823-826). `unlink`/`sceKernelUnlink` answered `EPERM`, `rmdir`/`sceKernelRmdir` answered `ENOTEMPTY` for "..", and `unlinkat` without `AT_REMOVEDIR` answered `EPERM`. They now run the `RenameDot` check from #8174 first, so a missing or non-directory parent still reports `ENOENT`/`ENOTDIR`. `remove` (libc) is not touched. The `unlinkat(base, ".", 0)` expectation in `GuestKernelSys.cpp` changes from `EPERM` to `EINVAL` for the same source line; I did not measure a console. Fixes #8796 ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main 57fdffd915 + this commit: - `guest_kernel_sys` (line 549, guest errno 1) and `guest_filesystem` (line 243, guest errno 1) fail with the three source files taken back and the new cases in place; both pass with the change. - Tests that call these functions pass (11): guest_filesystem, guest_kernel_sync, guest_kernel_sys, guest_kqueue, guest_path_name_length, guest_poll, guest_posix_scalar_io, guest_rename_dot, guest_select, guest_select_wait, guest_truncate_extend. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux and macOS, a console, a title run. ### Checklist - [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 #8796 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-21 00:57:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T00:57:38Z.

Review: CHANGES_REQUESTED


The conventions check is failing because the PR body does not fill in the required ### What section. Please move the implementation summary currently above ### Tested under ### What (keeping the remaining template headings), then rerun the check. No code issue identified in this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5593665964 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#pullrequestreview-5593665964). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T00:57:38Z. **Review:** CHANGES_REQUESTED --- The conventions check is failing because the PR body does not fill in the required `### What` section. Please move the implementation summary currently above `### Tested` under `### What` (keeping the remaining template headings), then rerun the check. No code issue identified in this review.
MarcZX10 commented 2026-10-21 00:59:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T00:59:04Z.

Review: APPROVED


I independently ran python3 tools/check_conventions.py --base upstream/main on the current head 864baa17d4382da3575e6331ea3f2009dec0ea2f; it passes. The Windows test results are documented in the PR. I could not run a fresh local build here because the workspace filesystem has only 141 MB free. The hosted build/test jobs were skipped on this PR head. I found no additional code issue in this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5593675883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#pullrequestreview-5593675883). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T00:59:04Z. **Review:** APPROVED --- I independently ran `python3 tools/check_conventions.py --base upstream/main` on the current head `864baa17d4382da3575e6331ea3f2009dec0ea2f`; it passes. The Windows test results are documented in the PR. I could not run a fresh local build here because the workspace filesystem has only 141 MB free. The hosted build/test jobs were skipped on this PR head. I found no additional code issue in this review.
MarcZX10 commented 2026-10-21 00:59:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T00:59:10Z.


Follow-up: I ran the repository script locally, and it passes against upstream/main. The hosted conventions check is a separate PR-template validation and remains failed because ### What is not filled; please fix that section and rerun checks. My approval only covers the code review, not the failing required check.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6305329947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#issuecomment-6305329947). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T00:59:10Z. --- Follow-up: I ran the repository script locally, and it passes against upstream/main. The hosted conventions check is a separate PR-template validation and remains failed because `### What` is not filled; please fix that section and rerun checks. My approval only covers the code review, not the failing required check.
MarcZX10 commented 2026-10-21 00:59:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T00:59:47Z.

Review: CHANGES_REQUESTED


I am maintaining the request to fill the required ### What section. The earlier approval was only for the code review and does not apply while the hosted conventions check still fails; please move the summary under ### What and rerun checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5593680997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#pullrequestreview-5593680997). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T00:59:47Z. **Review:** CHANGES_REQUESTED --- I am maintaining the request to fill the required `### What` section. The earlier approval was only for the code review and does not apply while the hosted conventions check still fails; please move the summary under `### What` and rerun checks.
oneandonlydean commented 2026-10-21 02:03:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T02:03:08Z.


@xXPBSXx the new case at GuestFilesystem.cpp:248 fails on Linux in our CI: unlink("/parent-test-mount/present.txt/.") returns ENOENT (2) instead of ENOTDIR (20). Windows passes.

The cause is in RenameDotError_nid_no_patch. The parent is std::string(path, start), which keeps the trailing separator (.../present.txt/). On Linux the resolver keeps a trailing /, and std::filesystem::exists("present.txt/") is false there, with no error code. So the helper takes the "missing parent" branch. On Windows the slash is dropped, which is why it passes.

Trimming trailing separators from parent before resolving (keeping a lone /) fixes it, e.g. while (parent.size() > 1 && separator(parent.back())) parent.pop_back();. That also corrects rename("file/.", ...) on Linux, which goes through the same helper and has the same wrong answer on main today.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6306178863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#issuecomment-6306178863). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-21T02:03:08Z. --- @xXPBSXx the new case at `GuestFilesystem.cpp:248` fails on Linux in our CI: `unlink("/parent-test-mount/present.txt/.")` returns `ENOENT` (2) instead of `ENOTDIR` (20). Windows passes. The cause is in `RenameDotError_nid_no_patch`. The parent is `std::string(path, start)`, which keeps the trailing separator (`.../present.txt/`). On Linux the resolver keeps a trailing `/`, and `std::filesystem::exists("present.txt/")` is false there, with no error code. So the helper takes the "missing parent" branch. On Windows the slash is dropped, which is why it passes. Trimming trailing separators from `parent` before resolving (keeping a lone `/`) fixes it, e.g. `while (parent.size() > 1 && separator(parent.back())) parent.pop_back();`. That also corrects `rename("file/.", ...)` on Linux, which goes through the same helper and has the same wrong answer on `main` today.
xXPBSXx commented 2026-10-21 02:31:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T02:31:14Z.


Thanks, that was the cause. RenameDotError_nid_no_patch resolved the parent with its trailing separator, and std::filesystem::exists("present.txt/") is false on Linux, so file/. took the missing-parent branch. I now trim trailing separators from parent before resolving (a lone / stays), as you suggested. This also corrects rename("file/.", ...) on Linux, so guest_filesystem gets two more cases: rename of present.txt/. and present.txt/.. returns ENOTDIR.

Checked on Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release, so not a bare-metal CI run): at the old head 864baa17d4 guest_filesystem fails with "Filesystem check failed at line 248 (guest errno 2, host errno 2)"; at the new head 2f82e47f5f it passes. The tests that call unlink/rmdir/rename/unlinkat also pass on Linux (guest_filesystem, guest_kernel_sync, guest_kernel_sys, guest_kqueue, guest_path_name_length, guest_poll, guest_posix_scalar_io, guest_rename_dot, guest_select, guest_select_wait, guest_truncate_extend), and on Windows 10 (MinGW GCC 15.2, Release) guest_filesystem, guest_kernel_sys and guest_rename_dot pass. check_conventions passes. Not tested: macOS, a console.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6306533380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#issuecomment-6306533380). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T02:31:14Z. --- Thanks, that was the cause. `RenameDotError_nid_no_patch` resolved the parent with its trailing separator, and `std::filesystem::exists("present.txt/")` is false on Linux, so `file/.` took the missing-parent branch. I now trim trailing separators from `parent` before resolving (a lone `/` stays), as you suggested. This also corrects `rename("file/.", ...)` on Linux, so `guest_filesystem` gets two more cases: `rename` of `present.txt/.` and `present.txt/..` returns ENOTDIR. Checked on Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release, so not a bare-metal CI run): at the old head 864baa17d4 `guest_filesystem` fails with "Filesystem check failed at line 248 (guest errno 2, host errno 2)"; at the new head 2f82e47f5f it passes. The tests that call `unlink`/`rmdir`/`rename`/`unlinkat` also pass on Linux (guest_filesystem, guest_kernel_sync, guest_kernel_sys, guest_kqueue, guest_path_name_length, guest_poll, guest_posix_scalar_io, guest_rename_dot, guest_select, guest_select_wait, guest_truncate_extend), and on Windows 10 (MinGW GCC 15.2, Release) `guest_filesystem`, `guest_kernel_sys` and `guest_rename_dot` pass. `check_conventions` passes. Not tested: macOS, a console. AI-assisted (Claude Code).
MarcZX10 commented 2026-10-21 05:18:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T05:18:26Z.

Review: COMMENTED


The Linux fix looks right, but this trimming also strips a Windows drive root: for C:\\., parent is C:\\, then the loop changes it to C:. On Windows C: is drive-relative to that drive’s current directory, not the root C:\\; the check can inspect the wrong directory (and UNC share roots have the same root-preservation concern). Could you trim only while parent.size() > std::filesystem::path(parent).root_path().native().size() (or otherwise preserve root_path()), and add a Windows-root regression if this helper is meant to cover drive-root paths?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5595264016 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#pullrequestreview-5595264016). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T05:18:26Z. **Review:** COMMENTED --- The Linux fix looks right, but this trimming also strips a Windows drive root: for `C:\\.`, `parent` is `C:\\`, then the loop changes it to `C:`. On Windows `C:` is drive-relative to that drive’s current directory, not the root `C:\\`; the check can inspect the wrong directory (and UNC share roots have the same root-preservation concern). Could you trim only while `parent.size() > std::filesystem::path(parent).root_path().native().size()` (or otherwise preserve `root_path()`), and add a Windows-root regression if this helper is meant to cover drive-root paths?
MarcZX10 commented 2026-10-21 06:08:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:08:56Z.


Follow-up to my root-path review: I prepared commit d68de63527 on a branch based on this PR head. It trims trailing separators only until the candidate reaches std::filesystem::path::root_path(), and adds Windows checks for C:\\, repeated root separators and a non-root trailing separator. The commit is authored by MarcZX10 and passes check_conventions, git diff --check, MinGW syntax checking of GuestFilesystem.cpp (with a define for this package’s missing FILE_CS_FLAG_CASE_SENSITIVE_DIR SDK constant), and a native Linux root/trailing-separator smoke test. I tried to push it to this PR’s branch, but GitHub denied write access to the head fork (403), so the commit is available on my fork for cherry-picking.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6309336501 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#issuecomment-6309336501). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T06:08:56Z. --- Follow-up to my root-path review: I prepared commit [`d68de63527`](https://github.com/MarcZX10/AnyPS5/commit/d68de63527) on a branch based on this PR head. It trims trailing separators only until the candidate reaches `std::filesystem::path::root_path()`, and adds Windows checks for `C:\\`, repeated root separators and a non-root trailing separator. The commit is authored by MarcZX10 and passes `check_conventions`, `git diff --check`, MinGW syntax checking of `GuestFilesystem.cpp` (with a define for this package’s missing `FILE_CS_FLAG_CASE_SENSITIVE_DIR` SDK constant), and a native Linux root/trailing-separator smoke test. I tried to push it to this PR’s branch, but GitHub denied write access to the head fork (403), so the commit is available on my fork for cherry-picking.
MarcZX10 commented 2026-10-21 06:39:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:39:23Z.


I checked the updated head 0de8266c6f4f. TrimTrailingSeparators_nid_no_patch now stops at root_path(), and GuestFilesystem.cpp adds Windows cases for C:\\, C:/, repeated drive-root separators, and non-root trailing separators. That addresses the root-preservation concern I raised. The current conventions check passes; Windows and macOS x86_64 relinker jobs are still in progress, so I will recheck those results before concluding review.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6309774433 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#issuecomment-6309774433). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T06:39:23Z. --- I checked the updated head `0de8266c6f4f`. `TrimTrailingSeparators_nid_no_patch` now stops at `root_path()`, and `GuestFilesystem.cpp` adds Windows cases for `C:\\`, `C:/`, repeated drive-root separators, and non-root trailing separators. That addresses the root-preservation concern I raised. The current conventions check passes; Windows and macOS x86_64 relinker jobs are still in progress, so I will recheck those results before concluding review.
MarcZX10 commented 2026-10-21 06:40:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:40:36Z.

Review: APPROVED


Reviewed the updated head 0de8266c6f4f: the Linux trailing-separator fix is retained, trimming now preserves root_path(), and the added Windows drive-root cases cover the concern I raised. The PR body now satisfies the required ### What section. All applicable hosted relinker, sanitizer, register-context, write-watch, conventions, and intake checks are passing; Linux/windows full-build jobs are skipped by workflow design. Approved.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5595843950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8797#pullrequestreview-5595843950). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T06:40:36Z. **Review:** APPROVED --- Reviewed the updated head `0de8266c6f4f`: the Linux trailing-separator fix is retained, trimming now preserves `root_path()`, and the added Windows drive-root cases cover the concern I raised. The PR body now satisfies the required `### What` section. All applicable hosted relinker, sanitizer, register-context, write-watch, conventions, and intake checks are passing; Linux/windows full-build jobs are skipped by workflow design. Approved.
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!8797
No description provided.