Debian Codegraph @

progs/capsh.c

raw
1 /*
2 * Copyright (c) 2008-11,16,19,2020 Andrew G. Morgan <[email protected]>
3 *
4 * This is a multifunction shell wrapper tool that can be used to
5 * launch capable files in various ways with a variety of settings. It
6 * also supports some testing modes, which are used extensively as
7 * part of the libcap build system.
8 *
9 * The --print option can be used as a quick test whether various
10 * capability manipulations work as expected (or not).
11 */
12
13 #ifndef _DEFAULT_SOURCE
14 #define _DEFAULT_SOURCE
15 #endif
16
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20
21 #include <signal.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <pwd.h>
27 #include <grp.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <sys/capability.h>
31 #include <sys/prctl.h>
32 #include <sys/securebits.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35
36 #ifndef SHELL
37 #define SHELL "/bin/bash"
38 #endif /* ndef SHELL */
39
40 #include "./capshdoc.h"
41
42 #define MAX_GROUPS 100 /* max number of supplementary groups for user */
43
44 /* parse a non-negative integer with some error handling */
45 static unsigned long nonneg_uint(const char *text, const char *prefix, int *ok)
46 {
47 char *remains;
48 unsigned long value;
49 ssize_t len = strlen(text);
50
51 if (len == 0 || *text == '-') {
52 goto fail;
53 }
54 value = strtoul(text, &remains, 0);
55 if (*remains) {
56 goto fail;
57 }
58 if (ok != NULL) {
59 *ok = 1;
60 }
61 return value;
62
63 fail:
64 if (ok == NULL) {
65 fprintf(stderr, "%s: want non-negative integer, got \"%s\"\n",
66 prefix, text);
67 exit(1);
68 }
69 *ok = 0;
70 return 0;
71 }
72
73 static char *binary(unsigned long value)
74 {
75 static char string[8*sizeof(unsigned long) + 1];
76 unsigned i;
77
78 i = sizeof(string);
79 string[--i] = '\0';
80 do {
81 string[--i] = (value & 1) ? '1' : '0';
82 value >>= 1;
83 } while ((i > 0) && value);
84 return string + i;
85 }
86
87 static void display_prctl_set(const char *name, int (*fn)(cap_value_t))
88 {
89 unsigned cap;
90 const char *sep;
91 int set;
92
93 printf("%s set =", name);
94 for (sep = "", cap=0; (set = fn(cap)) >= 0; cap++) {
95 char *ptr;
96 if (!set) {
97 continue;
98 }
99
100 ptr = cap_to_name(cap);
101 if (ptr == NULL) {
102 printf("%s%u", sep, cap);
103 } else {
104 printf("%s%s", sep, ptr);
105 cap_free(ptr);
106 }
107 sep = ",";
108 }
109 if (!cap) {
110 printf(" <unsupported>\n");
111 } else {
112 printf("\n");
113 }
114 }
115
116 static void display_current(void)
117 {
118 cap_t all;
119 char *text;
120
121 all = cap_get_proc();
122 if (all == NULL) {
123 perror("failed to get process capabilities");
124 exit(1);
125 }
126 text = cap_to_text(all, NULL);
127 printf("Current: %s\n", text);
128 cap_free(text);
129 cap_free(all);
130 }
131
132 static void display_current_iab(void)
133 {
134 cap_iab_t iab;
135 char *text;
136
137 iab = cap_iab_get_proc();
138 if (iab == NULL) {
139 perror("failed to get IAB for process");
140 exit(1);
141 }
142 text = cap_iab_to_text(iab);
143 if (text == NULL) {
144 perror("failed to obtain text for IAB");
145 cap_free(iab);
146 exit(1);
147 }
148 printf("Current IAB: %s\n", text);
149 cap_free(text);
150 cap_free(iab);
151 }
152
153 /* arg_print displays the current capability state of the process */
154 static void arg_print(void)
155 {
156 long set;
157 int status, j;
158 const char *sep;
159 struct group *g;
160 gid_t groups[MAX_GROUPS], gid;
161 uid_t uid, euid;
162 struct passwd *u, *eu;
163
164 display_current();
165 display_prctl_set("Bounding", cap_get_bound);
166 display_prctl_set("Ambient", cap_get_ambient);
167 display_current_iab();
168
169 set = cap_get_secbits();
170 if (set >= 0) {
171 const char *b = binary(set); /* verilog convention for binary string */
172 printf("Securebits: 0%lo/0x%lx/%u'b%s (no-new-privs=%d)\n", set, set,
173 (unsigned) strlen(b), b,
174 prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0, 0));
175 printf(" secure-noroot: %s (%s)\n",
176 (set & SECBIT_NOROOT) ? "yes":"no",
177 (set & SECBIT_NOROOT_LOCKED) ? "locked":"unlocked");
178 printf(" secure-no-suid-fixup: %s (%s)\n",
179 (set & SECBIT_NO_SETUID_FIXUP) ? "yes":"no",
180 (set & SECBIT_NO_SETUID_FIXUP_LOCKED) ? "locked":"unlocked");
181 printf(" secure-keep-caps: %s (%s)\n",
182 (set & SECBIT_KEEP_CAPS) ? "yes":"no",
183 (set & SECBIT_KEEP_CAPS_LOCKED) ? "locked":"unlocked");
184 if (CAP_AMBIENT_SUPPORTED()) {
185 printf(" secure-no-ambient-raise: %s (%s)\n",
186 (set & SECBIT_NO_CAP_AMBIENT_RAISE) ? "yes":"no",
187 (set & SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED) ?
188 "locked":"unlocked");
189 }
190 } else {
191 printf("[Securebits ABI not supported]\n");
192 set = prctl(PR_GET_KEEPCAPS);
193 if (set >= 0) {
194 printf(" prctl-keep-caps: %s (locking not supported)\n",
195 set ? "yes":"no");
196 } else {
197 printf("[Keepcaps ABI not supported]\n");
198 }
199 }
200 uid = getuid();
201 u = getpwuid(uid);
202 euid = geteuid();
203 eu = getpwuid(euid);
204 printf("uid=%u(%s) euid=%u(%s)\n", uid, u ? u->pw_name : "???", euid, eu ? eu->pw_name : "???");
205 gid = getgid();
206 g = getgrgid(gid);
207 printf("gid=%u(%s)\n", gid, g ? g->gr_name : "???");
208 printf("groups=");
209 status = getgroups(MAX_GROUPS, groups);
210 sep = "";
211 for (j=0; j < status; j++) {
212 g = getgrgid(groups[j]);
213 printf("%s%u(%s)", sep, groups[j], g ? g->gr_name : "???");
214 sep = ",";
215 }
216 printf("\n");
217 cap_mode_t mode = cap_get_mode();
218 printf("Guessed mode: %s (%d)\n", cap_mode_name(mode), mode);
219 }
220
221 static const cap_value_t raise_setpcap[1] = { CAP_SETPCAP };
222 static const cap_value_t raise_chroot[1] = { CAP_SYS_CHROOT };
223
224 static cap_t will_need_setpcap(int strict)
225 {
226 cap_flag_value_t enabled;
227 cap_t raised = NULL;
228
229 if (strict) {
230 return NULL;
231 }
232
233 raised = cap_get_proc();
234 if (raised == NULL) {
235 perror("Capabilities not available");
236 exit(1);
237 }
238 if (cap_get_flag(raised, CAP_SETPCAP, CAP_EFFECTIVE, &enabled) != 0) {
239 perror("Unable to check CAP_EFFECTIVE CAP_SETPCAP value");
240 exit(1);
241 }
242 if (enabled != CAP_SET) {
243 cap_set_flag(raised, CAP_EFFECTIVE, 1, raise_setpcap, CAP_SET);
244 } else {
245 /* no need to raise - since already raised */
246 cap_free(raised);
247 raised = NULL;
248 }
249 return raised;
250 }
251
252 static void push_pcap(int strict, cap_t *orig_p, cap_t *raised_for_setpcap_p)
253 {
254 *orig_p = cap_get_proc();
255 if (NULL == *orig_p) {
256 perror("Capabilities not available");
257 exit(1);
258 }
259 *raised_for_setpcap_p = will_need_setpcap(strict);
260 }
261
262 static void pop_pcap(cap_t orig, cap_t raised_for_setpcap)
263 {
264 cap_free(raised_for_setpcap);
265 cap_free(orig);
266 }
267
268 static void arg_drop(int strict, const char *arg_names)
269 {
270 char *ptr;
271 cap_t orig, raised_for_setpcap;
272 char *names;
273
274 push_pcap(strict, &orig, &raised_for_setpcap);
275 if (strcmp("all", arg_names) == 0) {
276 unsigned j = 0;
277 while (CAP_IS_SUPPORTED(j)) {
278 int status;
279 if (raised_for_setpcap != NULL &&
280 cap_set_proc(raised_for_setpcap) != 0) {
281 perror("unable to raise CAP_SETPCAP for BSET changes");
282 exit(1);
283 }
284 status = cap_drop_bound(j);
285 if (raised_for_setpcap != NULL && cap_set_proc(orig) != 0) {
286 perror("unable to lower CAP_SETPCAP post BSET change");
287 exit(1);
288 }
289 if (status != 0) {
290 char *name_ptr;
291
292 name_ptr = cap_to_name(j);
293 fprintf(stderr, "Unable to drop bounding capability [%s]\n",
294 name_ptr);
295 cap_free(name_ptr);
296 exit(1);
297 }
298 j++;
299 }
300 pop_pcap(orig, raised_for_setpcap);
301 return;
302 }
303
304 names = strdup(arg_names);
305 if (NULL == names) {
306 fprintf(stderr, "failed to allocate names\n");
307 exit(1);
308 }
309 for (ptr = names; (ptr = strtok(ptr, ",")); ptr = NULL) {
310 /* find name for token */
311 cap_value_t cap;
312 int status;
313
314 if (cap_from_name(ptr, &cap) != 0) {
315 fprintf(stderr, "capability [%s] is unknown to libcap\n", ptr);
316 exit(1);
317 }
318 if (raised_for_setpcap != NULL &&
319 cap_set_proc(raised_for_setpcap) != 0) {
320 perror("unable to raise CAP_SETPCAP for BSET changes");
321 exit(1);
322 }
323 status = cap_drop_bound(cap);
324 if (raised_for_setpcap != NULL && cap_set_proc(orig) != 0) {
325 perror("unable to lower CAP_SETPCAP post BSET change");
326 exit(1);
327 }
328 if (status != 0) {
329 fprintf(stderr, "failed to drop [%s=%u]\n", ptr, cap);
330 exit(1);
331 }
332 }
333 pop_pcap(orig, raised_for_setpcap);
334 free(names);
335 }
336
337 static void arg_change_amb(const char *arg_names, cap_flag_value_t set)
338 {
339 char *ptr;
340 cap_t orig;
341 char *names;
342
343 orig = cap_get_proc();
344 if (strcmp("all", arg_names) == 0) {
345 unsigned j = 0;
346 while (CAP_IS_SUPPORTED(j)) {
347 int status;
348 status = cap_set_ambient(j, set);
349 if (status != 0) {
350 char *name_ptr;
351
352 name_ptr = cap_to_name(j);
353 fprintf(stderr, "Unable to %s ambient capability [%s]\n",
354 set == CAP_CLEAR ? "clear":"raise", name_ptr);
355 cap_free(name_ptr);
356 exit(1);
357 }
358 j++;
359 }
360 cap_free(orig);
361 return;
362 }
363
364 names = strdup(arg_names);
365 if (NULL == names) {
366 fprintf(stderr, "failed to allocate names\n");
367 exit(1);
368 }
369 for (ptr = names; (ptr = strtok(ptr, ",")); ptr = NULL) {
370 /* find name for token */
371 cap_value_t cap;
372 int status;
373
374 if (cap_from_name(ptr, &cap) != 0) {
375 fprintf(stderr, "capability [%s] is unknown to libcap\n", ptr);
376 exit(1);
377 }
378 status = cap_set_ambient(cap, set);
379 if (status != 0) {
380 fprintf(stderr, "failed to %s ambient [%s=%u]\n",
381 set == CAP_CLEAR ? "clear":"raise", ptr, cap);
382 exit(1);
383 }
384 }
385 cap_free(orig);
386 free(names);
387 }
388
389 /*
390 * find_self locates and returns the full pathname of the named binary
391 * that is running. Importantly, it looks in the context of the
392 * prevailing CHROOT. Further, it does not fail over to invoking a
393 * shell if the target binary looks like something other than a
394 * executable. If an executable is not found, the function terminates
395 * the program with an error.
396 */
397 static char *find_self(const char *arg0)
398 {
399 int i, status=1;
400 char *p = NULL, *parts, *dir, *scratch;
401 const char *path;
402
403 for (i = strlen(arg0)-1; i >= 0 && arg0[i] != '/'; i--);
404 if (i >= 0) {
405 return strdup(arg0);
406 }
407
408 path = getenv("PATH");
409 if (path == NULL) {
410 fprintf(stderr, "no PATH environment variable found for re-execing\n");
411 exit(1);
412 }
413
414 parts = strdup(path);
415 if (parts == NULL) {
416 fprintf(stderr, "insufficient memory for parts of path\n");
417 exit(1);
418 }
419
420 scratch = malloc(2+strlen(path)+strlen(arg0));
421 if (scratch == NULL) {
422 fprintf(stderr, "insufficient memory for path building\n");
423 goto free_parts;
424 }
425
426 for (p = parts; (dir = strtok(p, ":")); p = NULL) {
427 sprintf(scratch, "%s/%s", dir, arg0);
428 if (access(scratch, X_OK) == 0) {
429 status = 0;
430 break;
431 }
432 }
433 if (status) {
434 fprintf(stderr, "unable to find executable '%s' in PATH\n", arg0);
435 free(scratch);
436 }
437
438 free_parts:
439 free(parts);
440 if (status) {
441 exit(status);
442 }
443 return scratch;
444 }
445
446 static long safe_sysconf(int name)
447 {
448 long ans = sysconf(name);
449 if (ans <= 0) {
450 fprintf(stderr, "sysconf(%d) returned a non-positive number: %ld\n", name, ans);
451 exit(1);
452 }
453 return ans;
454 }
455
456 static void describe(cap_value_t cap) {
457 int j;
458 const char **lines = explanations[cap];
459 char *name = cap_to_name(cap);
460 if (cap < cap_max_bits()) {
461 printf("%s (%d)", name, cap);
462 } else {
463 printf("<reserved for> %s (%d)", name, cap);
464 }
465 cap_free(name);
466 printf(" [/proc/self/status:CapXXX: 0x%016llx]\n\n", 1ULL<<cap);
467 for (j=0; lines[j]; j++) {
468 printf(" %s\n", lines[j]);
469 }
470 }
471
472 __attribute__ ((noreturn))
473 static void do_launch(char *args[], char *envp[])
474 {
475 cap_launch_t lau;
476 pid_t child;
477 int ret, result;
478
479 lau = cap_new_launcher(args[0], (void *) args, (void *) envp);
480 if (lau == NULL) {
481 perror("failed to create launcher");
482 exit(1);
483 }
484 child = cap_launch(lau, NULL);
485 if (child <= 0) {
486 perror("child failed to start");
487 exit(1);
488 }
489 cap_free(lau);
490 ret = waitpid(child, &result, 0);
491 if (ret != child) {
492 fprintf(stderr, "failed to wait for PID=%d, result=%x: ",
493 child, result);
494 perror("");
495 exit(1);
496 }
497 if (WIFEXITED(result)) {
498 exit(WEXITSTATUS(result));
499 }
500 if (WIFSIGNALED(result)) {
501 fprintf(stderr, "child PID=%d terminated by signo=%d\n",
502 child, WTERMSIG(result));
503 exit(1);
504 }
505 fprintf(stderr, "child PID=%d generated result=%0x\n", child, result);
506 exit(1);
507 }
508
509 int main(int argc, char *argv[], char *envp[])
510 {
511 pid_t child = 0;
512 unsigned i;
513 int strict = 0, quiet_start = 0, dont_set_env = 0;
514 const char *shell = SHELL;
515
516 for (i=1; i<argc; ++i) {
517 if (!strcmp("--quiet", argv[i])) {
518 quiet_start = 1;
519 continue;
520 }
521 if (i == 1) {
522 char *temp_name = cap_to_name(cap_max_bits() - 1);
523 if (temp_name == NULL) {
524 perror("obtaining highest capability name");
525 exit(1);
526 }
527 if (temp_name[0] != 'c') {
528 printf("WARNING: libcap needs an update"
529 " (cap=%d should have a name).\n",
530 cap_max_bits() - 1);
531 }
532 cap_free(temp_name);
533 }
534 if (!strncmp("--drop=", argv[i], 7)) {
535 arg_drop(strict, argv[i]+7);
536 } else if (!strncmp("--dropped=", argv[i], 10)) {
537 cap_value_t cap;
538 if (cap_from_name(argv[i]+10, &cap) < 0) {
539 fprintf(stderr, "cap[%s] not recognized by library\n",
540 argv[i] + 10);
541 exit(1);
542 }
543 if (cap_get_bound(cap) > 0) {
544 fprintf(stderr, "cap[%s] raised in bounding vector\n",
545 argv[i]+10);
546 exit(1);
547 }
548 } else if (!strcmp("--has-ambient", argv[i])) {
549 if (!CAP_AMBIENT_SUPPORTED()) {
550 perror("ambient set not supported");
551 exit(1);
552 }
553 } else if (!strncmp("--addamb=", argv[i], 9)) {
554 arg_change_amb(argv[i]+9, CAP_SET);
555 } else if (!strncmp("--delamb=", argv[i], 9)) {
556 arg_change_amb(argv[i]+9, CAP_CLEAR);
557 } else if (!strncmp("--noamb", argv[i], 7)) {
558 if (cap_reset_ambient() != 0) {
559 perror("failed to reset ambient set");
560 exit(1);
561 }
562 } else if (!strcmp("--noenv", argv[i])) {
563 dont_set_env = 1;
564 } else if (!strncmp("--inh=", argv[i], 6)) {
565 cap_t all, raised_for_setpcap;
566 char *text;
567 char *ptr;
568
569 push_pcap(strict, &all, &raised_for_setpcap);
570 if (cap_clear_flag(all, CAP_INHERITABLE) != 0) {
571 perror("libcap:cap_clear_flag() internal error");
572 exit(1);
573 }
574 text = cap_to_text(all, NULL);
575 cap_free(all);
576 if (text == NULL) {
577 perror("Fatal error concerning process capabilities");
578 exit(1);
579 }
580 ptr = malloc(10 + strlen(argv[i]+6) + strlen(text));
581 if (ptr == NULL) {
582 perror("Out of memory for inh set");
583 exit(1);
584 }
585 if (argv[i][6] && strcmp("none", argv[i]+6)) {
586 sprintf(ptr, "%s %s+i", text, argv[i]+6);
587 } else {
588 strcpy(ptr, text);
589 }
590 cap_free(text);
591
592 all = cap_from_text(ptr);
593 if (all == NULL) {
594 perror("Fatal error internalizing capabilities");
595 exit(1);
596 }
597 free(ptr);
598
599 if (raised_for_setpcap != NULL) {
600 /*
601 * This is only for the case that pP does not contain
602 * the requested change to pI.. Failing here is not
603 * indicative of the cap_set_proc(all) failing (always).
604 */
605 (void) cap_set_proc(raised_for_setpcap);
606 cap_free(raised_for_setpcap);
607 raised_for_setpcap = NULL;
608 }
609
610 if (cap_set_proc(all) != 0) {
611 perror("Unable to set inheritable capabilities");
612 exit(1);
613 }
614 cap_free(all);
615 } else if (!strcmp("--strict", argv[i])) {
616 strict = !strict;
617 } else if (!strncmp("--caps=", argv[i], 7)) {
618 cap_t all, raised_for_setpcap;
619
620 raised_for_setpcap = will_need_setpcap(strict);
621 all = cap_from_text(argv[i]+7);
622 if (all == NULL) {
623 fprintf(stderr, "unable to interpret [%s]\n", argv[i]);
624 exit(1);
625 }
626 if (raised_for_setpcap != NULL) {
627 /*
628 * This is actually only for the case that pP does not
629 * contain the requested change to pI.. Failing here
630 * is not always indicative of the cap_set_proc(all)
631 * failing.
632 */
633 (void) cap_set_proc(raised_for_setpcap);
634 cap_free(raised_for_setpcap);
635 raised_for_setpcap = NULL;
636 }
637 if (cap_set_proc(all) != 0) {
638 fprintf(stderr, "Unable to set capabilities [%s]\n", argv[i]);
639 exit(1);
640 }
641 /*
642 * Since status is based on orig, we don't want to restore
643 * the previous value of 'all' again here!
644 */
645 cap_free(all);
646 } else if (!strcmp("--modes", argv[i])) {
647 cap_mode_t c;
648 printf("Supported modes:");
649 for (c = 1; ; c++) {
650 const char *m = cap_mode_name(c);
651 if (strcmp("UNKNOWN", m) == 0) {
652 break;
653 }
654 printf(" %s", m);
655 }
656 printf("\n");
657 } else if (!strncmp("--mode", argv[i], 6)) {
658 if (argv[i][6] == '=') {
659 const char *target = argv[i]+7;
660 cap_mode_t c;
661 int found = 0;
662 for (c = 1; ; c++) {
663 const char *m = cap_mode_name(c);
664 if (!strcmp("UNKNOWN", m)) {
665 found = 0;
666 break;
667 }
668 if (!strcmp(m, target)) {
669 found = 1;
670 break;
671 }
672 }
673 if (!found) {
674 printf("unsupported mode: %s\n", target);
675 exit(1);
676 }
677 int ret = cap_set_mode(c);
678 if (ret != 0) {
679 printf("failed to set mode [%s]: %s\n",
680 target, strerror(errno));
681 exit(1);
682 }
683 } else if (argv[i][6]) {
684 printf("unrecognized command [%s]\n", argv[i]);
685 goto usage;
686 } else {
687 cap_mode_t m = cap_get_mode();
688 printf("Mode: %s\n", cap_mode_name(m));
689 }
690 } else if (!strncmp("--inmode=", argv[i], 9)) {
691 const char *target = argv[i]+9;
692 cap_mode_t c = cap_get_mode();
693 const char *m = cap_mode_name(c);
694 if (strcmp(m, target)) {
695 printf("mismatched mode got=%s want=%s\n", m, target);
696 exit(1);
697 }
698 } else if (!strncmp("--keep=", argv[i], 7)) {
699 unsigned value;
700 int set;
701
702 value = nonneg_uint(argv[i]+7, "invalid --keep value", NULL);
703 set = prctl(PR_SET_KEEPCAPS, value);
704 if (set < 0) {
705 fprintf(stderr, "prctl(PR_SET_KEEPCAPS, %u) failed: %s\n",
706 value, strerror(errno));
707 exit(1);
708 }
709 } else if (!strncmp("--chroot=", argv[i], 9)) {
710 int status;
711 cap_t orig, raised_for_chroot;
712
713 orig = cap_get_proc();
714 if (orig == NULL) {
715 perror("Capabilities not available");
716 exit(1);
717 }
718
719 raised_for_chroot = cap_dup(orig);
720 if (raised_for_chroot == NULL) {
721 perror("Unable to duplicate capabilities");
722 exit(1);
723 }
724
725 if (cap_set_flag(raised_for_chroot, CAP_EFFECTIVE, 1, raise_chroot,
726 CAP_SET) != 0) {
727 perror("unable to select CAP_SET_SYS_CHROOT");
728 exit(1);
729 }
730
731 if (cap_set_proc(raised_for_chroot) != 0) {
732 perror("unable to raise CAP_SYS_CHROOT");
733 exit(1);
734 }
735 cap_free(raised_for_chroot);
736
737 status = chroot(argv[i]+9);
738 if (cap_set_proc(orig) != 0) {
739 perror("unable to lower CAP_SYS_CHROOT");
740 exit(1);
741 }
742 /*
743 * Given we are now in a new directory tree, its good practice
744 * to start off in a sane location
745 */
746 if (status == 0) {
747 status = chdir("/");
748 }
749
750 cap_free(orig);
751
752 if (status != 0) {
753 fprintf(stderr, "Unable to chroot/chdir to [%s]", argv[i]+9);
754 exit(1);
755 }
756 } else if (!strncmp("--secbits=", argv[i], 10)) {
757 unsigned value;
758 int status;
759 value = nonneg_uint(argv[i]+10, "invalid --secbits value", NULL);
760 status = cap_set_secbits(value);
761 if (status < 0) {
762 fprintf(stderr, "failed to set securebits to 0%o/0x%x\n",
763 value, value);
764 exit(1);
765 }
766 } else if (!strncmp("--forkfor=", argv[i], 10)) {
767 unsigned value;
768 if (child != 0) {
769 fprintf(stderr, "already forked\n");
770 exit(1);
771 }
772 value = nonneg_uint(argv[i]+10, "invalid --forkfor value", NULL);
773 if (value == 0) {
774 fprintf(stderr, "require non-zero --forkfor value\n");
775 goto usage;
776 }
777 child = fork();
778 if (child < 0) {
779 perror("unable to fork()");
780 } else if (!child) {
781 sleep(value);
782 exit(0);
783 }
784 } else if (!strncmp("--killit=", argv[i], 9)) {
785 int retval, status;
786 pid_t result;
787 unsigned value;
788
789 value = nonneg_uint(argv[i]+9, "invalid --killit signo value",
790 NULL);
791 if (!child) {
792 fprintf(stderr, "no forked process to kill\n");
793 exit(1);
794 }
795 retval = kill(child, value);
796 if (retval != 0) {
797 perror("Unable to kill child process");
798 exit(1);
799 }
800 result = waitpid(child, &status, 0);
801 if (result != child) {
802 fprintf(stderr, "waitpid didn't match child: %u != %u\n",
803 child, result);
804 exit(1);
805 }
806 if (WTERMSIG(status) != value) {
807 fprintf(stderr, "child terminated with odd signal (%d != %d)\n"
808 , value, WTERMSIG(status));
809 exit(1);
810 }
811 child = 0;
812 } else if (!strncmp("--uid=", argv[i], 6)) {
813 unsigned value;
814 int status;
815
816 value = nonneg_uint(argv[i]+6, "invalid --uid value", NULL);
817 status = setuid(value);
818 if (status < 0) {
819 fprintf(stderr, "Failed to set uid=%u: %s\n",
820 value, strerror(errno));
821 exit(1);
822 }
823 } else if (!strncmp("--cap-uid=", argv[i], 10)) {
824 unsigned value;
825 int status;
826
827 value = nonneg_uint(argv[i]+10, "invalid --cap-uid value", NULL);
828 status = cap_setuid(value);
829 if (status < 0) {
830 fprintf(stderr, "Failed to cap_setuid(%u): %s\n",
831 value, strerror(errno));
832 exit(1);
833 }
834 } else if (!strncmp("--gid=", argv[i], 6)) {
835 unsigned value;
836 int status;
837
838 value = nonneg_uint(argv[i]+6, "invalid --gid value", NULL);
839 status = setgid(value);
840 if (status < 0) {
841 fprintf(stderr, "Failed to set gid=%u: %s\n",
842 value, strerror(errno));
843 exit(1);
844 }
845 } else if (!strncmp("--groups=", argv[i], 9)) {
846 char *ptr, *buf;
847 long length, max_groups;
848 gid_t *group_list;
849 int g_count;
850
851 length = safe_sysconf(_SC_GETGR_R_SIZE_MAX);
852 buf = calloc(1, length);
853 if (NULL == buf) {
854 fprintf(stderr, "No memory for [%s] operation\n", argv[i]);
855 exit(1);
856 }
857
858 max_groups = safe_sysconf(_SC_NGROUPS_MAX);
859 group_list = calloc(max_groups, sizeof(gid_t));
860 if (NULL == group_list) {
861 fprintf(stderr, "No memory for gid list\n");
862 exit(1);
863 }
864
865 g_count = 0;
866 for (ptr = argv[i] + 9; (ptr = strtok(ptr, ","));
867 ptr = NULL, g_count++) {
868 if (max_groups <= g_count) {
869 fprintf(stderr, "Too many groups specified (%d)\n", g_count);
870 exit(1);
871 }
872 if (!isdigit(*ptr)) {
873 struct group *g, grp;
874 if (getgrnam_r(ptr, &grp, buf, length, &g) || NULL == g) {
875 fprintf(stderr, "Failed to identify gid for group [%s]\n", ptr);
876 exit(1);
877 }
878 group_list[g_count] = g->gr_gid;
879 } else {
880 group_list[g_count] = strtoul(ptr, NULL, 0);
881 }
882 }
883 free(buf);
884 if (setgroups(g_count, group_list) != 0) {
885 fprintf(stderr, "Failed to setgroups.\n");
886 exit(1);
887 }
888 free(group_list);
889 } else if (!strncmp("--user=", argv[i], 7)) {
890 struct passwd *pwd;
891 const char *user;
892 gid_t groups[MAX_GROUPS];
893 int status, ngroups;
894
895 user = argv[i] + 7;
896 pwd = getpwnam(user);
897 if (pwd == NULL) {
898 fprintf(stderr, "User [%s] not known\n", user);
899 exit(1);
900 }
901 ngroups = MAX_GROUPS;
902 status = getgrouplist(user, pwd->pw_gid, groups, &ngroups);
903 if (status < 1) {
904 perror("Unable to get group list for user");
905 exit(1);
906 }
907 status = cap_setgroups(pwd->pw_gid, ngroups, groups);
908 if (status != 0) {
909 perror("Unable to set group list for user");
910 exit(1);
911 }
912 status = cap_setuid(pwd->pw_uid);
913 if (status < 0) {
914 fprintf(stderr, "Failed to set uid=%u(user=%s): %s\n",
915 pwd->pw_uid, user, strerror(errno));
916 exit(1);
917 }
918 if (!dont_set_env) {
919 /*
920 * not setting this confuses bash at start up, but use
921 * --noenv to preserve the HOME etc values instead.
922 */
923 if (setenv("HOME", pwd->pw_dir, 1) != 0) {
924 perror("unable to set HOME");
925 exit(1);
926 }
927 if (setenv("USER", user, 1) != 0) {
928 perror("unable to set USER");
929 exit(1);
930 }
931 }
932 } else if (!strncmp("--decode=", argv[i], 9)) {
933 unsigned long long value;
934 unsigned cap;
935 const char *sep = "";
936
937 /* Note, if capabilities become longer than 64-bits we'll need
938 to fixup the following code.. */
939 value = strtoull(argv[i]+9, NULL, 16);
940 printf("0x%016llx=", value);
941
942 for (cap=0; (cap < 64) && (value >> cap); ++cap) {
943 if (value & (1ULL << cap)) {
944 char *ptr;
945
946 ptr = cap_to_name(cap);
947 if (ptr != NULL) {
948 printf("%s%s", sep, ptr);
949 cap_free(ptr);
950 } else {
951 printf("%s%u", sep, cap);
952 }
953 sep = ",";
954 }
955 }
956 printf("\n");
957 } else if (!strncmp("--supports=", argv[i], 11)) {
958 cap_value_t cap;
959
960 if (cap_from_name(argv[i] + 11, &cap) < 0) {
961 fprintf(stderr, "cap[%s] not recognized by library\n",
962 argv[i] + 11);
963 exit(1);
964 }
965 if (!CAP_IS_SUPPORTED(cap)) {
966 fprintf(stderr, "cap[%s=%d] not supported by kernel\n",
967 argv[i] + 11, cap);
968 exit(1);
969 }
970 } else if (!strcmp("--print", argv[i])) {
971 arg_print();
972 } else if ((!strcmp("--", argv[i])) || (!strcmp("==", argv[i]))
973 || (!strcmp("-+", argv[i])) || (!strcmp("=+", argv[i]))) {
974 int launch = argv[i][1] == '+';
975 if (argv[i][0] == '=') {
976 if (quiet_start) {
977 argv[i--] = strdup("--quiet");
978 }
979 argv[i] = find_self(argv[0]);
980 } else {
981 argv[i] = strdup(shell);
982 }
983 argv[argc] = NULL;
984 /* Two ways to chain load - use cap_launch() or execve() */
985 if (launch) {
986 do_launch(argv+i, envp);
987 }
988 execve(argv[i], argv+i, envp);
989 fprintf(stderr, "execve '%s' failed!\n", argv[i]);
990 free(argv[i]);
991 exit(1);
992 } else if (!strncmp("--shell=", argv[i], 8)) {
993 shell = argv[i]+8;
994 } else if (!strncmp("--has-p=", argv[i], 8)) {
995 cap_value_t cap;
996 cap_flag_value_t enabled;
997 cap_t orig;
998
999 if (cap_from_name(argv[i]+8, &cap) < 0) {
1000 fprintf(stderr, "cap[%s] not recognized by library\n",
1001 argv[i] + 8);
1002 exit(1);
1003 }
1004 orig = cap_get_proc();
1005 if (orig == NULL) {
1006 perror("failed to get process capabilities");
1007 exit(1);
1008 }
1009 if (cap_get_flag(orig, cap, CAP_PERMITTED, &enabled) || !enabled) {
1010 fprintf(stderr, "cap[%s] not permitted\n", argv[i]+8);
1011 exit(1);
1012 }
1013 cap_free(orig);
1014 } else if (!strncmp("--has-i=", argv[i], 8)) {
1015 cap_value_t cap;
1016 cap_flag_value_t enabled;
1017 cap_t orig;
1018
1019 if (cap_from_name(argv[i]+8, &cap) < 0) {
1020 fprintf(stderr, "cap[%s] not recognized by library\n",
1021 argv[i] + 8);
1022 exit(1);
1023 }
1024 orig = cap_get_proc();
1025 if (orig == NULL) {
1026 perror("failed to get process capabilities");
1027 exit(1);
1028 }
1029 if (cap_get_flag(orig, cap, CAP_INHERITABLE, &enabled)
1030 || !enabled) {
1031 fprintf(stderr, "cap[%s] not inheritable\n", argv[i]+8);
1032 exit(1);
1033 }
1034 cap_free(orig);
1035 } else if (!strncmp("--has-a=", argv[i], 8)) {
1036 cap_value_t cap;
1037 if (cap_from_name(argv[i]+8, &cap) < 0) {
1038 fprintf(stderr, "cap[%s] not recognized by library\n",
1039 argv[i] + 8);
1040 exit(1);
1041 }
1042 if (!cap_get_ambient(cap)) {
1043 fprintf(stderr, "cap[%s] not in ambient vector\n", argv[i]+8);
1044 exit(1);
1045 }
1046 } else if (!strncmp("--has-b=", argv[i], 8)) {
1047 cap_value_t cap;
1048 if (cap_from_name(argv[i]+8, &cap) < 0) {
1049 fprintf(stderr, "cap[%s] not recognized by library\n",
1050 argv[i] + 8);
1051 exit(1);
1052 }
1053 if (!cap_get_bound(cap)) {
1054 fprintf(stderr, "cap[%s] not in bounding vector\n", argv[i]+8);
1055 exit(1);
1056 }
1057 } else if (!strncmp("--is-uid=", argv[i], 9)) {
1058 unsigned value;
1059 uid_t uid;
1060 value = nonneg_uint(argv[i]+9, "invalid --is-uid value", NULL);
1061 uid = getuid();
1062 if (uid != value) {
1063 fprintf(stderr, "uid: got=%d, want=%d\n", uid, value);
1064 exit(1);
1065 }
1066 } else if (!strncmp("--is-gid=", argv[i], 9)) {
1067 unsigned value;
1068 gid_t gid;
1069 value = nonneg_uint(argv[i]+9, "invalid --is-gid value", NULL);
1070 gid = getgid();
1071 if (gid != value) {
1072 fprintf(stderr, "gid: got=%d, want=%d\n", gid, value);
1073 exit(1);
1074 }
1075 } else if (!strncmp("--iab=", argv[i], 6)) {
1076 cap_iab_t iab = cap_iab_from_text(argv[i]+6);
1077 if (iab == NULL) {
1078 fprintf(stderr, "iab: '%s' malformed\n", argv[i]+6);
1079 exit(1);
1080 }
1081 if (cap_iab_set_proc(iab)) {
1082 perror("unable to set IAB tuple");
1083 exit(1);
1084 }
1085 cap_free(iab);
1086 } else if (!strcmp("--no-new-privs", argv[i])) {
1087 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0, 0) != 0) {
1088 perror("unable to set no-new-privs");
1089 exit(1);
1090 }
1091 } else if (!strcmp("--has-no-new-privs", argv[i])) {
1092 if (prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0, 0) != 1) {
1093 fprintf(stderr, "no-new-privs not set\n");
1094 exit(1);
1095 }
1096 } else if (!strcmp("--license", argv[i])) {
1097 printf(
1098 "%s see License file for details.\n"
1099 "Copyright (c) 2008-11,16,19-21 Andrew G. Morgan"
1100 " <[email protected]>\n", argv[0]);
1101 exit(0);
1102 } else if (!strncmp("--explain=", argv[i], 10)) {
1103 cap_value_t cap;
1104 if (cap_from_name(argv[i]+10, &cap) != 0) {
1105 fprintf(stderr, "unrecognised value '%s'\n", argv[i]+10);
1106 exit(1);
1107 }
1108 if (cap < 0) {
1109 fprintf(stderr, "negative capability (%d) invalid\n", cap);
1110 exit(1);
1111 }
1112 if (cap < capsh_doc_limit) {
1113 describe(cap);
1114 continue;
1115 }
1116 if (cap < cap_max_bits()) {
1117 printf("<unnamed in libcap> (%d)", cap);
1118 } else {
1119 printf("<unsupported> (%d)", cap);
1120 }
1121 printf(" [/proc/self/status:CapXXX: 0x%016llx]\n", 1ULL<<cap);
1122 } else if (!strncmp("--suggest=", argv[i], 10)) {
1123 cap_value_t cap;
1124 int hits = 0;
1125 for (cap=0; cap < capsh_doc_limit; cap++) {
1126 const char **lines = explanations[cap];
1127 int j;
1128 char *name = cap_to_name(cap);
1129 if (name == NULL) {
1130 perror("invalid named cap");
1131 exit(1);
1132 }
1133 char *match = strcasestr(name, argv[i]+10);
1134 cap_free(name);
1135 if (match != NULL) {
1136 if (hits++) {
1137 printf("\n");
1138 }
1139 describe(cap);
1140 continue;
1141 }
1142 for (j=0; lines[j]; j++) {
1143 if (strcasestr(lines[j], argv[i]+10) != NULL) {
1144 if (hits++) {
1145 printf("\n");
1146 }
1147 describe(cap);
1148 break;
1149 }
1150 }
1151 }
1152 } else if (strcmp("--current", argv[i]) == 0) {
1153 display_current();
1154 display_current_iab();
1155 } else {
1156 usage:
1157 printf("usage: %s [args ...]\n"
1158 " --addamb=xxx add xxx,... capabilities to ambient set\n"
1159 " --cap-uid=<n> use libcap cap_setuid() to change uid\n"
1160 " --caps=xxx set caps as per cap_from_text()\n"
1161 " --chroot=path chroot(2) to this path\n"
1162 " --current show current caps and IAB vectors\n"
1163 " --decode=xxx decode a hex string to a list of caps\n"
1164 " --delamb=xxx remove xxx,... capabilities from ambient\n"
1165 " --drop=xxx drop xxx,... caps from bounding set\n"
1166 " --explain=xxx explain what capability xxx permits\n"
1167 " --forkfor=<n> fork and make child sleep for <n> sec\n"
1168 " --gid=<n> set gid to <n> (hint: id <username>)\n"
1169 " --groups=g,... set the supplemental groups\n"
1170 " --has-a=xxx exit 1 if capability xxx not ambient\n"
1171 " --has-b=xxx exit 1 if capability xxx not dropped\n"
1172 " --has-ambient exit 1 unless ambient vector supported\n"
1173 " --has-i=xxx exit 1 if capability xxx not inheritable\n"
1174 " --has-p=xxx exit 1 if capability xxx not permitted\n"
1175 " --has-no-new-privs exit 1 if privs not limited\n"
1176 " --help, -h this message (or try 'man capsh')\n"
1177 " --iab=... use cap_iab_from_text() to set iab\n"
1178 " --inh=xxx set xxx,.. inheritable set\n"
1179 " --inmode=<xxx> exit 1 if current mode is not <xxx>\n"
1180 " --is-uid=<n> exit 1 if uid != <n>\n"
1181 " --is-gid=<n> exit 1 if gid != <n>\n"
1182 " --keep=<n> set keep-capability bit to <n>\n"
1183 " --killit=<n> send signal(n) to child\n"
1184 " --license display license info\n"
1185 " --mode display current libcap mode\n"
1186 " --mode=<xxx> set libcap mode to <xxx>\n"
1187 " --modes list libcap named modes\n"
1188 " --no-new-privs set sticky process privilege limiter\n"
1189 " --noamb reset (drop) all ambient capabilities\n"
1190 " --noenv no fixup of env vars (for --user)\n"
1191 " --print display capability relevant state\n"
1192 " --quiet if first argument skip max cap check\n"
1193 " --secbits=<n> write a new value for securebits\n"
1194 " --shell=/xx/yy use /xx/yy instead of " SHELL " for --\n"
1195 " --strict toggle --caps, --drop and --inh fixups\n"
1196 " --suggest=text search cap descriptions for text\n"
1197 " --supports=xxx exit 1 if capability xxx unsupported\n"
1198 " --uid=<n> set uid to <n> (hint: id <username>)\n"
1199 " --user=<name> set uid,gid and groups to that of user\n"
1200 " == re-exec(capsh) with args as for --\n"
1201 " =+ cap_launch capsh with args as for -+\n"
1202 " -- remaining arguments are for " SHELL "\n"
1203 " -+ cap_launch " SHELL " with remaining args\n"
1204 " (without -- [%s] will simply exit(0))\n",
1205 argv[0], argv[0]);
1206 if (strcmp("--help", argv[1]) && strcmp("-h", argv[1])) {
1207 exit(1);
1208 }
1209 exit(0);
1210 }
1211 }
1212
1213 exit(0);
1214 }