2 * setpriv(1) - set various kernel privilege bits and run something
4 * Copyright (C) 2012 Andy Lutomirski <luto@amacapital.net>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <linux/securebits.h>
30 #include <sys/prctl.h>
31 #include <sys/types.h>
35 #include "closestream.h"
40 #include "pathnames.h"
44 #ifndef PR_SET_NO_NEW_PRIVS
45 # define PR_SET_NO_NEW_PRIVS 38
47 #ifndef PR_GET_NO_NEW_PRIVS
48 # define PR_GET_NO_NEW_PRIVS 39
51 #ifndef PR_CAP_AMBIENT
52 # define PR_CAP_AMBIENT 47
53 # define PR_CAP_AMBIENT_IS_SET 1
54 # define PR_CAP_AMBIENT_RAISE 2
55 # define PR_CAP_AMBIENT_LOWER 3
58 #define SETPRIV_EXIT_PRIVERR 127 /* how we exit when we fail to set privs */
60 /* The shell to set SHELL env.variable if none is given in the user's passwd entry. */
61 #define DEFAULT_SHELL "/bin/sh"
63 static gid_t
get_group(const char *s
, const char *err
);
66 CAP_TYPE_EFFECTIVE
= CAPNG_EFFECTIVE
,
67 CAP_TYPE_PERMITTED
= CAPNG_PERMITTED
,
68 CAP_TYPE_INHERITABLE
= CAPNG_INHERITABLE
,
69 CAP_TYPE_BOUNDING
= CAPNG_BOUNDING_SET
,
70 CAP_TYPE_AMBIENT
= (1 << 4)
74 * Note: We are subject to https://bugzilla.redhat.com/show_bug.cgi?id=895105
75 * and we will therefore have problems if new capabilities are added. Once
76 * that bug is fixed, I'll (Andy Lutomirski) submit a corresponding fix to
77 * setpriv. In the mean time, the code here tries to work reasonably well.
82 nnp
:1, /* no_new_privs */
83 have_ruid
:1, /* real uid */
84 have_euid
:1, /* effective uid */
85 have_rgid
:1, /* real gid */
86 have_egid
:1, /* effective gid */
87 have_passwd
:1, /* passwd entry */
88 have_groups
:1, /* add groups */
89 keep_groups
:1, /* keep groups */
90 clear_groups
:1, /* remove groups */
91 init_groups
:1, /* initialize groups */
92 reset_env
:1, /* reset environment */
93 have_securebits
:1; /* remove groups */
99 /* real user passwd entry */
100 struct passwd passwd
;
102 /* supplementary groups */
107 const char *caps_to_inherit
;
108 const char *ambient_caps
;
109 const char *bounding_set
;
113 /* parent death signal (<0 clear, 0 nothing, >0 signal) */
117 const char *selinux_label
;
118 const char *apparmor_profile
;
121 static void __attribute__((__noreturn__
)) usage(void)
124 fputs(USAGE_HEADER
, out
);
125 fprintf(out
, _(" %s [options] <program> [<argument>...]\n"),
126 program_invocation_short_name
);
128 fputs(USAGE_SEPARATOR
, out
);
129 fputs(_("Run a program with different privilege settings.\n"), out
);
131 fputs(USAGE_OPTIONS
, out
);
132 fputs(_(" -d, --dump show current state (and do not exec)\n"), out
);
133 fputs(_(" --nnp, --no-new-privs disallow granting new privileges\n"), out
);
134 fputs(_(" --ambient-caps <caps,...> set ambient capabilities\n"), out
);
135 fputs(_(" --inh-caps <caps,...> set inheritable capabilities\n"), out
);
136 fputs(_(" --bounding-set <caps> set capability bounding set\n"), out
);
137 fputs(_(" --ruid <uid|user> set real uid\n"), out
);
138 fputs(_(" --euid <uid|user> set effective uid\n"), out
);
139 fputs(_(" --rgid <gid|user> set real gid\n"), out
);
140 fputs(_(" --egid <gid|group> set effective gid\n"), out
);
141 fputs(_(" --reuid <uid|user> set real and effective uid\n"), out
);
142 fputs(_(" --regid <gid|group> set real and effective gid\n"), out
);
143 fputs(_(" --clear-groups clear supplementary groups\n"), out
);
144 fputs(_(" --keep-groups keep supplementary groups\n"), out
);
145 fputs(_(" --init-groups initialize supplementary groups\n"), out
);
146 fputs(_(" --groups <group,...> set supplementary groups by UID or name\n"), out
);
147 fputs(_(" --securebits <bits> set securebits\n"), out
);
148 fputs(_(" --pdeathsig keep|clear|<signame>\n"
149 " set or clear parent death signal\n"), out
);
150 fputs(_(" --selinux-label <label> set SELinux label\n"), out
);
151 fputs(_(" --apparmor-profile <pr> set AppArmor profile\n"), out
);
152 fputs(_(" --reset-env clear all environment and initialize\n"
153 " HOME, SHELL, USER, LOGNAME and PATH\n"), out
);
155 fputs(USAGE_SEPARATOR
, out
);
156 printf(USAGE_HELP_OPTIONS(29));
157 fputs(USAGE_SEPARATOR
, out
);
158 fputs(_(" This tool can be dangerous. Read the manpage, and be careful.\n"), out
);
159 printf(USAGE_MAN_TAIL("setpriv(1)"));
164 static int real_cap_last_cap(void)
166 /* CAP_LAST_CAP is untrustworthy. */
174 f
= fopen(_PATH_PROC_CAPLASTCAP
, "r");
176 ret
= CAP_LAST_CAP
; /* guess */
180 matched
= fscanf(f
, "%d", &ret
);
184 ret
= CAP_LAST_CAP
; /* guess */
189 static int has_cap(enum cap_type which
, unsigned int i
)
192 case CAP_TYPE_EFFECTIVE
:
193 case CAP_TYPE_BOUNDING
:
194 case CAP_TYPE_INHERITABLE
:
195 case CAP_TYPE_PERMITTED
:
196 return capng_have_capability((capng_type_t
)which
, i
);
197 case CAP_TYPE_AMBIENT
:
198 return prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_IS_SET
,
199 (unsigned long) i
, 0UL, 0UL);
201 warnx(_("invalid capability type"));
206 /* Returns the number of capabilities printed. */
207 static int print_caps(FILE *f
, enum cap_type which
)
209 int i
, n
= 0, max
= real_cap_last_cap();
211 for (i
= 0; i
<= max
; i
++) {
212 int ret
= has_cap(which
, i
);
214 if (i
== 0 && ret
< 0)
218 const char *name
= capng_capability_to_name(i
);
224 /* cap-ng has very poor handling of
225 * CAP_LAST_CAP changes. This is the
235 static void dump_one_secbit(int *first
, int *bits
, int bit
, const char *name
)
247 static void dump_securebits(void)
250 int bits
= prctl(PR_GET_SECUREBITS
, 0, 0, 0, 0);
253 warnx(_("getting process secure bits failed"));
257 printf(_("Securebits: "));
259 dump_one_secbit(&first
, &bits
, SECBIT_NOROOT
, "noroot");
260 dump_one_secbit(&first
, &bits
, SECBIT_NOROOT_LOCKED
, "noroot_locked");
261 dump_one_secbit(&first
, &bits
, SECBIT_NO_SETUID_FIXUP
,
263 dump_one_secbit(&first
, &bits
, SECBIT_NO_SETUID_FIXUP_LOCKED
,
264 "no_setuid_fixup_locked");
265 bits
&= ~SECBIT_KEEP_CAPS
;
266 dump_one_secbit(&first
, &bits
, SECBIT_KEEP_CAPS_LOCKED
,
273 printf("0x%x", (unsigned)bits
);
277 printf(_("[none]\n"));
282 static void dump_label(const char *name
)
288 fd
= open(_PATH_PROC_ATTR_CURRENT
, O_RDONLY
);
290 warn(_("cannot open %s"), _PATH_PROC_ATTR_CURRENT
);
294 len
= read(fd
, buf
, sizeof(buf
));
299 warn(_("cannot read %s"), name
);
302 if (sizeof(buf
) - 1 <= (size_t)len
) {
303 warnx(_("%s: too long"), name
);
308 if (0 < len
&& buf
[len
- 1] == '\n')
310 printf("%s: %s\n", name
, buf
);
313 static void dump_groups(void)
315 int n
= getgroups(0, NULL
);
319 warn("getgroups failed");
323 groups
= xmalloc(n
* sizeof(gid_t
));
324 n
= getgroups(n
, groups
);
327 warn("getgroups failed");
331 printf(_("Supplementary groups: "));
336 for (i
= 0; i
< n
; i
++) {
339 printf("%ld", (long)groups
[i
]);
346 static void dump_pdeathsig(void)
350 if (prctl(PR_GET_PDEATHSIG
, &pdeathsig
) != 0) {
351 warn(_("get pdeathsig failed"));
355 printf("Parent death signal: ");
356 if (pdeathsig
&& signum_to_signame(pdeathsig
) != NULL
)
357 printf("%s\n", signum_to_signame(pdeathsig
));
359 printf("%d\n", pdeathsig
);
364 static void dump(int dumplevel
)
370 if (getresuid(&ru
, &eu
, &su
) == 0) {
371 printf(_("uid: %u\n"), ru
);
372 printf(_("euid: %u\n"), eu
);
373 /* Saved and fs uids always equal euid. */
375 printf(_("suid: %u\n"), su
);
377 warn(_("getresuid failed"));
379 if (getresgid(&rg
, &eg
, &sg
) == 0) {
380 printf("gid: %ld\n", (long)rg
);
381 printf("egid: %ld\n", (long)eg
);
382 /* Saved and fs gids always equal egid. */
384 printf("sgid: %ld\n", (long)sg
);
386 warn(_("getresgid failed"));
390 x
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
392 printf("no_new_privs: %d\n", x
);
394 warn("setting no_new_privs failed");
396 if (2 <= dumplevel
) {
397 printf(_("Effective capabilities: "));
398 if (print_caps(stdout
, CAP_TYPE_EFFECTIVE
) == 0)
402 printf(_("Permitted capabilities: "));
403 if (print_caps(stdout
, CAP_TYPE_PERMITTED
) == 0)
408 printf(_("Inheritable capabilities: "));
409 if (print_caps(stdout
, CAP_TYPE_INHERITABLE
) == 0)
413 printf(_("Ambient capabilities: "));
414 x
= print_caps(stdout
, CAP_TYPE_AMBIENT
);
418 printf(_("[unsupported]"));
421 printf(_("Capability bounding set: "));
422 if (print_caps(stdout
, CAP_TYPE_BOUNDING
) == 0)
429 if (access(_PATH_SYS_SELINUX
, F_OK
) == 0)
430 dump_label(_("SELinux label"));
432 if (access(_PATH_SYS_APPARMOR
, F_OK
) == 0) {
433 dump_label(_("AppArmor profile"));
437 static void list_known_caps(void)
439 int i
, max
= real_cap_last_cap();
441 for (i
= 0; i
<= max
; i
++) {
442 const char *name
= capng_capability_to_name(i
);
444 printf("%s\n", name
);
446 warnx(_("cap %d: libcap-ng is broken"), i
);
450 static void parse_groups(struct privctx
*opts
, const char *str
)
452 char *groups
= xstrdup(str
);
453 char *buf
= groups
; /* We'll reuse it */
457 opts
->have_groups
= 1;
458 opts
->num_groups
= 0;
459 while ((c
= strsep(&groups
, ",")))
463 strcpy(buf
, str
); /* It's exactly the right length */
466 opts
->groups
= xcalloc(opts
->num_groups
, sizeof(gid_t
));
467 while ((c
= strsep(&groups
, ",")))
468 opts
->groups
[i
++] = get_group(c
, _("Invalid supplementary group id"));
473 static void parse_pdeathsig(struct privctx
*opts
, const char *str
)
475 if (!strcmp(str
, "keep")) {
476 if (prctl(PR_GET_PDEATHSIG
, &opts
->pdeathsig
) != 0)
477 errx(SETPRIV_EXIT_PRIVERR
,
478 _("failed to get parent death signal"));
479 } else if (!strcmp(str
, "clear")) {
480 opts
->pdeathsig
= -1;
481 } else if ((opts
->pdeathsig
= signame_to_signum(str
)) < 0) {
482 errx(EXIT_FAILURE
, _("unknown signal: %s"), str
);
486 static void do_setresuid(const struct privctx
*opts
)
488 uid_t ruid
, euid
, suid
;
489 if (getresuid(&ruid
, &euid
, &suid
) != 0)
490 err(SETPRIV_EXIT_PRIVERR
, _("getresuid failed"));
496 /* Also copy effective to saved (for paranoia). */
497 if (setresuid(ruid
, euid
, euid
) != 0)
498 err(SETPRIV_EXIT_PRIVERR
, _("setresuid failed"));
501 static void do_setresgid(const struct privctx
*opts
)
503 gid_t rgid
, egid
, sgid
;
504 if (getresgid(&rgid
, &egid
, &sgid
) != 0)
505 err(SETPRIV_EXIT_PRIVERR
, _("getresgid failed"));
511 /* Also copy effective to saved (for paranoia). */
512 if (setresgid(rgid
, egid
, egid
) != 0)
513 err(SETPRIV_EXIT_PRIVERR
, _("setresgid failed"));
516 static void bump_cap(unsigned int cap
)
518 if (capng_have_capability(CAPNG_PERMITTED
, cap
))
519 capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, cap
);
522 static int cap_update(capng_act_t action
,
523 enum cap_type type
, unsigned int cap
)
526 case CAP_TYPE_EFFECTIVE
:
527 case CAP_TYPE_BOUNDING
:
528 case CAP_TYPE_INHERITABLE
:
529 case CAP_TYPE_PERMITTED
:
530 return capng_update(action
, (capng_type_t
) type
, cap
);
531 case CAP_TYPE_AMBIENT
:
535 if (action
== CAPNG_ADD
)
536 ret
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_RAISE
,
537 (unsigned long) cap
, 0UL, 0UL);
539 ret
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_LOWER
,
540 (unsigned long) cap
, 0UL, 0UL);
545 errx(EXIT_FAILURE
, _("unsupported capability type"));
550 static void do_caps(enum cap_type type
, const char *caps
)
552 char *my_caps
= xstrdup(caps
);
555 while ((c
= strsep(&my_caps
, ","))) {
562 errx(EXIT_FAILURE
, _("bad capability string"));
564 if (!strcmp(c
+ 1, "all")) {
566 /* It would be really bad if -all didn't drop all
567 * caps. It's better to just fail. */
568 if (real_cap_last_cap() > CAP_LAST_CAP
)
569 errx(SETPRIV_EXIT_PRIVERR
,
570 _("libcap-ng is too old for \"all\" caps"));
571 for (i
= 0; i
<= CAP_LAST_CAP
; i
++)
572 cap_update(action
, type
, i
);
574 int cap
= capng_name_to_capability(c
+ 1);
576 cap_update(action
, type
, cap
);
577 else if (sscanf(c
+ 1, "cap_%d", &cap
) == 1
578 && 0 <= cap
&& cap
<= real_cap_last_cap())
579 cap_update(action
, type
, cap
);
582 _("unknown capability \"%s\""), c
+ 1);
589 static void parse_securebits(struct privctx
*opts
, const char *arg
)
591 char *buf
= xstrdup(arg
);
594 opts
->have_securebits
= 1;
595 opts
->securebits
= prctl(PR_GET_SECUREBITS
, 0, 0, 0, 0);
596 if (opts
->securebits
< 0)
597 err(SETPRIV_EXIT_PRIVERR
, _("getting process secure bits failed"));
599 if (opts
->securebits
& ~(int)(SECBIT_NOROOT
|
600 SECBIT_NOROOT_LOCKED
|
601 SECBIT_NO_SETUID_FIXUP
|
602 SECBIT_NO_SETUID_FIXUP_LOCKED
|
604 SECBIT_KEEP_CAPS_LOCKED
))
605 errx(SETPRIV_EXIT_PRIVERR
,
606 _("unrecognized securebit set -- refusing to adjust"));
608 while ((c
= strsep(&buf
, ","))) {
609 if (*c
!= '+' && *c
!= '-')
610 errx(EXIT_FAILURE
, _("bad securebits string"));
612 if (!strcmp(c
+ 1, "all")) {
614 opts
->securebits
= 0;
617 _("+all securebits is not allowed"));
620 if (!strcmp(c
+ 1, "noroot"))
622 else if (!strcmp(c
+ 1, "noroot_locked"))
623 bit
= SECBIT_NOROOT_LOCKED
;
624 else if (!strcmp(c
+ 1, "no_setuid_fixup"))
625 bit
= SECBIT_NO_SETUID_FIXUP
;
626 else if (!strcmp(c
+ 1, "no_setuid_fixup_locked"))
627 bit
= SECBIT_NO_SETUID_FIXUP_LOCKED
;
628 else if (!strcmp(c
+ 1, "keep_caps"))
630 _("adjusting keep_caps does not make sense"));
631 else if (!strcmp(c
+ 1, "keep_caps_locked"))
632 bit
= SECBIT_KEEP_CAPS_LOCKED
; /* sigh */
634 errx(EXIT_FAILURE
, _("unrecognized securebit"));
637 opts
->securebits
|= bit
;
639 opts
->securebits
&= ~bit
;
643 opts
->securebits
|= SECBIT_KEEP_CAPS
; /* We need it, and it's reset on exec */
648 static void do_selinux_label(const char *label
)
653 if (access(_PATH_SYS_SELINUX
, F_OK
) != 0)
654 errx(SETPRIV_EXIT_PRIVERR
, _("SELinux is not running"));
656 fd
= open(_PATH_PROC_ATTR_EXEC
, O_RDWR
);
658 err(SETPRIV_EXIT_PRIVERR
,
659 _("cannot open %s"), _PATH_PROC_ATTR_EXEC
);
663 if (write(fd
, label
, len
) != (ssize_t
) len
)
664 err(SETPRIV_EXIT_PRIVERR
,
665 _("write failed: %s"), _PATH_PROC_ATTR_EXEC
);
668 err(SETPRIV_EXIT_PRIVERR
,
669 _("close failed: %s"), _PATH_PROC_ATTR_EXEC
);
672 static void do_apparmor_profile(const char *label
)
676 if (access(_PATH_SYS_APPARMOR
, F_OK
) != 0)
677 errx(SETPRIV_EXIT_PRIVERR
, _("AppArmor is not running"));
679 f
= fopen(_PATH_PROC_ATTR_EXEC
, "r+");
681 err(SETPRIV_EXIT_PRIVERR
,
682 _("cannot open %s"), _PATH_PROC_ATTR_EXEC
);
684 fprintf(f
, "exec %s", label
);
686 if (close_stream(f
) != 0)
687 err(SETPRIV_EXIT_PRIVERR
,
688 _("write failed: %s"), _PATH_PROC_ATTR_EXEC
);
692 static void do_reset_environ(struct passwd
*pw
)
694 char *term
= getenv("TERM");
697 term
= xstrdup(term
);
704 xsetenv("TERM", term
, 1);
708 if (pw
->pw_shell
&& *pw
->pw_shell
)
709 xsetenv("SHELL", pw
->pw_shell
, 1);
711 xsetenv("SHELL", DEFAULT_SHELL
, 1);
713 xsetenv("HOME", pw
->pw_dir
, 1);
714 xsetenv("USER", pw
->pw_name
, 1);
715 xsetenv("LOGNAME", pw
->pw_name
, 1);
718 xsetenv("PATH", _PATH_DEFPATH
, 1);
720 xsetenv("PATH", _PATH_DEFPATH_ROOT
, 1);
723 static uid_t
get_user(const char *s
, const char *err
)
730 tmp
= strtol_or_err(s
, err
);
734 static gid_t
get_group(const char *s
, const char *err
)
741 tmp
= strtol_or_err(s
, err
);
745 static struct passwd
*get_passwd(const char *s
, uid_t
*uid
, const char *err
)
753 tmp
= strtol_or_err(s
, err
);
760 static struct passwd
*passwd_copy(struct passwd
*dst
, const struct passwd
*src
)
763 rv
= memcpy(dst
, src
, sizeof(*dst
));
764 rv
->pw_name
= xstrdup(rv
->pw_name
);
765 rv
->pw_passwd
= xstrdup(rv
->pw_passwd
);
766 rv
->pw_gecos
= xstrdup(rv
->pw_gecos
);
767 rv
->pw_dir
= xstrdup(rv
->pw_dir
);
768 rv
->pw_shell
= xstrdup(rv
->pw_shell
);
772 int main(int argc
, char **argv
)
797 static const struct option longopts
[] = {
798 { "dump", no_argument
, NULL
, 'd' },
799 { "nnp", no_argument
, NULL
, NNP
},
800 { "no-new-privs", no_argument
, NULL
, NNP
},
801 { "inh-caps", required_argument
, NULL
, INHCAPS
},
802 { "ambient-caps", required_argument
, NULL
, AMBCAPS
},
803 { "list-caps", no_argument
, NULL
, LISTCAPS
},
804 { "ruid", required_argument
, NULL
, RUID
},
805 { "euid", required_argument
, NULL
, EUID
},
806 { "rgid", required_argument
, NULL
, RGID
},
807 { "egid", required_argument
, NULL
, EGID
},
808 { "reuid", required_argument
, NULL
, REUID
},
809 { "regid", required_argument
, NULL
, REGID
},
810 { "clear-groups", no_argument
, NULL
, CLEAR_GROUPS
},
811 { "keep-groups", no_argument
, NULL
, KEEP_GROUPS
},
812 { "init-groups", no_argument
, NULL
, INIT_GROUPS
},
813 { "groups", required_argument
, NULL
, GROUPS
},
814 { "bounding-set", required_argument
, NULL
, CAPBSET
},
815 { "securebits", required_argument
, NULL
, SECUREBITS
},
816 { "pdeathsig", required_argument
, NULL
, PDEATHSIG
, },
817 { "selinux-label", required_argument
, NULL
, SELINUX_LABEL
},
818 { "apparmor-profile", required_argument
, NULL
, APPARMOR_PROFILE
},
819 { "help", no_argument
, NULL
, 'h' },
820 { "reset-env", no_argument
, NULL
, RESET_ENV
, },
821 { "version", no_argument
, NULL
, 'V' },
825 static const ul_excl_t excl
[] = {
826 /* keep in same order with enum definitions */
827 {CLEAR_GROUPS
, KEEP_GROUPS
, INIT_GROUPS
, GROUPS
},
830 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
834 struct passwd
*pw
= NULL
;
839 setlocale(LC_ALL
, "");
840 bindtextdomain(PACKAGE
, LOCALEDIR
);
842 close_stdout_atexit();
844 memset(&opts
, 0, sizeof(opts
));
846 while ((c
= getopt_long(argc
, argv
, "+dhV", longopts
, NULL
)) != -1) {
847 err_exclusive_options(c
, longopts
, excl
, excl_st
);
856 _("duplicate --no-new-privs option"));
861 errx(EXIT_FAILURE
, _("duplicate ruid"));
863 pw
= get_passwd(optarg
, &opts
.ruid
, _("failed to parse ruid"));
865 passwd_copy(&opts
.passwd
, pw
);
866 opts
.have_passwd
= 1;
871 errx(EXIT_FAILURE
, _("duplicate euid"));
873 opts
.euid
= get_user(optarg
, _("failed to parse euid"));
876 if (opts
.have_ruid
|| opts
.have_euid
)
877 errx(EXIT_FAILURE
, _("duplicate ruid or euid"));
878 opts
.have_ruid
= opts
.have_euid
= 1;
879 pw
= get_passwd(optarg
, &opts
.ruid
, _("failed to parse reuid"));
880 opts
.euid
= opts
.ruid
;
882 passwd_copy(&opts
.passwd
, pw
);
883 opts
.have_passwd
= 1;
888 errx(EXIT_FAILURE
, _("duplicate rgid"));
890 opts
.rgid
= get_group(optarg
, _("failed to parse rgid"));
894 errx(EXIT_FAILURE
, _("duplicate egid"));
896 opts
.egid
= get_group(optarg
, _("failed to parse egid"));
899 if (opts
.have_rgid
|| opts
.have_egid
)
900 errx(EXIT_FAILURE
, _("duplicate rgid or egid"));
901 opts
.have_rgid
= opts
.have_egid
= 1;
902 opts
.rgid
= opts
.egid
= get_group(optarg
, _("failed to parse regid"));
905 if (opts
.clear_groups
)
907 _("duplicate --clear-groups option"));
908 opts
.clear_groups
= 1;
911 if (opts
.keep_groups
)
913 _("duplicate --keep-groups option"));
914 opts
.keep_groups
= 1;
917 if (opts
.init_groups
)
919 _("duplicate --init-groups option"));
920 opts
.init_groups
= 1;
923 if (opts
.have_groups
)
925 _("duplicate --groups option"));
926 parse_groups(&opts
, optarg
);
931 _("duplicate --keep-pdeathsig option"));
932 parse_pdeathsig(&opts
, optarg
);
938 if (opts
.caps_to_inherit
)
940 _("duplicate --inh-caps option"));
941 opts
.caps_to_inherit
= optarg
;
944 if (opts
.ambient_caps
)
946 _("duplicate --ambient-caps option"));
947 opts
.ambient_caps
= optarg
;
950 if (opts
.bounding_set
)
952 _("duplicate --bounding-set option"));
953 opts
.bounding_set
= optarg
;
956 if (opts
.have_securebits
)
958 _("duplicate --securebits option"));
959 parse_securebits(&opts
, optarg
);
962 if (opts
.selinux_label
)
964 _("duplicate --selinux-label option"));
965 opts
.selinux_label
= optarg
;
967 case APPARMOR_PROFILE
:
968 if (opts
.apparmor_profile
)
970 _("duplicate --apparmor-profile option"));
971 opts
.apparmor_profile
= optarg
;
980 print_version(EXIT_SUCCESS
);
982 errtryhelp(EXIT_FAILURE
);
987 if (total_opts
!= dumplevel
|| optind
< argc
)
989 _("--dump is incompatible with all other options"));
995 if (total_opts
!= 1 || optind
< argc
)
997 _("--list-caps must be specified alone"));
1003 errx(EXIT_FAILURE
, _("No program specified"));
1005 if ((opts
.have_rgid
|| opts
.have_egid
)
1006 && !opts
.keep_groups
&& !opts
.clear_groups
&& !opts
.init_groups
1007 && !opts
.have_groups
)
1009 _("--[re]gid requires --keep-groups, --clear-groups, --init-groups, or --groups"));
1011 if (opts
.init_groups
&& !opts
.have_ruid
)
1013 _("--init-groups requires --ruid or --reuid"));
1015 if (opts
.init_groups
&& !opts
.have_passwd
)
1017 _("uid %ld not found, --init-groups requires an user that "
1018 "can be found on the system"),
1021 if (opts
.reset_env
) {
1022 if (opts
.have_passwd
)
1023 /* pwd according to --ruid or --reuid */
1026 /* pwd for the current user */
1027 pw
= getpwuid(getuid());
1028 do_reset_environ(pw
);
1031 if (opts
.nnp
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) == -1)
1032 err(EXIT_FAILURE
, _("disallow granting new privileges failed"));
1034 if (opts
.selinux_label
)
1035 do_selinux_label(opts
.selinux_label
);
1036 if (opts
.apparmor_profile
)
1037 do_apparmor_profile(opts
.apparmor_profile
);
1039 if (prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0) == -1)
1040 err(EXIT_FAILURE
, _("keep process capabilities failed"));
1042 /* We're going to want CAP_SETPCAP, CAP_SETUID, and CAP_SETGID if
1044 bump_cap(CAP_SETPCAP
);
1045 bump_cap(CAP_SETUID
);
1046 bump_cap(CAP_SETGID
);
1047 if (capng_apply(CAPNG_SELECT_CAPS
) != 0)
1048 err(SETPRIV_EXIT_PRIVERR
, _("activate capabilities"));
1050 if (opts
.have_ruid
|| opts
.have_euid
) {
1051 do_setresuid(&opts
);
1052 /* KEEPCAPS doesn't work for the effective mask. */
1053 if (capng_apply(CAPNG_SELECT_CAPS
) != 0)
1054 err(SETPRIV_EXIT_PRIVERR
, _("reactivate capabilities"));
1057 if (opts
.have_rgid
|| opts
.have_egid
)
1058 do_setresgid(&opts
);
1060 if (opts
.have_groups
) {
1061 if (setgroups(opts
.num_groups
, opts
.groups
) != 0)
1062 err(SETPRIV_EXIT_PRIVERR
, _("setgroups failed"));
1063 } else if (opts
.init_groups
) {
1064 if (initgroups(opts
.passwd
.pw_name
, opts
.passwd
.pw_gid
) != 0)
1065 err(SETPRIV_EXIT_PRIVERR
, _("initgroups failed"));
1066 } else if (opts
.clear_groups
) {
1068 if (setgroups(0, &x
) != 0)
1069 err(SETPRIV_EXIT_PRIVERR
, _("setgroups failed"));
1072 if (opts
.have_securebits
&& prctl(PR_SET_SECUREBITS
, opts
.securebits
, 0, 0, 0) != 0)
1073 err(SETPRIV_EXIT_PRIVERR
, _("set process securebits failed"));
1075 if (opts
.bounding_set
) {
1076 do_caps(CAP_TYPE_BOUNDING
, opts
.bounding_set
);
1077 errno
= EPERM
; /* capng doesn't set errno if we're missing CAP_SETPCAP */
1078 if (capng_apply(CAPNG_SELECT_BOUNDS
) != 0)
1079 err(SETPRIV_EXIT_PRIVERR
, _("apply bounding set"));
1082 if (opts
.caps_to_inherit
) {
1083 do_caps(CAP_TYPE_INHERITABLE
, opts
.caps_to_inherit
);
1084 if (capng_apply(CAPNG_SELECT_CAPS
) != 0)
1085 err(SETPRIV_EXIT_PRIVERR
, _("apply capabilities"));
1088 if (opts
.ambient_caps
) {
1089 do_caps(CAP_TYPE_AMBIENT
, opts
.ambient_caps
);
1092 /* Clear or set parent death signal */
1093 if (opts
.pdeathsig
&& prctl(PR_SET_PDEATHSIG
, opts
.pdeathsig
< 0 ? 0 : opts
.pdeathsig
) != 0)
1094 err(SETPRIV_EXIT_PRIVERR
, _("set parent death signal failed"));
1096 execvp(argv
[optind
], argv
+ optind
);
1097 errexec(argv
[optind
]);