mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03:00
chcon: avoid a race when partial contexts are specified
* src/chcon.c: Include fcntl--.h. (change_file_context): Open a file descriptor when only a partial context is specified. Use it to get and subsequently set the file context, instead of using getfileconat followed by setfileconat, if libselinux supports it on a file descriptor opened with O_PATH. * tests/chcon/traversal-missing.sh: Adjust the test using partial contexts to also inject open and openat errors. Add a test for complete contexts. * NEWS: Mention the bug fix. Discovered by Anthropic's OSS Scanner, as bug ANT-2026-9PFL6OLT. Co-authored-by: Pádraig Brady <P@draigBrady.com>
This commit is contained in:
1 parent
7fc1d7199d
commit
e018661bd9
3 files changed
+85
-22
No files matched your search
@@ -11,6 +11,10 @@ GNU coreutils NEWS -*- outline -*-
|
||||
security context fails instead of the basename.
|
||||
[bug introduced with the chcon program in coreutils-6.9.90]
|
||||
|
||||
'chcon' avoids a race between getting and setting security contexts of a file
|
||||
when only a partial context is specified.
|
||||
[bug introduced with the chcon program in coreutils-6.9.90]
|
||||
|
||||
'chroot --userspec=USER' now uses the primary group of USER as looked up
|
||||
inside the chroot. Previously the group looked up outside the chroot
|
||||
was used, even when the look-up inside the chroot succeeded.
|
||||
|
||||
+54
-16
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "system.h"
|
||||
#include "dev-ino.h"
|
||||
#include "fcntl--.h"
|
||||
#include "fts-missing.h"
|
||||
#include "ignore-value.h"
|
||||
#include "quote.h"
|
||||
@@ -133,34 +134,63 @@ compute_context_from_mask (char const *context, context_t *ret)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Change the context of FILE, using specified components.
|
||||
/* Change the context of FILE relative to DIRFD, using specified components.
|
||||
If it is a directory and -R is given, recurse.
|
||||
Return 0 if successful, 1 if errors occurred. */
|
||||
|
||||
static int
|
||||
change_file_context (char const *file_full_name, int fd, char const *file,
|
||||
bool ignore_missing)
|
||||
change_file_context (char const *file_full_name, int const dirfd,
|
||||
char const *file, bool ignore_missing)
|
||||
{
|
||||
char *file_context = NULL;
|
||||
context_t context IF_LINT (= NULL);
|
||||
char const * context_string;
|
||||
int errors = 0;
|
||||
int file_fd = -1;
|
||||
|
||||
if (specified_context == NULL)
|
||||
{
|
||||
int status = (affect_symlink_referent
|
||||
? getfileconat (fd, file, &file_context)
|
||||
: lgetfileconat (fd, file, &file_context));
|
||||
|
||||
if (status < 0 && errno != ENODATA)
|
||||
int status;
|
||||
#ifdef O_PATH
|
||||
/* Pin the file while deriving and applying a partial context.
|
||||
O_PATH also works for symlinks and unreadable files. */
|
||||
file_fd = openat (dirfd, file,
|
||||
(O_PATH | (affect_symlink_referent ? 0 : O_NOFOLLOW)));
|
||||
if (file_fd < 0)
|
||||
{
|
||||
if (ignore_missing && ignorable_traversal_errno (errno))
|
||||
return 0;
|
||||
error (0, errno, _("failed to get security context of %s"),
|
||||
quoteaf (file_full_name));
|
||||
error (0, errno, _("failed to open %s"), quoteaf (file_full_name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
status = fgetfilecon (file_fd, &file_context);
|
||||
if (status < 0 && errno == EBADF)
|
||||
{
|
||||
/* libselinux before 3.4, or an unavailable /proc/self/fd,
|
||||
cannot handle O_PATH descriptors, so fall back to
|
||||
potentially racy behavior with partial contexts. */
|
||||
close (file_fd);
|
||||
file_fd = -1;
|
||||
}
|
||||
#endif
|
||||
if (file_fd < 0)
|
||||
status = (affect_symlink_referent
|
||||
? getfileconat (dirfd, file, &file_context)
|
||||
: lgetfileconat (dirfd, file, &file_context));
|
||||
|
||||
if (status < 0 && errno != ENODATA)
|
||||
{
|
||||
if (! (file_fd < 0 && ignore_missing
|
||||
&& ignorable_traversal_errno (errno)))
|
||||
{
|
||||
error (0, errno, _("failed to get security context of %s"),
|
||||
quoteaf (file_full_name));
|
||||
errors = 1;
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* If the file doesn't have a context, and we're not setting all of
|
||||
the context components, there isn't really an obvious default.
|
||||
Thus, we just give up. */
|
||||
@@ -168,13 +198,15 @@ change_file_context (char const *file_full_name, int fd, char const *file,
|
||||
{
|
||||
error (0, 0, _("can't apply partial context to unlabeled file %s"),
|
||||
quoteaf (file_full_name));
|
||||
return 1;
|
||||
errors = 1;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (compute_context_from_mask (file_context, &context))
|
||||
{
|
||||
freecon (file_context);
|
||||
return 1;
|
||||
errors = 1;
|
||||
goto done;
|
||||
}
|
||||
|
||||
context_string = context_str (context);
|
||||
@@ -186,13 +218,16 @@ change_file_context (char const *file_full_name, int fd, char const *file,
|
||||
|
||||
if (file_context == NULL || ! streq (context_string, file_context))
|
||||
{
|
||||
int fail = (affect_symlink_referent
|
||||
? setfileconat (fd, file, context_string)
|
||||
: lsetfileconat (fd, file, context_string));
|
||||
int fail = (0 <= file_fd
|
||||
? fsetfilecon (file_fd, context_string)
|
||||
: affect_symlink_referent
|
||||
? setfileconat (dirfd, file, context_string)
|
||||
: lsetfileconat (dirfd, file, context_string));
|
||||
|
||||
if (fail)
|
||||
{
|
||||
if (! (ignore_missing && ignorable_traversal_errno (errno)))
|
||||
if (! (file_fd < 0 && ignore_missing
|
||||
&& ignorable_traversal_errno (errno)))
|
||||
{
|
||||
errors = 1;
|
||||
error (0, errno, _("failed to change context of %s to %s"),
|
||||
@@ -208,6 +243,9 @@ change_file_context (char const *file_full_name, int fd, char const *file,
|
||||
freecon (file_context);
|
||||
}
|
||||
|
||||
done:
|
||||
if (0 <= file_fd)
|
||||
close (file_fd);
|
||||
return errors;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,22 +20,25 @@
|
||||
print_ver_ chcon
|
||||
require_strace_ getxattr
|
||||
|
||||
mkdir d || framework_failure_
|
||||
touch d/foo || framework_failure_
|
||||
mkdir -p d/foo || framework_failure_
|
||||
touch d/foo/bar || framework_failure_
|
||||
opens=open,openat
|
||||
xattrs=getxattr,lgetxattr,setxattr,lsetxattr
|
||||
|
||||
(cd d && chcon -R -t user_tmp_t . > /dev/null 2>&1) \
|
||||
|| skip_ 'chcon does not work on the test file system'
|
||||
|
||||
# Skip if strace does not support path-filtered syscall injection.
|
||||
strace --quiet=all -o /dev/null -P /proc/self/fd/4/foo \
|
||||
-e inject=$xattrs:error=ENOENT true 2> /dev/null \
|
||||
strace --quiet=all -o /dev/null -P d/foo -P d/foo/bar \
|
||||
-e inject=$opens,$xattrs:error=ENOENT true 2> /dev/null \
|
||||
|| skip_ 'strace does not support the required options and syscalls'
|
||||
|
||||
# Test traversal using a partial context, where we use openat to
|
||||
# avoid races.
|
||||
for errnum in ENOENT; do
|
||||
rm -f err trace
|
||||
(cd d && strace --quiet=all -o ../trace -P /proc/self/fd/4/foo \
|
||||
-e inject=$xattrs:error=$errnum chcon -R -t user_tmp_t . \
|
||||
(cd d && strace --quiet=all -o ../trace -P foo -P foo/bar \
|
||||
-e inject=$opens,$xattrs:error=$errnum chcon -R -t user_tmp_t . \
|
||||
> /dev/null 2> ../err)
|
||||
status=$?
|
||||
|
||||
@@ -45,4 +48,22 @@ for errnum in ENOENT; do
|
||||
test ! -s err || { cat err; fail=1; }
|
||||
done
|
||||
|
||||
# Test traversal on a complete context, where getfileconat
|
||||
# or lgetfileconat are used. This requires root, and we use
|
||||
# root:object_r:tmp_t:s0 since it works without mcstransd running.
|
||||
if uid_is_privileged_; then
|
||||
for errnum in ENOENT; do
|
||||
rm -f err trace
|
||||
(cd d && strace --quiet=all -o ../trace -P /proc/self/fd/4/foo \
|
||||
-P /proc/self/fd/5/bar -e inject=$xattrs:error=$errnum \
|
||||
chcon -R root:object_r:tmp_t:s0 . > /dev/null 2> ../err)
|
||||
status=$?
|
||||
|
||||
grep ' (INJECTED)' trace > /dev/null 2>&1 \
|
||||
|| skip_ 'strace did not inject the requested failure'
|
||||
test $status -eq 0 || fail=1
|
||||
test ! -s err || { cat err; fail=1; }
|
||||
done
|
||||
fi
|
||||
|
||||
Exit $fail
|
||||
Reference in new issue
Block a user