| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/nsfs.h> |
| 5 |
#include <sched.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <sys/ioctl.h> |
| 8 |
#include <sys/prctl.h> |
| 9 |
#include <sys/socket.h> |
| 10 |
#include <sys/stat.h> |
| 11 |
#include <sysexits.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include "sd-id128.h" |
| 15 |
|
| 16 |
#include "alloc-util.h" |
| 17 |
#include "fd-util.h" |
| 18 |
#include "fileio.h" |
| 19 |
#include "libmount-util.h" |
| 20 |
#include "namespace-util.h" |
| 21 |
#include "namespace.h" |
| 22 |
#include "pidref.h" |
| 23 |
#include "process-util.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "tests.h" |
| 26 |
#include "tmpfile-util.h" |
| 27 |
#include "uid-range.h" |
| 28 |
#include "user-util.h" |
| 29 |
#include "virt.h" |
| 30 |
|
| 31 |
TEST(namespace_cleanup_tmpdir) { |
| 32 |
{ |
| 33 |
_cleanup_(namespace_cleanup_tmpdirp) char *dir = NULL; |
| 34 |
ASSERT_NOT_NULL(dir = strdup(RUN_SYSTEMD_EMPTY)); |
| 35 |
} |
| 36 |
|
| 37 |
{ |
| 38 |
_cleanup_(namespace_cleanup_tmpdirp) char *dir = NULL; |
| 39 |
ASSERT_OK(mkdtemp_malloc("/tmp/systemd-test-namespace.XXXXXX", &dir)); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
static void test_tmpdir_one(const char *id, const char *A, const char *B) { |
| 44 |
_cleanup_free_ char *a, *b; |
| 45 |
struct stat x, y; |
| 46 |
char *c, *d; |
| 47 |
|
| 48 |
ASSERT_OK(setup_tmp_dir_one(id, "/tmp", &a)); |
| 49 |
ASSERT_OK(setup_tmp_dir_one(id, "/var/tmp", &b)); |
| 50 |
|
| 51 |
ASSERT_OK_ERRNO(stat(a, &x)); |
| 52 |
ASSERT_OK_ERRNO(stat(b, &y)); |
| 53 |
|
| 54 |
ASSERT_TRUE(S_ISDIR(x.st_mode)); |
| 55 |
ASSERT_TRUE(S_ISDIR(y.st_mode)); |
| 56 |
|
| 57 |
if (!streq(a, RUN_SYSTEMD_EMPTY)) { |
| 58 |
ASSERT_TRUE(startswith(a, A)); |
| 59 |
ASSERT_EQ((x.st_mode & 01777), 0700U); |
| 60 |
ASSERT_NOT_NULL(c = strjoina(a, "/tmp")); |
| 61 |
ASSERT_OK_ERRNO(stat(c, &x)); |
| 62 |
ASSERT_TRUE(S_ISDIR(x.st_mode)); |
| 63 |
ASSERT_TRUE(FLAGS_SET(x.st_mode, 01777)); |
| 64 |
ASSERT_OK_ERRNO(rmdir(c)); |
| 65 |
ASSERT_OK_ERRNO(rmdir(a)); |
| 66 |
} |
| 67 |
|
| 68 |
if (!streq(b, RUN_SYSTEMD_EMPTY)) { |
| 69 |
ASSERT_TRUE(startswith(b, B)); |
| 70 |
ASSERT_EQ((y.st_mode & 01777), 0700U); |
| 71 |
ASSERT_NOT_NULL(d = strjoina(b, "/tmp")); |
| 72 |
ASSERT_OK_ERRNO(stat(d, &y)); |
| 73 |
ASSERT_TRUE(S_ISDIR(y.st_mode)); |
| 74 |
ASSERT_TRUE(FLAGS_SET(y.st_mode, 01777)); |
| 75 |
ASSERT_OK_ERRNO(rmdir(d)); |
| 76 |
ASSERT_OK_ERRNO(rmdir(b)); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
TEST(tmpdir) { |
| 81 |
_cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL; |
| 82 |
sd_id128_t bid; |
| 83 |
|
| 84 |
ASSERT_OK(sd_id128_get_boot(&bid)); |
| 85 |
|
| 86 |
ASSERT_NOT_NULL(x = strjoin("/tmp/systemd-private-", SD_ID128_TO_STRING(bid), "-abcd.service-")); |
| 87 |
ASSERT_NOT_NULL(y = strjoin("/var/tmp/systemd-private-", SD_ID128_TO_STRING(bid), "-abcd.service-")); |
| 88 |
|
| 89 |
test_tmpdir_one("abcd.service", x, y); |
| 90 |
|
| 91 |
ASSERT_NOT_NULL(z = strjoin("/tmp/systemd-private-", SD_ID128_TO_STRING(bid), "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-")); |
| 92 |
ASSERT_NOT_NULL(zz = strjoin("/var/tmp/systemd-private-", SD_ID128_TO_STRING(bid), "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-")); |
| 93 |
|
| 94 |
test_tmpdir_one("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz); |
| 95 |
} |
| 96 |
|
| 97 |
static void test_shareable_ns(unsigned long nsflag) { |
| 98 |
_cleanup_close_pair_ int s[2] = EBADF_PAIR; |
| 99 |
bool permission_denied = false; |
| 100 |
_cleanup_(pidref_done) PidRef pidref1 = PIDREF_NULL, pidref2 = PIDREF_NULL, pidref3 = PIDREF_NULL; |
| 101 |
int r, n = 0; |
| 102 |
siginfo_t si; |
| 103 |
|
| 104 |
if (geteuid() > 0) { |
| 105 |
(void) log_tests_skipped("not root"); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, s)); |
| 110 |
|
| 111 |
r = ASSERT_OK(pidref_safe_fork("(share-ns-1)", FORK_LOG|FORK_DEATHSIG_SIGKILL, &pidref1)); |
| 112 |
|
| 113 |
if (r == 0) { |
| 114 |
r = setup_shareable_ns(s, nsflag); |
| 115 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 116 |
ASSERT_OK(r); |
| 117 |
_exit(r >= 0 ? r : EX_NOPERM); |
| 118 |
} |
| 119 |
|
| 120 |
r = ASSERT_OK(pidref_safe_fork("(share-ns-2)", FORK_LOG|FORK_DEATHSIG_SIGKILL, &pidref2)); |
| 121 |
|
| 122 |
if (r == 0) { |
| 123 |
r = setup_shareable_ns(s, nsflag); |
| 124 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 125 |
ASSERT_OK(r); |
| 126 |
_exit(r >= 0 ? r : EX_NOPERM); |
| 127 |
} |
| 128 |
|
| 129 |
r = ASSERT_OK(pidref_safe_fork("(share-ns-3)", FORK_LOG|FORK_DEATHSIG_SIGKILL, &pidref3)); |
| 130 |
|
| 131 |
if (r == 0) { |
| 132 |
r = setup_shareable_ns(s, nsflag); |
| 133 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 134 |
ASSERT_OK(r); |
| 135 |
_exit(r >= 0 ? r : EX_NOPERM); |
| 136 |
} |
| 137 |
|
| 138 |
ASSERT_OK(pidref_wait_for_terminate(&pidref1, &si)); |
| 139 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 140 |
if (si.si_status == EX_NOPERM) |
| 141 |
permission_denied = true; |
| 142 |
else |
| 143 |
n += si.si_status; |
| 144 |
|
| 145 |
ASSERT_OK(pidref_wait_for_terminate(&pidref2, &si)); |
| 146 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 147 |
if (si.si_status == EX_NOPERM) |
| 148 |
permission_denied = true; |
| 149 |
else |
| 150 |
n += si.si_status; |
| 151 |
|
| 152 |
ASSERT_OK(pidref_wait_for_terminate(&pidref3, &si)); |
| 153 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 154 |
if (si.si_status == EX_NOPERM) |
| 155 |
permission_denied = true; |
| 156 |
else |
| 157 |
n += si.si_status; |
| 158 |
|
| 159 |
/* LSMs can cause setup_shareable_ns() to fail with permission denied, do not fail the test in that |
| 160 |
* case (e.g.: LXC with AppArmor on kernel < v6.2). */ |
| 161 |
if (permission_denied) |
| 162 |
return (void) log_tests_skipped("insufficient privileges"); |
| 163 |
|
| 164 |
ASSERT_EQ(n, 1); |
| 165 |
} |
| 166 |
|
| 167 |
TEST(netns) { |
| 168 |
test_shareable_ns(CLONE_NEWNET); |
| 169 |
} |
| 170 |
|
| 171 |
TEST(ipcns) { |
| 172 |
test_shareable_ns(CLONE_NEWIPC); |
| 173 |
} |
| 174 |
|
| 175 |
TEST(fd_is_namespace) { |
| 176 |
_cleanup_close_ int fd = -EBADF; |
| 177 |
|
| 178 |
ASSERT_OK_ZERO(fd_is_namespace(STDIN_FILENO, NAMESPACE_NET)); |
| 179 |
ASSERT_OK_ZERO(fd_is_namespace(STDOUT_FILENO, NAMESPACE_NET)); |
| 180 |
ASSERT_OK_ZERO(fd_is_namespace(STDERR_FILENO, NAMESPACE_NET)); |
| 181 |
|
| 182 |
fd = namespace_open_by_type(NAMESPACE_MOUNT); |
| 183 |
if (IN_SET(fd, -ENOSYS, -ENOPKG)) { |
| 184 |
log_notice("Path %s not found, skipping test", "/proc/self/ns/mnt"); |
| 185 |
return; |
| 186 |
} |
| 187 |
ASSERT_OK(fd); |
| 188 |
ASSERT_OK_POSITIVE(fd_is_namespace(fd, NAMESPACE_MOUNT)); |
| 189 |
ASSERT_OK_ZERO(fd_is_namespace(fd, NAMESPACE_NET)); |
| 190 |
fd = safe_close(fd); |
| 191 |
|
| 192 |
ASSERT_OK(fd = namespace_open_by_type(NAMESPACE_IPC)); |
| 193 |
ASSERT_OK_POSITIVE(fd_is_namespace(fd, NAMESPACE_IPC)); |
| 194 |
fd = safe_close(fd); |
| 195 |
|
| 196 |
ASSERT_OK(fd = namespace_open_by_type(NAMESPACE_NET)); |
| 197 |
ASSERT_OK_POSITIVE(fd_is_namespace(fd, NAMESPACE_NET)); |
| 198 |
} |
| 199 |
|
| 200 |
TEST(protect_kernel_logs) { |
| 201 |
static const NamespaceParameters p = { |
| 202 |
.runtime_scope = RUNTIME_SCOPE_SYSTEM, |
| 203 |
.protect_kernel_logs = true, |
| 204 |
}; |
| 205 |
int r; |
| 206 |
|
| 207 |
if (geteuid() > 0) { |
| 208 |
(void) log_tests_skipped("not root"); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
/* In a container we likely don't have access to /dev/kmsg */ |
| 213 |
if (detect_container() > 0) { |
| 214 |
(void) log_tests_skipped("in container"); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
r = dlopen_libmount(LOG_DEBUG); |
| 219 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) { |
| 220 |
(void) log_tests_skipped("libmount support not compiled in"); |
| 221 |
return; |
| 222 |
} |
| 223 |
ASSERT_OK(r); |
| 224 |
|
| 225 |
r = ASSERT_OK(pidref_safe_fork("(protect)", FORK_WAIT|FORK_LOG|FORK_DEATHSIG_SIGKILL, /* ret= */ NULL)); |
| 226 |
|
| 227 |
if (r == 0) { |
| 228 |
_cleanup_close_ int fd = -EBADF; |
| 229 |
|
| 230 |
ASSERT_OK_ERRNO(fd = open("/dev/kmsg", O_RDONLY | O_CLOEXEC)); |
| 231 |
|
| 232 |
ASSERT_OK_ZERO(setup_namespace(&p, NULL)); |
| 233 |
|
| 234 |
ASSERT_OK_ERRNO(setresuid(UID_NOBODY, UID_NOBODY, UID_NOBODY)); |
| 235 |
ASSERT_ERROR_ERRNO(open("/dev/kmsg", O_RDONLY | O_CLOEXEC), EACCES); |
| 236 |
|
| 237 |
_exit(EXIT_SUCCESS); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
TEST(idmapping_supported) { |
| 242 |
ASSERT_OK(is_idmapping_supported("/run")); |
| 243 |
ASSERT_OK(is_idmapping_supported("/var/lib")); |
| 244 |
ASSERT_OK(is_idmapping_supported("/var/cache")); |
| 245 |
ASSERT_OK(is_idmapping_supported("/var/log")); |
| 246 |
ASSERT_OK(is_idmapping_supported("/etc")); |
| 247 |
} |
| 248 |
|
| 249 |
TEST(namespace_is_init) { |
| 250 |
int r; |
| 251 |
|
| 252 |
for (NamespaceType t = 0; t < _NAMESPACE_TYPE_MAX; t++) { |
| 253 |
r = namespace_is_init(t); |
| 254 |
if (r == -EBADR) |
| 255 |
log_info_errno(r, "In root namespace of type '%s': don't know", namespace_info[t].proc_name); |
| 256 |
else { |
| 257 |
ASSERT_OK(r); |
| 258 |
log_info("In root namespace of type '%s': %s", namespace_info[t].proc_name, yes_no(r)); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
TEST(userns_get_base_uid) { |
| 264 |
_cleanup_close_ int fd = -EBADF; |
| 265 |
|
| 266 |
fd = userns_acquire("0 1 1", "0 2 1", /* setgroups_deny= */ true); |
| 267 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(fd)) |
| 268 |
return (void) log_tests_skipped("userns is not supported"); |
| 269 |
if (ERRNO_IS_NEG_PRIVILEGE(fd)) |
| 270 |
return (void) log_tests_skipped("lacking userns privileges"); |
| 271 |
|
| 272 |
uid_t base_uid, base_gid; |
| 273 |
ASSERT_OK(userns_get_base_uid(fd, &base_uid, &base_gid)); |
| 274 |
ASSERT_EQ(base_uid, 1U); |
| 275 |
ASSERT_EQ(base_gid, 2U); |
| 276 |
|
| 277 |
ASSERT_ERROR(userns_get_base_uid(fd, &base_uid, NULL), EUCLEAN); |
| 278 |
|
| 279 |
fd = safe_close(fd); |
| 280 |
|
| 281 |
fd = userns_acquire_empty(); |
| 282 |
ASSERT_OK(fd); |
| 283 |
|
| 284 |
ASSERT_ERROR(userns_get_base_uid(fd, &base_uid, &base_gid), ENOMSG); |
| 285 |
} |
| 286 |
|
| 287 |
TEST(namespace_open_by_id) { |
| 288 |
/* Try our own user namespace first to see if the kernel exposes ns_id at all. */ |
| 289 |
_cleanup_close_ int userns_fd = ASSERT_OK_ERRNO(open("/proc/self/ns/user", O_RDONLY|O_CLOEXEC)); |
| 290 |
|
| 291 |
uint64_t ns_id; |
| 292 |
int r = RET_NERRNO(ioctl(userns_fd, NS_GET_ID, &ns_id)); |
| 293 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 294 |
return (void) log_tests_skipped("NS_GET_ID is not supported by this kernel"); |
| 295 |
ASSERT_OK(r); |
| 296 |
|
| 297 |
/* namespace_open_by_id() refuses with -EPERM outside the initial user/pid namespace, since |
| 298 |
* the kernel restricts open_by_handle_at() on nsfs to the initial userns and pidns and to |
| 299 |
* CAP_SYS_ADMIN. */ |
| 300 |
_cleanup_close_ int opened = namespace_open_by_id(ns_id); |
| 301 |
if (opened == -EPERM) |
| 302 |
return (void) log_tests_skipped("not in initial user namespace or missing CAP_SYS_ADMIN"); |
| 303 |
if (IN_SET(opened, -EOPNOTSUPP, -EINVAL)) |
| 304 |
return (void) log_tests_skipped("nsfs lookup by ns_id is not supported by this kernel"); |
| 305 |
ASSERT_OK(opened); |
| 306 |
|
| 307 |
struct stat orig_st, opened_st; |
| 308 |
ASSERT_OK_ERRNO(fstat(userns_fd, &orig_st)); |
| 309 |
ASSERT_OK_ERRNO(fstat(opened, &opened_st)); |
| 310 |
ASSERT_EQ(orig_st.st_ino, opened_st.st_ino); |
| 311 |
|
| 312 |
opened = safe_close(opened); |
| 313 |
|
| 314 |
ASSERT_ERROR(namespace_open_by_id(0), EINVAL); |
| 315 |
|
| 316 |
_cleanup_close_ int transient_fd = userns_acquire_empty(); |
| 317 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(transient_fd) || ERRNO_IS_NEG_PRIVILEGE(transient_fd)) |
| 318 |
return (void) log_tests_skipped("cannot acquire userns for transient lookup test"); |
| 319 |
ASSERT_OK(transient_fd); |
| 320 |
|
| 321 |
uint64_t transient_id; |
| 322 |
ASSERT_OK_ERRNO(ioctl(transient_fd, NS_GET_ID, &transient_id)); |
| 323 |
ASSERT_NE(transient_id, ns_id); |
| 324 |
|
| 325 |
opened = ASSERT_OK(namespace_open_by_id(transient_id)); |
| 326 |
|
| 327 |
struct stat transient_st, transient_opened_st; |
| 328 |
ASSERT_OK_ERRNO(fstat(transient_fd, &transient_st)); |
| 329 |
ASSERT_OK_ERRNO(fstat(opened, &transient_opened_st)); |
| 330 |
ASSERT_EQ(transient_st.st_ino, transient_opened_st.st_ino); |
| 331 |
opened = safe_close(opened); |
| 332 |
|
| 333 |
/* Close the only reference. The namespace is now dead — lookup must fail. */ |
| 334 |
transient_fd = safe_close(transient_fd); |
| 335 |
ASSERT_ERROR(namespace_open_by_id(transient_id), ESTALE); |
| 336 |
} |
| 337 |
|
| 338 |
TEST(process_is_owned_by_uid) { |
| 339 |
int r; |
| 340 |
|
| 341 |
/* Test our own PID */ |
| 342 |
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL; |
| 343 |
ASSERT_OK(pidref_set_self(&pid)); |
| 344 |
ASSERT_OK_POSITIVE(process_is_owned_by_uid(&pid, getuid())); |
| 345 |
pidref_done(&pid); |
| 346 |
|
| 347 |
if (getuid() != 0) |
| 348 |
return (void) log_tests_skipped("lacking userns privileges"); |
| 349 |
|
| 350 |
_cleanup_(uid_range_freep) UIDRange *range = NULL; |
| 351 |
ASSERT_OK(uid_range_load_userns(/* path= */ NULL, UID_RANGE_USERNS_INSIDE, &range)); |
| 352 |
if (!uid_range_contains(range, 1)) |
| 353 |
return (void) log_tests_skipped("UID 1 not included in userns UID delegation, skipping test"); |
| 354 |
|
| 355 |
/* Test a child that runs as uid 1 */ |
| 356 |
_cleanup_close_pair_ int p[2] = EBADF_PAIR; |
| 357 |
ASSERT_OK_ERRNO(pipe2(p, O_CLOEXEC)); |
| 358 |
|
| 359 |
r = pidref_safe_fork("(child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pid); |
| 360 |
ASSERT_OK(r); |
| 361 |
if (r == 0) { |
| 362 |
p[0] = safe_close(p[0]); |
| 363 |
ASSERT_OK(fully_set_uid_gid(1, 1, NULL, 0)); |
| 364 |
|
| 365 |
/* After successfully changing id/gid DEATHSIG is reset, so it has to be set again */ |
| 366 |
ASSERT_OK_ERRNO(prctl(PR_SET_PDEATHSIG, SIGKILL)); |
| 367 |
|
| 368 |
ASSERT_OK_EQ_ERRNO(write(p[1], &(const char[]) { 'x' }, 1), 1); |
| 369 |
p[1] = safe_close(p[1]); |
| 370 |
freeze(); |
| 371 |
} |
| 372 |
|
| 373 |
p[1] = safe_close(p[1]); |
| 374 |
char x = 0; |
| 375 |
ASSERT_OK_EQ_ERRNO(read(p[0], &x, 1), 1); |
| 376 |
ASSERT_EQ(x, 'x'); |
| 377 |
p[0] = safe_close(p[0]); |
| 378 |
|
| 379 |
ASSERT_OK_ZERO(process_is_owned_by_uid(&pid, getuid())); |
| 380 |
|
| 381 |
ASSERT_OK(pidref_kill(&pid, SIGKILL)); |
| 382 |
ASSERT_OK(pidref_wait_for_terminate(&pid, NULL)); |
| 383 |
|
| 384 |
/* Test a child that runs in a userns as uid 1, but the userns is owned by us */ |
| 385 |
ASSERT_OK_ERRNO(pipe2(p, O_CLOEXEC)); |
| 386 |
|
| 387 |
_cleanup_close_pair_ int pp[2] = EBADF_PAIR; |
| 388 |
ASSERT_OK_ERRNO(pipe2(pp, O_CLOEXEC)); |
| 389 |
|
| 390 |
r = pidref_safe_fork("(child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_NEW_USERNS, &pid); |
| 391 |
ASSERT_OK(r); |
| 392 |
if (r == 0) { |
| 393 |
p[0] = safe_close(p[0]); |
| 394 |
pp[1] = safe_close(pp[1]); |
| 395 |
|
| 396 |
x = 0; |
| 397 |
ASSERT_OK_EQ_ERRNO(read(pp[0], &x, 1), 1); |
| 398 |
ASSERT_EQ(x, 'x'); |
| 399 |
pp[0] = safe_close(pp[0]); |
| 400 |
|
| 401 |
ASSERT_OK(reset_uid_gid()); |
| 402 |
|
| 403 |
/* After successfully changing id/gid DEATHSIG is reset, so it has to be set again */ |
| 404 |
ASSERT_OK_ERRNO(prctl(PR_SET_PDEATHSIG, SIGKILL)); |
| 405 |
|
| 406 |
ASSERT_OK_EQ_ERRNO(write(p[1], &(const char[]) { 'x' }, 1), 1); |
| 407 |
p[1] = safe_close(p[1]); |
| 408 |
freeze(); |
| 409 |
} |
| 410 |
|
| 411 |
p[1] = safe_close(p[1]); |
| 412 |
pp[0] = safe_close(pp[0]); |
| 413 |
|
| 414 |
ASSERT_OK(write_string_file(procfs_file_alloca(pid.pid, "uid_map"), "0 1 1\n", 0)); |
| 415 |
ASSERT_OK(write_string_file(procfs_file_alloca(pid.pid, "setgroups"), "deny", 0)); |
| 416 |
ASSERT_OK(write_string_file(procfs_file_alloca(pid.pid, "gid_map"), "0 1 1\n", 0)); |
| 417 |
|
| 418 |
ASSERT_OK_EQ_ERRNO(write(pp[1], &(const char[]) { 'x' }, 1), 1); |
| 419 |
pp[1] = safe_close(pp[1]); |
| 420 |
|
| 421 |
x = 0; |
| 422 |
ASSERT_OK_EQ_ERRNO(read(p[0], &x, 1), 1); |
| 423 |
ASSERT_EQ(x, 'x'); |
| 424 |
p[0] = safe_close(p[0]); |
| 425 |
|
| 426 |
ASSERT_OK_POSITIVE(process_is_owned_by_uid(&pid, getuid())); |
| 427 |
|
| 428 |
ASSERT_OK(pidref_kill(&pid, SIGKILL)); |
| 429 |
ASSERT_OK(pidref_wait_for_terminate(&pid, NULL)); |
| 430 |
} |
| 431 |
|
| 432 |
TEST(namespace_get_leader) { |
| 433 |
int r; |
| 434 |
|
| 435 |
_cleanup_(pidref_done) PidRef original = PIDREF_NULL; |
| 436 |
ASSERT_OK(pidref_set_self(&original)); |
| 437 |
|
| 438 |
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL; |
| 439 |
r = pidref_safe_fork("(child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_NEW_MOUNTNS|FORK_WAIT|FORK_LOG, &pid); |
| 440 |
ASSERT_OK(r); |
| 441 |
if (r == 0) { |
| 442 |
|
| 443 |
_cleanup_(pidref_done) PidRef pid2 = PIDREF_NULL; |
| 444 |
r = pidref_safe_fork("(child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT|FORK_LOG, &pid2); |
| 445 |
ASSERT_OK(r); |
| 446 |
|
| 447 |
if (r == 0) { |
| 448 |
log_info("PID hierarchy: " PID_FMT " ← " PID_FMT " ← " PID_FMT, original.pid, pid.pid, pid2.pid); |
| 449 |
|
| 450 |
_cleanup_(pidref_done) PidRef self = PIDREF_NULL; |
| 451 |
ASSERT_OK(pidref_set_self(&self)); |
| 452 |
ASSERT_TRUE(pidref_equal(&self, &pid2)); |
| 453 |
|
| 454 |
_cleanup_(pidref_done) PidRef parent = PIDREF_NULL; |
| 455 |
ASSERT_OK(pidref_set_parent(&parent)); |
| 456 |
ASSERT_TRUE(pidref_equal(&parent, &pid)); |
| 457 |
ASSERT_TRUE(!pidref_equal(&self, &pid)); |
| 458 |
ASSERT_TRUE(!pidref_equal(&self, &parent)); |
| 459 |
|
| 460 |
_cleanup_(pidref_done) PidRef grandparent = PIDREF_NULL; |
| 461 |
ASSERT_OK(pidref_get_ppid_as_pidref(&parent, &grandparent)); |
| 462 |
ASSERT_TRUE(pidref_equal(&grandparent, &original)); |
| 463 |
ASSERT_TRUE(!pidref_equal(&grandparent, &self)); |
| 464 |
ASSERT_TRUE(!pidref_equal(&grandparent, &pid)); |
| 465 |
ASSERT_TRUE(!pidref_equal(&grandparent, &pid2)); |
| 466 |
ASSERT_TRUE(!pidref_equal(&grandparent, &parent)); |
| 467 |
|
| 468 |
_cleanup_(pidref_done) PidRef leader = PIDREF_NULL; |
| 469 |
ASSERT_OK(namespace_get_leader(&self, NAMESPACE_MOUNT, &leader)); |
| 470 |
ASSERT_TRUE(pidref_equal(&parent, &leader)); |
| 471 |
ASSERT_TRUE(pidref_equal(&pid, &leader)); |
| 472 |
ASSERT_TRUE(!pidref_equal(&self, &leader)); |
| 473 |
ASSERT_TRUE(!pidref_equal(&pid2, &leader)); |
| 474 |
ASSERT_TRUE(!pidref_equal(&original, &leader)); |
| 475 |
ASSERT_TRUE(!pidref_equal(&grandparent, &leader)); |
| 476 |
|
| 477 |
_exit(EXIT_SUCCESS); |
| 478 |
} |
| 479 |
|
| 480 |
_exit(EXIT_SUCCESS); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
TEST(detach_mount_namespace_harder) { |
| 485 |
_cleanup_(pidref_done_sigkill_wait) PidRef pid = PIDREF_NULL; |
| 486 |
_cleanup_close_pair_ int p[2] = EBADF_PAIR; |
| 487 |
char x = 0; |
| 488 |
int r; |
| 489 |
|
| 490 |
ASSERT_OK_ERRNO(pipe2(p, O_CLOEXEC)); |
| 491 |
|
| 492 |
ASSERT_OK(r = pidref_safe_fork("(child)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_LOG, &pid)); |
| 493 |
if (r == 0) { |
| 494 |
p[0] = safe_close(p[0]); |
| 495 |
|
| 496 |
ASSERT_OK(detach_mount_namespace_harder(0, 0)); |
| 497 |
|
| 498 |
ASSERT_OK_EQ_ERRNO(write(p[1], &(const char[]) { 'x' }, 1), 1); |
| 499 |
freeze(); |
| 500 |
} |
| 501 |
|
| 502 |
p[1] = safe_close(p[1]); |
| 503 |
ASSERT_OK_EQ_ERRNO(read(p[0], &x, 1), 1); |
| 504 |
ASSERT_EQ(x, 'x'); |
| 505 |
|
| 506 |
ASSERT_OK_POSITIVE(pidref_in_same_namespace(NULL, &pid, NAMESPACE_USER)); |
| 507 |
ASSERT_OK_ZERO(pidref_in_same_namespace(NULL, &pid, NAMESPACE_MOUNT)); |
| 508 |
} |
| 509 |
|
| 510 |
static int intro(void) { |
| 511 |
if (!have_namespaces()) |
| 512 |
return log_tests_skipped("Don't have namespace support or lacking privileges"); |
| 513 |
|
| 514 |
return EXIT_SUCCESS; |
| 515 |
} |
| 516 |
|
| 517 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |