fix(libc): fail a rename of a path that ends in a dot component with EINVAL #8174

Merged
xXPBSXx merged 2 commits from upstream-pr/8174 into main 2026-10-20 03:23:34 +00:00
xXPBSXx commented 2026-10-20 01:18:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T01:18:26Z.


What

rename and sceKernelRename resolved a path such as dir/. to dir before renaming, so rename("dir/.", "other") moved the directory dir. FreeBSD 11.4's lookup (vfs_lookup.c) fails a last path component of . for the DELETE and RENAME operations with EINVAL (22), for the source and the destination alike: rename("dir/.", x), rename(x, "dir/."), rename(".", x) and rename(x, ".") all fail.

A small header, RenameDot.hpp, checks the guest string before it is resolved (resolving removes the .): a last component of . or .. (also under trailing slashes, dir/./) fails with EINVAL when its parent directory exists, ENOENT when the parent does not and ENOTDIR when it is not a directory. rename (libc) and sceKernelRename (libkernel) call it for both arguments. A last component of .. is refused the same way: vfs_lookup.c fails ISDOTDOT together with ISLASTCN for DELETE and RENAME with EINVAL. The same lookup rule applies in FreeBSD to rmdir("dir/."), unlink("dir/.") and the same calls with ..; those are not changed here (the guest still resolves the dot component away and acts on dir or its parent) and stay open. Both open points are in the new TechnicalDebt entry for libc (docs/dev/debt/libc.md).

Order of the checks: the header looks at both arguments before any host access, so rename("missing", "dir/.") answers EINVAL here. FreeBSD's kern_renameat looks the source up first (namei with DELETE) and the destination afterwards (namei with RENAME), so there the source error comes first: ENOENT for a missing source (read in release/11.4.0). This difference and the backslash separator are in the TechnicalDebt entry for libc (next commit).

No title evidence: found by calling rename with dot components in a failure-path probe. This is FreeBSD conformance; the effect on a title would be a directory moved by a call that should have failed.

Overlap: git merge-tree against upstream/main is clean. This PR and #8164 each add one line to docs/dev/debt/libc.md; whichever lands second may need a rebase. pr_overlap was not run.

Fixes #8173

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main 340247a083 + these commits:

  • guest_rename_dot: Exit code 0xc0000409 without the changes to Rename.cpp and Stdio.cpp, passes with them.
  • guest_filesystem, guest_working_directory and guest_directory pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, 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 in TechnicalDebt (docs/dev/debt/libc.md)
  • 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
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4929613357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8174). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T01:18:26Z. --- ### What `rename` and `sceKernelRename` resolved a path such as `dir/.` to `dir` before renaming, so `rename("dir/.", "other")` moved the directory `dir`. FreeBSD 11.4's lookup ([`vfs_lookup.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_lookup.c)) fails a last path component of `.` for the `DELETE` and `RENAME` operations with `EINVAL` (22), for the source and the destination alike: `rename("dir/.", x)`, `rename(x, "dir/.")`, `rename(".", x)` and `rename(x, ".")` all fail. A small header, `RenameDot.hpp`, checks the guest string before it is resolved (resolving removes the `.`): a last component of `.` or `..` (also under trailing slashes, `dir/./`) fails with `EINVAL` when its parent directory exists, `ENOENT` when the parent does not and `ENOTDIR` when it is not a directory. `rename` (libc) and `sceKernelRename` (libkernel) call it for both arguments. A last component of `..` is refused the same way: `vfs_lookup.c` fails `ISDOTDOT` together with `ISLASTCN` for `DELETE` and `RENAME` with `EINVAL`. The same lookup rule applies in FreeBSD to `rmdir("dir/.")`, `unlink("dir/.")` and the same calls with `..`; those are not changed here (the guest still resolves the dot component away and acts on `dir` or its parent) and stay open. Both open points are in the new TechnicalDebt entry for libc (`docs/dev/debt/libc.md`). Order of the checks: the header looks at both arguments before any host access, so `rename("missing", "dir/.")` answers `EINVAL` here. FreeBSD's `kern_renameat` looks the source up first (`namei` with `DELETE`) and the destination afterwards (`namei` with `RENAME`), so there the source error comes first: `ENOENT` for a missing source (read in `release/11.4.0`). This difference and the backslash separator are in the TechnicalDebt entry for libc (next commit). No title evidence: found by calling `rename` with dot components in a failure-path probe. This is FreeBSD conformance; the effect on a title would be a directory moved by a call that should have failed. Overlap: `git merge-tree` against `upstream/main` is clean. This PR and #8164 each add one line to `docs/dev/debt/libc.md`; whichever lands second may need a rebase. `pr_overlap` was not run. Fixes #8173 ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main 340247a083 + these commits: - `guest_rename_dot`: `Exit code 0xc0000409` without the changes to `Rename.cpp` and `Stdio.cpp`, passes with them. - `guest_filesystem`, `guest_working_directory` and `guest_directory` pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt (`docs/dev/debt/libc.md`) - [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] Unblocks: none - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 01:26:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T01:26:10Z.


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 4972 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libc/src/Rename.cpp
core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/prx/libkernel/CMakeLists.txt
PR 6226 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libc.md
PR 6469 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libc.md
PR 6819 docs/dev/debt/libc.md
PR 6879 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 6977 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7081 docs/dev/debt/libc.md
PR 7121 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
PR 7201 core/libs/prx/libc/src/Rename.cpp
core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7386 docs/dev/debt/libc.md
PR 7473 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 7949 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 8037 docs/dev/debt/libc.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6281149618 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8174#issuecomment-6281149618). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T01:26:10Z. --- <!-- 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 4972](https://www.github.com/boykopovar/AnyPS5/pull/4972) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/src/Rename.cpp`<br>`core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp`<br>`docs/dev/debt/libc.md` | | [PR 5303](https://www.github.com/boykopovar/AnyPS5/pull/5303) | `docs/dev/debt/libc.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 6819](https://www.github.com/boykopovar/AnyPS5/pull/6819) | `docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `docs/dev/debt/libc.md` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/src/Rename.cpp`<br>`core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 7386](https://www.github.com/boykopovar/AnyPS5/pull/7386) | `docs/dev/debt/libc.md` | | [PR 7473](https://www.github.com/boykopovar/AnyPS5/pull/7473) | `docs/dev/debt/libc.md` | | [PR 7715](https://www.github.com/boykopovar/AnyPS5/pull/7715) | `docs/dev/debt/libc.md` | | [PR 7949](https://www.github.com/boykopovar/AnyPS5/pull/7949) | `core/libs/prx/libkernel/File/src/Stdio.cpp` | | [PR 8037](https://www.github.com/boykopovar/AnyPS5/pull/8037) | `docs/dev/debt/libc.md` |
MarcZX10 commented 2026-10-20 01:42:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T01:42:23Z.

