Repository navigation
shadow-core: write and read lock files as pid plus NUL, like upstream - #427
Open
pierre-warnier wants to merge 2 commits into
Open
pierre-warnier wants to merge 2 commits into
pierre-warnier wants to merge 2 commits into
Conversation
shadow-utils writes a lock file as the holder's PID followed by one NUL byte: on Ubuntu 26.04 (passwd 1:4.17.4-2ubuntu3) `vipw` holding /etc/passwd.lock as pid 83 leaves `8 3 \0`, and `useradd` writes "119\0" to each of passwd, group, gshadow, subuid, subgid and shadow .lock. shadow-rs parsed the content with `trim().parse::<i32>()`, and `trim()` leaves the NUL in place, so a live upstream lock read as unparsable, was judged stale and deleted, and the lock was taken while its holder was still working. Parse a lock as a PID followed by optional NUL and/or whitespace (`parse_lock_pid`); anything else is still invalid and treated as stale. Write our own locks as PID plus NUL, byte-identical to shadow-utils, which reads both that form and the old bare PID as a live holder. Unit tests cover the accepted forms, garbage, a live NUL-terminated lock (excluded, file left intact) and a dead one (swept away). The end-to-end suite checks the bytes vigr writes and that groupadd waits on a live upstream-form lock, then proceeds once the holder has exited. Reported-by: chariri (@cqjjjzr) Fixes #399
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #399
shadow-utils writes a lock file as the holder's pid followed by one NUL byte (
"PID\0"). shadow-rs parsed the content withtrim().parse::<i32>(), andtrim()leaves the NUL in place, so a live upstream lock read as unparsable, was judged stale and deleted, and the lock was taken while its holder was still working. That breaks mutual exclusion between the two suites.In practice
/etc/.pwd.lock(thelckpwdf(3)lock, which we also take) already serialises passwd, shadow, group and gshadow, so the exposure is mainly /etc/subuid and /etc/subgid, and library users ofshadow-core.Probed on Ubuntu 26.04's packaged
passwd(1:4.17.4-2ubuntu3) inubuntu:26.04:vipw,vigranduseraddwritepid\0to every lock they take (foruseradd: passwd, group, gshadow, subuid, subgid and shadow).groupaddwaits on a live lock in either form,"6\0"and"15".The fix: a lock is read as a pid optionally followed by a NUL and/or whitespace (
parse_lock_pid); anything else is still invalid and treated as stale. Our own locks are written as pid plus NUL, byte-identical to shadow-utils, which reads both that form and the old bare pid as a live holder.Tests: unit tests in
shadow-corecover the accepted forms, garbage, a live NUL-terminated lock (not taken over, file left intact) and a dead one (swept away), and check the bytes we write. The end-to-end testtest_lock_interopchecks the bytesvigrwrites while it holds /etc/group.lock, and thatgroupaddwaits on a live upstream-form lock, leaves it in place, and proceeds once the holder has exited. They fail on main. There is a CHANGELOG entry.Thanks @cqjjjzr for the precise report.
Reviews welcome. Without objection, this will be merged after 7 days.