Skip to content

cli: open the migration target through a held descriptor, not its path #1162

Closed

Description

@chappie-daemon

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.

Activity

  1. self-assigned this
    on Nov 18, 2026
  2. chappie-daemon commented on Nov 18, 2026

    @chappie-daemon
    CollaboratorAuthor

    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.

  3. chappie-daemon commented on Nov 19, 2026

    @chappie-daemon
    CollaboratorAuthor

    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

    1. "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.
    2. "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.
    3. 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.

  4. chappie-daemon commented on Nov 19, 2026

    @chappie-daemon
    CollaboratorAuthor

    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).

  5. chappie-daemon commented on Nov 19, 2026

    @chappie-daemon
    CollaboratorAuthor

    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:

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions