1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/capability.h>
13 #include <sys/mount.h>
18 #include <sys/prctl.h>
21 #include "../kselftest.h"
23 #ifndef PR_CAP_AMBIENT
24 #define PR_CAP_AMBIENT 47
25 # define PR_CAP_AMBIENT_IS_SET 1
26 # define PR_CAP_AMBIENT_RAISE 2
27 # define PR_CAP_AMBIENT_LOWER 3
28 # define PR_CAP_AMBIENT_CLEAR_ALL 4
32 static pid_t mpid; /* main() pid is used to avoid duplicate test counts */
34 static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
41 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
43 ksft_exit_fail_msg("vsnprintf failed - %s\n", strerror(errno));
45 if (buf_len >= sizeof(buf))
46 ksft_exit_fail_msg("vsnprintf output truncated\n");
49 fd = open(filename, O_WRONLY);
51 if ((errno == ENOENT) && enoent_ok)
53 ksft_exit_fail_msg("open of %s failed - %s\n",
54 filename, strerror(errno));
56 written = write(fd, buf, buf_len);
57 if (written != buf_len) {
59 ksft_exit_fail_msg("short write to %s\n", filename);
61 ksft_exit_fail_msg("write to %s failed - %s\n",
62 filename, strerror(errno));
66 ksft_exit_fail_msg("close of %s failed - %s\n",
67 filename, strerror(errno));
71 static void maybe_write_file(char *filename, char *fmt, ...)
76 vmaybe_write_file(true, filename, fmt, ap);
80 static void write_file(char *filename, char *fmt, ...)
85 vmaybe_write_file(false, filename, fmt, ap);
89 static bool create_and_enter_ns(uid_t inner_uid)
94 bool have_outer_privilege;
100 * TODO: If we're already root, we could skip creating the userns.
103 if (unshare(CLONE_NEWNS) == 0) {
104 ksft_print_msg("[NOTE]\tUsing global UIDs for tests\n");
105 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0)
106 ksft_exit_fail_msg("PR_SET_KEEPCAPS - %s\n",
108 if (setresuid(inner_uid, inner_uid, -1) != 0)
109 ksft_exit_fail_msg("setresuid - %s\n", strerror(errno));
111 // Re-enable effective caps
112 capng_get_caps_process();
113 for (i = 0; i < CAP_LAST_CAP; i++)
114 if (capng_have_capability(CAPNG_PERMITTED, i))
115 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i);
116 if (capng_apply(CAPNG_SELECT_CAPS) != 0)
118 "capng_apply - %s\n", strerror(errno));
120 have_outer_privilege = true;
121 } else if (unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) {
122 ksft_print_msg("[NOTE]\tUsing a user namespace for tests\n");
123 maybe_write_file("/proc/self/setgroups", "deny");
124 write_file("/proc/self/uid_map", "%d %d 1", inner_uid, outer_uid);
125 write_file("/proc/self/gid_map", "0 %d 1", outer_gid);
127 have_outer_privilege = false;
129 ksft_exit_skip("must be root or be able to create a userns\n");
132 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)
133 ksft_exit_fail_msg("remount everything private - %s\n",
136 return have_outer_privilege;
139 static void chdir_to_tmpfs(void)
142 if (getcwd(cwd, sizeof(cwd)) != cwd)
143 ksft_exit_fail_msg("getcwd - %s\n", strerror(errno));
145 if (mount("private_tmp", ".", "tmpfs", 0, "mode=0777") != 0)
146 ksft_exit_fail_msg("mount private tmpfs - %s\n",
150 ksft_exit_fail_msg("chdir to private tmpfs - %s\n",
154 static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)
156 int from = openat(fromfd, fromname, O_RDONLY);
158 ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));
160 int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);
164 ssize_t sz = read(from, buf, sizeof(buf));
168 ksft_exit_fail_msg("read - %s\n", strerror(errno));
170 if (write(to, buf, sz) != sz)
171 /* no short writes on tmpfs */
172 ksft_exit_fail_msg("write - %s\n", strerror(errno));
179 static bool fork_wait(void)
181 pid_t child = fork();
185 } else if (child > 0) {
187 if (waitpid(child, &status, 0) != child ||
188 !WIFEXITED(status)) {
189 ksft_print_msg("Child died\n");
191 } else if (WEXITSTATUS(status) != 0) {
192 ksft_print_msg("Child failed\n");
195 /* don't print this message for mpid */
196 if (getpid() != mpid)
197 ksft_test_result_pass("Passed\n");
201 ksft_exit_fail_msg("fork - %s\n", strerror(errno));
206 static void exec_other_validate_cap(const char *name,
207 bool eff, bool perm, bool inh, bool ambient)
209 execl(name, name, (eff ? "1" : "0"),
210 (perm ? "1" : "0"), (inh ? "1" : "0"), (ambient ? "1" : "0"),
212 ksft_exit_fail_msg("execl - %s\n", strerror(errno));
215 static void exec_validate_cap(bool eff, bool perm, bool inh, bool ambient)
217 exec_other_validate_cap("./validate_cap", eff, perm, inh, ambient);
220 static int do_tests(int uid, const char *our_path)
222 bool have_outer_privilege = create_and_enter_ns(uid);
224 int ourpath_fd = open(our_path, O_RDONLY | O_DIRECTORY);
225 if (ourpath_fd == -1)
226 ksft_exit_fail_msg("open '%s' - %s\n",
227 our_path, strerror(errno));
231 copy_fromat_to(ourpath_fd, "validate_cap", "validate_cap");
233 if (have_outer_privilege) {
234 uid_t gid = getegid();
236 copy_fromat_to(ourpath_fd, "validate_cap",
237 "validate_cap_suidroot");
238 if (chown("validate_cap_suidroot", 0, -1) != 0)
239 ksft_exit_fail_msg("chown - %s\n", strerror(errno));
240 if (chmod("validate_cap_suidroot", S_ISUID | 0700) != 0)
241 ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
243 copy_fromat_to(ourpath_fd, "validate_cap",
244 "validate_cap_suidnonroot");
245 if (chown("validate_cap_suidnonroot", uid + 1, -1) != 0)
246 ksft_exit_fail_msg("chown - %s\n", strerror(errno));
247 if (chmod("validate_cap_suidnonroot", S_ISUID | 0700) != 0)
248 ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
250 copy_fromat_to(ourpath_fd, "validate_cap",
251 "validate_cap_sgidroot");
252 if (chown("validate_cap_sgidroot", -1, 0) != 0)
253 ksft_exit_fail_msg("chown - %s\n", strerror(errno));
254 if (chmod("validate_cap_sgidroot", S_ISGID | 0710) != 0)
255 ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
257 copy_fromat_to(ourpath_fd, "validate_cap",
258 "validate_cap_sgidnonroot");
259 if (chown("validate_cap_sgidnonroot", -1, gid + 1) != 0)
260 ksft_exit_fail_msg("chown - %s\n", strerror(errno));
261 if (chmod("validate_cap_sgidnonroot", S_ISGID | 0710) != 0)
262 ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
265 capng_get_caps_process();
267 /* Make sure that i starts out clear */
268 capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
269 if (capng_apply(CAPNG_SELECT_CAPS) != 0)
270 ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
273 ksft_print_msg("[RUN]\tRoot => ep\n");
275 exec_validate_cap(true, true, false, false);
277 ksft_print_msg("[RUN]\tNon-root => no caps\n");
279 exec_validate_cap(false, false, false, false);
282 ksft_print_msg("Check cap_ambient manipulation rules\n");
284 /* We should not be able to add ambient caps yet. */
285 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != -1 || errno != EPERM) {
287 ksft_test_result_fail(
288 "PR_CAP_AMBIENT_RAISE isn't supported\n");
290 ksft_test_result_fail(
291 "PR_CAP_AMBIENT_RAISE should have failed eith EPERM on a non-inheritable cap\n");
294 ksft_test_result_pass(
295 "PR_CAP_AMBIENT_RAISE failed on non-inheritable cap\n");
297 capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_RAW);
298 capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW);
299 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW);
300 if (capng_apply(CAPNG_SELECT_CAPS) != 0)
301 ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
302 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) {
303 ksft_test_result_fail(
304 "PR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n");
307 ksft_test_result_pass(
308 "PR_CAP_AMBIENT_RAISE failed on non-permitted cap\n");
310 capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
311 if (capng_apply(CAPNG_SELECT_CAPS) != 0)
312 ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
313 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
314 ksft_test_result_fail(
315 "PR_CAP_AMBIENT_RAISE should have succeeded\n");
318 ksft_test_result_pass("PR_CAP_AMBIENT_RAISE worked\n");
320 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 1) {
321 ksft_test_result_fail("PR_CAP_AMBIENT_IS_SET is broken\n");
325 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0) != 0)
326 ksft_exit_fail_msg("PR_CAP_AMBIENT_CLEAR_ALL - %s\n",
329 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
330 ksft_test_result_fail(
331 "PR_CAP_AMBIENT_CLEAR_ALL didn't work\n");
335 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
336 ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n",
339 capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
340 if (capng_apply(CAPNG_SELECT_CAPS) != 0)
341 ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
343 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
344 ksft_test_result_fail("Dropping I should have dropped A\n");
348 ksft_test_result_pass("Basic manipulation appears to work\n");
350 capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
351 if (capng_apply(CAPNG_SELECT_CAPS) != 0)
352 ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
354 ksft_print_msg("[RUN]\tRoot +i => eip\n");
356 exec_validate_cap(true, true, true, false);
358 ksft_print_msg("[RUN]\tNon-root +i => i\n");
360 exec_validate_cap(false, false, true, false);
363 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
364 ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n",
367 ksft_print_msg("[RUN]\tUID %d +ia => eipa\n", uid);
369 exec_validate_cap(true, true, true, true);
371 /* The remaining tests need real privilege */
373 if (!have_outer_privilege) {
374 ksft_test_result_skip("SUID/SGID tests (needs privilege)\n");
379 ksft_print_msg("[RUN]\tRoot +ia, suidroot => eipa\n");
381 exec_other_validate_cap("./validate_cap_suidroot",
382 true, true, true, true);
384 ksft_print_msg("[RUN]\tRoot +ia, suidnonroot => ip\n");
386 exec_other_validate_cap("./validate_cap_suidnonroot",
387 false, true, true, false);
389 ksft_print_msg("[RUN]\tRoot +ia, sgidroot => eipa\n");
391 exec_other_validate_cap("./validate_cap_sgidroot",
392 true, true, true, true);
396 "[RUN]\tRoot, gid != 0, +ia, sgidroot => eip\n");
397 if (setresgid(1, 1, 1) != 0)
398 ksft_exit_fail_msg("setresgid - %s\n",
400 exec_other_validate_cap("./validate_cap_sgidroot",
401 true, true, true, false);
404 ksft_print_msg("[RUN]\tRoot +ia, sgidnonroot => eip\n");
406 exec_other_validate_cap("./validate_cap_sgidnonroot",
407 true, true, true, false);
409 ksft_print_msg("[RUN]\tNon-root +ia, sgidnonroot => i\n");
411 exec_other_validate_cap("./validate_cap_sgidnonroot",
412 false, false, true, false);
415 ksft_print_msg("[RUN]\tNon-root +ia, sgidroot => i\n");
416 if (setresgid(1, 1, 1) != 0)
417 ksft_exit_fail_msg("setresgid - %s\n",
419 exec_other_validate_cap("./validate_cap_sgidroot",
420 false, false, true, false);
426 return nerrs ? 1 : 0;
429 int main(int argc, char **argv)
431 char *tmp1, *tmp2, *our_path;
436 tmp1 = strdup(argv[0]);
438 ksft_exit_fail_msg("strdup - %s\n", strerror(errno));
439 tmp2 = dirname(tmp1);
440 our_path = strdup(tmp2);
442 ksft_exit_fail_msg("strdup - %s\n", strerror(errno));
448 ksft_print_msg("[RUN]\t+++ Tests with uid == 0 +++\n");
449 return do_tests(0, our_path);
452 ksft_print_msg("==================================================\n");
455 ksft_print_msg("[RUN]\t+++ Tests with uid != 0 +++\n");
456 return do_tests(1, our_path);
459 return nerrs ? 1 : 0;