Xue Haolin <[email protected]> writes:

> 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(-)

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