fleet: migrate all fleet writers to an fd-held advisory lock (retire pathname sentinel locks; close release TOCTOU) #814
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
All three fleet writers — agent CRUD, reconcile, and regen (added in #813) — serialize on pathname-based sentinel locks (
fleet/roster.yaml.mutation.lock,fleet/roster.yaml.reconcile.lock) created withwxand released by proving ownership (device/inode + a random token) and thenunlink-ing the path.That ownership-proof-then-
unlinkis a check-then-act TOCTOU (CWE-367): between the final ownership check and theunlink, an external actor — a stale-lock reaper or an operator runningrm— can vacate our inode, another writer canwx-create a new lock at the same path, and ourunlinkthen deletes that writer's live lock, admitting a third writer and breaking mutual exclusion over roster/projection mutation.This is pre-existing — byte-identical to the reconcile-lock release already merged on
main— and is unreachable within thewxwriter protocol itself (no Mosaic writer removes a lock it doesn't own; only external interference can vacate the inode mid-release). #813 deliberately did not attempt to fix it, because the only correct fix is a cross-cutting mechanism change and #813 is a projection-only recovery PR. Both the independent reviews and Codex's code/security reviews of #813 agreed it is non-blocking for that PR, but it should not linger unaddressed.Proposed fix
Migrate all fleet writers to a single fd-held advisory lock (
flock/lockfor a vetted equivalent) on a stable lock file:wxsentinels and fd locks;Acceptance
unlinkfor release.wxsentinel locks and theassertLockOwnership/removeOwnedLockLeafBestEffortpath-based ownership machinery are removed once superseded.References
packages/mosaic/src/fleet/fleet-reconciler.ts,acquirePrivateManagedRosterLockrelease closure + init-cleanup).