From: Haolin Xue <[email protected]>

When chown or chgrp is invoked with -RH --preserve-root (and without
--from or -h), two distinct bugs allowed '/' to be silently re-owned:

1. TOCTOU race (the originally reported variant): restricted_chown
   short-circuited and returned RC_do_ordinary_chown when required_uid
   and required_gid were both -1 (i.e., no --from).  The caller then
   used chownat() by name with flags=0, which follows symlinks.  An
   attacker who can write in a directory being traversed can replace a
   directory entry with a symlink to '/' between the FTS_D stat and the
   FTS_DP chown, causing '/' itself to be re-owned silently.

2. Pre-existing symlink (no race needed): with -RH (no -h), a symlink
   to '/' already present in the traversed tree is dereferenced via
   fstatat (flags=0, following the symlink) to obtain file_stats, which
   then holds '/' own dev/ino.  The second root guard in
   change_file_owner only fired when FTSENT_IS_DIRECTORY(ent) was true,
   but a symlink entry has fts_info=FTS_SL and is not a directory for
   FTS, so the guard was skipped.  restricted_chown then opened the
   symlink (openat without O_NOFOLLOW follows it), fstat on the fd
   returned '/' own stat, psame_inode compared '/' to '/' and passed,
   and fchown re-owned '/'.

Fix #1: remove the short-circuit in restricted_chown so that all chown
operations go through the openat + fstat + psame_inode + fchown path.
If a directory entry was replaced by a symlink during the race window,
the inode obtained by fstat on the opened fd will differ from the
original stat cached by FTS, psame_inode returns false, and the
operation is aborted with RC_inode_changed.

Fix #2: remove the FTSENT_IS_DIRECTORY(ent) condition from the second
root guard in change_file_owner so that the guard also fires for
symlink entries whose dereferenced stat (file_stats) matches '/'.
Remove the now-unused FTSENT_IS_DIRECTORY macro definition.

Note: the protection in restricted_chown relies on fstat +
psame_inode, NOT on O_NOFOLLOW (which is not used in chown-core.c).
openat follows symlinks; the TOCTOU defense is the inode comparison
between the FTS-cached stat and the fstat of the opened fd.

* src/chown-core.c (restricted_chown): Remove early return when
required_uid and required_gid are both -1.
(change_file_owner): Remove FTSENT_IS_DIRECTORY condition from the
root-dev-ino guard so it also protects against dereferenced
symlinks-to-/.
(FTSENT_IS_DIRECTORY): Remove now-unused macro.
* tests/chown/preserve-root.sh: Add regression test for -RH without -h.

Reported by SecBuddyF (with Hy4-preview) of Tencent KeenSecurityLab
via Anthropic's OSS Scanner.
See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=82115
---
 src/chown-core.c             | 15 ++++-----------
 tests/chown/preserve-root.sh | 23 +++++++++++++++++++----
 2 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/src/chown-core.c b/src/chown-core.c
index 4698e27eb..b1875ad3a 100644
--- a/src/chown-core.c
+++ b/src/chown-core.c
@@ -30,12 +30,6 @@
 #include "root-dev-ino.h"
 #include "xfts.h"
 
-#define FTSENT_IS_DIRECTORY(E) \
-  ((E)->fts_info == FTS_D      \
-   || (E)->fts_info == FTS_DC  \
-   || (E)->fts_info == FTS_DP  \
-   || (E)->fts_info == FTS_DNR)
-
 enum RCH_status
   {
     /* we called fchown and close, and both succeeded */
@@ -230,9 +224,6 @@ restricted_chown (int cwd_fd, char const *file,
                   uid_t uid, gid_t gid,
                   uid_t required_uid, gid_t required_gid)
 {
-  if (required_uid == (uid_t) -1 && required_gid == (gid_t) -1)
-    return RC_do_ordinary_chown;
-
   int open_flags = O_NONBLOCK | O_NOCTTY;
   if (! S_ISREG (orig_st->st_mode))
     {
@@ -406,9 +397,11 @@ change_file_owner (FTS *fts, FTSENT *ent,
                       || required_gid == file_stats->st_gid));
     }
 
-  /* This happens when chown -LR --preserve-root encounters a symlink-to-/.  */
+  /* This happens when chown -LR --preserve-root encounters a symlink-to-/,
+     or when chown -RH --preserve-root encounters a symlink-to-/ in the
+     traversed tree (the symlink entry is not a directory for FTS, but
+     file_stats has been dereferenced to point to /).  */
   if (ok
-      && FTSENT_IS_DIRECTORY (ent)
       && ROOT_DEV_INO_CHECK (chopt->root_dev_ino, file_stats))
     {
       ROOT_DEV_INO_WARN (file_full_name);
diff --git a/tests/chown/preserve-root.sh b/tests/chown/preserve-root.sh
index fea84adc1..a14310aae 100755
--- a/tests/chown/preserve-root.sh
+++ b/tests/chown/preserve-root.sh
@@ -51,8 +51,14 @@ echo '==== test -RLh' >> out || framework_failure_
 chown -RLh --preserve-root "+$(id -u)" d >> out 2>&1 && fail=1
 test -n "$id_g" && chgrp -RLh --preserve-root "+$id_g" d >> out 2>&1 && fail=1
 
-(
- cat <<\EOF &&
+# These must also fail: -RH without -h dereferences a symlink-to-/ found
+# in the traversed tree; --preserve-root must catch it (bug#82115).
+echo '==== test -RH (without -h)' >> out || framework_failure_
+chown -RH --preserve-root "+$(id -u)" d >> out 2>&1 && fail=1
+test -n "$id_g" && chgrp -RH --preserve-root "+$id_g" d >> out 2>&1 && fail=1
+
+{
+ cat <<\EOF
 chown: it is dangerous to operate recursively on '/'
 chown: use --no-preserve-root to override this failsafe
 chgrp: it is dangerous to operate recursively on '/'
@@ -64,11 +70,20 @@ chmod: use --no-preserve-root to override this failsafe
 chown: it is dangerous to operate recursively on 'd/slink-to-root' (same as 
'/')
 chown: use --no-preserve-root to override this failsafe
 EOF
- { test -z "$id_g" || cat <<\EOF; }
+ test -z "$id_g" || cat <<\EOF
+chgrp: it is dangerous to operate recursively on 'd/slink-to-root' (same as 
'/')
+chgrp: use --no-preserve-root to override this failsafe
+EOF
+ cat <<\EOF
+==== test -RH (without -h)
+chown: it is dangerous to operate recursively on 'd/slink-to-root' (same as 
'/')
+chown: use --no-preserve-root to override this failsafe
+EOF
+ test -z "$id_g" || cat <<\EOF
 chgrp: it is dangerous to operate recursively on 'd/slink-to-root' (same as 
'/')
 chgrp: use --no-preserve-root to override this failsafe
 EOF
-) > exp || framework_failure_
+} > exp || framework_failure_
 
 compare exp out || fail=1
 
-- 
2.43.0




Reply via email to