Repository navigation
Conversation
Follow up on 2545596 (`cvd: Use 0777 permissions on shared instance locks directory`) to fix three multi-user and concurrency hazards on shared `/var/tmp/cvd/lock/local-instance-<N>.lock` files: 1. `LockFileManager::OpenLockFile` called `open(O_CREAT | O_RDWR, 0666)` even when the lock file already existed. On kernels with `fs.protected_regular = 2` (default on Debian 13), opening an existing regular file owned by another UID inside a world-writable (`0777`) directory with `O_CREAT` without `O_EXCL` fails with `EACCES`. Open existing lock files with `O_RDWR | O_CLOEXEC` first, and only fall back to `O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC` (using `fchmod(0666)` on the newly created fd and skipping `chmod(parent, 0777)` when `parent_dir` is owned by another UID). 2. `AcquireLock` and `TryAcquireLock` did not re-check that the locked fd still matched the path's inode (`fstat` vs `stat`), allowing a concurrent `RemoveLockFile` / `DeleteLockFile` to unlink a locked inode and let a second caller create and lock a distinct inode for the same instance ID simultaneously. Retry if `IsSameInode` fails after `flock(LOCK_EX)`. 3. Add `AcquireCurrentUserLock`, `AcquireOwnedLockIfPresent`, `ReleaseHeldLock`, and `ReleaseInstanceLock` on `InstanceLockFileManager`, and update `AcquireUnusedLock` to reject active (`kInUse`) locks held by other UIDs while replacing stale (`kNotInUse`) foreign-UID lock files with fresh inodes owned by `geteuid()` so subsequent `fchmod` status updates cannot fail with `EPERM`. Add `instance_lock_test` (`instances/lock/BUILD.bazel`) covering `O_CLOEXEC`, concurrent `AcquireLock` + `RemoveLockFile` inode races, and `AcquireCurrentUserLock` / `ReleaseInstanceLock` semantics. Bug: b/554100008 Bug: b/584825999 Bug: b/586523782 Test: bazel test //cuttlefish/host/commands/cvd/instances/lock/...
Databean
left a comment
There was a problem hiding this comment.
If I understand the description correctly, the problem is that the atomic create-or-open behavior with open(..., O_CREAT) doesn't work anymore on a directory owned by another user, which happens now with the locks directory. Most of the PR tries to work around this deficiency in C++ logic, now having to deal with races between flock and unlink.
Another new behavior introduced by #3593 is to set up /var/tmp/cvd/lock in the debian postinst file. Rather than introduce all this multi-process synchronization, would it work to have the systemd service defined by the debian package create all the lock files in advance on startup? Then the C++ code doesn't have to worry about files existing / not existing and can open without O_CREAT.
Follow up on 2545596 (
cvd: Use 0777 permissions on shared instance locks directory) to fix three multi-user and concurrency hazards on shared/var/tmp/cvd/lock/local-instance-<N>.lockfiles:fs.protected_regular = 2EACCES:LockFileManager::OpenLockFilecalledopen(O_CREAT | O_RDWR, 0666)even when the lock file already existed. On kernels withfs.protected_regular = 2(default on Debian 13), opening an existing regular file owned by another UID inside a world-writable (0777) directory withO_CREATwithoutO_EXCLfails withEACCES. Open existing lock files withO_RDWR | O_CLOEXECfirst, and only fall back toO_CREAT | O_EXCL | O_RDWR | O_CLOEXEC(usingfchmod(0666)on the newly created fd and skippingchmod(parent, 0777)whenparent_diris owned by another UID).flock+unlinksplit-brain race:AcquireLockandTryAcquireLockdid not re-check that the locked fd still matched the path's inode (fstatvsstat), allowing a concurrentRemoveLockFile/DeleteLockFileto unlink a locked inode and let a second caller create and lock a distinct inode for the same instance ID simultaneously. Retry ifIsSameInodefails afterflock(LOCK_EX).AcquireCurrentUserLock,AcquireOwnedLockIfPresent,ReleaseHeldLock, andReleaseInstanceLockonInstanceLockFileManager, and updateAcquireUnusedLockto reject active (kInUse) locks held by other UIDs while replacing stale (kNotInUse) foreign-UID lock files with fresh inodes owned bygeteuid()so subsequentfchmodstatus updates cannot fail withEPERM.Add
instance_lock_test(instances/lock/BUILD.bazel) coveringO_CLOEXEC, concurrentAcquireLock+ReleaseHeldLockinode races, andAcquireCurrentUserLock/ReleaseInstanceLocksemantics.Bug: b/554100008
Bug: b/584825999
Bug: b/586523782
Test: bazel test //cuttlefish/host/commands/cvd/instances/lock/...