Repository navigation
fix(migrate): close TOCTOU between target check and LMDB open (#1128) - #1161
Conversation
(cherry picked from commit 4bb8c4b)
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
🦋 Changeset detectedLatest commit: 69132c4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen 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 |
[Medium impact] The latest changes appear safe to merge, with no new actionable issues found.
|
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.
|
P1 taken in two parts — one closed here, one filed with the plan. 1. The ancestor-owner gap is closed. You are right that 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 2. The swap-and-restore between |
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.
|
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 The ownership rule now stands alone: an ancestor owned by neither the caller nor root is refused, sticky or not. Test The remaining half of your P1 — swap the target during the store's open and restore it before |
Closes #1128.
Written by @tirthraj01 (Tirthraj Chavan) — this is a cherry-pick of their work, applied with
-xso the original authorship is preserved rather than re-posted as ours. Their branch:tirthraj01:nostream:fix/1128-migrate-toctou-handle-checkat4bb8c4b7, one commit on top ofrewrite/main.What it changes
crates/nostream-cli/src/commands/migrate.rsTargetPinopens a handle on the target directory and records its(dev, ino). Holding the handle keeps the inode alive acrossopen_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_targetnow returns the pin, so there is no call site that can create the target and forget to verify it.A
.changeset/migrate-target-toctou-pin.mdrides 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 ofrewrite/mainwith only this commit applied.+105 / −10. No.github/workflow edits, noCargo.toml/Cargo.lockchanges, nobuild.rsand 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_ancestryasked only about mode bits, and an ancestor's owner renames its entries whatever the bits say, so--target /home/alice/storerun 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 the0755case and the same ancestor at1755.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.verifystays as the line that catches a swap that is not restored.Also
style(migrate):cargo fmt --all --checkwas red, which failed Rust Checks and the Windows CLI Check before either could run.