Code review location: core/libs/prx/libc/include/RenameDot.hpp; line 22

@@ -0,0 +1,26 @@
+#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_RENAMEDOT_HPP
+#define CORE_LIBS_PRX_LIBC_INCLUDE_RENAMEDOT_HPP
+
+#include <filesystem>
+#include <string>
+#include "FilesystemError.hpp"
+
+inline int RenameDotError_nid_no_patch(const char* path) {
+    std::size_t length = std::char_traits<char>::length(path);
+    const auto separator = [](char character) { return character == '/' || character == '\\'; };
+    while (length > 1 && separator(path[length - 1])) --length;
+    std::size_t start = length;
+    while (start > 0 && !separator(path[start - 1])) --start;
+    const std::size_t name = length - start;
+    const bool dot = name == 1 && path[start] == '.';
+    const bool dotDot = name == 2 && path[start] == '.' && path[start + 1] == '.';
+    if (!dot && !dotDot) return 0;
+    const std::string parent = start == 0 ? std::string(".") : std::string(path, start);
+    std::filesystem::path resolved;
+    if (const int error = ResolvePathError_nid_no_patch(parent.c_str(), resolved)) return error;
+    std::error_code status;
+    if (!std::filesystem::exists(resolved, status)) return 2;

Could this check propagate status when the filesystem query itself fails? With the error_code overload, exists(resolved, status) returns false both for a missing path and for errors such as permission denied; this currently maps both to ENOENT. Likewise, an error from is_directory is treated as ENOTDIR. That can mask the parent lookup error instead of preserving it. Please check status before classifying missing versus non-directory (for example, map a nonzero status with FilesystemError(status)).

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4320642138 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8174#discussion_r4320642138). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T01:42:23Z. **Code review location:** `core/libs/prx/libc/include/RenameDot.hpp`; line 22 ~~~~diff @@ -0,0 +1,26 @@ +#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_RENAMEDOT_HPP +#define CORE_LIBS_PRX_LIBC_INCLUDE_RENAMEDOT_HPP + +#include <filesystem> +#include <string> +#include "FilesystemError.hpp" + +inline int RenameDotError_nid_no_patch(const char* path) { + std::size_t length = std::char_traits<char>::length(path); + const auto separator = [](char character) { return character == '/' || character == '\\'; }; + while (length > 1 && separator(path[length - 1])) --length; + std::size_t start = length; + while (start > 0 && !separator(path[start - 1])) --start; + const std::size_t name = length - start; + const bool dot = name == 1 && path[start] == '.'; + const bool dotDot = name == 2 && path[start] == '.' && path[start + 1] == '.'; + if (!dot && !dotDot) return 0; + const std::string parent = start == 0 ? std::string(".") : std::string(path, start); + std::filesystem::path resolved; + if (const int error = ResolvePathError_nid_no_patch(parent.c_str(), resolved)) return error; + std::error_code status; + if (!std::filesystem::exists(resolved, status)) return 2; ~~~~ --- Could this check propagate `status` when the filesystem query itself fails? With the `error_code` overload, `exists(resolved, status)` returns false both for a missing path and for errors such as permission denied; this currently maps both to `ENOENT`. Likewise, an error from `is_directory` is treated as `ENOTDIR`. That can mask the parent lookup error instead of preserving it. Please check `status` before classifying missing versus non-directory (for example, map a nonzero status with `FilesystemError(status)`).
xXPBSXx commented 2026-10-20 03:58:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T03:58:16Z.


Thanks for the review. #8174 was merged before the change was ready; the fix is in 8217: RenameDotError now reads the parent with status(), a missing path stays ENOENT (ENOTDIR for not_a_directory), any other status error is returned through FilesystemError(status). I could not add a negative test: on Windows I have no reliable way to make the status of a parent fail with something other than 'not found'.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6283032669 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8174#issuecomment-6283032669). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T03:58:16Z. --- Thanks for the review. #8174 was merged before the change was ready; the fix is in 8217: `RenameDotError` now reads the parent with `status()`, a missing path stays `ENOENT` (`ENOTDIR` for `not_a_directory`), any other status error is returned through `FilesystemError(status)`. I could not add a negative test: on Windows I have no reliable way to make the status of a parent fail with something other than 'not found'.
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!8174
No description provided.