Skip to content

Harden RootLock against planted lock files - #4

Merged
HaoZeke merged 1 commit into
mainfrom
fix/rootlock-truncate
Nov 16, 2026
Merged

HaoZeke merged 1 commit into
mainfrom
fix/rootlock-truncate

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

RootLock.acquire truncated .lock and wrote the pid without checking what the name pointed at. A hard link, symlink or FIFO planted at either lock name, or a symlinked state root, let it overwrite or block on a file outside the state root.

Now acquire:

  • opens the state root with O_DIRECTORY|O_NOFOLLOW and strips trailing slashes, since a trailing slash makes the kernel follow a symlink;
  • opens both lock files relative to that descriptor with O_NOFOLLOW|O_NONBLOCK, created mode 0600;
  • refuses a lock name that isn't a regular file with one link, before and after the open;
  • checks again right before the truncate that .lock still has one link and the same device and inode as the descriptor.

A refused .lock raises the new AcquireError.GateNotPrivate. A symlinked root raises NotDir. LockOps.open_sidecar now takes the directory descriptor and a name. The acquire signature is unchanged.

Six new tests cover hard links (planted before and during the wait), a gate renamed during the wait, a FIFO at either name, symlinks and the 0600 mode; all fail on main. zig build test passes. eb-staged-rebuild, the only caller, builds and passes its tests against this branch.

Not covered: symlinks in parent directories of the state root, and existing 0644 lock files keep their mode.

main has no towncrier, so the entry is in CHANGELOG.md under Unreleased. This conflicts with fix/safety; whichever lands second needs a rebase.

acquire opened STATE_ROOT/.lock by path, took the flock and truncated it
without looking at the descriptor, so a hard link planted at that name
lost the other name's bytes, and a FIFO there was opened and locked; a
symbolic link used as the state root was followed. The state root is now
opened O_DIRECTORY|O_NOFOLLOW and both lock files relative to it with
O_NOFOLLOW, created mode 0600. A name that is not absent or a regular
file with one link is refused before the open, the descriptor is checked
after it, and the gate is checked again after the locks are taken, right
before the truncate: still a regular file with one link, still the one
at .lock, so unlinking the planted name during the wait does not pass.

A refused gate is GateNotPrivate; LockOps.open_sidecar takes the state
root's descriptor and a name.
@HaoZeke
HaoZeke merged commit 6cf778a into main Nov 16, 2026
8 checks passed
@HaoZeke
HaoZeke deleted the fix/rootlock-truncate branch November 16, 2026 05:17
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.

1 participant