tests: Assume we're running in a chroot if check fails

running_in_chroot() will fail when a test is executed as a non-root
user without CAP_DAC_READ_SEARCH as it won't be able to access
/proc/1/root.

Let's make things more robust by skipping tests if we can't detect
if we're in a chroot or not, since if we can't even detect if we're
in a chroot or not, chances are we're missing the required privileges
to execute the test anyway.
This commit is contained in:
Daan De Meyer
2025-11-24 13:07:39 +01:00
parent ab5a79ff5d
commit 43687c22ab
5 changed files with 5 additions and 5 deletions

View File

@@ -1612,7 +1612,7 @@ static int intro(void) {
if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
return log_tests_skipped("not privileged");
if (running_in_chroot() > 0)
if (running_in_chroot() != 0)
return log_tests_skipped("running in chroot");
if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)

View File

@@ -240,7 +240,7 @@ static int run(int argc, char *argv[]) {
if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
return log_tests_skipped("not running privileged");
if (detect_container() > 0 || running_in_chroot() > 0)
if (detect_container() != 0 || running_in_chroot() != 0)
return log_tests_skipped("Test not supported in a container/chroot, requires udev/uevent notifications");
assert_se(loop_device_make(fd, O_RDWR, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_EX, &loop) >= 0);

View File

@@ -38,7 +38,7 @@
#define CHECK_PRIV \
if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) \
return (void) log_tests_skipped("Not privileged"); \
if (running_in_chroot() > 0) \
if (running_in_chroot() != 0) \
return (void) log_tests_skipped("running in chroot");
TEST(mount_option_mangle) {

View File

@@ -457,7 +457,7 @@ static int intro(void) {
/* let's move into our own mount namespace with all propagation from the host turned off, so
* that /proc/self/mountinfo is static and constant for the whole time our test runs. */
if (running_in_chroot() > 0) {
if (running_in_chroot() != 0) {
/* We cannot remount file system with MS_PRIVATE when running in chroot. */
log_notice("Running in chroot, proceeding in originating mount namespace.");
return EXIT_SUCCESS;

View File

@@ -49,7 +49,7 @@ TEST(rereadpt) {
return (void) log_tests_skipped("test not available in container");
if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
return (void) log_tests_skipped("test requires privileges");
if (running_in_chroot() > 0)
if (running_in_chroot() != 0)
return (void) log_tests_skipped("test not available in chroot()");
_cleanup_free_ char *sfdisk_path = NULL;