blob: 37e8821a132f960c58fd6013dfee968608c37cf5 [file] [log] [blame]
Assaf Gordon62d1e982017-06-14 11:46:52 +02001/* 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.
Assaf Gordon62d1e982017-06-14 11:46:52 +02008 */
9//config:config SETPRIV
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "setpriv (6.6 kb)"
Assaf Gordon62d1e982017-06-14 11:46:52 +020011//config: default y
Assaf Gordon62d1e982017-06-14 11:46:52 +020012//config: select LONG_OPTS
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Run a program with different Linux privilege settings.
15//config: Requires kernel >= 3.5
Denys Vlasenko67984862017-07-04 18:49:24 +020016//config:
17//config:config FEATURE_SETPRIV_DUMP
18//config: bool "Support dumping current privilege state"
19//config: default y
20//config: depends on SETPRIV
21//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Enables the "--dump" switch to print out the current privilege
23//config: state. This is helpful for diagnosing problems.
Patrick Steinhardtad631022017-07-06 22:47:16 +020024//config:
25//config:config FEATURE_SETPRIV_CAPABILITIES
26//config: bool "Support capabilities"
27//config: default y
28//config: depends on SETPRIV
29//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020030//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.
Patrick Steinhardtad631022017-07-06 22:47:16 +020034//config:
35//config:config FEATURE_SETPRIV_CAPABILITY_NAMES
36//config: bool "Support capability names"
37//config: default y
38//config: depends on SETPRIV && FEATURE_SETPRIV_CAPABILITIES
39//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020040//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.
Assaf Gordon62d1e982017-06-14 11:46:52 +020044
45//applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP))
46
47//kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o
48
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"
Denys Vlasenko67984862017-07-04 18:49:24 +020053//usage: IF_FEATURE_SETPRIV_DUMP(
54//usage: "\n-d,--dump Show current capabilities"
55//usage: )
Assaf Gordon62d1e982017-06-14 11:46:52 +020056//usage: "\n--nnp,--no-new-privs Ignore setuid/setgid bits and file capabilities"
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +020057//usage: IF_FEATURE_SETPRIV_CAPABILITIES(
Patrick Steinhardt6842d002017-07-07 02:14:23 +020058//usage: "\n--inh-caps CAP,CAP Set inheritable capabilities"
59//usage: "\n--ambient-caps CAP,CAP Set ambient capabilities"
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +020060//usage: )
Assaf Gordon62d1e982017-06-14 11:46:52 +020061
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
79
Patrick Steinhardtad631022017-07-06 22:47:16 +020080#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
81#include <linux/capability.h>
Denys Vlasenkob0c0b6d2017-07-07 17:59:40 +020082// #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 */
86extern int capset(cap_user_header_t header, cap_user_data_t data);
87extern 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.
Patrick Steinhardtad631022017-07-06 22:47:16 +020090#endif
Assaf Gordon62d1e982017-06-14 11:46:52 +020091#include <sys/prctl.h>
92#include "libbb.h"
93
Patrick Steinhardtf34c7012017-07-06 22:59:23 +020094#ifndef PR_CAPBSET_READ
95#define PR_CAPBSET_READ 23
96#endif
97
Assaf Gordon62d1e982017-06-14 11:46:52 +020098#ifndef PR_SET_NO_NEW_PRIVS
99#define PR_SET_NO_NEW_PRIVS 38
100#endif
101
Patrick Steinhardt10c53b82017-07-06 15:21:43 +0200102#ifndef PR_GET_NO_NEW_PRIVS
103#define PR_GET_NO_NEW_PRIVS 39
104#endif
105
Patrick Steinhardt5e098742017-07-06 23:02:33 +0200106#ifndef PR_CAP_AMBIENT
107#define PR_CAP_AMBIENT 47
108#define PR_CAP_AMBIENT_IS_SET 1
Patrick Steinhardt6842d002017-07-07 02:14:23 +0200109#define PR_CAP_AMBIENT_RAISE 2
110#define PR_CAP_AMBIENT_LOWER 3
Patrick Steinhardt5e098742017-07-06 23:02:33 +0200111#endif
112
Patrick Steinhardt6a3bcf32017-07-02 15:42:51 +0200113enum {
Denys Vlasenko67984862017-07-04 18:49:24 +0200114 IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,)
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200115 IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_INH,)
Patrick Steinhardt6842d002017-07-07 02:14:23 +0200116 IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_AMB,)
Patrick Steinhardt6a3bcf32017-07-02 15:42:51 +0200117 OPTBIT_NNP,
118
Denys Vlasenko67984862017-07-04 18:49:24 +0200119 IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),)
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200120 IF_FEATURE_SETPRIV_CAPABILITIES(OPT_INH = (1 << OPTBIT_INH),)
Patrick Steinhardt6842d002017-07-07 02:14:23 +0200121 IF_FEATURE_SETPRIV_CAPABILITIES(OPT_AMB = (1 << OPTBIT_AMB),)
Denys Vlasenko67984862017-07-04 18:49:24 +0200122 OPT_NNP = (1 << OPTBIT_NNP),
Patrick Steinhardt6a3bcf32017-07-02 15:42:51 +0200123};
124
Patrick Steinhardtad631022017-07-06 22:47:16 +0200125#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
Denys Vlasenko200bcc82017-08-21 19:30:01 +0200126DEFINE_STRUCT_CAPS;
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200127
Denys Vlasenko44b3f2f2017-08-21 02:14:19 +0200128static unsigned parse_cap(const char *cap)
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200129{
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200130 switch (cap[0]) {
131 case '-':
132 break;
133 case '+':
134 break;
135 default:
136 bb_error_msg_and_die("invalid capability '%s'", cap);
137 break;
138 }
139
140 cap++;
Denys Vlasenko44b3f2f2017-08-21 02:14:19 +0200141 return cap_name_to_number(cap);
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200142}
143
144static void set_inh_caps(char *capstring)
145{
146 struct caps caps;
147
148 getcaps(&caps);
149
150 capstring = strtok(capstring, ",");
151 while (capstring) {
Denys Vlasenko44b3f2f2017-08-21 02:14:19 +0200152 unsigned cap;
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200153
Denys Vlasenkoec2482e2017-08-20 20:21:31 +0200154 cap = parse_cap(capstring);
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200155 if (CAP_TO_INDEX(cap) >= caps.u32s)
Denys Vlasenko200bcc82017-08-21 19:30:01 +0200156 bb_error_msg_and_die("invalid capability '%s'", capstring);
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200157
158 if (capstring[0] == '+')
159 caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap);
160 else
161 caps.data[CAP_TO_INDEX(cap)].inheritable &= ~CAP_TO_MASK(cap);
162 capstring = strtok(NULL, ",");
163 }
164
Denys Vlasenko200bcc82017-08-21 19:30:01 +0200165 if (capset(&caps.header, caps.data) != 0)
James Byrne69374872019-07-02 11:35:03 +0200166 bb_simple_perror_msg_and_die("capset");
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200167}
168
169static void set_ambient_caps(char *string)
170{
171 char *cap;
172
173 cap = strtok(string, ",");
174 while (cap) {
Denys Vlasenko0180b572017-08-29 15:34:38 +0200175 unsigned idx;
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200176
Denys Vlasenko0180b572017-08-29 15:34:38 +0200177 idx = parse_cap(cap);
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200178 if (cap[0] == '+') {
Denys Vlasenko0180b572017-08-29 15:34:38 +0200179 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, idx, 0, 0) < 0)
James Byrne69374872019-07-02 11:35:03 +0200180 bb_simple_perror_msg("cap_ambient_raise");
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200181 } else {
Denys Vlasenko0180b572017-08-29 15:34:38 +0200182 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0)
James Byrne69374872019-07-02 11:35:03 +0200183 bb_simple_perror_msg("cap_ambient_lower");
Denys Vlasenkocf5748c2017-07-07 16:00:07 +0200184 }
185 cap = strtok(NULL, ",");
186 }
187}
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200188#endif /* FEATURE_SETPRIV_CAPABILITIES */
Patrick Steinhardtad631022017-07-06 22:47:16 +0200189
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200190#if ENABLE_FEATURE_SETPRIV_DUMP
Denys Vlasenko44b3f2f2017-08-21 02:14:19 +0200191# if !ENABLE_FEATURE_SETPRIV_CAPABILITY_NAMES
Denys Vlasenko2bfe7832017-07-07 16:09:45 +0200192# define printf_cap(pfx, cap_no) printf("%scap_%u", (pfx), (cap_no))
193# endif
194
Denys Vlasenko67984862017-07-04 18:49:24 +0200195static int dump(void)
196{
Patrick Steinhardtad631022017-07-06 22:47:16 +0200197 IF_FEATURE_SETPRIV_CAPABILITIES(struct caps caps;)
198 const char *fmt;
Denys Vlasenko67984862017-07-04 18:49:24 +0200199 uid_t ruid, euid, suid;
200 gid_t rgid, egid, sgid;
201 gid_t *gids;
Patrick Steinhardtad631022017-07-06 22:47:16 +0200202 int i, ngids, nnp;
Denys Vlasenko67984862017-07-04 18:49:24 +0200203
204 getresuid(&ruid, &euid, &suid); /* never fails in Linux */
205 getresgid(&rgid, &egid, &sgid); /* never fails in Linux */
206 ngids = 0;
207 gids = bb_getgroups(&ngids, NULL); /* never fails in Linux */
208
Patrick Steinhardt10c53b82017-07-06 15:21:43 +0200209 nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
210 if (nnp < 0)
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200211 bb_perror_msg_and_die("prctl: %s", "GET_NO_NEW_PRIVS");
Patrick Steinhardt10c53b82017-07-06 15:21:43 +0200212
Denys Vlasenko67984862017-07-04 18:49:24 +0200213 printf("uid: %u\n", (unsigned)ruid);
214 printf("euid: %u\n", (unsigned)euid);
215 printf("gid: %u\n", (unsigned)rgid);
216 printf("egid: %u\n", (unsigned)egid);
217
218 printf("Supplementary groups: ");
219 if (ngids == 0) {
220 printf("[none]");
221 } else {
Patrick Steinhardtad631022017-07-06 22:47:16 +0200222 fmt = ",%u" + 1;
Denys Vlasenko67984862017-07-04 18:49:24 +0200223 for (i = 0; i < ngids; i++) {
224 printf(fmt, (unsigned)gids[i]);
225 fmt = ",%u";
226 }
227 }
Patrick Steinhardt10c53b82017-07-06 15:21:43 +0200228 printf("\nno_new_privs: %d\n", nnp);
Denys Vlasenko67984862017-07-04 18:49:24 +0200229
Patrick Steinhardtad631022017-07-06 22:47:16 +0200230# if ENABLE_FEATURE_SETPRIV_CAPABILITIES
231 getcaps(&caps);
232 printf("Inheritable capabilities: ");
233 fmt = "";
234 for (i = 0; cap_valid(i); i++) {
235 unsigned idx = CAP_TO_INDEX(i);
236 if (idx >= caps.u32s) {
237 printf("\nindex: %u u32s: %u capability: %u\n", idx, caps.u32s, i);
James Byrne69374872019-07-02 11:35:03 +0200238 bb_simple_error_msg_and_die("unsupported capability");
Patrick Steinhardtad631022017-07-06 22:47:16 +0200239 }
240 if (caps.data[idx].inheritable & CAP_TO_MASK(i)) {
Denys Vlasenko2bfe7832017-07-07 16:09:45 +0200241 printf_cap(fmt, i);
Patrick Steinhardtad631022017-07-06 22:47:16 +0200242 fmt = ",";
243 }
244 }
245 if (!fmt[0])
246 printf("[none]");
Patrick Steinhardtf34c7012017-07-06 22:59:23 +0200247
Patrick Steinhardt5e098742017-07-06 23:02:33 +0200248 printf("\nAmbient capabilities: ");
249 fmt = "";
250 for (i = 0; cap_valid(i); i++) {
251 int ret = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, (unsigned long) i, 0UL, 0UL);
252 if (ret < 0)
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200253 bb_perror_msg_and_die("prctl: %s", "CAP_AMBIENT_IS_SET");
Patrick Steinhardt5e098742017-07-06 23:02:33 +0200254 if (ret) {
Denys Vlasenko2bfe7832017-07-07 16:09:45 +0200255 printf_cap(fmt, i);
Patrick Steinhardt5e098742017-07-06 23:02:33 +0200256 fmt = ",";
257 }
258 }
259 if (i == 0)
260 printf("[unsupported]");
261 else if (!fmt[0])
262 printf("[none]");
263
Patrick Steinhardtf34c7012017-07-06 22:59:23 +0200264 printf("\nCapability bounding set: ");
265 fmt = "";
266 for (i = 0; cap_valid(i); i++) {
267 int ret = prctl(PR_CAPBSET_READ, (unsigned long) i, 0UL, 0UL, 0UL);
268 if (ret < 0)
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200269 bb_perror_msg_and_die("prctl: %s", "CAPBSET_READ");
Patrick Steinhardtf34c7012017-07-06 22:59:23 +0200270 if (ret) {
Denys Vlasenko2bfe7832017-07-07 16:09:45 +0200271 printf_cap(fmt, i);
Patrick Steinhardtf34c7012017-07-06 22:59:23 +0200272 fmt = ",";
273 }
274 }
275 if (!fmt[0])
276 printf("[none]");
Patrick Steinhardtad631022017-07-06 22:47:16 +0200277 bb_putchar('\n');
278# endif
279
Denys Vlasenko200bcc82017-08-21 19:30:01 +0200280 if (ENABLE_FEATURE_CLEAN_UP)
Denys Vlasenko67984862017-07-04 18:49:24 +0200281 free(gids);
Denys Vlasenko200bcc82017-08-21 19:30:01 +0200282
Denys Vlasenko67984862017-07-04 18:49:24 +0200283 return EXIT_SUCCESS;
284}
285#endif /* FEATURE_SETPRIV_DUMP */
286
Assaf Gordon62d1e982017-06-14 11:46:52 +0200287int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
288int setpriv_main(int argc UNUSED_PARAM, char **argv)
289{
290 static const char setpriv_longopts[] ALIGN1 =
Denys Vlasenko67984862017-07-04 18:49:24 +0200291 IF_FEATURE_SETPRIV_DUMP(
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200292 "dump\0" No_argument "d"
Denys Vlasenko67984862017-07-04 18:49:24 +0200293 )
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200294 "nnp\0" No_argument "\xff"
295 "no-new-privs\0" No_argument "\xff"
296 IF_FEATURE_SETPRIV_CAPABILITIES(
297 "inh-caps\0" Required_argument "\xfe"
Patrick Steinhardt6842d002017-07-07 02:14:23 +0200298 "ambient-caps\0" Required_argument "\xfd"
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200299 )
Assaf Gordon62d1e982017-06-14 11:46:52 +0200300 ;
301 int opts;
Patrick Steinhardt6842d002017-07-07 02:14:23 +0200302 IF_FEATURE_SETPRIV_CAPABILITIES(char *inh_caps, *ambient_caps;)
Assaf Gordon62d1e982017-06-14 11:46:52 +0200303
Denys Vlasenkoec2482e2017-08-20 20:21:31 +0200304 opts = getopt32long(argv, "+"
305 IF_FEATURE_SETPRIV_DUMP("d")
Denys Vlasenko036585a2017-08-08 16:38:18 +0200306 IF_FEATURE_SETPRIV_CAPABILITIES("\xfe:\xfd:"),
307 setpriv_longopts
308 IF_FEATURE_SETPRIV_CAPABILITIES(, &inh_caps, &ambient_caps)
309 );
Patrick Steinhardt6a3bcf32017-07-02 15:42:51 +0200310 argv += optind;
311
Denys Vlasenko67984862017-07-04 18:49:24 +0200312#if ENABLE_FEATURE_SETPRIV_DUMP
313 if (opts & OPT_DUMP) {
314 if (argv[0] || (opts - OPT_DUMP) != 0)
315 bb_show_usage();
316 return dump();
317 }
318#endif
Patrick Steinhardt6a3bcf32017-07-02 15:42:51 +0200319 if (opts & OPT_NNP) {
Assaf Gordon62d1e982017-06-14 11:46:52 +0200320 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200321 bb_perror_msg_and_die("prctl: %s", "SET_NO_NEW_PRIVS");
Assaf Gordon62d1e982017-06-14 11:46:52 +0200322 }
323
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200324#if ENABLE_FEATURE_SETPRIV_CAPABILITIES
325 if (opts & OPT_INH)
326 set_inh_caps(inh_caps);
Patrick Steinhardt6842d002017-07-07 02:14:23 +0200327 if (opts & OPT_AMB)
328 set_ambient_caps(ambient_caps);
Patrick Steinhardt0f49f6f2017-07-07 01:59:45 +0200329#endif
330
Patrick Steinhardt6a3bcf32017-07-02 15:42:51 +0200331 if (!argv[0])
332 bb_show_usage();
Assaf Gordon62d1e982017-06-14 11:46:52 +0200333 BB_EXECVP_or_die(argv);
334}