| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "capability-util.h" |
| 7 |
#include "process-util.h" |
| 8 |
#include "rm-rf.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "tests.h" |
| 11 |
#include "tmpfile-util.h" |
| 12 |
|
| 13 |
static void test_rm_rf_chmod_inner(void) { |
| 14 |
_cleanup_(rm_rf_physical_and_freep) char *d = NULL; |
| 15 |
const char *a, *b, *x, *y; |
| 16 |
struct stat st; |
| 17 |
|
| 18 |
ASSERT_NE(getuid(), 0U); |
| 19 |
|
| 20 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-rm-rf.XXXXXXX", &d)); |
| 21 |
a = strjoina(d, "/a"); |
| 22 |
b = strjoina(a, "/b"); |
| 23 |
x = strjoina(d, "/x"); |
| 24 |
y = strjoina(x, "/y"); |
| 25 |
|
| 26 |
ASSERT_OK_ERRNO(mkdir(x, 0700)); |
| 27 |
ASSERT_OK_ERRNO(mknod(y, S_IFREG | 0600, 0)); |
| 28 |
|
| 29 |
ASSERT_OK_ERRNO(chmod(y, 0400)); |
| 30 |
ASSERT_OK_ERRNO(chmod(x, 0500)); |
| 31 |
ASSERT_OK_ERRNO(chmod(d, 0500)); |
| 32 |
|
| 33 |
if (!have_effective_cap(CAP_DAC_OVERRIDE)) |
| 34 |
ASSERT_ERROR(rm_rf(d, REMOVE_PHYSICAL), EACCES); |
| 35 |
|
| 36 |
ASSERT_OK_ERRNO(access(d, F_OK)); |
| 37 |
ASSERT_OK_ERRNO(access(x, F_OK)); |
| 38 |
ASSERT_OK_ERRNO(access(y, F_OK)); |
| 39 |
|
| 40 |
ASSERT_OK(rm_rf(d, REMOVE_PHYSICAL|REMOVE_CHMOD)); |
| 41 |
|
| 42 |
ASSERT_OK_ERRNO(access(d, F_OK)); |
| 43 |
ASSERT_ERROR_ERRNO(access(x, F_OK), ENOENT); |
| 44 |
ASSERT_ERROR_ERRNO(access(y, F_OK), ENOENT); |
| 45 |
|
| 46 |
ASSERT_OK_ERRNO(mkdir(a, 0700)); |
| 47 |
ASSERT_OK_ERRNO(mkdir(b, 0700)); |
| 48 |
ASSERT_OK_ERRNO(mkdir(x, 0700)); |
| 49 |
ASSERT_OK_ERRNO(mknod(y, S_IFREG | 0600, 0)); |
| 50 |
|
| 51 |
ASSERT_OK_ERRNO(chmod(b, 0000)); |
| 52 |
ASSERT_OK_ERRNO(chmod(a, 0000)); |
| 53 |
ASSERT_OK_ERRNO(chmod(y, 0000)); |
| 54 |
ASSERT_OK_ERRNO(chmod(x, 0000)); |
| 55 |
ASSERT_OK_ERRNO(chmod(d, 0500)); |
| 56 |
|
| 57 |
ASSERT_ERROR(rm_rf(d, REMOVE_PHYSICAL|REMOVE_CHMOD|REMOVE_CHMOD_RESTORE|REMOVE_ONLY_DIRECTORIES), ENOTEMPTY); |
| 58 |
|
| 59 |
ASSERT_ERROR_ERRNO(access(a, F_OK), ENOENT); |
| 60 |
ASSERT_OK_ERRNO(access(d, F_OK)); |
| 61 |
ASSERT_OK_ERRNO(stat(d, &st)); |
| 62 |
ASSERT_EQ(st.st_mode & 07777, 0500U); |
| 63 |
ASSERT_OK_ERRNO(access(x, F_OK)); |
| 64 |
ASSERT_OK_ERRNO(stat(x, &st)); |
| 65 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 66 |
ASSERT_OK_ERRNO(chmod(x, 0700)); |
| 67 |
ASSERT_OK_ERRNO(access(y, F_OK)); |
| 68 |
ASSERT_OK_ERRNO(stat(y, &st)); |
| 69 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 70 |
|
| 71 |
ASSERT_OK_ERRNO(chmod(y, 0000)); |
| 72 |
ASSERT_OK_ERRNO(chmod(x, 0000)); |
| 73 |
ASSERT_OK_ERRNO(chmod(d, 0000)); |
| 74 |
|
| 75 |
ASSERT_OK(rm_rf(d, REMOVE_PHYSICAL|REMOVE_CHMOD|REMOVE_CHMOD_RESTORE)); |
| 76 |
|
| 77 |
ASSERT_OK_ERRNO(stat(d, &st)); |
| 78 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 79 |
ASSERT_OK_ERRNO(access(d, F_OK)); |
| 80 |
ASSERT_OK_ERRNO(chmod(d, 0700)); |
| 81 |
ASSERT_ERROR_ERRNO(access(x, F_OK), ENOENT); |
| 82 |
|
| 83 |
ASSERT_OK_ERRNO(mkdir(x, 0700)); |
| 84 |
ASSERT_OK_ERRNO(mknod(y, S_IFREG | 0600, 0)); |
| 85 |
|
| 86 |
ASSERT_OK_ERRNO(chmod(y, 0000)); |
| 87 |
ASSERT_OK_ERRNO(chmod(x, 0000)); |
| 88 |
ASSERT_OK_ERRNO(chmod(d, 0000)); |
| 89 |
|
| 90 |
ASSERT_OK(rm_rf(d, REMOVE_PHYSICAL|REMOVE_CHMOD|REMOVE_ROOT)); |
| 91 |
|
| 92 |
ASSERT_ERROR_ERRNO(access(d, F_OK), ENOENT); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(rm_rf_chmod) { |
| 96 |
int r; |
| 97 |
|
| 98 |
if (getuid() == 0 && userns_has_single_user()) |
| 99 |
return (void) log_tests_skipped("running as root or in userns with single user"); |
| 100 |
|
| 101 |
if (getuid() == 0) { |
| 102 |
/* This test only works unpriv (as only then the access mask for the owning user matters), |
| 103 |
* hence drop privs here */ |
| 104 |
|
| 105 |
r = ASSERT_OK(pidref_safe_fork("(setresuid)", FORK_DEATHSIG_SIGTERM|FORK_WAIT, NULL)); |
| 106 |
|
| 107 |
if (r == 0) { |
| 108 |
/* child */ |
| 109 |
|
| 110 |
ASSERT_OK_ERRNO(setresuid(1, 1, 1)); |
| 111 |
|
| 112 |
test_rm_rf_chmod_inner(); |
| 113 |
_exit(EXIT_SUCCESS); |
| 114 |
} |
| 115 |
|
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
test_rm_rf_chmod_inner(); |
| 120 |
} |
| 121 |
|
| 122 |
DEFINE_TEST_MAIN(LOG_DEBUG); |