Skip to content

shell: address recursive rm deletions relative to open parent directory fds - #31337

Open
Jarred-Sumner wants to merge 8 commits into
mainfrom
claude/hardening-fix-r4-61-recursive-delete-re-resolves-paths-from
Open

Jarred-Sumner wants to merge 8 commits into
mainfrom
claude/hardening-fix-r4-61-recursive-delete-re-resolves-paths-from

Conversation

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Recursive rm in 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 as unlinkat(parent_fd, name, AT_REMOVEDIR) against a freshly opened, identity-verified parent. Display paths for errors and -v output 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.ts that 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.

…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.
@coderabbitai

coderabbitai Bot commented May 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Jarred-Sumner, we couldn't start this review because you've used your available PR reviews for now.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d3b8fe41-831a-44e0-80e4-e827105466ee

📥 Commits

Reviewing files that changed from the base of the PR and between e4a6b09 and e19d02e.

📒 Files selected for processing (2)
  • src/runtime/shell/builtin/rm.rs
  • test/js/bun/shell/commands/rm.test.ts

Walkthrough

This PR hardens the Unix rm -r implementation against race conditions where intermediate path components are replaced by symlinks after the walker validates directory identity. It introduces FileId (dev/ino) capture at discovery time, refactors the addressing model to use directory fd + single-component name instead of full paths, and adds identity re-verification across async task boundaries.

Changes

Directory identity verification for recursive rm

Layer / File(s) Summary
Directory identity types and DirTask structure
src/runtime/shell/builtin/rm.rs
FileId (dev/ino pair) and VerifiedParentFd (RAII) types enable identity capture. DirTask extended with Unix-only name and dir_id fields.
Task spawning and verified parent helpers
src/runtime/shell/builtin/rm.rs
enqueue/enqueue_no_join pass Unix expected_id to children. open_verified_parent re-checks directory identity and returns ENOENT on mismatch. rmdir_self removes empty directories via verified parent fd.
Root entry point routing updates
src/runtime/shell/builtin/rm.rs
remove_entry routes through new remove_entry_file signature with separate dirfd, path, and name. remove_entry_dir for -d without -r uses root operand addressing.
Directory opening with identity verification
src/runtime/shell/builtin/rm.rs
remove_entry_dir verifies opened directory identity via fstat before descent. Treats identity mismatches as ENOENT or forces verbose deletion. Handles ENOTDIR/ELOOP/EMLINK by re-opening verified parent and retrying via vtable.
Directory traversal with identity stamping
src/runtime/shell/builtin/rm.rs
Directory iteration discovers entries with lstatat, captures FileId, and enqueues child DirTask with expected FileId. File deletion calls refactored to use separate dirfd/name/path parameters.
Empty directory removal with verified parents
src/runtime/shell/builtin/rm.rs
remove_entry_dir_after_children uses verified parent resolution and unlinkat with AT_REMOVEDIR on Unix. Empty-directory removal stage uses rmdir_self. Handles ENOTDIR after replacement via verified parent unlink.
File deletion refactoring with dirfd/name addressing
src/runtime/shell/builtin/rm.rs
remove_entry_file adopts dirfd + single-component name addressing on Unix while retaining path for error output. EISDIR and EPERM paths capture expected_id via lstatat before vtable dispatch.
Vtable evolution for identity propagation
src/runtime/shell/builtin/rm.rs
RemoveFileHandler and RemoveFileVTable accept Unix name and expected_id on on_is_dir and on_dir_not_empty. RemoveFileParent impl enqueues child DirTask with name and expected_id.
Regression tests for symlink swap race conditions
test/js/bun/shell/commands/rm.test.ts
Two skipped-on-Windows tests race rm -rf with swapping intermediate directory components to symlinks. Both use canary-file timing, verify victim files/directories survive, and assert at least one swap occurred.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: refactoring recursive rm to use open parent directory file descriptors instead of the original cwd fd, which is the primary objective of the changeset.
Description check ✅ Passed The PR description addresses both required sections: it explains what the PR does (the recursive rm fix with identity verification) and how it was verified (adds two race-condition tests that validate the fix, notes existing tests still pass).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@robobun

robobun commented May 24, 2026 •

Copy link
Copy Markdown
Collaborator
Updated 2:35 AM PT - May 25th, 2026

✅ @Jarred-Sumner, your commit e19d02e63f1e3885961056383550a0d461449384 passed in Build #57871! 🎉


🧪   To try this PR locally:

bunx bun-pr 31337

That installs a local version of the PR into your bun-31337 executable, so you can run:

bun-31337 --bun

@github-actions

Copy link
Copy Markdown
Contributor

Found 1 issue this PR may fix:

  1. rm -rf removes wrong files by following symbolic directory link #20633 - Reports rm -rf following symbolic directory links and deleting wrong files, which is exactly the class of bug this PR's parent-fd-relative unlinkat and dev/ino identity verification address

