Hi Collin,

Thanks for reviewing the patch.  I'd like to clarify how the patch
actually works, because I believe the race you describe does not apply
to the approach taken here.


You describe the sequence: fstatat (by name) -> compare dev/ino to
root stat -> fchmodat (by name).  In that sequence there is indeed a
TOCTOU race between the stat and the chmod, because both operate on
the path name and the entry can be swapped between them.  This is
essentially the pattern used by chmod.c (fstatat at line 278,
ROOT_DEV_INO_CHECK at line 307, fchmodat at line 328).


However, the chown patch does not use that sequence.  restricted_chown
uses:


  1. openat(cwd_fd, file, O_RDONLY|O_DIRECTORY)  -> fd (follows 
symlinks)
  2. fstat(fd, &st)               
                -> stat of the 
opened inode
  3. psame_inode(orig_st, &st)           
         -> compare with FTS-cached stat
  4. fchown(fd, uid, gid)               
          -> operate on the SAME fd


The critical difference is step 4: fchown operates on the file
descriptor, not on the path name.  Once openat succeeds, the fd is
bound to a specific inode.  Steps 3 and 4 both reference that same
fd/inode, so there is no TOCTOU window between the check and the
operation.


The residual race window is between the FTS-cached stat (at FTS_D
time) and openat (at FTS_DP time).  If an attacker swaps the entry
with a symlink to '/' during this window, openat follows the symlink
and opens '/', fstat returns '/' own dev/ino, psame_inode compares
the FTS-cached stat with '/' stat -> mismatch -> RC_inode_changed ->
fchown is never called -> '/' is not modified.  So the race is
detected (not prevented), but the detection happens before any
modification, so no harm is done.


For the pre-existing symlink case (Fix #2), there is no race at all:
fstatat with flags=0 obtains '/' own dev/ino into file_stats, and the
root-dev-ino guard (now without the FTSENT_IS_DIRECTORY condition)
catches it before restricted_chown is even called.


That said, the fd-based protection in restricted_chown only covers
regular files and directories.  For other file types (FIFOs, devices,
sockets), restricted_chown returns RC_do_ordinary_chown, and the
caller falls back to chownat by name with flags=0, which still has
the classic TOCTOU race.  This is pre-existing behavior, not
introduced by this patch.


Regarding your mention of O_PATH: I think that's a great follow-up.
An O_PATH-based approach would be strictly better on platforms that
support it, since it could cover all file types and prevent the race
rather than detecting it.  Since you're already working on this, I'm
looking forward to seeing the patch.  I'd be happy to help with
testing if needed.


Best regards,
Xue Haolin










                    Collin Funk<[email protected]&gt;&nbsp;在 2026年11月18日 
周三 11:56 写道:

Xue Haolin <[email protected]&gt; writes:

&gt; When chown or chgrp is invoked with -RH --preserve-root (and without
&gt; --from or -h), two distinct bugs allowed '/' to be silently re-owned:
&gt;
&gt; 1. TOCTOU race (the originally reported variant): restricted_chown
&gt;&nbsp;&nbsp;&nbsp; short-circuited and returned RC_do_ordinary_chown when 
required_uid
&gt;&nbsp;&nbsp;&nbsp; and required_gid were both -1 (i.e., no --from).&nbsp; 
The caller then
&gt;&nbsp;&nbsp;&nbsp; used chownat() by name with flags=0, which follows 
symlinks.&nbsp; An
&gt;&nbsp;&nbsp;&nbsp; attacker who can write in a directory being traversed 
can replace a
&gt;&nbsp;&nbsp;&nbsp; directory entry with a symlink to '/' between the FTS_D 
stat and the
&gt;&nbsp;&nbsp;&nbsp; FTS_DP chown, causing '/' itself to be re-owned silently.
&gt;
&gt; 2. Pre-existing symlink (no race needed): with -RH (no -h), a symlink
&gt;&nbsp;&nbsp;&nbsp; to '/' already present in the traversed tree is 
dereferenced via
&gt;&nbsp;&nbsp;&nbsp; fstatat (flags=0, following the symlink) to obtain 
file_stats, which
&gt;&nbsp;&nbsp;&nbsp; then holds '/' own dev/ino.&nbsp; The second root guard 
in
&gt;&nbsp;&nbsp;&nbsp; change_file_owner only fired when 
FTSENT_IS_DIRECTORY(ent) was true,
&gt;&nbsp;&nbsp;&nbsp; but a symlink entry has fts_info=FTS_SL and is not a 
directory for
&gt;&nbsp;&nbsp;&nbsp; FTS, so the guard was skipped.&nbsp; restricted_chown 
then opened the
&gt;&nbsp;&nbsp;&nbsp; symlink (openat without O_NOFOLLOW follows it), fstat on 
the fd
&gt;&nbsp;&nbsp;&nbsp; returned '/' own stat, psame_inode compared '/' to '/' 
and passed,
&gt;&nbsp;&nbsp;&nbsp; and fchown re-owned '/'.
&gt;
&gt; Fix #1: remove the short-circuit in restricted_chown so that all chown
&gt; operations go through the openat + fstat + psame_inode + fchown path.
&gt; If a directory entry was replaced by a symlink during the race window,
&gt; the inode obtained by fstat on the opened fd will differ from the
&gt; original stat cached by FTS, psame_inode returns false, and the
&gt; operation is aborted with RC_inode_changed.
&gt;
&gt; Fix #2: remove the FTSENT_IS_DIRECTORY(ent) condition from the second
&gt; root guard in change_file_owner so that the guard also fires for
&gt; symlink entries whose dereferenced stat (file_stats) matches '/'.
&gt; Remove the now-unused FTSENT_IS_DIRECTORY macro definition.
&gt;
&gt; Note: the protection in restricted_chown relies on fstat +
&gt; psame_inode, NOT on O_NOFOLLOW (which is not used in chown-core.c).
&gt; openat follows symlinks; the TOCTOU defense is the inode comparison
&gt; between the FTS-cached stat and the fstat of the opened fd.
&gt;
&gt; * src/chown-core.c (restricted_chown): Remove early return when
&gt; required_uid and required_gid are both -1.
&gt; (change_file_owner): Remove FTSENT_IS_DIRECTORY condition from the
&gt; root-dev-ino guard so it also protects against dereferenced
&gt; symlinks-to-/.
&gt; (FTSENT_IS_DIRECTORY): Remove now-unused macro.
&gt; * tests/chown/preserve-root.sh: Add regression test for -RH without -h.
&gt;
&gt; Reported by SecBuddyF (with Hy4-preview) of Tencent KeenSecurityLab
&gt; via Anthropic's OSS Scanner.
&gt; See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=82115
&gt; ---
&gt;&nbsp; 
src/chown-core.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 | 15 ++++-----------
&gt;&nbsp; tests/chown/preserve-root.sh | 23 +++++++++++++++++++----
&gt;&nbsp; 2 files changed, 23 insertions(+), 15 deletions(-)

Thanks for the patch.

Unfortunately, I don't think it is possible to avoid races portably
here.

Without O_PATH you have to do fstatat, compare dev/ino to the root stat,
and then fchmodat, so you still have a race. I'll see if I can come up
with something for platforms with O_PATH.

Collin

Reply via email to