setpriv: dump no-new-privs info
[oweals/busybox.git] / util-linux / setpriv.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * setpriv implementation for busybox based on linux-utils-ng 2.29
4  *
5  * Copyright (C) 2017 by  <assafgordon@gmail.com>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  *
9  */
10 //config:config SETPRIV
11 //config:       bool "setpriv"
12 //config:       default y
13 //config:       select PLATFORM_LINUX
14 //config:       select LONG_OPTS
15 //config:       help
16 //config:         Run a program with different Linux privilege settings.
17 //config:         Requires kernel >= 3.5
18 //config:
19 //config:config FEATURE_SETPRIV_DUMP
20 //config:       bool "Support dumping current privilege state"
21 //config:       default y
22 //config:       depends on SETPRIV
23 //config:       help
24 //config:         Enables the "--dump" switch to print out the current privilege
25 //config:         state. This is helpful for diagnosing problems.
26
27 //applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP))
28
29 //kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o
30
31 //usage:#define setpriv_trivial_usage
32 //usage:        "[OPTIONS] PROG [ARGS]"
33 //usage:#define setpriv_full_usage "\n\n"
34 //usage:       "Run PROG with different privilege settings\n"
35 //usage:        IF_FEATURE_SETPRIV_DUMP(
36 //usage:     "\n-d,--dump               Show current capabilities"
37 //usage:        )
38 //usage:     "\n--nnp,--no-new-privs    Ignore setuid/setgid bits and file capabilities"
39
40 //setpriv from util-linux 2.28:
41 // -d, --dump               show current state (and do not exec anything)
42 // --nnp, --no-new-privs    disallow granting new privileges
43 // --inh-caps <caps,...>    set inheritable capabilities
44 // --bounding-set <caps>    set capability bounding set
45 // --ruid <uid>             set real uid
46 // --euid <uid>             set effective uid
47 // --rgid <gid>             set real gid
48 // --egid <gid>             set effective gid
49 // --reuid <uid>            set real and effective uid
50 // --regid <gid>            set real and effective gid
51 // --clear-groups           clear supplementary groups
52 // --keep-groups            keep supplementary groups
53 // --groups <group,...>     set supplementary groups
54 // --securebits <bits>      set securebits
55 // --selinux-label <label>  set SELinux label
56 // --apparmor-profile <pr>  set AppArmor profile
57
58 #include <sys/prctl.h>
59 #include "libbb.h"
60
61 #ifndef PR_SET_NO_NEW_PRIVS
62 #define PR_SET_NO_NEW_PRIVS 38
63 #endif
64
65 #ifndef PR_GET_NO_NEW_PRIVS
66 #define PR_GET_NO_NEW_PRIVS 39
67 #endif
68
69 enum {
70         IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,)
71         OPTBIT_NNP,
72
73         IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),)
74         OPT_NNP  = (1 << OPTBIT_NNP),
75 };
76
77 #if ENABLE_FEATURE_SETPRIV_DUMP
78 static int dump(void)
79 {
80         uid_t ruid, euid, suid;
81         gid_t rgid, egid, sgid;
82         gid_t *gids;
83         int ngids, nnp;
84
85         getresuid(&ruid, &euid, &suid); /* never fails in Linux */
86         getresgid(&rgid, &egid, &sgid); /* never fails in Linux */
87         ngids = 0;
88         gids = bb_getgroups(&ngids, NULL); /* never fails in Linux */
89
90         nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
91         if (nnp < 0)
92                 bb_simple_perror_msg_and_die("prctl: GET_NO_NEW_PRIVS");
93
94         printf("uid: %u\n", (unsigned)ruid);
95         printf("euid: %u\n", (unsigned)euid);
96         printf("gid: %u\n", (unsigned)rgid);
97         printf("egid: %u\n", (unsigned)egid);
98
99         printf("Supplementary groups: ");
100         if (ngids == 0) {
101                 printf("[none]");
102         } else {
103                 const char *fmt = ",%u" + 1;
104                 int i;
105                 for (i = 0; i < ngids; i++) {
106                         printf(fmt, (unsigned)gids[i]);
107                         fmt = ",%u";
108                 }
109         }
110         printf("\nno_new_privs: %d\n", nnp);
111
112         if (ENABLE_FEATURE_CLEAN_UP)
113                 free(gids);
114         return EXIT_SUCCESS;
115 }
116 #endif /* FEATURE_SETPRIV_DUMP */
117
118 int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
119 int setpriv_main(int argc UNUSED_PARAM, char **argv)
120 {
121         static const char setpriv_longopts[] ALIGN1 =
122                 IF_FEATURE_SETPRIV_DUMP(
123                 "dump\0"         No_argument    "d"
124                 )
125                 "nnp\0"          No_argument    "\xff"
126                 "no-new-privs\0" No_argument    "\xff"
127                 ;
128         int opts;
129
130         applet_long_options = setpriv_longopts;
131         opts = getopt32(argv, "+"IF_FEATURE_SETPRIV_DUMP("d"));
132         argv += optind;
133
134 #if ENABLE_FEATURE_SETPRIV_DUMP
135         if (opts & OPT_DUMP) {
136                 if (argv[0] || (opts - OPT_DUMP) != 0)
137                         bb_show_usage();
138                 return dump();
139         }
140 #endif
141         if (opts & OPT_NNP) {
142                 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
143                         bb_simple_perror_msg_and_die("prctl: NO_NEW_PRIVS");
144         }
145
146         if (!argv[0])
147                 bb_show_usage();
148         BB_EXECVP_or_die(argv);
149 }