If this is helpful, copy the block below into the PR description to auto-close this issue on merge.

Fixes #20633

🤖 Generated with Claude Code

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/js/bun/shell/commands/rm.test.ts Resolved
Comment thread src/runtime/shell/builtin/rm.rs Resolved
Comment thread src/runtime/shell/builtin/rm.rs Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-cases E::ENOENT; if unlinkat(parent_fd, name, AT_REMOVEDIR) lands on a symlink it returns ENOTDIR, which falls into the catch-all Err arm — whereas remove_entry_dir_after_children handles ENOTDIR by 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 window rm -rf can exit 1 with Not a directory on stderr and flake the new expect(stderr).toBe("") / expect(exitCode).toBe(0) assertions. Adding an E::ENOTDIR arm here that mirrors remove_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_dir matches only E::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 particular ENOTDIR — falls into the catch-all Err, reaches handle_err, sets error_signal, and makes rm -rf exit 1 with stderr. By contrast, the sibling path remove_entry_dir_after_children handles E::ENOTDIR from the same unlinkat(..., AT_REMOVEDIR) by setting state.treat_as_dir = false and falling through to remove_entry_file (rm.rs:1565-1568). Per POSIX, rmdir/unlinkat(AT_REMOVEDIR) on a symlink fails with ENOTDIR, 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 > 1 and took the need_to_wait → remove_entry_dir_after_children path. With the new check, a child whose open lands on a swapped inode does open + fstat + close and returns Ok via verbose_deleted under -f, so post_run decrements the parent's subtask_count within ~3 syscalls — opening a window where the parent's tail subtask_count.load() reads 1 and takes the inline rmdir instead of after_children.

    Why open_verified_parent's mapping doesn't help

    bba09a5 added ELOOP | EMLINK | ENOTDIR → ENOENT mapping inside open_verified_parent, but that only covers errors from opening the parent path. In this scenario the parent (target) is unchanged, so open_verified_parent(sub) opens and verifies it fine; the ENOTDIR comes from the subsequent unlinkat(target_fd, "sub", AT_REMOVEDIR) on the entry itself, which is now a symlink. That errno is returned verbatim by rmdir_self and hits this match.

    Step-by-step trigger (using the PR's first new test)

    With readdir order [canary.txt, inner] for target/sub:

    1. sub's worker opens target/sub, iterates, unlinks canary.txt via the open fd.
    2. The JS thread observes the canary gone, renames target/sub → stash/sub, and creates symlink target/sub → victim.
    3. sub's worker lstatat(fd, "inner"), records the original inode id, and enqueues an inner DirTask (sub.subtask_count goes 1→2).
    4. inner's worker opens target/sub/inner by path. O_NOFOLLOW only guards the final component, so the intermediate symlink is followed and the open lands on victim/inner. fstat mismatches the recorded id; under -f it returns Ok via verbose_deleted, and post_run does sub.subtask_count.fetch_sub(1) (2→1).
    5. sub's worker exits the readdir loop (one getdents → EOF), loads subtask_count == 1, skips need_to_wait, and calls self.rmdir_self(sub).
    6. rmdir_self → open_verified_parent(sub) opens target (unchanged real dir, dev/ino verifies), then unlinkat(target_fd, "sub", AT_REMOVEDIR). target/sub is now a symlink → kernel returns ENOTDIR.
    7. e.get_errno() is not ENOENT, so the catch-all arm returns Err; handle_err sets error_signal; rm -rf prints rm: …/target/sub: Not a directory and exits 1.

    Because bba09a5 added expect(result.stderr.toString()).toBe("") and expect(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 — sub does ~1 getdents + an atomic load while inner does dispatch + openat + fstat + close + atomics — so sub usually wins, but there is no synchronization preventing inner from winning on a loaded multi-core CI box (preemption at the WorkPool::schedule wake 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::ENOTDIR arm to this match that mirrors remove_entry_dir_after_children — either fall through to remove_entry_file against the verified parent fd (so the symlink itself is unlinked), or under -f treat it the same as the synthetic-ENOENT case and return via verbose_deleted. Either keeps the two rmdir paths behaviorally equivalent in the swap scenario.

Comment thread src/runtime/shell/builtin/rm.rs Resolved
Comment thread src/runtime/shell/builtin/rm.rs Resolved
Comment thread test/js/bun/shell/commands/rm.test.ts Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Jarred-Sumner left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete all your comments.

@Jarred-Sumner Jarred-Sumner left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete all code comments

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator Author

Removed every comment this branch added in rm.rs and rm.test.ts (the // SAFETY: annotations stay — clippy::undocumented_unsafe_blocks is deny in the workspace lints — and comments that already exist on main are untouched). Commit: e19d02e

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Jarred-Sumner added a commit that referenced this pull request Jun 2, 2026
…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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants