Debian Codegraph @

progs/setcap.c

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