1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*
* Copyright (c) 1997,2007-8,2020,21 Andrew G. Morgan <morgan@kernel.org>
*
* This sets/verifies the capabilities of a given file.
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/capability.h>
#include <unistd.h>
static void usage(int status)
{
fprintf(stderr,
"usage: setcap [--license] [-f] [-h] [-n <rootid>] [-q] [-v]"
" (-r|-|<caps>) <filename> [ ... (-r|-|<capsN>) <filenameN> ]\n"
"\n"
" Note <filename> must be a regular (non-symlink) file.\n"
" -r remove capability from file\n"
" - read capability text from stdin\n"
" <capsN> cap_from_text(3) formatted file capability\n"
" [ Note: capsh --suggest=\"something...\" might help you pick. ]"
"\n"
" --license display the license info\n"
" -f force setting even when the capability is invalid\n"
" -h this message and exit status 0\n"
" -n <rootid> write a user namespace (!= 0) limited capability\n"
" -q quietly\n"
" -v validate supplied capability matches file\n"
);
exit(status);
}
/* parse a positive integer with some error handling */
static unsigned long pos_uint(const char *text, const char *prefix, int *ok)
{
char *remains;
unsigned long value;
ssize_t len = strlen(text);
if (len == 0 || *text == '-') {
goto fail;
}
value = strtoul(text, &remains, 0);
if (*remains || value == 0) {
goto fail;
}
if (ok != NULL) {
*ok = 1;
}
return value;
fail:
if (ok == NULL) {
fprintf(stderr, "%s: want positive integer, got \"%s\"\n",
prefix, text);
exit(1);
}
*ok = 0;
return 0;
}
#define MAXCAP 2048
static int read_caps(int quiet, const char *filename, char *buffer)
{
int i = MAXCAP;
if (!quiet) {
fprintf(stderr, "Please enter caps for file [empty line to end]:\n");
}
while (i > 0) {
int j = read(STDIN_FILENO, buffer, i);
if (j < 0) {
fprintf(stderr, "\n[Error - aborting]\n");
exit(1);
}
if (j==0 || buffer[0] == '\n') {
/* we're done */
break;
}
/* move on... */
i -= j;
buffer += j;
}
/* <NUL> terminate */
buffer[0] = '\0';
return (i < MAXCAP ? 0:-1);
}
int main(int argc, char **argv)
{
int tried_to_cap_setfcap = 0;
char buffer[MAXCAP+1];
int retval, quiet = 0, verify = 0, forced = 0;
cap_t mycaps;
cap_value_t capflag;
uid_t rootid = 0, f_rootid;
if (argc < 2) {
usage(1);
}
mycaps = cap_get_proc();
if (mycaps == NULL) {
fprintf(stderr, "warning - unable to get process capabilities"
" (old libcap?)\n");
}
cap_t cap_d = NULL;
char **arg = argv+1;
for (; --argc > 0; arg++) {
const char *text;
cap_free(cap_d);
cap_d = NULL;
if (!strcmp("--license", *arg)) {
printf(
"%s see LICENSE file for details.\n"
"Copyright (c) 1997,2007-8,2020-21 Andrew G. Morgan"
" <morgan@kernel.org>\n", argv[0]);
exit(0);
}
if (!strcmp(*arg, "-f")) {
forced = 1;
continue;
}
if (!strcmp(*arg, "-h")) {
usage(0);
}
if (!strcmp(*arg, "-n")) {
printf("got here\n");
if (argc < 2) {
fprintf(stderr,
"usage: .. -n <rootid> .. - rootid!=0 file caps");
exit(1);
}
--argc;
rootid = (uid_t) pos_uint(*++arg, "bad ns rootid", NULL);
continue;
}
if (!strcmp(*arg, "-q")) {
quiet = 1;
continue;
}
if (!strcmp(*arg, "-v")) {
verify = 1;
continue;
}
if (!strcmp(*arg, "-r")) {
cap_free(cap_d);
cap_d = NULL;
} else {
if (!strcmp(*arg,"-")) {
retval = read_caps(quiet, *arg, buffer);
if (retval)
usage(1);
text = buffer;
} else {
text = *arg;
}
int non_space = 0, j;
for (j = 0; text[j]; j++) {
if (!isspace(text[j])) {
non_space = 1;
break;
}
}
if (!non_space) {
fprintf(stderr, "empty space is an invalid capability, did you mean -r?\n");
exit(1);
}
cap_d = cap_from_text(text);
if (cap_d == NULL) {
printf("argument: %s\n", text);
perror("fatal error");
usage(1);
}
if (cap_set_nsowner(cap_d, rootid)) {
perror("unable to set nsowner");
exit(1);
}
#ifdef DEBUG
{
char *result = cap_to_text(cap_d, NULL);
fprintf(stderr, "caps set to: [%s]\n", result);
cap_free(result);
}
#endif
}
if (--argc <= 0) {
usage(1);
}
/*
* Set the filesystem capability for this file.
*/
if (verify) {
cap_t cap_on_file;
int cmp;
if (cap_d == NULL) {
cap_d = cap_init();
if (cap_d == NULL) {
perror("unable to obtain empty capability");
exit(1);
}
}
cap_on_file = cap_get_file(*++arg);
if (cap_on_file == NULL) {
cap_on_file = cap_init();
if (cap_on_file == NULL) {
perror("unable to use missing capability");
exit(1);
}
}
cmp = cap_compare(cap_on_file, cap_d);
f_rootid = cap_get_nsowner(cap_on_file);
cap_free(cap_on_file);
if (cmp != 0 || rootid != f_rootid) {
if (!quiet) {
if (rootid != f_rootid) {
printf("nsowner[got=%d, want=%d],", f_rootid, rootid);
}
printf("%s differs in [%s%s%s]\n", *arg,
CAP_DIFFERS(cmp, CAP_PERMITTED) ? "p" : "",
CAP_DIFFERS(cmp, CAP_INHERITABLE) ? "i" : "",
CAP_DIFFERS(cmp, CAP_EFFECTIVE) ? "e" : "");
}
exit(1);
}
if (!quiet) {
printf("%s: OK\n", *arg);
}
} else {
if (!tried_to_cap_setfcap) {
capflag = CAP_SETFCAP;
/*
* Raise the effective CAP_SETFCAP.
*/
if (cap_set_flag(mycaps, CAP_EFFECTIVE, 1, &capflag, CAP_SET)
!= 0) {
perror("unable to manipulate CAP_SETFCAP - "
"try a newer libcap?");
exit(1);
}
if (cap_set_proc(mycaps) != 0) {
perror("unable to set CAP_SETFCAP effective capability");
exit(1);
}
tried_to_cap_setfcap = 1;
}
#ifdef linux
{
/* Linux's file capabilities have a compressed representation. */
int explained = 0;
int somebits = 0;
cap_value_t cap;
cap_flag_value_t per_state;
for (cap = 0;
cap_get_flag(cap_d, cap, CAP_PERMITTED, &per_state) != -1;
cap++) {
cap_flag_value_t inh_state, eff_state, combined;
cap_get_flag(cap_d, cap, CAP_INHERITABLE, &inh_state);
cap_get_flag(cap_d, cap, CAP_EFFECTIVE, &eff_state);
combined = (inh_state | per_state);
somebits |= !!eff_state;
if (combined != eff_state) {
explained = 1;
break;
}
}
if (somebits && explained) {
fprintf(stderr, "Error: under Linux, effective file capabilities must either be empty, or\n"
" exactly match the union of selected permitted and inheritable bits.\n");
if (!forced) {
exit(1);
}
}
}
#endif /* def linux */
errno = 0;
retval = cap_set_file(*++arg, cap_d);
if (retval != 0) {
switch (errno) {
case EINVAL:
fprintf(stderr,
"Invalid file '%s' for capability operation\n",
argv[0]);
exit(1);
case ENODATA:
if (cap_d == NULL) {
fprintf(stderr,
"File '%s' has no capablity to remove\n",
argv[0]);
if (forced) {
break;
}
exit(1);
}
/* FALLTHROUGH */
default:
fprintf(stderr,
"Failed to set capabilities on file '%s': %s\n",
argv[0], strerror(errno));
exit(1);
}
}
}
}
if (cap_d) {
cap_free(cap_d);
}
exit(0);
}
|