You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
migrate: close the remaining TOCTOU between the target check and LMDB's open #1128
Follow-up to the P1 security comment on #1125 (greptile, comment id 4548318952), tracked here so the PR can merge.
What the comment asked for
Existing target directories must belong to the running user.
Their parent must not be swappable before the store opens.
What is already done
(1) was in the branch before the review ran: the uid comparison over the target and <target>/lmdb (2e1627f) and over the data file's own metadata (7c4e287). Pinned by a_target_owned_by_another_user_is_refused.
(2) was real and is in 0066049:
create_target creates the target 0o700 right before the open and re-runs the full check, so a directory another user planted while the source was being probed is refused rather than adopted.
refuse_swappable_ancestry refuses any ancestor that is group- or world-writable without the sticky bit. The question is mode, not uid: a rename takes write permission on the holding directory and no ownership of the entry.
Pinned by a_target_under_a_swappable_directory_is_refused (0777 refused, 01777 allowed) and a_missing_target_is_created_and_then_judged; the first was verified to fail with the new call removed.
What remains
The check is still path-based, so a window exists in principle between the last symlink_metadata and LMDB's open. Closing it properly needs a handle-based open:
open the target with openat(dirfd, comp, O_DIRECTORY | O_NOFOLLOW) component by component, or O_PATH the final directory and fstat it, then hand the descriptor to LMDB;
heed's Env::open takes a path, not a descriptor, so this needs either an upstream change or an EnvOpenOptions path that keeps the directory handle alive across the open.
Until then the practical exposure is limited to a target under a sticky and writable ancestor (the /tmp case), where another user can create entries but cannot rename or remove ones they do not own.
Also noted
Greptile's check on #1125 stays red at 3/5 confidence against the repository's 5/5 threshold, with no inline comment or review body naming a finding on the current head. That is its scoring, not an unreported defect.
Hi @cameri@chappie-daemon, I'd like to take this up if it's open. Having just worked on the migration CLI code in #1121, I'd be happy to dig into this and put together a fix. Could you assign it to me?
I've put together the fix for #1128 on a clean branch rebased on upstream/rewrite/main:
Handle & Inode Pinning (crates/nostream-cli/src/commands/migrate.rs): Introduced TargetPin, which opens a file handle to the target directory and records its (dev, ino). Holding the open handle keeps the directory inode active across open_target, and pin.verify() checks that the directory opened by LMDB matches the exact device and inode validated before the open. If swapped, it fails with a clear refusal.
Cross-platform: Added standard stubs so non-Unix / Windows builds remain clean.
Tests & Changeset: Added the unit test a_target_swapped_between_check_and_open_is_refused and the empty-frontmatter changeset.
Delivered in #1161, merged as f971d6f3. The original commit — 946232dd, by @tirthraj01 — is in the history unchanged; the cherry-pick carried it with -x rather than re-authoring it.
Three follow-ups landed on top of it:
style(migrate): cargo fmt --all --check was red on a wrapped error.to_string() chain, which failed Rust Checks and the Windows CLI Check before either could run a test.
fix(migrate): the target's ancestry is now judged by owner, not only by mode. An ancestor's owner renames its entries whatever the bits say, so --target /home/alice/store run as root passed every mode check while alice could still move the target.
fix(migrate): that rule's first version exempted sticky directories, which was wrong — sticky restrains other users, not the directory's own owner, who may still rename inside it. /tmp is safe because root owns it, not because it is sticky. Both points were found by Greptile's review.
The one race no path check can close — swap the target during the store's open and restore it before verify — is #1162, with the fd-relative shape of the fix and the test that would prove it. verify stays as the line that catches a swap that is not restored.
Activity
tirthraj01 commentedon Nov 16, 2026
Hi @cameri @chappie-daemon, I'd like to take this up if it's open. Having just worked on the migration CLI code in #1121, I'd be happy to dig into this and put together a fix. Could you assign it to me?
cameri commentedon Nov 16, 2026
@tirthraj01 Assigned to you. Thanks!
tirthraj01 commentedon Nov 16, 2026
Thank you so much @cameri! Excited to dive into this one, will keep you posted!
tirthraj01 commentedon Nov 17, 2026
Hi @cameri, @chappie-daemon,
I've put together the fix for #1128 on a clean branch rebased on
upstream/rewrite/main:crates/nostream-cli/src/commands/migrate.rs): IntroducedTargetPin, which opens a file handle to the target directory and records its(dev, ino). Holding the open handle keeps the directory inode active acrossopen_target, andpin.verify()checks that the directory opened by LMDB matches the exact device and inode validated before the open. If swapped, it fails with a clear refusal.a_target_swapped_between_check_and_open_is_refusedand the empty-frontmatter changeset.Due to the repo's interaction limits on external PRs, here is the branch on my fork:
rewrite/main...tirthraj01:nostream:fix/1128-migrate-toctou-handle-check
Please let me know if this looks good to you or if you'd like any adjustments!
fix(migrate): close TOCTOU between target check and LMDB open (#1128) (…
chappie-daemon commentedon Nov 17, 2026
Delivered in #1161, merged as
f971d6f3. The original commit —946232dd, by @tirthraj01 — is in the history unchanged; the cherry-pick carried it with-xrather than re-authoring it.Three follow-ups landed on top of it:
style(migrate):cargo fmt --all --checkwas red on a wrappederror.to_string()chain, which failed Rust Checks and the Windows CLI Check before either could run a test.fix(migrate): the target's ancestry is now judged by owner, not only by mode. An ancestor's owner renames its entries whatever the bits say, so--target /home/alice/storerun as root passed every mode check while alice could still move the target.fix(migrate): that rule's first version exempted sticky directories, which was wrong — sticky restrains other users, not the directory's own owner, who may still rename inside it./tmpis safe because root owns it, not because it is sticky. Both points were found by Greptile's review.The one race no path check can close — swap the target during the store's open and restore it before
verify— is #1162, with the fd-relative shape of the fix and the test that would prove it.verifystays as the line that catches a swap that is not restored.