Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #define _GNU_SOURCE
0003 
0004 #include <cap-ng.h>
0005 #include <linux/capability.h>
0006 #include <stdbool.h>
0007 #include <string.h>
0008 #include <stdio.h>
0009 #include <fcntl.h>
0010 #include <errno.h>
0011 #include <stdarg.h>
0012 #include <sched.h>
0013 #include <sys/mount.h>
0014 #include <limits.h>
0015 #include <libgen.h>
0016 #include <malloc.h>
0017 #include <sys/wait.h>
0018 #include <sys/prctl.h>
0019 #include <sys/stat.h>
0020 
0021 #include "../kselftest.h"
0022 
0023 #ifndef PR_CAP_AMBIENT
0024 #define PR_CAP_AMBIENT          47
0025 # define PR_CAP_AMBIENT_IS_SET      1
0026 # define PR_CAP_AMBIENT_RAISE       2
0027 # define PR_CAP_AMBIENT_LOWER       3
0028 # define PR_CAP_AMBIENT_CLEAR_ALL   4
0029 #endif
0030 
0031 static int nerrs;
0032 static pid_t mpid;  /*  main() pid is used to avoid duplicate test counts */
0033 
0034 static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
0035 {
0036     char buf[4096];
0037     int fd;
0038     ssize_t written;
0039     int buf_len;
0040 
0041     buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
0042     if (buf_len < 0)
0043         ksft_exit_fail_msg("vsnprintf failed - %s\n", strerror(errno));
0044 
0045     if (buf_len >= sizeof(buf))
0046         ksft_exit_fail_msg("vsnprintf output truncated\n");
0047 
0048 
0049     fd = open(filename, O_WRONLY);
0050     if (fd < 0) {
0051         if ((errno == ENOENT) && enoent_ok)
0052             return;
0053         ksft_exit_fail_msg("open of %s failed - %s\n",
0054                     filename, strerror(errno));
0055     }
0056     written = write(fd, buf, buf_len);
0057     if (written != buf_len) {
0058         if (written >= 0) {
0059             ksft_exit_fail_msg("short write to %s\n", filename);
0060         } else {
0061             ksft_exit_fail_msg("write to %s failed - %s\n",
0062                         filename, strerror(errno));
0063         }
0064     }
0065     if (close(fd) != 0) {
0066         ksft_exit_fail_msg("close of %s failed - %s\n",
0067                     filename, strerror(errno));
0068     }
0069 }
0070 
0071 static void maybe_write_file(char *filename, char *fmt, ...)
0072 {
0073     va_list ap;
0074 
0075     va_start(ap, fmt);
0076     vmaybe_write_file(true, filename, fmt, ap);
0077     va_end(ap);
0078 }
0079 
0080 static void write_file(char *filename, char *fmt, ...)
0081 {
0082     va_list ap;
0083 
0084     va_start(ap, fmt);
0085     vmaybe_write_file(false, filename, fmt, ap);
0086     va_end(ap);
0087 }
0088 
0089 static bool create_and_enter_ns(uid_t inner_uid)
0090 {
0091     uid_t outer_uid;
0092     gid_t outer_gid;
0093     int i;
0094     bool have_outer_privilege;
0095 
0096     outer_uid = getuid();
0097     outer_gid = getgid();
0098 
0099     /*
0100      * TODO: If we're already root, we could skip creating the userns.
0101      */
0102 
0103     if (unshare(CLONE_NEWNS) == 0) {
0104         ksft_print_msg("[NOTE]\tUsing global UIDs for tests\n");
0105         if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0)
0106             ksft_exit_fail_msg("PR_SET_KEEPCAPS - %s\n",
0107                         strerror(errno));
0108         if (setresuid(inner_uid, inner_uid, -1) != 0)
0109             ksft_exit_fail_msg("setresuid - %s\n", strerror(errno));
0110 
0111         // Re-enable effective caps
0112         capng_get_caps_process();
0113         for (i = 0; i < CAP_LAST_CAP; i++)
0114             if (capng_have_capability(CAPNG_PERMITTED, i))
0115                 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i);
0116         if (capng_apply(CAPNG_SELECT_CAPS) != 0)
0117             ksft_exit_fail_msg(
0118                     "capng_apply - %s\n", strerror(errno));
0119 
0120         have_outer_privilege = true;
0121     } else if (unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) {
0122         ksft_print_msg("[NOTE]\tUsing a user namespace for tests\n");
0123         maybe_write_file("/proc/self/setgroups", "deny");
0124         write_file("/proc/self/uid_map", "%d %d 1", inner_uid, outer_uid);
0125         write_file("/proc/self/gid_map", "0 %d 1", outer_gid);
0126 
0127         have_outer_privilege = false;
0128     } else {
0129         ksft_exit_skip("must be root or be able to create a userns\n");
0130     }
0131 
0132     if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)
0133         ksft_exit_fail_msg("remount everything private - %s\n",
0134                     strerror(errno));
0135 
0136     return have_outer_privilege;
0137 }
0138 
0139 static void chdir_to_tmpfs(void)
0140 {
0141     char cwd[PATH_MAX];
0142     if (getcwd(cwd, sizeof(cwd)) != cwd)
0143         ksft_exit_fail_msg("getcwd - %s\n", strerror(errno));
0144 
0145     if (mount("private_tmp", ".", "tmpfs", 0, "mode=0777") != 0)
0146         ksft_exit_fail_msg("mount private tmpfs - %s\n",
0147                     strerror(errno));
0148 
0149     if (chdir(cwd) != 0)
0150         ksft_exit_fail_msg("chdir to private tmpfs - %s\n",
0151                     strerror(errno));
0152 }
0153 
0154 static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)
0155 {
0156     int from = openat(fromfd, fromname, O_RDONLY);
0157     if (from == -1)
0158         ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));
0159 
0160     int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);
0161 
0162     while (true) {
0163         char buf[4096];
0164         ssize_t sz = read(from, buf, sizeof(buf));
0165         if (sz == 0)
0166             break;
0167         if (sz < 0)
0168             ksft_exit_fail_msg("read - %s\n", strerror(errno));
0169 
0170         if (write(to, buf, sz) != sz)
0171             /* no short writes on tmpfs */
0172             ksft_exit_fail_msg("write - %s\n", strerror(errno));
0173     }
0174 
0175     close(from);
0176     close(to);
0177 }
0178 
0179 static bool fork_wait(void)
0180 {
0181     pid_t child = fork();
0182     if (child == 0) {
0183         nerrs = 0;
0184         return true;
0185     } else if (child > 0) {
0186         int status;
0187         if (waitpid(child, &status, 0) != child ||
0188             !WIFEXITED(status)) {
0189             ksft_print_msg("Child died\n");
0190             nerrs++;
0191         } else if (WEXITSTATUS(status) != 0) {
0192             ksft_print_msg("Child failed\n");
0193             nerrs++;
0194         } else {
0195             /* don't print this message for mpid */
0196             if (getpid() != mpid)
0197                 ksft_test_result_pass("Passed\n");
0198         }
0199         return false;
0200     } else {
0201         ksft_exit_fail_msg("fork - %s\n", strerror(errno));
0202         return false;
0203     }
0204 }
0205 
0206 static void exec_other_validate_cap(const char *name,
0207                     bool eff, bool perm, bool inh, bool ambient)
0208 {
0209     execl(name, name, (eff ? "1" : "0"),
0210           (perm ? "1" : "0"), (inh ? "1" : "0"), (ambient ? "1" : "0"),
0211           NULL);
0212     ksft_exit_fail_msg("execl - %s\n", strerror(errno));
0213 }
0214 
0215 static void exec_validate_cap(bool eff, bool perm, bool inh, bool ambient)
0216 {
0217     exec_other_validate_cap("./validate_cap", eff, perm, inh, ambient);
0218 }
0219 
0220 static int do_tests(int uid, const char *our_path)
0221 {
0222     bool have_outer_privilege = create_and_enter_ns(uid);
0223 
0224     int ourpath_fd = open(our_path, O_RDONLY | O_DIRECTORY);
0225     if (ourpath_fd == -1)
0226         ksft_exit_fail_msg("open '%s' - %s\n",
0227                     our_path, strerror(errno));
0228 
0229     chdir_to_tmpfs();
0230 
0231     copy_fromat_to(ourpath_fd, "validate_cap", "validate_cap");
0232 
0233     if (have_outer_privilege) {
0234         uid_t gid = getegid();
0235 
0236         copy_fromat_to(ourpath_fd, "validate_cap",
0237                    "validate_cap_suidroot");
0238         if (chown("validate_cap_suidroot", 0, -1) != 0)
0239             ksft_exit_fail_msg("chown - %s\n", strerror(errno));
0240         if (chmod("validate_cap_suidroot", S_ISUID | 0700) != 0)
0241             ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
0242 
0243         copy_fromat_to(ourpath_fd, "validate_cap",
0244                    "validate_cap_suidnonroot");
0245         if (chown("validate_cap_suidnonroot", uid + 1, -1) != 0)
0246             ksft_exit_fail_msg("chown - %s\n", strerror(errno));
0247         if (chmod("validate_cap_suidnonroot", S_ISUID | 0700) != 0)
0248             ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
0249 
0250         copy_fromat_to(ourpath_fd, "validate_cap",
0251                    "validate_cap_sgidroot");
0252         if (chown("validate_cap_sgidroot", -1, 0) != 0)
0253             ksft_exit_fail_msg("chown - %s\n", strerror(errno));
0254         if (chmod("validate_cap_sgidroot", S_ISGID | 0710) != 0)
0255             ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
0256 
0257         copy_fromat_to(ourpath_fd, "validate_cap",
0258                    "validate_cap_sgidnonroot");
0259         if (chown("validate_cap_sgidnonroot", -1, gid + 1) != 0)
0260             ksft_exit_fail_msg("chown - %s\n", strerror(errno));
0261         if (chmod("validate_cap_sgidnonroot", S_ISGID | 0710) != 0)
0262             ksft_exit_fail_msg("chmod - %s\n", strerror(errno));
0263     }
0264 
0265     capng_get_caps_process();
0266 
0267     /* Make sure that i starts out clear */
0268     capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
0269     if (capng_apply(CAPNG_SELECT_CAPS) != 0)
0270         ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
0271 
0272     if (uid == 0) {
0273         ksft_print_msg("[RUN]\tRoot => ep\n");
0274         if (fork_wait())
0275             exec_validate_cap(true, true, false, false);
0276     } else {
0277         ksft_print_msg("[RUN]\tNon-root => no caps\n");
0278         if (fork_wait())
0279             exec_validate_cap(false, false, false, false);
0280     }
0281 
0282     ksft_print_msg("Check cap_ambient manipulation rules\n");
0283 
0284     /* We should not be able to add ambient caps yet. */
0285     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != -1 || errno != EPERM) {
0286         if (errno == EINVAL)
0287             ksft_test_result_fail(
0288                 "PR_CAP_AMBIENT_RAISE isn't supported\n");
0289         else
0290             ksft_test_result_fail(
0291                 "PR_CAP_AMBIENT_RAISE should have failed eith EPERM on a non-inheritable cap\n");
0292         return 1;
0293     }
0294     ksft_test_result_pass(
0295         "PR_CAP_AMBIENT_RAISE failed on non-inheritable cap\n");
0296 
0297     capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_RAW);
0298     capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW);
0299     capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW);
0300     if (capng_apply(CAPNG_SELECT_CAPS) != 0)
0301         ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
0302     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) {
0303         ksft_test_result_fail(
0304             "PR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n");
0305         return 1;
0306     }
0307     ksft_test_result_pass(
0308         "PR_CAP_AMBIENT_RAISE failed on non-permitted cap\n");
0309 
0310     capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
0311     if (capng_apply(CAPNG_SELECT_CAPS) != 0)
0312         ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
0313     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
0314         ksft_test_result_fail(
0315             "PR_CAP_AMBIENT_RAISE should have succeeded\n");
0316         return 1;
0317     }
0318     ksft_test_result_pass("PR_CAP_AMBIENT_RAISE worked\n");
0319 
0320     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 1) {
0321         ksft_test_result_fail("PR_CAP_AMBIENT_IS_SET is broken\n");
0322         return 1;
0323     }
0324 
0325     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0) != 0)
0326         ksft_exit_fail_msg("PR_CAP_AMBIENT_CLEAR_ALL - %s\n",
0327                     strerror(errno));
0328 
0329     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
0330         ksft_test_result_fail(
0331             "PR_CAP_AMBIENT_CLEAR_ALL didn't work\n");
0332         return 1;
0333     }
0334 
0335     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
0336         ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n",
0337                     strerror(errno));
0338 
0339     capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
0340     if (capng_apply(CAPNG_SELECT_CAPS) != 0)
0341         ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
0342 
0343     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
0344         ksft_test_result_fail("Dropping I should have dropped A\n");
0345         return 1;
0346     }
0347 
0348     ksft_test_result_pass("Basic manipulation appears to work\n");
0349 
0350     capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
0351     if (capng_apply(CAPNG_SELECT_CAPS) != 0)
0352         ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));
0353     if (uid == 0) {
0354         ksft_print_msg("[RUN]\tRoot +i => eip\n");
0355         if (fork_wait())
0356             exec_validate_cap(true, true, true, false);
0357     } else {
0358         ksft_print_msg("[RUN]\tNon-root +i => i\n");
0359         if (fork_wait())
0360             exec_validate_cap(false, false, true, false);
0361     }
0362 
0363     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
0364         ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n",
0365                     strerror(errno));
0366 
0367     ksft_print_msg("[RUN]\tUID %d +ia => eipa\n", uid);
0368     if (fork_wait())
0369         exec_validate_cap(true, true, true, true);
0370 
0371     /* The remaining tests need real privilege */
0372 
0373     if (!have_outer_privilege) {
0374         ksft_test_result_skip("SUID/SGID tests (needs privilege)\n");
0375         goto done;
0376     }
0377 
0378     if (uid == 0) {
0379         ksft_print_msg("[RUN]\tRoot +ia, suidroot => eipa\n");
0380         if (fork_wait())
0381             exec_other_validate_cap("./validate_cap_suidroot",
0382                         true, true, true, true);
0383 
0384         ksft_print_msg("[RUN]\tRoot +ia, suidnonroot => ip\n");
0385         if (fork_wait())
0386             exec_other_validate_cap("./validate_cap_suidnonroot",
0387                         false, true, true, false);
0388 
0389         ksft_print_msg("[RUN]\tRoot +ia, sgidroot => eipa\n");
0390         if (fork_wait())
0391             exec_other_validate_cap("./validate_cap_sgidroot",
0392                         true, true, true, true);
0393 
0394         if (fork_wait()) {
0395             ksft_print_msg(
0396                 "[RUN]\tRoot, gid != 0, +ia, sgidroot => eip\n");
0397             if (setresgid(1, 1, 1) != 0)
0398                 ksft_exit_fail_msg("setresgid - %s\n",
0399                             strerror(errno));
0400             exec_other_validate_cap("./validate_cap_sgidroot",
0401                         true, true, true, false);
0402         }
0403 
0404         ksft_print_msg("[RUN]\tRoot +ia, sgidnonroot => eip\n");
0405         if (fork_wait())
0406             exec_other_validate_cap("./validate_cap_sgidnonroot",
0407                         true, true, true, false);
0408     } else {
0409         ksft_print_msg("[RUN]\tNon-root +ia, sgidnonroot => i\n");
0410         if (fork_wait())
0411             exec_other_validate_cap("./validate_cap_sgidnonroot",
0412                     false, false, true, false);
0413 
0414         if (fork_wait()) {
0415             ksft_print_msg("[RUN]\tNon-root +ia, sgidroot => i\n");
0416             if (setresgid(1, 1, 1) != 0)
0417                 ksft_exit_fail_msg("setresgid - %s\n",
0418                             strerror(errno));
0419             exec_other_validate_cap("./validate_cap_sgidroot",
0420                         false, false, true, false);
0421         }
0422     }
0423 
0424 done:
0425     ksft_print_cnts();
0426     return nerrs ? 1 : 0;
0427 }
0428 
0429 int main(int argc, char **argv)
0430 {
0431     char *tmp1, *tmp2, *our_path;
0432 
0433     /* Find our path */
0434     tmp1 = strdup(argv[0]);
0435     if (!tmp1)
0436         ksft_exit_fail_msg("strdup - %s\n", strerror(errno));
0437     tmp2 = dirname(tmp1);
0438     our_path = strdup(tmp2);
0439     if (!our_path)
0440         ksft_exit_fail_msg("strdup - %s\n", strerror(errno));
0441     free(tmp1);
0442 
0443     mpid = getpid();
0444 
0445     if (fork_wait()) {
0446         ksft_print_header();
0447         ksft_set_plan(12);
0448         ksft_print_msg("[RUN]\t+++ Tests with uid == 0 +++\n");
0449         return do_tests(0, our_path);
0450     }
0451 
0452     ksft_print_msg("==================================================\n");
0453 
0454     if (fork_wait()) {
0455         ksft_print_header();
0456         ksft_set_plan(9);
0457         ksft_print_msg("[RUN]\t+++ Tests with uid != 0 +++\n");
0458         return do_tests(1, our_path);
0459     }
0460 
0461     return nerrs ? 1 : 0;
0462 }