Skip to content

fix(migrate): close TOCTOU between target check and LMDB open (#1128) - #1161

Merged
cameri merged 4 commits into
rewrite/mainfrom
fix/1128-migrate-toctou-pin
Nov 17, 2026
Merged

cameri merged 4 commits into
rewrite/mainfrom
fix/1128-migrate-toctou-pin

Conversation

@chappie-daemon

@chappie-daemon chappie-daemon commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1128.

Written by @tirthraj01 (Tirthraj Chavan) — this is a cherry-pick of their work, applied with -x so the original authorship is preserved rather than re-posted as ours. Their branch: tirthraj01:nostream:fix/1128-migrate-toctou-handle-check at 4bb8c4b7, one commit on top of rewrite/main.

What it changes

crates/nostream-cli/src/commands/migrate.rs

  • TargetPin opens a handle on the target directory and records its (dev, ino). Holding the handle keeps the inode alive across open_target.
  • pin.verify() runs after the store opens and refuses the run when the directory LMDB opened is not the exact device and inode that passed the security checks.
  • create_target now returns the pin, so there is no call site that can create the target and forget to verify it.
  • Non-Unix builds get stubs that keep the original behaviour, so Windows compiles and keeps the pre-existing window — a documented limitation rather than a silent regression.

A .changeset/migrate-target-toctou-pin.md rides along, with empty frontmatter, so the release tooling picks the change up without fixing a bump here.

Verification

  • cargo test -p nostream-cli --lib a_target_swapped_between_check_and_open_is_refused — 1 passed, on a clean worktree of rewrite/main with only this commit applied.
  • The diff is 2 files, +105 / −10. No .github/ workflow edits, no Cargo.toml/Cargo.lock changes, no build.rs and no install-time hooks.

Review note

The swap detection compares the path's current metadata against the pinned inode, so it catches replacement of the directory entry itself, which is the attack #1128 describes. It does not detect a target whose contents are swapped through a bind mount while the inode stays the same; that is a narrower case than the one filed, worth knowing but not a blocker.

Update — the target's holder is now judged by owner, not only by mode

Greptile's P1 was right about a shape the pin could not cover: refuse_swappable_ancestry asked only about mode bits, and an ancestor's owner renames its entries whatever the bits say, so --target /home/alice/store run as root passed and alice could move the target. It now refuses an ancestor owned by neither the caller nor root, sticky or not — sticky restrains other users, not the directory's own owner — which makes every permitted target's holder caller- or root-owned, so no other local user can rename into it. Test: a_target_under_an_ancestor_another_user_owns_is_refused, which asserts the 0755 case and the same ancestor at 1755.

The one path no path-based check can close — swap the target during the store's open, restore it before verify — is #1162, with the fd-relative fix and its test, rather than a claim made here. verify stays as the line that catches a swap that is not restored.

Also style(migrate): cargo fmt --all --check was red, which failed Rust Checks and the Windows CLI Check before either could run.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9caf69f7-db5a-4338-9136-f282ce8e2f2d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 69132c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest changes appear safe to merge, with no new actionable issues found.

Summary

Adds a held directory handle and an identity check after the migration store opens.

  • The migration checks that LMDB opened the directory it approved.
  • The migration rejects target paths under foreign-owned directories.

Reviews (4) · Last reviewed commit: "fix(migrate): a sticky ancestor is no ex..." · Reviewed by Greptile

cargo fmt --all --check was red on the wrapped error.to_string() chain, which failed Rust Checks and the Windows CLI Check before either could run. Formatting only; no behaviour change.
The pin narrowed the check-to-open window but could not close it: an ancestor's owner renames its entries whatever the bits say, so `--target /home/alice/store` run as root passes every mode check and alice can still move the target aside. Refuse an ancestor owned by neither the caller nor root unless its sticky bit makes the caller's own entry unrenamable by its owner.

With that rule in place every permitted target's parent is caller- or root-owned, or sticky over a caller-owned entry, so no other local user can mount the swap and the pin's verify is a second line rather than the only one. Found by Greptile's P1 on #1161.
@chappie-daemon

Copy link
Copy Markdown
Collaborator Author

P1 taken in two parts — one closed here, one filed with the plan.

1. The ancestor-owner gap is closed. You are right that refuse_swappable_ancestry allowed the shape: it asked only about mode bits, and an ancestor's owner renames its entries whatever the bits say, so --target /home/alice/store run as root passed every check with alice able to move the target. It now also refuses an ancestor owned by neither the caller nor root, unless the sticky bit is set — sticky forbids the rename by a directory's owner, which is why /tmp stays usable. Test: a_target_under_an_ancestor_another_user_owns_is_refused.

That rule is what makes your second path non-exploitable by anyone but the caller or root. After it, every permitted target's holder is caller- or root-owned — so only those two can rename into it — or sticky over a caller-owned 0700 entry, where sticky refuses the rename by the holder's owner. The pin and its verify remain as a second line: it is the only check that sees a swap that is not restored.

2. The swap-and-restore between open_target and verify is real, and no path check closes it. verify compares the path against the pinned inode, so an actor who swaps, lets LMDB open, then restores passes it — LMDB keeps descriptors into the directory it opened. Closing that needs fd-relative opening: hold the directory descriptor and hand LMDB /proc/self/fd/<n>, so resolution at open time is the pinned inode. That is a larger change than this PR, so it is filed as #1162 with the shape of the fix and the test that proves it, rather than claimed here.

The previous commit allowed a foreign-owned ancestor when its sticky bit was set, on the reasoning that sticky refuses the rename. It refuses it for other users, not for the directory's owner, who may rename an entry inside it — so alice could still mount the swap on /home/alice with 1755. The ownership rule now stands alone; /tmp stays usable because root owns it, not because it is sticky.
@chappie-daemon

Copy link
Copy Markdown
Collaborator Author

You were right, and my previous reply was wrong on one point: the sticky bit is not an exemption for a directory's own owner. Sticky restrains other users; the directory's owner may still rename an entry inside it, so a foreign-owned 1755 ancestor left the swap available exactly as you said. /tmp is safe because root owns it, not because it is sticky.

The ownership rule now stands alone: an ancestor owned by neither the caller nor root is refused, sticky or not. Test a_target_under_an_ancestor_another_user_owns_is_refused asserts both halves — the plain 0755 case and the same ancestor made 1755, which is still refused. TMPDIR no longer affects it: the walk refuses at the fixture's own ancestor before it reaches anything above it.

The remaining half of your P1 — swap the target during the store's open and restore it before verify — is not closable by a path check, which is why it is #1162 rather than a claim here. verify stays for the swap that is not restored.

@cameri
cameri merged commit f971d6f into rewrite/main Nov 17, 2026
18 checks passed
@cameri
cameri deleted the fix/1128-migrate-toctou-pin branch November 17, 2026 18:49
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.

3 participants