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
cli: open the migration target through a held descriptor, not its path #1162
The migration's TOCTOU fix (#1128, PR #1161) pins the target directory and checks its device/inode after the store opens. The check cannot prove what the store opened: verify stats the path, so an actor who can rename entries in the parent can swap the target during open_target and restore it before verify, which then compares the original inode against itself and passes. LMDB keeps file descriptors into whatever it opened.
The ancestry rule added with #1161 narrows who that actor can be: refuse_swappable_ancestry now refuses an ancestor owned by neither the caller nor root, unless it is sticky, and a sticky parent cannot have the caller's own entry renamed by its owner. So only the caller or root can mount the swap today — not a privilege boundary. The residual window is real all the same, and it closes the class rather than narrowing it.
The fix is fd-relative opening, not another path check. On Linux, open the target directory once, keep the descriptor for the whole command, and hand LMDB the directory as /proc/self/fd/<n> so path resolution at open time is the pinned inode and no rename can redirect it. The manifest writes that precede the open would follow the same descriptor (openat).
Notes for whoever takes it:
crates/nostream-cli/src/commands/migrate.rs: TargetPin already holds the fs::File; open_target(&options.target) is where the path is used.
The descriptor must outlive the LMDB environment's open. It does not need to outlive the open itself — LMDB holds its own file descriptors afterwards.
#[cfg(target_os = "linux")] for the procfs path; macOS has no /proc, and verify remains the check there.
tests: the existing a_target_swapped_between_check_and_open_is_refused covers the check-to-pin window. A test for this one swaps the directory after open_target and asserts the store's data file landed in the pinned inode.
Acceptance: with the descriptor pinned, a target renamed away and replaced after open_target leaves the store in the original directory, and the command either succeeds there or refuses; nothing is written into the replacement.
Opened as job 031-held-descriptor (factory 0.12.18), cut from rewrite/main at 3c33eb64. Expected cost $1.25 from the operator's $1–1.50. Recon starting now; the scope check is whether the change stays at the TargetPin/open_target seam in commands/migrate.rs.
Fixed on PR #1178 at 6ef635ca — and three premises in this issue are wrong, corrected here so a later reader does not inherit them.
The three corrections
"verify remains the check there" (macOS) — there is no check there to remain. The --verify arm (migrate.rs:56-71) and the completed-manifest arm (:84) never pinned at all: they called open_existing_target(&options.target) on the bare path. Both pin now, and both have a swap test.
"The manifest writes that precede the open" — none do. The first manifest write is at migrate.rs:175, after the store open at :110. What the manifest writer did was open by path at every step (read_manifest:505, write_manifest:565-595, sync_directory_of:604), which is why it had to follow the descriptor — but "precede the open" describes nothing.
The descriptor must outlive the whole run, not just the open.TargetPin's handle lives to the end of each arm; the spelling is /proc/self/fd/<n> for the whole command, and a closed spelling answers ENOENT rather than falling back to the name.
What shipped, and the limit
The target is opened through /proc/self/fd/<n> where procfs exists (runtime probe, no cfg(target_os) in production code); the manifest read/write/sync follow the same descriptor; operator-visible errors translate the root back to the target.
It narrows; it does not close. heed canonicalises the path (heed-0.20.5/src/env.rs:239) and opens the canonical path (:305), so the residual interval is that pair. Closing it needs a second unsafe/ffi site, which CLAUDE.md:81-84 forbids. Measured on a name-swapping thread over 300 rounds: 21 pinned, 0 in the wrong directory, 279 refused — the issue's acceptance ("succeeds there or refuses") holds; the class is not closed.
Follow-ups: #1176 (the copy arm's open has no swap test — its pin is unreachable without Postgres) and #1177 (the ADR's "no printed line names the descriptor" misses Store:'s canonicalize fallback).
Merge is Cameri's; Closes is inert against rewrite/main, so this issue needs closing by hand after the merge, with the merge sha.
PR: #1178 — cost $2.16 against the $1.25 estimate (13 sessions; the overrun is the round-1 message defect plus a rework and a second approver pass after a record escalation).
Fixed by #1178, merged as d16f28af on rewrite/main.
The migration target is now opened through a held descriptor: the target is pinned by an inode comparison after the descriptor is open, with the pin and the open repeated until they agree, so a rename swap in the parent directory cannot substitute a different file between the check and the open. Every error path reports the operator's target path rather than the descriptor.
Two things this does not close, stated in the PR rather than implied:
heed's canonicalise→open pair is narrowed, not closed. 300 randomised rounds: 21 pinned, 0 opened the wrong directory, 279 refused. The guard on after_pin proves it is called, not that it is called in the right place.
Activity
chappie-daemon commentedon Nov 18, 2026
Opened as job
031-held-descriptor(factory 0.12.18), cut fromrewrite/mainat3c33eb64. Expected cost $1.25 from the operator's $1–1.50. Recon starting now; the scope check is whether the change stays at theTargetPin/open_targetseam incommands/migrate.rs.fix(cli): drive the verifying arms through the pinned descriptor in t…
fix(cli): name the operator's target in every message the descriptor …
chappie-daemon commentedon Nov 19, 2026
Fixed on PR #1178 at
6ef635ca— and three premises in this issue are wrong, corrected here so a later reader does not inherit them.The three corrections
verifyremains the check there" (macOS) — there is no check there to remain. The--verifyarm (migrate.rs:56-71) and the completed-manifest arm (:84) never pinned at all: they calledopen_existing_target(&options.target)on the bare path. Both pin now, and both have a swap test.migrate.rs:175, after the store open at:110. What the manifest writer did was open by path at every step (read_manifest:505,write_manifest:565-595,sync_directory_of:604), which is why it had to follow the descriptor — but "precede the open" describes nothing.TargetPin's handle lives to the end of each arm; the spelling is/proc/self/fd/<n>for the whole command, and a closed spelling answersENOENTrather than falling back to the name.What shipped, and the limit
The target is opened through
/proc/self/fd/<n>where procfs exists (runtime probe, nocfg(target_os)in production code); the manifest read/write/sync follow the same descriptor; operator-visible errors translate the root back to the target.It narrows; it does not close. heed canonicalises the path (
heed-0.20.5/src/env.rs:239) and opens the canonical path (:305), so the residual interval is that pair. Closing it needs a secondunsafe/ffi site, whichCLAUDE.md:81-84forbids. Measured on a name-swapping thread over 300 rounds: 21 pinned, 0 in the wrong directory, 279 refused — the issue's acceptance ("succeeds there or refuses") holds; the class is not closed.Follow-ups: #1176 (the copy arm's open has no swap test — its pin is unreachable without Postgres) and #1177 (the ADR's "no printed line names the descriptor" misses
Store:'s canonicalize fallback).Merge is Cameri's;
Closesis inert againstrewrite/main, so this issue needs closing by hand after the merge, with the merge sha.chappie-daemon commentedon Nov 19, 2026
PR: #1178 — cost $2.16 against the $1.25 estimate (13 sessions; the overrun is the round-1 message defect plus a rework and a second approver pass after a record escalation).
chappie-daemon commentedon Nov 19, 2026
Fixed by #1178, merged as
d16f28afonrewrite/main.The migration target is now opened through a held descriptor: the target is pinned by an inode comparison after the descriptor is open, with the pin and the open repeated until they agree, so a rename swap in the parent directory cannot substitute a different file between the check and the open. Every error path reports the operator's target path rather than the descriptor.
Two things this does not close, stated in the PR rather than implied:
after_pinproves it is called, not that it is called in the right place.