Repository navigation
shell: address recursive rm deletions relative to open parent directory fds - #31337
Jarred-Sumner wants to merge 8 commits into
Conversation
…ry fds Recursive rm previously issued every unlink and rmdir against the original cwd fd with the full multi-component relative path accumulated during traversal, re-resolving every intermediate component on each syscall. File entries are now unlinked through the already-open parent directory fd plus the single entry name, each discovered subdirectory's identity is recorded through that fd and re-checked after the child task re-opens it by path, and a directory's final rmdir goes through a freshly opened, identity-verified parent fd. Display paths for errors and -v output are unchanged; the identity verification is POSIX-only. Adds two tests that rename an already-traversed directory to a symlink while deletions beneath it are still in flight and assert the link target's contents survive.
|
Warning Review limit reached
Your plan includes 5 reviews of capacity. Refill in 32 minutes and 25 seconds. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more review capacity refills, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThis PR hardens the Unix ChangesDirectory identity verification for recursive rm
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|
Updated 2:35 AM PT - May 25th, 2026
✅ @Jarred-Sumner, your commit e19d02e63f1e3885961056383550a0d461449384 passed in 🧪 To try this PR locally: bunx bun-pr 31337That installs a local version of the PR into your bun-31337 --bun |
|
Found 1 issue this PR may fix:
🤖 Generated with Claude Code |
There was a problem hiding this comment.
I didn't find correctness bugs beyond the test-timeout nit, but this is a ~500-line security-sensitive rework of the concurrent recursive-rm walker (TOCTOU hardening, new fd lifecycle, cross-thread reads of dir_id through raw pointers) and really wants human eyes on the synchronization and identity-check invariants.
Extended reasoning...
Overview
This PR reworks how Bun's shell rm -r addresses deletion syscalls: instead of issuing every unlinkat/rmdirat against the shell cwd fd with the full accumulated multi-component path, it now (on POSIX) unlinks file entries through the already-open iteration fd plus a single component name, records each subdirectory's dev/ino through the parent fd at discovery, re-verifies that identity after the child task re-opens by path, and performs each directory's final rmdir via a freshly opened, identity-checked parent fd. New types FileId and VerifiedParentFd are introduced, DirTask gains name and dir_id fields, and remove_entry_file / enqueue / the RemoveFileHandler trait all grow new #[cfg(unix)] parameters. Two new race tests are added that swap an intermediate path component for a symlink mid-walk.
Security risks
The change is itself a security hardening (closing a TOCTOU symlink-swap window in recursive deletion), so the primary risk is whether the mitigation is complete and doesn't introduce regressions. Points a human should weigh: open_verified_parent re-opens the parent via shell_openat(cwd, parent.path, O_NOFOLLOW) — O_NOFOLLOW only guards the final component, so the dev/ino comparison against the recorded dir_id is the load-bearing check, and its soundness depends on dir_id always being populated before any thread can call open_verified_parent for a child of that parent. The dir_id write in remove_entry_dir happens after the open but before any child is enqueued, and later reads happen via raw-pointer deref across worker threads relying on the work-pool enqueue / subtask_count protocol as the synchronization edge — that reasoning looks right but is subtle enough to merit a second reader. The new VerifiedParentFd RAII guard adds extra openat/fstat/close per directory; fd-leak behavior should be confirmed by the existing fd-leak test the description mentions.
Level of scrutiny
High. This is production filesystem-mutation code in a concurrent, unsafe-heavy module (raw *mut DirTask, manual lifetime protocol via atomics, cross-thread field reads). It changes the syscall pattern for every recursive rm on POSIX and introduces new failure modes (e.g. open_verified_parent failing with non-ENOENT now fails the rm where the old code would have just issued the rmdir). The Windows path is kept as-is via #[cfg], which adds surface for cfg-combination build breaks.
Other factors
The bug-hunting pass only surfaced a nit about explicit 60_000 test timeouts conflicting with test/CLAUDE.md and the file-level setDefaultTimeout. CI is still building. No prior human review on the timeline. Given the scope, concurrency subtlety, and that this is a deliberate security change to destructive-operation code, this should not be bot-approved.
There was a problem hiding this comment.
Additional findings (outside current diff — PR may have been updated during review):
-
🟡
src/runtime/shell/builtin/rm.rs:1487-1500— The tail-rmdir match here only special-casesE::ENOENT; ifunlinkat(parent_fd, name, AT_REMOVEDIR)lands on a symlink it returnsENOTDIR, which falls into the catch-allErrarm — whereasremove_entry_dir_after_childrenhandlesENOTDIRby falling back to unlink-as-file. The PR's new dev/ino-mismatch fast-return makes this inline tail path newly reachable in the swap scenario the new tests exercise, so under a narrow scheduling windowrm -rfcan exit 1 withNot a directoryon stderr and flake the newexpect(stderr).toBe("")/expect(exitCode).toBe(0)assertions. Adding anE::ENOTDIRarm here that mirrorsremove_entry_dir_after_children(or folding it into the force-ENOENT handling) closes the gap.Extended reasoning...
What the bug is
The inline "no pending children" rmdir at the tail of
remove_entry_dirmatches onlyE::ENOENT:match rmdir_result { Ok(()) => self.verbose_deleted(dir_task, path.as_bytes()), Err(e) => match e.get_errno() { E::ENOENT => { ... } _ => Err(self.error_with_path(&e, path.as_bytes())), }, }
Any other errno from
rmdir_self— in particularENOTDIR— falls into the catch-allErr, reacheshandle_err, setserror_signal, and makesrm -rfexit 1 with stderr. By contrast, the sibling pathremove_entry_dir_after_childrenhandlesE::ENOTDIRfrom the sameunlinkat(..., AT_REMOVEDIR)by settingstate.treat_as_dir = falseand falling through toremove_entry_file(rm.rs:1565-1568). Per POSIX,rmdir/unlinkat(AT_REMOVEDIR)on a symlink fails withENOTDIR, so the asymmetry matters exactly when the entry being rmdir'd has been swapped for a symlink.How the PR makes it reachable
The match arm itself is pre-existing, but the PR's new dev/ino-mismatch check (rm.rs:1340-1344) is what makes the inline tail path reachable in the swap scenario the new tests exercise. Before this PR, a child DirTask whose by-path open landed on a swapped target would proceed to iterate it (no identity check), so its parent essentially always observed
subtask_count > 1and took theneed_to_wait→remove_entry_dir_after_childrenpath. With the new check, a child whose open lands on a swapped inode doesopen + fstat + closeand returnsOkviaverbose_deletedunder-f, sopost_rundecrements the parent'ssubtask_countwithin ~3 syscalls — opening a window where the parent's tailsubtask_count.load()reads1and takes the inline rmdir instead ofafter_children.Why
open_verified_parent's mapping doesn't helpbba09a5 added
ELOOP | EMLINK | ENOTDIR → ENOENTmapping insideopen_verified_parent, but that only covers errors from opening the parent path. In this scenario the parent (target) is unchanged, soopen_verified_parent(sub)opens and verifies it fine; theENOTDIRcomes from the subsequentunlinkat(target_fd, "sub", AT_REMOVEDIR)on the entry itself, which is now a symlink. That errno is returned verbatim byrmdir_selfand hits this match.Step-by-step trigger (using the PR's first new test)
With readdir order
[canary.txt, inner]fortarget/sub:sub's worker openstarget/sub, iterates, unlinkscanary.txtvia the open fd.- The JS thread observes the canary gone, renames
target/sub → stash/sub, and creates symlinktarget/sub → victim. sub's workerlstatat(fd, "inner"), records the original inode id, andenqueues aninnerDirTask (sub.subtask_countgoes 1→2).inner's worker openstarget/sub/innerby path.O_NOFOLLOWonly guards the final component, so the intermediate symlink is followed and the open lands onvictim/inner.fstatmismatches the recorded id; under-fit returnsOkviaverbose_deleted, andpost_rundoessub.subtask_count.fetch_sub(1)(2→1).sub's worker exits the readdir loop (onegetdents→ EOF), loadssubtask_count == 1, skipsneed_to_wait, and callsself.rmdir_self(sub).rmdir_self→open_verified_parent(sub)openstarget(unchanged real dir, dev/ino verifies), thenunlinkat(target_fd, "sub", AT_REMOVEDIR).target/subis now a symlink → kernel returnsENOTDIR.e.get_errno()is notENOENT, so the catch-all arm returnsErr;handle_errsetserror_signal;rm -rfprintsrm: …/target/sub: Not a directoryand exits 1.
Because bba09a5 added
expect(result.stderr.toString()).toBe("")andexpect(result.exitCode).toBe(0)to both new tests, this manifests as a test failure.Impact
Safety is preserved (nothing in the victim tree is deleted). The race window between steps 4 and 5 is narrow —
subdoes ~1getdents+ an atomic load whileinnerdoes dispatch +openat+fstat+close+ atomics — sosubusually wins, but there is no synchronization preventinginnerfrom winning on a loaded multi-core CI box (preemption at theWorkPool::schedulewake point is sufficient). Filing as a nit because it would be a rare flake at most, but the fix is trivial and consistent with the PR's other errno-mapping work.Fix
Add an
E::ENOTDIRarm to this match that mirrorsremove_entry_dir_after_children— either fall through toremove_entry_fileagainst the verified parent fd (so the symlink itself is unlinked), or under-ftreat it the same as the synthetic-ENOENTcase and return viaverbose_deleted. Either keeps the two rmdir paths behaviorally equivalent in the swap scenario.
There was a problem hiding this comment.
All my earlier feedback has been addressed and I have nothing further to flag, but given this reworks the concurrent recursive-delete path with new unsafe pointer/fd-ownership protocols and TOCTOU identity checks, it's worth a human pass before merge.
Extended reasoning...
Overview
This PR changes the Bun shell's recursive rm to address every unlink/rmdir as unlinkat(parent_fd, single_component_name) instead of re-resolving the full multi-component path from the cwd on each syscall, and adds dev/ino identity verification (captured via lstatat through the open parent fd, re-checked via fstat after the child task re-opens by path) so a directory swapped for a symlink mid-walk cannot redirect deletions out of the target tree. ~500 lines changed in src/runtime/shell/builtin/rm.rs plus two new symlink-swap race tests in test/js/bun/shell/commands/rm.test.ts.
Security risks
This is squarely security-sensitive: it is the TOCTOU defence for recursive deletion. The risks to weigh are (a) whether the new identity-check protocol actually closes the symlink-swap window on all POSIX targets, (b) whether the new VerifiedParentFd RAII and the extra per-rmdir openat introduce fd leaks under error paths, and (c) whether reading ancestor DirTask fields (path, dir_id) from open_verified_parent across threads is sound under the existing subtask_count/work-pool happens-before protocol. I traced (b) and (c) and believe they hold, but the unsafe surface is large enough that a second pair of eyes is warranted.
Level of scrutiny
High. This is concurrent, unsafe-pointer-heavy filesystem code on a destructive operation, with platform-divergent errno handling (ENOTDIR vs ELOOP vs EMLINK) and a cross-thread ownership hand-off protocol. It went through five rounds of bot review that surfaced real issues (O_NOFOLLOW errno mapping, error-path display regression, tail-rmdir ENOTDIR asymmetry, test flakiness windows), all of which the author fixed; that history itself argues for a human reviewer to confirm the final shape.
Other factors
All five of my prior inline comments are resolved with code changes (commits bba09a5, ee03630, c0904f0, e4a6b09). The bug-hunting pass on the latest revision found nothing. robobun reported CI failures on c0904f0 (one commit before HEAD); worth confirming the latest build is green. Windows keeps the pre-existing cwd-relative behaviour via #[cfg(not(unix))], so the blast radius there should be nil but the cfg-split signatures are worth a compile check.
Jarred-Sumner
left a comment
There was a problem hiding this comment.
Delete all your comments.
Jarred-Sumner
left a comment
There was a problem hiding this comment.
delete all code comments
|
Removed every comment this branch added in rm.rs and rm.test.ts (the |
There was a problem hiding this comment.
All my earlier inline feedback has been addressed and I found nothing new on this pass, but given this reworks the recursive rm addressing model and TOCTOU defences across the multi-threaded DirTask tree, it's worth a human pass before merge.
Extended reasoning...
Overview
This PR changes Bun shell's recursive rm to issue unlinks/rmdirs relative to open parent directory fds (single-component names) instead of re-resolving full multi-component paths from the cwd on every syscall, and adds dev/ino identity capture + re-verification across the async DirTask boundary so a directory swapped for a symlink between discovery and re-open is detected. ~400 lines added to src/runtime/shell/builtin/rm.rs (new FileId/VerifiedParentFd types, open_verified_parent/rmdir_self helpers, DirTask.name/dir_id fields, signature changes through remove_entry_file and the RemoveFileHandler vtable) plus two new symlink-swap race tests in test/js/bun/shell/commands/rm.test.ts.
Security risks
This is squarely security-sensitive: it is the TOCTOU hardening for recursive deletion. The change reduces risk (the prior cwd-relative form re-resolved every intermediate component on every unlink, so an attacker swapping an intermediate for a symlink could redirect deletes into an unrelated tree — issue #20633). The new code uses O_NOFOLLOW + fstat dev/ino checks to refuse to act through a swapped path. The remaining risks are subtle: the parent fd is re-opened by path with identity verification rather than held across the task boundary, so correctness depends on the dev/ino check; the unsafe raw-pointer DirTask tree relies on the existing subtask_count/need_to_wait atomic protocol for liveness of ancestors read in open_verified_parent. I traced these and they look sound, but this is exactly the kind of logic that benefits from a second set of eyes.
Level of scrutiny
High. This is production filesystem-deletion code with concurrent unsafe Rust, platform-specific errno handling (ENOTDIR/ELOOP/EMLINK across Linux/macOS/BSD), and a security-relevant addressing-model change. It is not a mechanical or config-level change.
Other factors
I left six rounds of inline findings on earlier revisions (test timeouts, O_NOFOLLOW errno mapping in open_verified_parent, error-path display, ELOOP/EMLINK at the remove_entry_dir open, ENOTDIR at the tail rmdir, and an ENOTEMPTY test-robustness race) — every one was addressed and the threads are resolved. The most recent commit only strips explanatory comments. The bug-hunting system found nothing on this pass. The new tests assert the safety property (victim survives) plus exit-code/stderr, and existing rm tests are unchanged. Given all that, I'm not flagging anything new — just deferring because the scope and sensitivity warrant a human reviewer rather than a bot approval.
…ms (round 9) (#31606) Tightens input validation, pointer/lifetime handling, and Node-compatibility across the package manager, bun-lambda, node:zlib/net/http, Glob, the transpiler cache, postgres, and HTMLRewriter. Continuation of #31417, #31495, and #31559; same structure — small per-area commits with regression tests included. ### Scaffolding / Lambda - create: dependency names detected from scanned source files are passed to the spawned installer as positionals (after `--`), so a specifier can never be interpreted as a flag - bun-lambda: event-to-Request conversion builds the URL from the request context's domain; client-supplied path bytes can no longer change the URL authority ### node runtime - node:zlib: native compression handles re-resolve the caller-supplied output array on every write completion instead of caching a raw pointer, and reject detached/resized buffers - node:net: BlockList subnet matching handles IPv4-mapped IPv6 rules the same way Node does (verified against Node's behavior) - node:http: duplicate request headers follow Node's IncomingMessage precedence — first value wins for singleton headers, others are joined, `set-cookie` is arrayed (verified against Node's behavior) - Glob: an oversized `cwd` option throws a catchable error instead of aborting the process ### Runtime internals - transpiler: cached module metadata records with out-of-range string indices are rejected at load (previously a corrupted cache record could crash the process) - postgres: unknown backend messages are skipped by exactly their declared length, and NotificationResponse is consumed through the protocol decoder, so the stream can no longer desynchronize - HTMLRewriter: `comment.replace()` removes the original comment and emits only the replacement (previously both appeared in the output) ### Not changed The bin-link containment behavior for symlinked targets is unchanged (previously decided); the recursive-delete traversal change is already covered by the open #31337. ### Tests 11 regression tests across the touched areas. Most are validated to fail on the released build and pass here — including one where the released build segfaults on a corrupted cache record and one where it hangs on an oversized Glob cwd. The zlib write-state tests are regression-only (the released build does not exhibit the failure deterministically without ASAN). `cargo check` and the cross-target checks pass on all 10 CI targets; the touched suites pass locally with no new failures versus main. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: robobun <117481402+robobun@users.noreply.github.com>
Recursive
rmin the Bun shell issued every unlink and rmdir against the original cwd fd with the full multi-component relative path accumulated during traversal, so the kernel re-resolved every intermediate component on each deletion syscall. This change unlinks file entries through the already-open parent directory fd plus the single entry name, records each discovered subdirectory's dev/ino identity through that fd and re-checks it after the child task re-opens the directory by path, and performs each directory's final rmdir asunlinkat(parent_fd, name, AT_REMOVEDIR)against a freshly opened, identity-verified parent. Display paths for errors and-voutput are unchanged, and the identity verification is POSIX-only (Windows keeps the existing cwd-relative form).Adds two tests to
test/js/bun/shell/commands/rm.test.tsthat rename an already-traversed directory out from under the walker and replace it with a symlink while deletions beneath it are still in flight, asserting the link target's contents survive. The existing recursive/verbose/node_modules/fd-leak rm tests pass unchanged.