| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <grp.h> |
| 4 |
#include <sched.h> |
| 5 |
#include <sys/eventfd.h> |
| 6 |
#include <sys/mount.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "errno-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "namespace-util.h" |
| 14 |
#include "pidref.h" |
| 15 |
#include "process-util.h" |
| 16 |
#include "rm-rf.h" |
| 17 |
#include "tests.h" |
| 18 |
#include "tmpfile-util.h" |
| 19 |
#include "uid-classification.h" |
| 20 |
#include "userns-restrict.h" |
| 21 |
|
| 22 |
static int make_tmpfs_fsmount(void) { |
| 23 |
_cleanup_close_ int fsfd = -EBADF, mntfd = -EBADF; |
| 24 |
|
| 25 |
fsfd = ASSERT_OK_ERRNO(fsopen("tmpfs", FSOPEN_CLOEXEC)); |
| 26 |
ASSERT_OK_ERRNO(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); |
| 27 |
|
| 28 |
mntfd = ASSERT_OK_ERRNO(fsmount(fsfd, FSMOUNT_CLOEXEC, 0)); |
| 29 |
|
| 30 |
return TAKE_FD(mntfd); |
| 31 |
} |
| 32 |
|
| 33 |
static struct userns_restrict_bpf *bpf_obj = NULL; |
| 34 |
STATIC_DESTRUCTOR_REGISTER(bpf_obj, userns_restrict_bpf_freep); |
| 35 |
|
| 36 |
static int intro(void) { |
| 37 |
int r; |
| 38 |
|
| 39 |
r = userns_restrict_install(/* pin= */ false, &bpf_obj); |
| 40 |
if (ERRNO_IS_NOT_SUPPORTED(r)) |
| 41 |
return log_tests_skipped("LSM-BPF logic not supported"); |
| 42 |
if (ERRNO_IS_PRIVILEGE(r)) |
| 43 |
return log_tests_skipped("Lacking privileges"); |
| 44 |
ASSERT_OK(r); |
| 45 |
|
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
TEST(userns_restrict) { |
| 50 |
_cleanup_close_ int userns_fd = -EBADF, host_fd1 = -EBADF, host_tmpfs = -EBADF, afd = -EBADF, bfd = -EBADF; |
| 51 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 52 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 53 |
int r; |
| 54 |
|
| 55 |
ASSERT_OK(mkdtemp_malloc(NULL, &t)); |
| 56 |
/* Make sure the dir is owned by the transient UID we'll be using so we don't get rejected with a |
| 57 |
* permission error before we even get to the BPF-LSM. */ |
| 58 |
ASSERT_OK_ERRNO(chown(t, CONTAINER_UID_MIN, CONTAINER_UID_MIN)); |
| 59 |
|
| 60 |
host_fd1 = ASSERT_OK_ERRNO(open(t, O_DIRECTORY|O_CLOEXEC)); |
| 61 |
host_tmpfs = ASSERT_OK(make_tmpfs_fsmount()); |
| 62 |
|
| 63 |
_cleanup_free_ char *idmap = NULL; |
| 64 |
ASSERT_OK(asprintf(&idmap, "0 "UID_FMT" 1", CONTAINER_UID_MIN)); |
| 65 |
userns_fd = ASSERT_OK(userns_acquire(idmap, idmap, /* setgroups_deny= */ true)); |
| 66 |
|
| 67 |
ASSERT_OK(userns_restrict_put_by_fd( |
| 68 |
bpf_obj, |
| 69 |
userns_fd, |
| 70 |
/* replace= */ true, |
| 71 |
/* mount_fds= */ NULL, |
| 72 |
/* n_mount_fds= */ 0)); |
| 73 |
|
| 74 |
afd = ASSERT_OK_ERRNO(eventfd(0, EFD_CLOEXEC)); |
| 75 |
bfd = ASSERT_OK_ERRNO(eventfd(0, EFD_CLOEXEC)); |
| 76 |
|
| 77 |
r = ASSERT_OK(pidref_safe_fork("(test)", FORK_DEATHSIG_SIGKILL, &pidref)); |
| 78 |
if (r == 0) { |
| 79 |
_cleanup_close_ int private_tmpfs = -EBADF; |
| 80 |
|
| 81 |
ASSERT_OK(namespace_enter(-EBADF, -EBADF, -EBADF, userns_fd, -EBADF)); |
| 82 |
ASSERT_OK_ERRNO(unshare(CLONE_NEWNS)); |
| 83 |
|
| 84 |
/* Allocate tmpfs locally */ |
| 85 |
private_tmpfs = make_tmpfs_fsmount(); |
| 86 |
|
| 87 |
/* These two host mounts should be inaccessible */ |
| 88 |
ASSERT_ERROR_ERRNO(openat(host_fd1, "test", O_RDWR|O_CREAT|O_CLOEXEC, 0666), EPERM); |
| 89 |
ASSERT_ERROR_ERRNO(openat(host_tmpfs, "xxx", O_RDWR|O_CREAT|O_CLOEXEC, 0666), EPERM); |
| 90 |
ASSERT_ERROR_ERRNO(mkdirat(host_fd1, "test2", 0666), EPERM); |
| 91 |
ASSERT_ERROR_ERRNO(mkdirat(host_tmpfs, "xxx2", 0666), EPERM); |
| 92 |
|
| 93 |
/* But this mount created locally should be fine */ |
| 94 |
safe_close(ASSERT_OK_ERRNO(openat(private_tmpfs, "yyy", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 95 |
ASSERT_OK_ERRNO(mkdirat(private_tmpfs, "yyy2", 0666)); |
| 96 |
|
| 97 |
/* Let's sync with the parent, so that it allowlists more stuff for us */ |
| 98 |
ASSERT_OK_ERRNO(eventfd_write(afd, 1)); |
| 99 |
uint64_t x; |
| 100 |
ASSERT_OK_ERRNO(eventfd_read(bfd, &x)); |
| 101 |
|
| 102 |
/* And now we should also have access to the host tmpfs */ |
| 103 |
safe_close(ASSERT_OK_ERRNO(openat(host_tmpfs, "zzz", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 104 |
safe_close(ASSERT_OK_ERRNO(openat(private_tmpfs, "aaa", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 105 |
ASSERT_OK_ERRNO(mkdirat(host_tmpfs, "zzz2", 0666)); |
| 106 |
ASSERT_OK_ERRNO(mkdirat(private_tmpfs, "aaa2", 0666)); |
| 107 |
|
| 108 |
/* But this one should still fail */ |
| 109 |
ASSERT_ERROR_ERRNO(openat(host_fd1, "bbb", O_RDWR|O_CREAT|O_CLOEXEC, 0666), EPERM); |
| 110 |
ASSERT_ERROR_ERRNO(mkdirat(host_fd1, "bbb2", 0666), EPERM); |
| 111 |
|
| 112 |
/* Sync again, to get more stuff allowlisted */ |
| 113 |
ASSERT_OK_ERRNO(eventfd_write(afd, 1)); |
| 114 |
ASSERT_OK_ERRNO(eventfd_read(bfd, &x)); |
| 115 |
|
| 116 |
/* Everything should now be allowed */ |
| 117 |
safe_close(ASSERT_OK_ERRNO(openat(host_tmpfs, "ccc", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 118 |
safe_close(ASSERT_OK_ERRNO(openat(host_fd1, "ddd", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 119 |
safe_close(ASSERT_OK_ERRNO(openat(private_tmpfs, "eee", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 120 |
ASSERT_OK_ERRNO(mkdirat(host_tmpfs, "ccc2", 0666)); |
| 121 |
safe_close(ASSERT_OK_ERRNO(openat(host_fd1, "ddd2", O_RDWR|O_CREAT|O_CLOEXEC, 0666))); |
| 122 |
ASSERT_OK_ERRNO(mkdirat(private_tmpfs, "eee2", 0666)); |
| 123 |
|
| 124 |
_exit(EXIT_SUCCESS); |
| 125 |
} |
| 126 |
|
| 127 |
uint64_t x; |
| 128 |
ASSERT_OK_ERRNO(eventfd_read(afd, &x)); |
| 129 |
|
| 130 |
ASSERT_OK(userns_restrict_put_by_fd( |
| 131 |
bpf_obj, |
| 132 |
userns_fd, |
| 133 |
/* replace= */ false, |
| 134 |
&host_tmpfs, |
| 135 |
1)); |
| 136 |
|
| 137 |
ASSERT_OK_ERRNO(eventfd_write(bfd, 1)); |
| 138 |
ASSERT_OK_ERRNO(eventfd_read(afd, &x)); |
| 139 |
|
| 140 |
ASSERT_OK(userns_restrict_put_by_fd( |
| 141 |
bpf_obj, |
| 142 |
userns_fd, |
| 143 |
/* replace= */ false, |
| 144 |
&host_fd1, |
| 145 |
1)); |
| 146 |
|
| 147 |
ASSERT_OK_ERRNO(eventfd_write(bfd, 1)); |
| 148 |
|
| 149 |
ASSERT_OK(pidref_wait_for_terminate_and_check("(test)", &pidref, WAIT_LOG)); |
| 150 |
} |
| 151 |
|
| 152 |
static void write_child_mappings(PidRef *child, int parent_userns_fd) { |
| 153 |
/* The kernel requires uid_map/gid_map to be written from the parent user namespace of the |
| 154 |
* target namespace. Fork a helper that joins the parent userns and writes the mappings from |
| 155 |
* there, mirroring what write_userns() does in nsresourcework.c. */ |
| 156 |
int r; |
| 157 |
|
| 158 |
r = ASSERT_OK(pidref_safe_fork("(sd-write-map)", FORK_DEATHSIG_SIGKILL|FORK_WAIT|FORK_LOG, NULL)); |
| 159 |
if (r == 0) { |
| 160 |
char path[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
| 161 |
|
| 162 |
ASSERT_OK_ERRNO(setns(parent_userns_fd, CLONE_NEWUSER)); |
| 163 |
|
| 164 |
xsprintf(path, "/proc/" PID_FMT "/uid_map", child->pid); |
| 165 |
ASSERT_OK(write_string_file(path, "0 0 1\n", WRITE_STRING_FILE_DISABLE_BUFFER)); |
| 166 |
|
| 167 |
xsprintf(path, "/proc/" PID_FMT "/gid_map", child->pid); |
| 168 |
ASSERT_OK(write_string_file(path, "0 0 1\n", WRITE_STRING_FILE_DISABLE_BUFFER)); |
| 169 |
|
| 170 |
_exit(EXIT_SUCCESS); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
TEST(setgroups_deny) { |
| 175 |
_cleanup_close_ int deny_userns_fd = -EBADF, allow_userns_fd = -EBADF, |
| 176 |
afd = -EBADF, bfd = -EBADF; |
| 177 |
int r; |
| 178 |
|
| 179 |
_cleanup_free_ char *idmap = NULL; |
| 180 |
ASSERT_OK(asprintf(&idmap, "0 "UID_FMT" 1", CONTAINER_UID_MIN)); |
| 181 |
|
| 182 |
/* Create a userns that will have setgroups() denied via BPF. We don't set setgroups_deny here |
| 183 |
* because that uses /proc/self/setgroups which is transitive and we want to test the BPF-LSM |
| 184 |
* denial specifically. */ |
| 185 |
deny_userns_fd = ASSERT_OK(userns_acquire(idmap, idmap, /* setgroups_deny= */ false)); |
| 186 |
|
| 187 |
ASSERT_OK(userns_restrict_put_by_fd( |
| 188 |
bpf_obj, |
| 189 |
deny_userns_fd, |
| 190 |
/* replace= */ true, |
| 191 |
/* mount_fds= */ NULL, |
| 192 |
/* n_mount_fds= */ 0)); |
| 193 |
ASSERT_OK(userns_restrict_setgroups_deny_by_fd(bpf_obj, deny_userns_fd)); |
| 194 |
|
| 195 |
/* Create a userns that is managed (in mount ID hash) but does NOT have setgroups() denied */ |
| 196 |
allow_userns_fd = ASSERT_OK(userns_acquire(idmap, idmap, /* setgroups_deny= */ false)); |
| 197 |
|
| 198 |
ASSERT_OK(userns_restrict_put_by_fd( |
| 199 |
bpf_obj, |
| 200 |
allow_userns_fd, |
| 201 |
/* replace= */ true, |
| 202 |
/* mount_fds= */ NULL, |
| 203 |
/* n_mount_fds= */ 0)); |
| 204 |
|
| 205 |
afd = ASSERT_OK_ERRNO(eventfd(0, EFD_CLOEXEC)); |
| 206 |
bfd = ASSERT_OK_ERRNO(eventfd(0, EFD_CLOEXEC)); |
| 207 |
|
| 208 |
/* Test 1: setgroups() should be denied in the deny userns, including after unsharing into a child |
| 209 |
* user namespace (the ancestor walk should find the deny entry). */ |
| 210 |
{ |
| 211 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 212 |
|
| 213 |
r = ASSERT_OK(pidref_safe_fork("(test-deny)", FORK_LOG|FORK_DEATHSIG_SIGKILL, &pidref)); |
| 214 |
if (r == 0) { |
| 215 |
/* Enter the userns manually without going through namespace_enter(), because |
| 216 |
* that calls reset_uid_gid() which calls setgroups() internally. Since the |
| 217 |
* BPF LSM denies setgroups(), reset_uid_gid() would fail before calling |
| 218 |
* setresuid()/setresgid(), leaving us as the overflow UID without |
| 219 |
* capabilities. */ |
| 220 |
ASSERT_OK_ERRNO(setns(deny_userns_fd, CLONE_NEWUSER)); |
| 221 |
ASSERT_OK_ERRNO(setresgid(0, 0, 0)); |
| 222 |
ASSERT_OK_ERRNO(setresuid(0, 0, 0)); |
| 223 |
|
| 224 |
/* setgroups() should be denied by BPF LSM */ |
| 225 |
ASSERT_ERROR_ERRNO(setgroups(0, NULL), EPERM); |
| 226 |
|
| 227 |
/* Unshare into a child user namespace. The parent will write the mappings |
| 228 |
* for us since writing /proc/self/uid_map from inside the userns fails |
| 229 |
* because the proc mount belongs to the init user namespace. */ |
| 230 |
ASSERT_OK_ERRNO(unshare(CLONE_NEWUSER)); |
| 231 |
ASSERT_OK_ERRNO(eventfd_write(afd, 1)); |
| 232 |
uint64_t x; |
| 233 |
ASSERT_OK_ERRNO(eventfd_read(bfd, &x)); |
| 234 |
|
| 235 |
/* setgroups() should still be denied because the ancestor walk finds the |
| 236 |
* deny entry on the parent user namespace */ |
| 237 |
ASSERT_ERROR_ERRNO(setgroups(0, NULL), EPERM); |
| 238 |
|
| 239 |
_exit(EXIT_SUCCESS); |
| 240 |
} |
| 241 |
|
| 242 |
uint64_t x; |
| 243 |
ASSERT_OK_ERRNO(eventfd_read(afd, &x)); |
| 244 |
write_child_mappings(&pidref, deny_userns_fd); |
| 245 |
ASSERT_OK_ERRNO(eventfd_write(bfd, 1)); |
| 246 |
|
| 247 |
ASSERT_OK(pidref_wait_for_terminate_and_check("(test-deny)", &pidref, WAIT_LOG)); |
| 248 |
} |
| 249 |
|
| 250 |
/* Test 2: setgroups() should be allowed in the managed-only userns (mount ID hash but no setgroups |
| 251 |
* deny entry), including in a child user namespace. */ |
| 252 |
{ |
| 253 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 254 |
|
| 255 |
r = ASSERT_OK(pidref_safe_fork("(test-allow)", FORK_LOG|FORK_DEATHSIG_SIGKILL, &pidref)); |
| 256 |
if (r == 0) { |
| 257 |
ASSERT_OK_ERRNO(setns(allow_userns_fd, CLONE_NEWUSER)); |
| 258 |
ASSERT_OK_ERRNO(setresgid(0, 0, 0)); |
| 259 |
ASSERT_OK_ERRNO(setresuid(0, 0, 0)); |
| 260 |
|
| 261 |
/* setgroups() should succeed since this userns is only in the mount ID hash */ |
| 262 |
ASSERT_OK_ERRNO(setgroups(0, NULL)); |
| 263 |
|
| 264 |
/* Also should work in a child userns since the ancestor walk finds the |
| 265 |
* mount ID hash entry (not the setgroups deny entry) */ |
| 266 |
ASSERT_OK_ERRNO(unshare(CLONE_NEWUSER)); |
| 267 |
ASSERT_OK_ERRNO(eventfd_write(afd, 1)); |
| 268 |
uint64_t x; |
| 269 |
ASSERT_OK_ERRNO(eventfd_read(bfd, &x)); |
| 270 |
|
| 271 |
ASSERT_OK_ERRNO(setgroups(0, NULL)); |
| 272 |
|
| 273 |
_exit(EXIT_SUCCESS); |
| 274 |
} |
| 275 |
|
| 276 |
uint64_t x; |
| 277 |
ASSERT_OK_ERRNO(eventfd_read(afd, &x)); |
| 278 |
write_child_mappings(&pidref, allow_userns_fd); |
| 279 |
ASSERT_OK_ERRNO(eventfd_write(bfd, 1)); |
| 280 |
|
| 281 |
ASSERT_OK(pidref_wait_for_terminate_and_check("(test-allow)", &pidref, WAIT_LOG)); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |