* src/chroot.c (main): Clear username and groups for
a UID that's unknown within the chroot.
* tests/chroot/chroot-userspec-uid.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Update the bug fix.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-EOEDOY0L.
---
NEWS | 9 ++--
src/chroot.c | 14 ++++--
tests/chroot/chroot-userspec-uid.sh | 68 +++++++++++++++++++++++++++++
tests/local.mk | 1 +
4 files changed, 84 insertions(+), 8 deletions(-)
create mode 100755 tests/chroot/chroot-userspec-uid.sh
diff --git a/NEWS b/NEWS
index c19438e5f..f29045f68 100644
--- a/NEWS
+++ b/NEWS
@@ -43,10 +43,11 @@ GNU coreutils NEWS -*-
outline -*-
entry following a symbolic link to '/' encountered during traversal.
[bug introduced in coreutils-9.5]
- 'chroot --userspec=USER' now uses the primary group of USER from inside the
- chroot. Also this no longer sets supplemental groups of an unrelated user,
- or reads freed memory, when the USER ID is only known outside the chroot.
- [bug introduced in coreutils-8.23]
+ 'chroot --userspec=USER' now prefers the primary group from inside the
chroot.
+ If a numeric UID is known only outside the chroot, supplementary groups are
+ cleared unless --groups is given. Previously, chroot could assign groups
+ belonging to an unrelated user or read freed memory during group lookup.
+ [bugs introduced in coreutils-8.23]
'chown -v' and 'chgrp -v' no longer emit uninitialized user and group IDs
when operating on dangling symbolic links.
diff --git a/src/chroot.c b/src/chroot.c
index 92c93b794..946782447 100644
--- a/src/chroot.c
+++ b/src/chroot.c
@@ -379,11 +379,17 @@ main (int argc, char **argv)
gid = pwd->pw_gid;
username = pwd->pw_name;
}
- else if (gid_unset (gid))
+ else
{
- error (EXIT_CANCELED, errno,
- _("no group specified for unknown uid: %ju"),
- (uintmax_t) uid);
+ if (gid_unset (gid))
+ error (EXIT_CANCELED, errno,
+ _("no group specified for unknown uid: %ju"),
+ (uintmax_t) uid);
+
+ /* UID is unknown in the chroot, so treat like any unknown ID. */
+ username = NULL;
+ if (! groups)
+ n_gids = 0;
}
}
diff --git a/tests/chroot/chroot-userspec-uid.sh
b/tests/chroot/chroot-userspec-uid.sh
new file mode 100755
index 000000000..55e534769
--- /dev/null
+++ b/tests/chroot/chroot-userspec-uid.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+# Ensure --userspec=UID doesn't set the groups of an unrelated user,
+# when UID is only known outside the chroot.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ chroot
+
+require_root_
+require_strace_ setgroups
+
+grep '^#define HAVE_SETGROUPS 1' "$CONFIG_HEADER" >/dev/null \
+ || skip_ 'no setgroups support'
+
+# Avoid "nobody", which NSS modules may also provide within the chroot.
+uid=1
+name=$(id -nu $uid) || skip_ "no user with ID $uid"
+gid=$(id -g $uid) || framework_failure_
+
+in_uid=54321 # The ID of NAME within the chroot.
+in_gid=54322 # A group that NAME is a member of within the chroot.
+
+mkdir -p newroot/etc || framework_failure_
+chmod a+x newroot || framework_failure_ # Allow /true lookup after setuid.
+echo "ingroup:x:$in_gid:$name" > newroot/etc/group || framework_failure_
+
+# Trace the groups set for the specified user.
+# Note there is no command within the chroot to run.
+chroot_groups()
+{
+ returns_ 127 strace -o trace chroot --userspec="$1" newroot /true
+}
+
+# UID is unknown within the chroot, whether or not NAME is a user there.
+for in_name in "$name" "not$name"; do
+ echo "$in_name:x:$in_uid:$in_uid::/:/bin/sh" > newroot/etc/passwd \
+ || framework_failure_
+
+ if test "$in_name" = "$name"; then
+ # Skip unless account lookups inside newroot find our user and group.
+ chroot_groups "$name" &&
+ grep "^setuid[0-9]*($in_uid)" trace &&
+ grep "^setgroups.*[^0-9]$in_gid[^0-9]" trace ||
+ skip_ 'cannot establish the test user and group inside the chroot'
+ fi
+
+ for spec in +$uid $uid +$uid:+$gid $uid:$gid; do
+ chroot_groups "$spec" || fail=1
+ grep '^setgroups[0-9]*(0,' trace || fail=1
+ done
+done
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 66167d927..e0d0c949a 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -132,6 +132,7 @@ all_root_tests = \
tests/nproc/nproc-quota-systemd.sh \
tests/chcon/chcon.sh \
tests/chroot/chroot-credentials.sh \
+ tests/chroot/chroot-userspec-uid.sh \
tests/misc/selinux.sh \
tests/truncate/truncate-owned-by-other.sh \
tests/mkdir/writable-under-readonly.sh \
--
2.56.0