/* * Copyright (c) 2008-11,16 Andrew G. Morgan * * This is a simple 'bash' wrapper program that can be used to * raise and lower both the bset and pI capabilities before invoking * /bin/bash (hardcoded right now). * * The --print option can be used as a quick test whether various * capability manipulations work as expected (or not). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef SHELL #define SHELL "/bin/sh" #endif #define MAX_GROUPS 100 /* max number of supplementary groups for user */ static char *binary(unsigned long value) { static char string[8*sizeof(unsigned long) + 1]; unsigned i; i = sizeof(string); string[--i] = '\0'; do { string[--i] = (value & 1) ? '1' : '0'; value >>= 1; } while ((i > 0) && value); return string + i; } static void display_prctl_set(const char *name, int (*fn)(cap_value_t)) { unsigned cap; const char *sep; int set; printf("%s set =", name); for (sep = "", cap=0; (set = fn(cap)) >= 0; cap++) { char *ptr; if (!set) { continue; } ptr = cap_to_name(cap); if (ptr == NULL) { printf("%s%u", sep, cap); } else { printf("%s%s", sep, ptr); cap_free(ptr); } sep = ","; } if (!cap) { printf(" \n"); } else { printf("\n"); } } /* arg_print displays the current capability state of the process */ static void arg_print(void) { int set, status, j; cap_t all; char *text; const char *sep; struct group *g; gid_t groups[MAX_GROUPS], gid; uid_t uid; struct passwd *u; all = cap_get_proc(); text = cap_to_text(all, NULL); printf("Current: %s\n", text); cap_free(text); cap_free(all); display_prctl_set("Bounding", cap_get_bound); display_prctl_set("Ambient", cap_get_ambient); set = prctl(PR_GET_SECUREBITS); if (set >= 0) { const char *b; b = binary(set); /* use verilog convention for binary string */ printf("Securebits: 0%o/0x%x/%u'b%s\n", set, set, (unsigned) strlen(b), b); printf(" secure-noroot: %s (%s)\n", (set & SECBIT_NOROOT) ? "yes":"no", (set & SECBIT_NOROOT_LOCKED) ? "locked":"unlocked"); printf(" secure-no-suid-fixup: %s (%s)\n", (set & SECBIT_NO_SETUID_FIXUP) ? "yes":"no", (set & SECBIT_NO_SETUID_FIXUP_LOCKED) ? "locked":"unlocked"); printf(" secure-keep-caps: %s (%s)\n", (set & SECBIT_KEEP_CAPS) ? "yes":"no", (set & SECBIT_KEEP_CAPS_LOCKED) ? "locked":"unlocked"); if (CAP_AMBIENT_SUPPORTED()) { printf(" secure-no-ambient-raise: %s (%s)\n", (set & SECBIT_NO_CAP_AMBIENT_RAISE) ? "yes":"no", (set & SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED) ? "locked":"unlocked"); } } else { printf("[Securebits ABI not supported]\n"); set = prctl(PR_GET_KEEPCAPS); if (set >= 0) { printf(" prctl-keep-caps: %s (locking not supported)\n", set ? "yes":"no"); } else { printf("[Keepcaps ABI not supported]\n"); } } uid = getuid(); u = getpwuid(uid); printf("uid=%u(%s)\n", getuid(), u ? u->pw_name : "???"); gid = getgid(); g = getgrgid(gid); printf("gid=%u(%s)\n", gid, g ? g->gr_name : "???"); printf("groups="); status = getgroups(MAX_GROUPS, groups); sep = ""; for (j=0; j < status; j++) { g = getgrgid(groups[j]); printf("%s%u(%s)", sep, groups[j], g ? g->gr_name : "???"); sep = ","; } printf("\n"); } static const cap_value_t raise_setpcap[1] = { CAP_SETPCAP }; static const cap_value_t raise_chroot[1] = { CAP_SYS_CHROOT }; static void push_pcap(cap_t *orig_p, cap_t *raised_for_setpcap_p) { /* * We need to do this here because --inh=XXX may have reset * orig and it isn't until we are within the --drop code that * we know what the prevailing (orig) pI value is. */ *orig_p = cap_get_proc(); if (NULL == *orig_p) { perror("Capabilities not available"); exit(1); } *raised_for_setpcap_p = cap_dup(*orig_p); if (NULL == *raised_for_setpcap_p) { fprintf(stderr, "modification requires CAP_SETPCAP\n"); exit(1); } if (cap_set_flag(*raised_for_setpcap_p, CAP_EFFECTIVE, 1, raise_setpcap, CAP_SET) != 0) { perror("unable to select CAP_SETPCAP"); exit(1); } } static void pop_pcap(cap_t orig, cap_t raised_for_setpcap) { cap_free(raised_for_setpcap); cap_free(orig); } static void arg_drop(const char *arg_names) { char *ptr; cap_t orig, raised_for_setpcap; char *names; push_pcap(&orig, &raised_for_setpcap); if (strcmp("all", arg_names) == 0) { unsigned j = 0; while (CAP_IS_SUPPORTED(j)) { int status; if (cap_set_proc(raised_for_setpcap) != 0) { perror("unable to raise CAP_SETPCAP for BSET changes"); exit(1); } status = cap_drop_bound(j); if (cap_set_proc(orig) != 0) { perror("unable to lower CAP_SETPCAP post BSET change"); exit(1); } if (status != 0) { char *name_ptr; name_ptr = cap_to_name(j); fprintf(stderr, "Unable to drop bounding capability [%s]\n", name_ptr); cap_free(name_ptr); exit(1); } j++; } pop_pcap(orig, raised_for_setpcap); return; } names = strdup(arg_names); if (NULL == names) { fprintf(stderr, "failed to allocate names\n"); exit(1); } for (ptr = names; (ptr = strtok(ptr, ",")); ptr = NULL) { /* find name for token */ cap_value_t cap; int status; if (cap_from_name(ptr, &cap) != 0) { fprintf(stderr, "capability [%s] is unknown to libcap\n", ptr); exit(1); } if (cap_set_proc(raised_for_setpcap) != 0) { perror("unable to raise CAP_SETPCAP for BSET changes"); exit(1); } status = cap_drop_bound(cap); if (cap_set_proc(orig) != 0) { perror("unable to lower CAP_SETPCAP post BSET change"); exit(1); } if (status != 0) { fprintf(stderr, "failed to drop [%s=%u]\n", ptr, cap); exit(1); } } pop_pcap(orig, raised_for_setpcap); free(names); } static void arg_change_amb(const char *arg_names, cap_flag_value_t set) { char *ptr; cap_t orig, raised_for_setpcap; char *names; push_pcap(&orig, &raised_for_setpcap); if (strcmp("all", arg_names) == 0) { unsigned j = 0; while (CAP_IS_SUPPORTED(j)) { int status; if (cap_set_proc(raised_for_setpcap) != 0) { perror("unable to raise CAP_SETPCAP for AMBIENT changes"); exit(1); } status = cap_set_ambient(j, set); if (cap_set_proc(orig) != 0) { perror("unable to lower CAP_SETPCAP post AMBIENT change"); exit(1); } if (status != 0) { char *name_ptr; name_ptr = cap_to_name(j); fprintf(stderr, "Unable to %s ambient capability [%s]\n", set == CAP_CLEAR ? "clear":"raise", name_ptr); cap_free(name_ptr); exit(1); } j++; } pop_pcap(orig, raised_for_setpcap); return; } names = strdup(arg_names); if (NULL == names) { fprintf(stderr, "failed to allocate names\n"); exit(1); } for (ptr = names; (ptr = strtok(ptr, ",")); ptr = NULL) { /* find name for token */ cap_value_t cap; int status; if (cap_from_name(ptr, &cap) != 0) { fprintf(stderr, "capability [%s] is unknown to libcap\n", ptr); exit(1); } if (cap_set_proc(raised_for_setpcap) != 0) { perror("unable to raise CAP_SETPCAP for AMBIENT changes"); exit(1); } status = cap_set_ambient(cap, set); if (cap_set_proc(orig) != 0) { perror("unable to lower CAP_SETPCAP post AMBIENT change"); exit(1); } if (status != 0) { fprintf(stderr, "failed to %s ambient [%s=%u]\n", set == CAP_CLEAR ? "clear":"raise", ptr, cap); exit(1); } } pop_pcap(orig, raised_for_setpcap); free(names); } int main(int argc, char *argv[], char *envp[]) { pid_t child; unsigned i; child = 0; for (i=1; igr_gid; } else { group_list[g_count] = strtoul(ptr, NULL, 0); } } free(buf); if (setgroups(g_count, group_list) != 0) { fprintf(stderr, "Failed to setgroups.\n"); exit(1); } free(group_list); } else if (!strncmp("--user=", argv[i], 7)) { struct passwd *pwd; const char *user; gid_t groups[MAX_GROUPS]; int status, ngroups; user = argv[i] + 7; pwd = getpwnam(user); if (pwd == NULL) { fprintf(stderr, "User [%s] not known\n", user); exit(1); } ngroups = MAX_GROUPS; status = getgrouplist(user, pwd->pw_gid, groups, &ngroups); if (status < 1) { perror("Unable to get group list for user"); exit(1); } status = setgroups(ngroups, groups); if (status != 0) { perror("Unable to set group list for user"); exit(1); } status = setgid(pwd->pw_gid); if (status < 0) { fprintf(stderr, "Failed to set gid=%u(user=%s): %s\n", pwd->pw_gid, user, strerror(errno)); exit(1); } status = setuid(pwd->pw_uid); if (status < 0) { fprintf(stderr, "Failed to set uid=%u(user=%s): %s\n", pwd->pw_uid, user, strerror(errno)); exit(1); } } else if (!strncmp("--decode=", argv[i], 9)) { unsigned long long value; unsigned cap; const char *sep = ""; /* Note, if capabilities become longer than 64-bits we'll need to fixup the following code.. */ value = strtoull(argv[i]+9, NULL, 16); printf("0x%016llx=", value); for (cap=0; (cap < 64) && (value >> cap); ++cap) { if (value & (1ULL << cap)) { char *ptr; ptr = cap_to_name(cap); if (ptr != NULL) { printf("%s%s", sep, ptr); cap_free(ptr); } else { printf("%s%u", sep, cap); } sep = ","; } } printf("\n"); } else if (!strncmp("--supports=", argv[i], 11)) { cap_value_t cap; if (cap_from_name(argv[i] + 11, &cap) < 0) { fprintf(stderr, "cap[%s] not recognized by library\n", argv[i] + 11); exit(1); } if (!CAP_IS_SUPPORTED(cap)) { fprintf(stderr, "cap[%s=%d] not supported by kernel\n", argv[i] + 11, cap); exit(1); } } else if (!strcmp("--print", argv[i])) { arg_print(); } else if ((!strcmp("--", argv[i])) || (!strcmp("==", argv[i]))) { argv[i] = strdup(argv[i][0] == '-' ? SHELL : argv[0]); argv[argc] = NULL; execve(argv[i], argv+i, envp); fprintf(stderr, "execve " SHELL " failed!\n"); exit(1); } else { usage: printf("usage: %s [args ...]\n" " --help this message (or try 'man capsh')\n" " --print display capability relevant state\n" " --decode=xxx decode a hex string to a list of caps\n" " --supports=xxx exit 1 if capability xxx unsupported\n" " --drop=xxx remove xxx,.. capabilities from bset\n" " --addamb=xxx add xxx,... capabilities to ambient set\n" " --delamb=xxx remove xxx,... capabilities from ambient\n" " --noamb=xxx reset the ambient capabilities\n" " --caps=xxx set caps as per cap_from_text()\n" " --inh=xxx set xxx,.. inheritiable set\n" " --secbits= write a new value for securebits\n" " --keep= set keep-capabability bit to \n" " --uid= set uid to (hint: id )\n" " --gid= set gid to (hint: id )\n" " --groups=g,... set the supplemental groups\n" " --user= set uid,gid and groups to that of user\n" " --chroot=path chroot(2) to this path\n" " --killit= send signal(n) to child\n" " --forkfor= fork and make child sleep for sec\n" " == re-exec(capsh) with args as for --\n" " -- remaing arguments are for " SHELL "\n" " (without -- [%s] will simply exit(0))\n", argv[0], argv[0]); exit(strcmp("--help", argv[i]) != 0); } } exit(0); }