Path: blob/master/tools/testing/selftests/capabilities/test_execve.c
26285 views
// SPDX-License-Identifier: GPL-2.01#define _GNU_SOURCE23#include <cap-ng.h>4#include <linux/capability.h>5#include <stdbool.h>6#include <string.h>7#include <stdio.h>8#include <fcntl.h>9#include <errno.h>10#include <stdarg.h>11#include <sched.h>12#include <sys/mount.h>13#include <limits.h>14#include <libgen.h>15#include <malloc.h>16#include <sys/wait.h>17#include <sys/prctl.h>18#include <sys/stat.h>1920#include "../kselftest.h"2122static int nerrs;23static pid_t mpid; /* main() pid is used to avoid duplicate test counts */2425static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)26{27char buf[4096];28int fd;29ssize_t written;30int buf_len;3132buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);33if (buf_len < 0)34ksft_exit_fail_msg("vsnprintf failed - %s\n", strerror(errno));3536if (buf_len >= sizeof(buf))37ksft_exit_fail_msg("vsnprintf output truncated\n");383940fd = open(filename, O_WRONLY);41if (fd < 0) {42if ((errno == ENOENT) && enoent_ok)43return;44ksft_exit_fail_msg("open of %s failed - %s\n",45filename, strerror(errno));46}47written = write(fd, buf, buf_len);48if (written != buf_len) {49if (written >= 0) {50ksft_exit_fail_msg("short write to %s\n", filename);51} else {52ksft_exit_fail_msg("write to %s failed - %s\n",53filename, strerror(errno));54}55}56if (close(fd) != 0) {57ksft_exit_fail_msg("close of %s failed - %s\n",58filename, strerror(errno));59}60}6162static void maybe_write_file(char *filename, char *fmt, ...)63{64va_list ap;6566va_start(ap, fmt);67vmaybe_write_file(true, filename, fmt, ap);68va_end(ap);69}7071static void write_file(char *filename, char *fmt, ...)72{73va_list ap;7475va_start(ap, fmt);76vmaybe_write_file(false, filename, fmt, ap);77va_end(ap);78}7980static bool create_and_enter_ns(uid_t inner_uid)81{82uid_t outer_uid;83gid_t outer_gid;84int i, ret;85bool have_outer_privilege;8687outer_uid = getuid();88outer_gid = getgid();8990if (outer_uid == 0 && unshare(CLONE_NEWNS) == 0) {91ksft_print_msg("[NOTE]\tUsing global UIDs for tests\n");92if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0)93ksft_exit_fail_msg("PR_SET_KEEPCAPS - %s\n",94strerror(errno));95if (setresuid(inner_uid, inner_uid, -1) != 0)96ksft_exit_fail_msg("setresuid - %s\n", strerror(errno));9798// Re-enable effective caps99ret = capng_get_caps_process();100if (ret == -1)101ksft_exit_fail_msg("capng_get_caps_process failed\n");102103for (i = 0; i < CAP_LAST_CAP; i++)104if (capng_have_capability(CAPNG_PERMITTED, i))105capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i);106if (capng_apply(CAPNG_SELECT_CAPS) != 0)107ksft_exit_fail_msg(108"capng_apply - %s\n", strerror(errno));109110have_outer_privilege = true;111} else if (unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) {112ksft_print_msg("[NOTE]\tUsing a user namespace for tests\n");113maybe_write_file("/proc/self/setgroups", "deny");114write_file("/proc/self/uid_map", "%d %d 1", inner_uid, outer_uid);115write_file("/proc/self/gid_map", "0 %d 1", outer_gid);116117have_outer_privilege = false;118} else {119ksft_exit_skip("must be root or be able to create a userns\n");120}121122if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)123ksft_exit_fail_msg("remount everything private - %s\n",124strerror(errno));125126return have_outer_privilege;127}128129static void chdir_to_tmpfs(void)130{131char cwd[PATH_MAX];132if (getcwd(cwd, sizeof(cwd)) != cwd)133ksft_exit_fail_msg("getcwd - %s\n", strerror(errno));134135if (mount("private_tmp", ".", "tmpfs", 0, "mode=0777") != 0)136ksft_exit_fail_msg("mount private tmpfs - %s\n",137strerror(errno));138139if (chdir(cwd) != 0)140ksft_exit_fail_msg("chdir to private tmpfs - %s\n",141strerror(errno));142}143144static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)145{146int from = openat(fromfd, fromname, O_RDONLY);147if (from == -1)148ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));149150int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);151152while (true) {153char buf[4096];154ssize_t sz = read(from, buf, sizeof(buf));155if (sz == 0)156break;157if (sz < 0)158ksft_exit_fail_msg("read - %s\n", strerror(errno));159160if (write(to, buf, sz) != sz)161/* no short writes on tmpfs */162ksft_exit_fail_msg("write - %s\n", strerror(errno));163}164165close(from);166close(to);167}168169static bool fork_wait(void)170{171pid_t child = fork();172if (child == 0) {173nerrs = 0;174return true;175} else if (child > 0) {176int status;177if (waitpid(child, &status, 0) != child ||178!WIFEXITED(status)) {179ksft_print_msg("Child died\n");180nerrs++;181} else if (WEXITSTATUS(status) != 0) {182ksft_print_msg("Child failed\n");183nerrs++;184} else {185/* don't print this message for mpid */186if (getpid() != mpid)187ksft_test_result_pass("Passed\n");188}189return false;190} else {191ksft_exit_fail_msg("fork - %s\n", strerror(errno));192return false;193}194}195196static void exec_other_validate_cap(const char *name,197bool eff, bool perm, bool inh, bool ambient)198{199execl(name, name, (eff ? "1" : "0"),200(perm ? "1" : "0"), (inh ? "1" : "0"), (ambient ? "1" : "0"),201NULL);202ksft_exit_fail_msg("execl - %s\n", strerror(errno));203}204205static void exec_validate_cap(bool eff, bool perm, bool inh, bool ambient)206{207exec_other_validate_cap("./validate_cap", eff, perm, inh, ambient);208}209210static int do_tests(int uid, const char *our_path)211{212int ret;213bool have_outer_privilege = create_and_enter_ns(uid);214215int ourpath_fd = open(our_path, O_RDONLY | O_DIRECTORY);216if (ourpath_fd == -1)217ksft_exit_fail_msg("open '%s' - %s\n",218our_path, strerror(errno));219220chdir_to_tmpfs();221222copy_fromat_to(ourpath_fd, "validate_cap", "validate_cap");223224if (have_outer_privilege) {225uid_t gid = getegid();226227copy_fromat_to(ourpath_fd, "validate_cap",228"validate_cap_suidroot");229if (chown("validate_cap_suidroot", 0, -1) != 0)230ksft_exit_fail_msg("chown - %s\n", strerror(errno));231if (chmod("validate_cap_suidroot", S_ISUID | 0700) != 0)232ksft_exit_fail_msg("chmod - %s\n", strerror(errno));233234copy_fromat_to(ourpath_fd, "validate_cap",235"validate_cap_suidnonroot");236if (chown("validate_cap_suidnonroot", uid + 1, -1) != 0)237ksft_exit_fail_msg("chown - %s\n", strerror(errno));238if (chmod("validate_cap_suidnonroot", S_ISUID | 0700) != 0)239ksft_exit_fail_msg("chmod - %s\n", strerror(errno));240241copy_fromat_to(ourpath_fd, "validate_cap",242"validate_cap_sgidroot");243if (chown("validate_cap_sgidroot", -1, 0) != 0)244ksft_exit_fail_msg("chown - %s\n", strerror(errno));245if (chmod("validate_cap_sgidroot", S_ISGID | 0710) != 0)246ksft_exit_fail_msg("chmod - %s\n", strerror(errno));247248copy_fromat_to(ourpath_fd, "validate_cap",249"validate_cap_sgidnonroot");250if (chown("validate_cap_sgidnonroot", -1, gid + 1) != 0)251ksft_exit_fail_msg("chown - %s\n", strerror(errno));252if (chmod("validate_cap_sgidnonroot", S_ISGID | 0710) != 0)253ksft_exit_fail_msg("chmod - %s\n", strerror(errno));254}255256ret = capng_get_caps_process();257if (ret == -1)258ksft_exit_fail_msg("capng_get_caps_process failed\n");259260/* Make sure that i starts out clear */261capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);262if (capng_apply(CAPNG_SELECT_CAPS) != 0)263ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));264265if (uid == 0) {266ksft_print_msg("[RUN]\tRoot => ep\n");267if (fork_wait())268exec_validate_cap(true, true, false, false);269} else {270ksft_print_msg("[RUN]\tNon-root => no caps\n");271if (fork_wait())272exec_validate_cap(false, false, false, false);273}274275ksft_print_msg("Check cap_ambient manipulation rules\n");276277/* We should not be able to add ambient caps yet. */278if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != -1 || errno != EPERM) {279if (errno == EINVAL)280ksft_test_result_fail(281"PR_CAP_AMBIENT_RAISE isn't supported\n");282else283ksft_test_result_fail(284"PR_CAP_AMBIENT_RAISE should have failed eith EPERM on a non-inheritable cap\n");285return 1;286}287ksft_test_result_pass(288"PR_CAP_AMBIENT_RAISE failed on non-inheritable cap\n");289290capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_RAW);291capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW);292capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW);293if (capng_apply(CAPNG_SELECT_CAPS) != 0)294ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));295if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) {296ksft_test_result_fail(297"PR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n");298return 1;299}300ksft_test_result_pass(301"PR_CAP_AMBIENT_RAISE failed on non-permitted cap\n");302303capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);304if (capng_apply(CAPNG_SELECT_CAPS) != 0)305ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));306if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {307ksft_test_result_fail(308"PR_CAP_AMBIENT_RAISE should have succeeded\n");309return 1;310}311ksft_test_result_pass("PR_CAP_AMBIENT_RAISE worked\n");312313if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 1) {314ksft_test_result_fail("PR_CAP_AMBIENT_IS_SET is broken\n");315return 1;316}317318if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0) != 0)319ksft_exit_fail_msg("PR_CAP_AMBIENT_CLEAR_ALL - %s\n",320strerror(errno));321322if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {323ksft_test_result_fail(324"PR_CAP_AMBIENT_CLEAR_ALL didn't work\n");325return 1;326}327328if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)329ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n",330strerror(errno));331332capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);333if (capng_apply(CAPNG_SELECT_CAPS) != 0)334ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));335336if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {337ksft_test_result_fail("Dropping I should have dropped A\n");338return 1;339}340341ksft_test_result_pass("Basic manipulation appears to work\n");342343capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);344if (capng_apply(CAPNG_SELECT_CAPS) != 0)345ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno));346if (uid == 0) {347ksft_print_msg("[RUN]\tRoot +i => eip\n");348if (fork_wait())349exec_validate_cap(true, true, true, false);350} else {351ksft_print_msg("[RUN]\tNon-root +i => i\n");352if (fork_wait())353exec_validate_cap(false, false, true, false);354}355356if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)357ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n",358strerror(errno));359360ksft_print_msg("[RUN]\tUID %d +ia => eipa\n", uid);361if (fork_wait())362exec_validate_cap(true, true, true, true);363364/* The remaining tests need real privilege */365366if (!have_outer_privilege) {367ksft_test_result_skip("SUID/SGID tests (needs privilege)\n");368goto done;369}370371if (uid == 0) {372ksft_print_msg("[RUN]\tRoot +ia, suidroot => eipa\n");373if (fork_wait())374exec_other_validate_cap("./validate_cap_suidroot",375true, true, true, true);376377ksft_print_msg("[RUN]\tRoot +ia, suidnonroot => ip\n");378if (fork_wait())379exec_other_validate_cap("./validate_cap_suidnonroot",380false, true, true, false);381382ksft_print_msg("[RUN]\tRoot +ia, sgidroot => eipa\n");383if (fork_wait())384exec_other_validate_cap("./validate_cap_sgidroot",385true, true, true, true);386387if (fork_wait()) {388ksft_print_msg(389"[RUN]\tRoot, gid != 0, +ia, sgidroot => eip\n");390if (setresgid(1, 1, 1) != 0)391ksft_exit_fail_msg("setresgid - %s\n",392strerror(errno));393exec_other_validate_cap("./validate_cap_sgidroot",394true, true, true, false);395}396397ksft_print_msg("[RUN]\tRoot +ia, sgidnonroot => eip\n");398if (fork_wait())399exec_other_validate_cap("./validate_cap_sgidnonroot",400true, true, true, false);401} else {402ksft_print_msg("[RUN]\tNon-root +ia, sgidnonroot => i\n");403if (fork_wait())404exec_other_validate_cap("./validate_cap_sgidnonroot",405false, false, true, false);406407if (fork_wait()) {408ksft_print_msg("[RUN]\tNon-root +ia, sgidroot => i\n");409if (setresgid(1, 1, 1) != 0)410ksft_exit_fail_msg("setresgid - %s\n",411strerror(errno));412exec_other_validate_cap("./validate_cap_sgidroot",413false, false, true, false);414}415}416417done:418ksft_print_cnts();419return nerrs ? 1 : 0;420}421422int main(int argc, char **argv)423{424char *tmp1, *tmp2, *our_path;425426/* Find our path */427tmp1 = strdup(argv[0]);428if (!tmp1)429ksft_exit_fail_msg("strdup - %s\n", strerror(errno));430tmp2 = dirname(tmp1);431our_path = strdup(tmp2);432if (!our_path)433ksft_exit_fail_msg("strdup - %s\n", strerror(errno));434free(tmp1);435436mpid = getpid();437438if (fork_wait()) {439ksft_print_header();440ksft_set_plan(12);441ksft_print_msg("[RUN]\t+++ Tests with uid == 0 +++\n");442return do_tests(0, our_path);443}444445ksft_print_msg("==================================================\n");446447if (fork_wait()) {448ksft_print_header();449ksft_set_plan(9);450ksft_print_msg("[RUN]\t+++ Tests with uid != 0 +++\n");451return do_tests(1, our_path);452}453454return nerrs ? 1 : 0;455}456457458