1
/* vi: set sw=4 ts=4: */
3
* setpriv implementation for busybox based on linux-utils-ng 2.29
5
* Copyright (C) 2017 by <assafgordon@gmail.com>
7
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
9
//config:config SETPRIV
10
//config: bool "setpriv (6.6 kb)"
12
//config: select LONG_OPTS
14
//config: Run a program with different Linux privilege settings.
15
//config: Requires kernel >= 3.5
17
//config:config FEATURE_SETPRIV_DUMP
18
//config: bool "Support dumping current privilege state"
20
//config: depends on SETPRIV
22
//config: Enables the "--dump" switch to print out the current privilege
23
//config: state. This is helpful for diagnosing problems.
25
//config:config FEATURE_SETPRIV_CAPABILITIES
26
//config: bool "Support capabilities"
28
//config: depends on SETPRIV
30
//config: Capabilities can be used to grant processes additional rights
31
//config: without the necessity to always execute as the root user.
32
//config: Enabling this option enables "--dump" to show information on
33
//config: capabilities.
35
//config:config FEATURE_SETPRIV_CAPABILITY_NAMES
36
//config: bool "Support capability names"
38
//config: depends on SETPRIV && FEATURE_SETPRIV_CAPABILITIES
40
//config: Capabilities can be either referenced via a human-readble name,
41
//config: e.g. "net_admin", or using their index, e.g. "cap_12". Enabling
42
//config: this option allows using the human-readable names in addition to
43
//config: the index-based names.
45
//applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP))
47
//kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o
49
//usage:#define setpriv_trivial_usage
50
//usage: "[OPTIONS] PROG ARGS"
51
//usage:#define setpriv_full_usage "\n\n"
52
//usage: "Run PROG with different privilege settings\n"
53
//usage: IF_FEATURE_SETPRIV_DUMP(
54
//usage: "\n-d,--dump Show current capabilities"
56
//usage: "\n--nnp,--no-new-privs Ignore setuid/setgid bits and file capabilities"
57
//usage: IF_FEATURE_SETPRIV_CAPABILITIES(
58
//usage: "\n--inh-caps CAP,CAP Set inheritable capabilities"
59
//usage: "\n--ambient-caps CAP,CAP Set ambient capabilities"
62
//setpriv from util-linux 2.28:
63
// -d, --dump show current state (and do not exec anything)
64
// --nnp, --no-new-privs disallow granting new privileges
65
// --inh-caps <caps,...> set inheritable capabilities
66
// --bounding-set <caps> set capability bounding set
67
// --ruid <uid> set real uid
68
// --euid <uid> set effective uid
69
// --rgid <gid> set real gid
70
// --egid <gid> set effective gid
71
// --reuid <uid> set real and effective uid
72
// --regid <gid> set real and effective gid
73
// --clear-groups clear supplementary groups
74
// --keep-groups keep supplementary groups
75
// --groups <group,...> set supplementary groups
76
// --securebits <bits> set securebits
77
// --selinux-label <label> set SELinux label
78
// --apparmor-profile <pr> set AppArmor profile
80
#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
81
#include <linux/capability.h>
82
// #include <sys/capability.h>
83
// This header is in libcap, but the functions are in libc.
84
// Comment in the header says this above capset/capget:
85
/* system calls - look to libc for function to system call mapping */
86
extern int capset(cap_user_header_t header, cap_user_data_t data);
87
extern int capget(cap_user_header_t header, const cap_user_data_t data);
88
// so for bbox, let's just repeat the declarations.
89
// This way, libcap needs not be installed in build environment.
91
#include <sys/prctl.h>
94
#ifndef PR_CAPBSET_READ
95
#define PR_CAPBSET_READ 23
98
#ifndef PR_SET_NO_NEW_PRIVS
99
#define PR_SET_NO_NEW_PRIVS 38
102
#ifndef PR_GET_NO_NEW_PRIVS
103
#define PR_GET_NO_NEW_PRIVS 39
106
#ifndef PR_CAP_AMBIENT
107
#define PR_CAP_AMBIENT 47
108
#define PR_CAP_AMBIENT_IS_SET 1
109
#define PR_CAP_AMBIENT_RAISE 2
110
#define PR_CAP_AMBIENT_LOWER 3
114
IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,)
115
IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_INH,)
116
IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_AMB,)
119
IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),)
120
IF_FEATURE_SETPRIV_CAPABILITIES(OPT_INH = (1 << OPTBIT_INH),)
121
IF_FEATURE_SETPRIV_CAPABILITIES(OPT_AMB = (1 << OPTBIT_AMB),)
122
OPT_NNP = (1 << OPTBIT_NNP),
125
#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
128
static unsigned parse_cap(const char *cap)
136
bb_error_msg_and_die("invalid capability '%s'", cap);
141
return cap_name_to_number(cap);
144
static void set_inh_caps(char *capstring)
151
capstring = strtok_r(capstring, ",", &string);
155
cap = parse_cap(capstring);
156
if (CAP_TO_INDEX(cap) >= caps.u32s)
157
bb_error_msg_and_die("invalid capability '%s'", capstring);
159
if (capstring[0] == '+')
160
caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap);
162
caps.data[CAP_TO_INDEX(cap)].inheritable &= ~CAP_TO_MASK(cap);
163
capstring = strtok_r(NULL, ",", &string);
166
if (capset(&caps.header, caps.data) != 0)
167
bb_simple_perror_msg_and_die("capset");
170
static void set_ambient_caps(char *string)
174
cap = strtok_r(string, ",", &string);
178
idx = parse_cap(cap);
180
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, idx, 0, 0) < 0)
181
bb_simple_perror_msg("cap_ambient_raise");
183
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0)
184
bb_simple_perror_msg("cap_ambient_lower");
186
cap = strtok_r(NULL, ",", &string);
189
#endif /* FEATURE_SETPRIV_CAPABILITIES */
191
#if ENABLE_FEATURE_SETPRIV_DUMP
192
# if !ENABLE_FEATURE_SETPRIV_CAPABILITY_NAMES
193
# define printf_cap(pfx, cap_no) printf("%scap_%u", (pfx), (cap_no))
196
static int dump(void)
198
IF_FEATURE_SETPRIV_CAPABILITIES(struct caps caps;)
200
uid_t ruid, euid, suid;
201
gid_t rgid, egid, sgid;
205
getresuid(&ruid, &euid, &suid); /* never fails in Linux */
206
getresgid(&rgid, &egid, &sgid); /* never fails in Linux */
208
gids = bb_getgroups(&ngids, NULL); /* never fails in Linux */
210
nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
212
bb_perror_msg_and_die("prctl: %s", "GET_NO_NEW_PRIVS");
214
printf("uid: %u\n", (unsigned)ruid);
215
printf("euid: %u\n", (unsigned)euid);
216
printf("gid: %u\n", (unsigned)rgid);
217
printf("egid: %u\n", (unsigned)egid);
219
printf("Supplementary groups: ");
224
for (i = 0; i < ngids; i++) {
225
printf(fmt, (unsigned)gids[i]);
229
printf("\nno_new_privs: %d\n", nnp);
231
# if ENABLE_FEATURE_SETPRIV_CAPABILITIES
233
printf("Inheritable capabilities: ");
235
for (i = 0; cap_valid(i); i++) {
236
unsigned idx = CAP_TO_INDEX(i);
237
if (idx >= caps.u32s) {
238
printf("\nindex: %u u32s: %u capability: %u\n", idx, caps.u32s, i);
239
bb_simple_error_msg_and_die("unsupported capability");
241
if (caps.data[idx].inheritable & CAP_TO_MASK(i)) {
249
printf("\nAmbient capabilities: ");
251
for (i = 0; cap_valid(i); i++) {
252
int ret = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, (unsigned long) i, 0UL, 0UL);
254
bb_perror_msg_and_die("prctl: %s", "CAP_AMBIENT_IS_SET");
261
printf("[unsupported]");
265
printf("\nCapability bounding set: ");
267
for (i = 0; cap_valid(i); i++) {
268
int ret = prctl(PR_CAPBSET_READ, (unsigned long) i, 0UL, 0UL, 0UL);
270
bb_perror_msg_and_die("prctl: %s", "CAPBSET_READ");
281
if (ENABLE_FEATURE_CLEAN_UP)
286
#endif /* FEATURE_SETPRIV_DUMP */
288
int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
289
int setpriv_main(int argc UNUSED_PARAM, char **argv)
291
static const char setpriv_longopts[] ALIGN1 =
292
IF_FEATURE_SETPRIV_DUMP(
293
"dump\0" No_argument "d"
295
"nnp\0" No_argument "\xff"
296
"no-new-privs\0" No_argument "\xff"
297
IF_FEATURE_SETPRIV_CAPABILITIES(
298
"inh-caps\0" Required_argument "\xfe"
299
"ambient-caps\0" Required_argument "\xfd"
303
IF_FEATURE_SETPRIV_CAPABILITIES(char *inh_caps, *ambient_caps;)
305
opts = getopt32long(argv, "+"
306
IF_FEATURE_SETPRIV_DUMP("d")
307
IF_FEATURE_SETPRIV_CAPABILITIES("\xfe:\xfd:"),
309
IF_FEATURE_SETPRIV_CAPABILITIES(, &inh_caps, &ambient_caps)
313
#if ENABLE_FEATURE_SETPRIV_DUMP
314
if (opts & OPT_DUMP) {
315
if (argv[0] || (opts - OPT_DUMP) != 0)
320
if (opts & OPT_NNP) {
321
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
322
bb_perror_msg_and_die("prctl: %s", "SET_NO_NEW_PRIVS");
325
#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
327
set_inh_caps(inh_caps);
329
set_ambient_caps(ambient_caps);
334
BB_EXECVP_or_die(argv);