Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 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. |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 8 | */ |
| 9 | //config:config SETPRIV |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "setpriv (3.4 kb)" |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: select PLATFORM_LINUX |
| 13 | //config: select LONG_OPTS |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: Run a program with different Linux privilege settings. |
| 16 | //config: Requires kernel >= 3.5 |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 17 | //config: |
| 18 | //config:config FEATURE_SETPRIV_DUMP |
| 19 | //config: bool "Support dumping current privilege state" |
| 20 | //config: default y |
| 21 | //config: depends on SETPRIV |
| 22 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 23 | //config: Enables the "--dump" switch to print out the current privilege |
| 24 | //config: state. This is helpful for diagnosing problems. |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 25 | //config: |
| 26 | //config:config FEATURE_SETPRIV_CAPABILITIES |
| 27 | //config: bool "Support capabilities" |
| 28 | //config: default y |
| 29 | //config: depends on SETPRIV |
| 30 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 31 | //config: Capabilities can be used to grant processes additional rights |
| 32 | //config: without the necessity to always execute as the root user. |
| 33 | //config: Enabling this option enables "--dump" to show information on |
| 34 | //config: capabilities. |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 35 | //config: |
| 36 | //config:config FEATURE_SETPRIV_CAPABILITY_NAMES |
| 37 | //config: bool "Support capability names" |
| 38 | //config: default y |
| 39 | //config: depends on SETPRIV && FEATURE_SETPRIV_CAPABILITIES |
| 40 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 41 | //config: Capabilities can be either referenced via a human-readble name, |
| 42 | //config: e.g. "net_admin", or using their index, e.g. "cap_12". Enabling |
| 43 | //config: this option allows using the human-readable names in addition to |
| 44 | //config: the index-based names. |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 45 | |
| 46 | //applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP)) |
| 47 | |
| 48 | //kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o |
| 49 | |
| 50 | //usage:#define setpriv_trivial_usage |
| 51 | //usage: "[OPTIONS] PROG [ARGS]" |
| 52 | //usage:#define setpriv_full_usage "\n\n" |
| 53 | //usage: "Run PROG with different privilege settings\n" |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 54 | //usage: IF_FEATURE_SETPRIV_DUMP( |
| 55 | //usage: "\n-d,--dump Show current capabilities" |
| 56 | //usage: ) |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 57 | //usage: "\n--nnp,--no-new-privs Ignore setuid/setgid bits and file capabilities" |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 58 | //usage: IF_FEATURE_SETPRIV_CAPABILITIES( |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 59 | //usage: "\n--inh-caps CAP,CAP Set inheritable capabilities" |
| 60 | //usage: "\n--ambient-caps CAP,CAP Set ambient capabilities" |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 61 | //usage: ) |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 62 | |
| 63 | //setpriv from util-linux 2.28: |
| 64 | // -d, --dump show current state (and do not exec anything) |
| 65 | // --nnp, --no-new-privs disallow granting new privileges |
| 66 | // --inh-caps <caps,...> set inheritable capabilities |
| 67 | // --bounding-set <caps> set capability bounding set |
| 68 | // --ruid <uid> set real uid |
| 69 | // --euid <uid> set effective uid |
| 70 | // --rgid <gid> set real gid |
| 71 | // --egid <gid> set effective gid |
| 72 | // --reuid <uid> set real and effective uid |
| 73 | // --regid <gid> set real and effective gid |
| 74 | // --clear-groups clear supplementary groups |
| 75 | // --keep-groups keep supplementary groups |
| 76 | // --groups <group,...> set supplementary groups |
| 77 | // --securebits <bits> set securebits |
| 78 | // --selinux-label <label> set SELinux label |
| 79 | // --apparmor-profile <pr> set AppArmor profile |
| 80 | |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 81 | #if ENABLE_FEATURE_SETPRIV_CAPABILITIES |
| 82 | #include <linux/capability.h> |
Denys Vlasenko | b0c0b6d | 2017-07-07 17:59:40 +0200 | [diff] [blame] | 83 | // #include <sys/capability.h> |
| 84 | // This header is in libcap, but the functions are in libc. |
| 85 | // Comment in the header says this above capset/capget: |
| 86 | /* system calls - look to libc for function to system call mapping */ |
| 87 | extern int capset(cap_user_header_t header, cap_user_data_t data); |
| 88 | extern int capget(cap_user_header_t header, const cap_user_data_t data); |
| 89 | // so for bbox, let's just repeat the declarations. |
| 90 | // This way, libcap needs not be installed in build environment. |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 91 | #endif |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 92 | #include <sys/prctl.h> |
| 93 | #include "libbb.h" |
| 94 | |
Patrick Steinhardt | f34c701 | 2017-07-06 22:59:23 +0200 | [diff] [blame] | 95 | #ifndef PR_CAPBSET_READ |
| 96 | #define PR_CAPBSET_READ 23 |
| 97 | #endif |
| 98 | |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 99 | #ifndef PR_SET_NO_NEW_PRIVS |
| 100 | #define PR_SET_NO_NEW_PRIVS 38 |
| 101 | #endif |
| 102 | |
Patrick Steinhardt | 10c53b8 | 2017-07-06 15:21:43 +0200 | [diff] [blame] | 103 | #ifndef PR_GET_NO_NEW_PRIVS |
| 104 | #define PR_GET_NO_NEW_PRIVS 39 |
| 105 | #endif |
| 106 | |
Patrick Steinhardt | 5e09874 | 2017-07-06 23:02:33 +0200 | [diff] [blame] | 107 | #ifndef PR_CAP_AMBIENT |
| 108 | #define PR_CAP_AMBIENT 47 |
| 109 | #define PR_CAP_AMBIENT_IS_SET 1 |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 110 | #define PR_CAP_AMBIENT_RAISE 2 |
| 111 | #define PR_CAP_AMBIENT_LOWER 3 |
Patrick Steinhardt | 5e09874 | 2017-07-06 23:02:33 +0200 | [diff] [blame] | 112 | #endif |
| 113 | |
Patrick Steinhardt | 6a3bcf3 | 2017-07-02 15:42:51 +0200 | [diff] [blame] | 114 | enum { |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 115 | IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 116 | IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_INH,) |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 117 | IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_AMB,) |
Patrick Steinhardt | 6a3bcf3 | 2017-07-02 15:42:51 +0200 | [diff] [blame] | 118 | OPTBIT_NNP, |
| 119 | |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 120 | IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 121 | IF_FEATURE_SETPRIV_CAPABILITIES(OPT_INH = (1 << OPTBIT_INH),) |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 122 | IF_FEATURE_SETPRIV_CAPABILITIES(OPT_AMB = (1 << OPTBIT_AMB),) |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 123 | OPT_NNP = (1 << OPTBIT_NNP), |
Patrick Steinhardt | 6a3bcf3 | 2017-07-02 15:42:51 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 126 | #if ENABLE_FEATURE_SETPRIV_CAPABILITIES |
Denys Vlasenko | 200bcc8 | 2017-08-21 19:30:01 +0200 | [diff] [blame] | 127 | DEFINE_STRUCT_CAPS; |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 128 | |
Denys Vlasenko | 44b3f2f | 2017-08-21 02:14:19 +0200 | [diff] [blame] | 129 | static unsigned parse_cap(const char *cap) |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 130 | { |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 131 | switch (cap[0]) { |
| 132 | case '-': |
| 133 | break; |
| 134 | case '+': |
| 135 | break; |
| 136 | default: |
| 137 | bb_error_msg_and_die("invalid capability '%s'", cap); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | cap++; |
Denys Vlasenko | 44b3f2f | 2017-08-21 02:14:19 +0200 | [diff] [blame] | 142 | return cap_name_to_number(cap); |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void set_inh_caps(char *capstring) |
| 146 | { |
| 147 | struct caps caps; |
| 148 | |
| 149 | getcaps(&caps); |
| 150 | |
| 151 | capstring = strtok(capstring, ","); |
| 152 | while (capstring) { |
Denys Vlasenko | 44b3f2f | 2017-08-21 02:14:19 +0200 | [diff] [blame] | 153 | unsigned cap; |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 154 | |
Denys Vlasenko | ec2482e | 2017-08-20 20:21:31 +0200 | [diff] [blame] | 155 | cap = parse_cap(capstring); |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 156 | if (CAP_TO_INDEX(cap) >= caps.u32s) |
Denys Vlasenko | 200bcc8 | 2017-08-21 19:30:01 +0200 | [diff] [blame] | 157 | bb_error_msg_and_die("invalid capability '%s'", capstring); |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 158 | |
| 159 | if (capstring[0] == '+') |
| 160 | caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap); |
| 161 | else |
| 162 | caps.data[CAP_TO_INDEX(cap)].inheritable &= ~CAP_TO_MASK(cap); |
| 163 | capstring = strtok(NULL, ","); |
| 164 | } |
| 165 | |
Denys Vlasenko | 200bcc8 | 2017-08-21 19:30:01 +0200 | [diff] [blame] | 166 | if (capset(&caps.header, caps.data) != 0) |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 167 | bb_perror_msg_and_die("capset"); |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void set_ambient_caps(char *string) |
| 171 | { |
| 172 | char *cap; |
| 173 | |
| 174 | cap = strtok(string, ","); |
| 175 | while (cap) { |
Denys Vlasenko | 0180b57 | 2017-08-29 15:34:38 +0200 | [diff] [blame] | 176 | unsigned idx; |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 177 | |
Denys Vlasenko | 0180b57 | 2017-08-29 15:34:38 +0200 | [diff] [blame] | 178 | idx = parse_cap(cap); |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 179 | if (cap[0] == '+') { |
Denys Vlasenko | 0180b57 | 2017-08-29 15:34:38 +0200 | [diff] [blame] | 180 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, idx, 0, 0) < 0) |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 181 | bb_perror_msg("cap_ambient_raise"); |
| 182 | } else { |
Denys Vlasenko | 0180b57 | 2017-08-29 15:34:38 +0200 | [diff] [blame] | 183 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0) |
Denys Vlasenko | cf5748c | 2017-07-07 16:00:07 +0200 | [diff] [blame] | 184 | bb_perror_msg("cap_ambient_lower"); |
| 185 | } |
| 186 | cap = strtok(NULL, ","); |
| 187 | } |
| 188 | } |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 189 | #endif /* FEATURE_SETPRIV_CAPABILITIES */ |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 190 | |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 191 | #if ENABLE_FEATURE_SETPRIV_DUMP |
Denys Vlasenko | 44b3f2f | 2017-08-21 02:14:19 +0200 | [diff] [blame] | 192 | # if !ENABLE_FEATURE_SETPRIV_CAPABILITY_NAMES |
Denys Vlasenko | 2bfe783 | 2017-07-07 16:09:45 +0200 | [diff] [blame] | 193 | # define printf_cap(pfx, cap_no) printf("%scap_%u", (pfx), (cap_no)) |
| 194 | # endif |
| 195 | |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 196 | static int dump(void) |
| 197 | { |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 198 | IF_FEATURE_SETPRIV_CAPABILITIES(struct caps caps;) |
| 199 | const char *fmt; |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 200 | uid_t ruid, euid, suid; |
| 201 | gid_t rgid, egid, sgid; |
| 202 | gid_t *gids; |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 203 | int i, ngids, nnp; |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 204 | |
| 205 | getresuid(&ruid, &euid, &suid); /* never fails in Linux */ |
| 206 | getresgid(&rgid, &egid, &sgid); /* never fails in Linux */ |
| 207 | ngids = 0; |
| 208 | gids = bb_getgroups(&ngids, NULL); /* never fails in Linux */ |
| 209 | |
Patrick Steinhardt | 10c53b8 | 2017-07-06 15:21:43 +0200 | [diff] [blame] | 210 | nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0); |
| 211 | if (nnp < 0) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 212 | bb_perror_msg_and_die("prctl: %s", "GET_NO_NEW_PRIVS"); |
Patrick Steinhardt | 10c53b8 | 2017-07-06 15:21:43 +0200 | [diff] [blame] | 213 | |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 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); |
| 218 | |
| 219 | printf("Supplementary groups: "); |
| 220 | if (ngids == 0) { |
| 221 | printf("[none]"); |
| 222 | } else { |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 223 | fmt = ",%u" + 1; |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 224 | for (i = 0; i < ngids; i++) { |
| 225 | printf(fmt, (unsigned)gids[i]); |
| 226 | fmt = ",%u"; |
| 227 | } |
| 228 | } |
Patrick Steinhardt | 10c53b8 | 2017-07-06 15:21:43 +0200 | [diff] [blame] | 229 | printf("\nno_new_privs: %d\n", nnp); |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 230 | |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 231 | # if ENABLE_FEATURE_SETPRIV_CAPABILITIES |
| 232 | getcaps(&caps); |
| 233 | printf("Inheritable capabilities: "); |
| 234 | fmt = ""; |
| 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_error_msg_and_die("unsupported capability"); |
| 240 | } |
| 241 | if (caps.data[idx].inheritable & CAP_TO_MASK(i)) { |
Denys Vlasenko | 2bfe783 | 2017-07-07 16:09:45 +0200 | [diff] [blame] | 242 | printf_cap(fmt, i); |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 243 | fmt = ","; |
| 244 | } |
| 245 | } |
| 246 | if (!fmt[0]) |
| 247 | printf("[none]"); |
Patrick Steinhardt | f34c701 | 2017-07-06 22:59:23 +0200 | [diff] [blame] | 248 | |
Patrick Steinhardt | 5e09874 | 2017-07-06 23:02:33 +0200 | [diff] [blame] | 249 | printf("\nAmbient capabilities: "); |
| 250 | fmt = ""; |
| 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); |
| 253 | if (ret < 0) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 254 | bb_perror_msg_and_die("prctl: %s", "CAP_AMBIENT_IS_SET"); |
Patrick Steinhardt | 5e09874 | 2017-07-06 23:02:33 +0200 | [diff] [blame] | 255 | if (ret) { |
Denys Vlasenko | 2bfe783 | 2017-07-07 16:09:45 +0200 | [diff] [blame] | 256 | printf_cap(fmt, i); |
Patrick Steinhardt | 5e09874 | 2017-07-06 23:02:33 +0200 | [diff] [blame] | 257 | fmt = ","; |
| 258 | } |
| 259 | } |
| 260 | if (i == 0) |
| 261 | printf("[unsupported]"); |
| 262 | else if (!fmt[0]) |
| 263 | printf("[none]"); |
| 264 | |
Patrick Steinhardt | f34c701 | 2017-07-06 22:59:23 +0200 | [diff] [blame] | 265 | printf("\nCapability bounding set: "); |
| 266 | fmt = ""; |
| 267 | for (i = 0; cap_valid(i); i++) { |
| 268 | int ret = prctl(PR_CAPBSET_READ, (unsigned long) i, 0UL, 0UL, 0UL); |
| 269 | if (ret < 0) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 270 | bb_perror_msg_and_die("prctl: %s", "CAPBSET_READ"); |
Patrick Steinhardt | f34c701 | 2017-07-06 22:59:23 +0200 | [diff] [blame] | 271 | if (ret) { |
Denys Vlasenko | 2bfe783 | 2017-07-07 16:09:45 +0200 | [diff] [blame] | 272 | printf_cap(fmt, i); |
Patrick Steinhardt | f34c701 | 2017-07-06 22:59:23 +0200 | [diff] [blame] | 273 | fmt = ","; |
| 274 | } |
| 275 | } |
| 276 | if (!fmt[0]) |
| 277 | printf("[none]"); |
Patrick Steinhardt | ad63102 | 2017-07-06 22:47:16 +0200 | [diff] [blame] | 278 | bb_putchar('\n'); |
| 279 | # endif |
| 280 | |
Denys Vlasenko | 200bcc8 | 2017-08-21 19:30:01 +0200 | [diff] [blame] | 281 | if (ENABLE_FEATURE_CLEAN_UP) |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 282 | free(gids); |
Denys Vlasenko | 200bcc8 | 2017-08-21 19:30:01 +0200 | [diff] [blame] | 283 | |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 284 | return EXIT_SUCCESS; |
| 285 | } |
| 286 | #endif /* FEATURE_SETPRIV_DUMP */ |
| 287 | |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 288 | int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 289 | int setpriv_main(int argc UNUSED_PARAM, char **argv) |
| 290 | { |
| 291 | static const char setpriv_longopts[] ALIGN1 = |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 292 | IF_FEATURE_SETPRIV_DUMP( |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 293 | "dump\0" No_argument "d" |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 294 | ) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 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" |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 299 | "ambient-caps\0" Required_argument "\xfd" |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 300 | ) |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 301 | ; |
| 302 | int opts; |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 303 | IF_FEATURE_SETPRIV_CAPABILITIES(char *inh_caps, *ambient_caps;) |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 304 | |
Denys Vlasenko | ec2482e | 2017-08-20 20:21:31 +0200 | [diff] [blame] | 305 | opts = getopt32long(argv, "+" |
| 306 | IF_FEATURE_SETPRIV_DUMP("d") |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 307 | IF_FEATURE_SETPRIV_CAPABILITIES("\xfe:\xfd:"), |
| 308 | setpriv_longopts |
| 309 | IF_FEATURE_SETPRIV_CAPABILITIES(, &inh_caps, &ambient_caps) |
| 310 | ); |
Patrick Steinhardt | 6a3bcf3 | 2017-07-02 15:42:51 +0200 | [diff] [blame] | 311 | argv += optind; |
| 312 | |
Denys Vlasenko | 6798486 | 2017-07-04 18:49:24 +0200 | [diff] [blame] | 313 | #if ENABLE_FEATURE_SETPRIV_DUMP |
| 314 | if (opts & OPT_DUMP) { |
| 315 | if (argv[0] || (opts - OPT_DUMP) != 0) |
| 316 | bb_show_usage(); |
| 317 | return dump(); |
| 318 | } |
| 319 | #endif |
Patrick Steinhardt | 6a3bcf3 | 2017-07-02 15:42:51 +0200 | [diff] [blame] | 320 | if (opts & OPT_NNP) { |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 321 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 322 | bb_perror_msg_and_die("prctl: %s", "SET_NO_NEW_PRIVS"); |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 325 | #if ENABLE_FEATURE_SETPRIV_CAPABILITIES |
| 326 | if (opts & OPT_INH) |
| 327 | set_inh_caps(inh_caps); |
Patrick Steinhardt | 6842d00 | 2017-07-07 02:14:23 +0200 | [diff] [blame] | 328 | if (opts & OPT_AMB) |
| 329 | set_ambient_caps(ambient_caps); |
Patrick Steinhardt | 0f49f6f | 2017-07-07 01:59:45 +0200 | [diff] [blame] | 330 | #endif |
| 331 | |
Patrick Steinhardt | 6a3bcf3 | 2017-07-02 15:42:51 +0200 | [diff] [blame] | 332 | if (!argv[0]) |
| 333 | bb_show_usage(); |
Assaf Gordon | 62d1e98 | 2017-06-14 11:46:52 +0200 | [diff] [blame] | 334 | BB_EXECVP_or_die(argv); |
| 335 | } |