/* * Copyright (c) 2008 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 /* prctl based API for altering character of current process */ #define PR_GET_KEEPCAPS 7 #define PR_SET_KEEPCAPS 8 #define PR_CAPBSET_READ 23 #define PR_CAPBSET_DROP 24 #define PR_GET_SECUREBITS 27 #define PR_SET_SECUREBITS 28 static const cap_value_t raise_setpcap[1] = { CAP_SETPCAP }; static const cap_value_t raise_chroot[1] = { CAP_SYS_CHROOT }; int main(int argc, char *argv[], char *envp[]) { pid_t child; unsigned i; child = 0; for (i=1; i= 0; cap++) { const char *ptr; if (!set) { continue; } ptr = cap_to_name(cap); if (ptr == 0) { printf("%s%u", sep, cap); } else { printf("%s%s", sep, ptr); } sep = ","; } printf("\n"); set = prctl(PR_GET_SECUREBITS); if (set >= 0) { printf("Securebits: 0%o/0x%x\n", set, set); 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"); } } printf("uid=%u\n", getuid()); } 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\n" " --print display capability relevant state\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" " --chroot=path chroot(2) to this path to invoke bash\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); }