Skip to content

shadow-core: write and read lock files as pid plus NUL, like upstream - #427

Open
pierre-warnier wants to merge 2 commits into
mainfrom
fix-399-lock-nul
Open

pierre-warnier wants to merge 2 commits into
mainfrom
fix-399-lock-nul

Conversation

@pierre-warnier

Copy link
Copy Markdown
Collaborator

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 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. That breaks mutual exclusion between the two suites.

In practice /etc/.pwd.lock (the lckpwdf(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 of shadow-core.

Probed on Ubuntu 26.04's packaged passwd (1:4.17.4-2ubuntu3) in ubuntu:26.04:

  • vipw, vigr and useradd write pid\0 to every lock they take (for useradd: passwd, group, gshadow, subuid, subgid and shadow).
  • Ubuntu's groupadd waits 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-core cover 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 test test_lock_interop checks the bytes vigr writes while it holds /etc/group.lock, and that groupadd waits 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.

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

shadow-utils write locks with NUL-terminated contents while uutils/shadow does not.

1 participant