1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // Copyright (c) 2019 Google, Inc.
4
5 #define _GNU_SOURCE
6
7 #include "config.h"
8
9 #include <errno.h>
10 #include <lapi/syscalls.h>
11 #include <sched.h>
12 #include <stdlib.h>
13
14 #include "tst_test.h"
15 #include "lapi/mount.h"
16
17 #ifdef HAVE_UNSHARE
18
19 #ifdef HAVE_LIBCAP
20 #include <sys/capability.h>
21 #endif
22
23 #define CHROOT_DIR "chroot"
24 #define NEW_ROOT "/new_root"
25 #define PUT_OLD "/new_root/put_old"
26 #define PUT_OLD_FS "/put_old_fs"
27 #define PUT_OLD_BAD "/put_old_fs/put_old"
28
29 enum {
30 /*
31 * Test consists of a series of steps that allow pivot_root to succeed,
32 * which is run when param is NORMAL. All other values tweak one of the
33 * steps to induce a failure, and check the errno is as expected.
34 */
35 NORMAL,
36
37 /*
38 * EBUSY
39 * new_root or put_old are on the current root file system
40 */
41 NEW_ROOT_ON_CURRENT_ROOT,
42
43 /*
44 * EINVAL
45 * put_old is not underneath new_root
46 * Note: if put_old and new_root are on the same fs,
47 * pivot_root fails with EBUSY before testing reachability
48 */
49 PUT_OLD_NOT_UNDERNEATH_NEW_ROOT,
50
51 /*
52 * ENOTDIR
53 * new_root or put_old is not a directory
54 */
55 PUT_OLD_NOT_DIR,
56
57 /*
58 * EPERM
59 * The calling process does not have the CAP_SYS_ADMIN capability.
60 */
61 NO_CAP_SYS_ADMIN,
62 };
63
64 static const struct test_case {
65 int test_case;
66 int expected_error;
67 } test_cases[] = {
68 {NORMAL, 0},
69 {NEW_ROOT_ON_CURRENT_ROOT, EBUSY},
70 {PUT_OLD_NOT_UNDERNEATH_NEW_ROOT, EINVAL},
71 {PUT_OLD_NOT_DIR, ENOTDIR},
72 {NO_CAP_SYS_ADMIN, EPERM},
73 };
74
75 #ifdef HAVE_LIBCAP
drop_cap_sys_admin(void)76 static void drop_cap_sys_admin(void)
77 {
78 cap_value_t cap_value[] = { CAP_SYS_ADMIN };
79 cap_t cap = cap_get_proc();
80 if (!cap)
81 tst_brk(TBROK | TERRNO, "cap_get_proc failed");
82
83 if (cap_set_flag(cap, CAP_EFFECTIVE, 1, cap_value, CAP_CLEAR))
84 tst_brk(TBROK | TERRNO, "cap_set_flag failed");
85
86 if (cap_set_proc(cap))
87 tst_brk(TBROK | TERRNO, "cap_set_proc failed");
88 }
89 #endif
90
run(unsigned int test_case)91 static void run(unsigned int test_case)
92 {
93 /* Work in child process - needed to undo unshare and chroot */
94 if (SAFE_FORK()) {
95 tst_reap_children();
96 return;
97 }
98
99 /* pivot_root requires no shared mounts exist in process namespace */
100 TEST(unshare(CLONE_NEWNS | CLONE_FS));
101 if (TST_RET == -1)
102 tst_brk(TFAIL | TTERRNO, "unshare failed");
103
104 /*
105 * Create an initial root dir. pivot_root doesn't work if the initial root
106 * dir is a initramfs, so use chroot to create a safe environment
107 */
108 SAFE_MOUNT("none", "/", NULL, MS_REC|MS_PRIVATE, NULL);
109 SAFE_MOUNT("none", CHROOT_DIR, "tmpfs", 0, 0);
110 SAFE_CHROOT(CHROOT_DIR);
111
112 SAFE_MKDIR(NEW_ROOT, 0777);
113
114 /*
115 * pivot_root only works if new_root is a mount point, so mount a tmpfs
116 * unless testing for that fail mode
117 */
118 if (test_cases[test_case].test_case != NEW_ROOT_ON_CURRENT_ROOT)
119 SAFE_MOUNT("none", NEW_ROOT, "tmpfs", 0, 0);
120
121 /*
122 * Create put_old under new_root, unless testing for that specific fail
123 * mode
124 */
125 const char* actual_put_old = NULL;
126 if (test_cases[test_case].test_case == PUT_OLD_NOT_UNDERNEATH_NEW_ROOT) {
127 actual_put_old = PUT_OLD_BAD;
128 SAFE_MKDIR(PUT_OLD_FS, 0777);
129 SAFE_MOUNT("none", PUT_OLD_FS, "tmpfs", 0, 0);
130 SAFE_MKDIR(PUT_OLD_BAD, 0777);
131 } else {
132 actual_put_old = PUT_OLD;
133
134 if (test_cases[test_case].test_case == PUT_OLD_NOT_DIR)
135 SAFE_CREAT(PUT_OLD, 0777);
136 else
137 SAFE_MKDIR(PUT_OLD, 0777);
138 }
139
140 if (test_cases[test_case].test_case == NO_CAP_SYS_ADMIN) {
141 #ifdef HAVE_LIBCAP
142 drop_cap_sys_admin();
143 #else
144 tst_res(TCONF,
145 "System doesn't have POSIX capabilities support");
146 return;
147 #endif
148 }
149
150 TEST(syscall(__NR_pivot_root, NEW_ROOT, actual_put_old));
151
152 if (test_cases[test_case].test_case == NORMAL) {
153 if (TST_RET)
154 tst_res(TFAIL | TTERRNO, "pivot_root failed");
155 else
156 tst_res(TPASS, "pivot_root succeeded");
157
158 return;
159 }
160
161 if (TST_RET == 0) {
162 tst_res(TFAIL, "pivot_root succeeded unexpectedly");
163 return;
164 }
165
166 if (errno != test_cases[test_case].expected_error) {
167 tst_res(TFAIL | TERRNO, "pivot_root failed with wrong errno");
168 return;
169 }
170
171 tst_res(TPASS | TERRNO, "pivot_root failed as expectedly");
172 }
173
setup(void)174 static void setup(void)
175 {
176 SAFE_MKDIR(CHROOT_DIR, 0777);
177 }
178
179 static struct tst_test test = {
180 .test = run,
181 .tcnt = ARRAY_SIZE(test_cases),
182 .needs_tmpdir = 1,
183 .needs_root = 1,
184 .forks_child = 1,
185 .setup = setup,
186 };
187
188 #else
189 TST_TEST_TCONF("unshare is undefined.");
190 #endif
191