/* * Copyright (c) 2008-11 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 #define MAX_GROUPS 100 /* max number of supplementary groups for user */ static const cap_value_t raise_setpcap[1] = { CAP_SETPCAP }; static const cap_value_t raise_chroot[1] = { CAP_SYS_CHROOT }; 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; } 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 (!memcmp("--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 (!memcmp("--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 (!memcmp("--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])) { unsigned cap; 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); printf("Bounding set ="); sep = ""; for (cap=0; (set = cap_get_bound(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 = ","; } printf("\n"); 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 & 1) ? "yes":"no", (set & 2) ? "locked":"unlocked"); printf(" secure-no-suid-fixup: %s (%s)\n", (set & 4) ? "yes":"no", (set & 8) ? "locked":"unlocked"); printf(" secure-keep-caps: %s (%s)\n", (set & 16) ? "yes":"no", (set & 32) ? "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"); } else if ((!strcmp("--", argv[i])) || (!strcmp("==", argv[i]))) { argv[i] = strdup(argv[i][0] == '-' ? "/bin/bash" : argv[0]); argv[argc] = NULL; execve(argv[i], argv+i, envp); fprintf(stderr, "execve /bin/bash 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" " --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 /bin/bash\n" " (without -- [%s] will simply exit(0))\n", argv[0], argv[0]); exit(strcmp("--help", argv[i]) != 0); } } exit(0); }