Debian Codegraph @

src/test/test-namespace-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "capability-util.h"
4 #include "errno-util.h"
5 #include "pidref.h"
6 #include "process-util.h"
7 #include "fd-util.h"
8 #include "namespace-util.h"
9 #include "tests.h"
10
11 TEST(namespace_enter) {
12 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
13 int r;
14
15 r = pidref_safe_fork(
16 "test-ns-enter-1",
17 FORK_NEW_USERNS|FORK_NEW_MOUNTNS|FORK_LOG|FORK_FREEZE|FORK_DEATHSIG_SIGKILL,
18 &pidref);
19 if (ERRNO_IS_NEG_PRIVILEGE(r))
20 return (void) log_tests_skipped_errno(r, "Unable to unshare user namespace");
21
22 ASSERT_OK(r);
23
24 _cleanup_close_ int mntns_fd = -EBADF, userns_fd = -EBADF, root_fd = -EBADF;
25 ASSERT_OK(pidref_namespace_open(&pidref, NULL, &mntns_fd, NULL, &userns_fd, &root_fd));
26
27 r = ASSERT_OK(pidref_safe_fork(
28 "test-ns-enter-2",
29 FORK_LOG|FORK_WAIT|FORK_DEATHSIG_SIGKILL,
30 NULL));
31 if (r == 0) {
32 ASSERT_OK(namespace_enter(-EBADF, mntns_fd, -EBADF, userns_fd, root_fd));
33 _exit(EXIT_SUCCESS);
34 }
35
36 /* Make sure we can enter the namespaces as well if we don't have CAP_SYS_ADMIN. */
37 r = ASSERT_OK(pidref_safe_fork(
38 "test-ns-enter-3",
39 FORK_LOG|FORK_WAIT|FORK_DEATHSIG_SIGKILL,
40 NULL));
41 if (r == 0) {
42 ASSERT_OK(drop_capability(CAP_SYS_ADMIN));
43 ASSERT_OK(namespace_enter(-EBADF, mntns_fd, -EBADF, userns_fd, root_fd));
44 _exit(EXIT_SUCCESS);
45 }
46 }
47
48 DEFINE_TEST_MAIN(LOG_DEBUG);