diff options
author | 2016-11-20 17:52:30 -0500 | |
---|---|---|
committer | 2016-11-20 17:52:30 -0500 | |
commit | 62b9173095929c7ef21b9fb2385f54ceba956aa7 (patch) | |
tree | 01cd4400eda9486b7160cf7d306149f92b6ca3e8 /src/firejail | |
parent | chroot testing (diff) | |
download | firejail-62b9173095929c7ef21b9fb2385f54ceba956aa7.tar.gz firejail-62b9173095929c7ef21b9fb2385f54ceba956aa7.tar.zst firejail-62b9173095929c7ef21b9fb2385f54ceba956aa7.zip |
caps testing
Diffstat (limited to 'src/firejail')
-rw-r--r-- | src/firejail/caps.c | 77 | ||||
-rw-r--r-- | src/firejail/firejail.h | 2 | ||||
-rw-r--r-- | src/firejail/main.c | 6 | ||||
-rw-r--r-- | src/firejail/profile.c | 6 |
4 files changed, 42 insertions, 49 deletions
diff --git a/src/firejail/caps.c b/src/firejail/caps.c index ba811cada..6cfa36629 100644 --- a/src/firejail/caps.c +++ b/src/firejail/caps.c | |||
@@ -181,12 +181,10 @@ static int caps_find_name(const char *name) { | |||
181 | } | 181 | } |
182 | 182 | ||
183 | // return 1 if error, 0 if OK | 183 | // return 1 if error, 0 if OK |
184 | int caps_check_list(const char *clist, void (*callback)(int)) { | 184 | void caps_check_list(const char *clist, void (*callback)(int)) { |
185 | // don't allow empty lists | 185 | // don't allow empty lists |
186 | if (clist == NULL || *clist == '\0') { | 186 | if (clist == NULL || *clist == '\0') |
187 | fprintf(stderr, "Error: empty capabilities lists are not allowed\n"); | 187 | goto errexit; |
188 | return -1; | ||
189 | } | ||
190 | 188 | ||
191 | // work on a copy of the string | 189 | // work on a copy of the string |
192 | char *str = strdup(clist); | 190 | char *str = strdup(clist); |
@@ -201,11 +199,8 @@ int caps_check_list(const char *clist, void (*callback)(int)) { | |||
201 | else if (*ptr == ',') { | 199 | else if (*ptr == ',') { |
202 | *ptr = '\0'; | 200 | *ptr = '\0'; |
203 | int nr = caps_find_name(start); | 201 | int nr = caps_find_name(start); |
204 | if (nr == -1) { | 202 | if (nr == -1) |
205 | fprintf(stderr, "Error: capability %s not found\n", start); | 203 | goto errexit; |
206 | free(str); | ||
207 | return -1; | ||
208 | } | ||
209 | else if (callback != NULL) | 204 | else if (callback != NULL) |
210 | callback(nr); | 205 | callback(nr); |
211 | 206 | ||
@@ -215,17 +210,18 @@ int caps_check_list(const char *clist, void (*callback)(int)) { | |||
215 | } | 210 | } |
216 | if (*start != '\0') { | 211 | if (*start != '\0') { |
217 | int nr = caps_find_name(start); | 212 | int nr = caps_find_name(start); |
218 | if (nr == -1) { | 213 | if (nr == -1) |
219 | fprintf(stderr, "Error: capability %s not found\n", start); | 214 | goto errexit; |
220 | free(str); | ||
221 | return -1; | ||
222 | } | ||
223 | else if (callback != NULL) | 215 | else if (callback != NULL) |
224 | callback(nr); | 216 | callback(nr); |
225 | } | 217 | } |
226 | 218 | ||
227 | free(str); | 219 | free(str); |
228 | return 0; | 220 | return; |
221 | |||
222 | errexit: | ||
223 | fprintf(stderr, "Error: capability \"%s\" not found\n", start); | ||
224 | exit(1); | ||
229 | } | 225 | } |
230 | 226 | ||
231 | void caps_print(void) { | 227 | void caps_print(void) { |
@@ -256,49 +252,53 @@ void caps_print(void) { | |||
256 | // enabled by default | 252 | // enabled by default |
257 | int caps_default_filter(void) { | 253 | int caps_default_filter(void) { |
258 | // drop capabilities | 254 | // drop capabilities |
259 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_MODULE, 0, 0, 0) && arg_debug) | 255 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_MODULE, 0, 0, 0)) |
260 | fprintf(stderr, "Warning: cannot drop CAP_SYS_MODULE"); | 256 | goto errexit; |
261 | else if (arg_debug) | 257 | else if (arg_debug) |
262 | printf("Drop CAP_SYS_MODULE\n"); | 258 | printf("Drop CAP_SYS_MODULE\n"); |
263 | 259 | ||
264 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_RAWIO, 0, 0, 0) && arg_debug) | 260 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_RAWIO, 0, 0, 0)) |
265 | fprintf(stderr, "Warning: cannot drop CAP_SYS_RAWIO"); | 261 | goto errexit; |
266 | else if (arg_debug) | 262 | else if (arg_debug) |
267 | printf("Drop CAP_SYS_RAWIO\n"); | 263 | printf("Drop CAP_SYS_RAWIO\n"); |
268 | 264 | ||
269 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0) && arg_debug) | 265 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) |
270 | fprintf(stderr, "Warning: cannot drop CAP_SYS_BOOT"); | 266 | goto errexit; |
271 | else if (arg_debug) | 267 | else if (arg_debug) |
272 | printf("Drop CAP_SYS_BOOT\n"); | 268 | printf("Drop CAP_SYS_BOOT\n"); |
273 | 269 | ||
274 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_NICE, 0, 0, 0) && arg_debug) | 270 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_NICE, 0, 0, 0)) |
275 | fprintf(stderr, "Warning: cannot drop CAP_SYS_NICE"); | 271 | goto errexit; |
276 | else if (arg_debug) | 272 | else if (arg_debug) |
277 | printf("Drop CAP_SYS_NICE\n"); | 273 | printf("Drop CAP_SYS_NICE\n"); |
278 | 274 | ||
279 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_TTY_CONFIG, 0, 0, 0) && arg_debug) | 275 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_TTY_CONFIG, 0, 0, 0)) |
280 | fprintf(stderr, "Warning: cannot drop CAP_SYS_TTY_CONFIG"); | 276 | goto errexit; |
281 | else if (arg_debug) | 277 | else if (arg_debug) |
282 | printf("Drop CAP_SYS_TTY_CONFIG\n"); | 278 | printf("Drop CAP_SYS_TTY_CONFIG\n"); |
283 | 279 | ||
284 | #ifdef CAP_SYSLOG | 280 | #ifdef CAP_SYSLOG |
285 | if (prctl(PR_CAPBSET_DROP, CAP_SYSLOG, 0, 0, 0) && arg_debug) | 281 | if (prctl(PR_CAPBSET_DROP, CAP_SYSLOG, 0, 0, 0)) |
286 | fprintf(stderr, "Warning: cannot drop CAP_SYSLOG"); | 282 | goto errexit; |
287 | else if (arg_debug) | 283 | else if (arg_debug) |
288 | printf("Drop CAP_SYSLOG\n"); | 284 | printf("Drop CAP_SYSLOG\n"); |
289 | #endif | 285 | #endif |
290 | 286 | ||
291 | if (prctl(PR_CAPBSET_DROP, CAP_MKNOD, 0, 0, 0) && arg_debug) | 287 | if (prctl(PR_CAPBSET_DROP, CAP_MKNOD, 0, 0, 0)) |
292 | fprintf(stderr, "Warning: cannot drop CAP_MKNOD"); | 288 | goto errexit; |
293 | else if (arg_debug) | 289 | else if (arg_debug) |
294 | printf("Drop CAP_MKNOD\n"); | 290 | printf("Drop CAP_MKNOD\n"); |
295 | 291 | ||
296 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_ADMIN, 0, 0, 0) && arg_debug) | 292 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_ADMIN, 0, 0, 0)) |
297 | fprintf(stderr, "Warning: cannot drop CAP_SYS_ADMIN"); | 293 | goto errexit; |
298 | else if (arg_debug) | 294 | else if (arg_debug) |
299 | printf("Drop CAP_SYS_ADMIN\n"); | 295 | printf("Drop CAP_SYS_ADMIN\n"); |
300 | 296 | ||
301 | return 0; | 297 | return 0; |
298 | |||
299 | errexit: | ||
300 | fprintf(stderr, "Error: cannot drop capabilities\n"); | ||
301 | exit(1); | ||
302 | } | 302 | } |
303 | 303 | ||
304 | void caps_drop_all(void) { | 304 | void caps_drop_all(void) { |
@@ -359,19 +359,14 @@ static uint64_t extract_caps(int pid) { | |||
359 | EUID_ASSERT(); | 359 | EUID_ASSERT(); |
360 | 360 | ||
361 | char *file; | 361 | char *file; |
362 | if (asprintf(&file, "/proc/%d/status", pid) == -1) { | 362 | if (asprintf(&file, "/proc/%d/status", pid) == -1) |
363 | errExit("asprintf"); | 363 | errExit("asprintf"); |
364 | exit(1); | ||
365 | } | ||
366 | 364 | ||
367 | EUID_ROOT(); // grsecurity | 365 | EUID_ROOT(); // grsecurity |
368 | FILE *fp = fopen(file, "r"); | 366 | FILE *fp = fopen(file, "r"); |
369 | EUID_USER(); // grsecurity | 367 | EUID_USER(); // grsecurity |
370 | if (!fp) { | 368 | if (!fp) |
371 | printf("Error: cannot open %s\n", file); | 369 | goto errexit; |
372 | free(file); | ||
373 | exit(1); | ||
374 | } | ||
375 | 370 | ||
376 | char buf[MAXBUF]; | 371 | char buf[MAXBUF]; |
377 | while (fgets(buf, MAXBUF, fp)) { | 372 | while (fgets(buf, MAXBUF, fp)) { |
@@ -385,6 +380,8 @@ static uint64_t extract_caps(int pid) { | |||
385 | } | 380 | } |
386 | } | 381 | } |
387 | fclose(fp); | 382 | fclose(fp); |
383 | |||
384 | errexit: | ||
388 | free(file); | 385 | free(file); |
389 | fprintf(stderr, "Error: cannot read caps configuration\n"); | 386 | fprintf(stderr, "Error: cannot read caps configuration\n"); |
390 | exit(1); | 387 | exit(1); |
diff --git a/src/firejail/firejail.h b/src/firejail/firejail.h index 2e031ce04..4ae3cfd9f 100644 --- a/src/firejail/firejail.h +++ b/src/firejail/firejail.h | |||
@@ -508,7 +508,7 @@ int caps_default_filter(void); | |||
508 | void caps_print(void); | 508 | void caps_print(void); |
509 | void caps_drop_all(void); | 509 | void caps_drop_all(void); |
510 | void caps_set(uint64_t caps); | 510 | void caps_set(uint64_t caps); |
511 | int caps_check_list(const char *clist, void (*callback)(int)); | 511 | void caps_check_list(const char *clist, void (*callback)(int)); |
512 | void caps_drop_list(const char *clist); | 512 | void caps_drop_list(const char *clist); |
513 | void caps_keep_list(const char *clist); | 513 | void caps_keep_list(const char *clist); |
514 | void caps_print_filter(pid_t pid); | 514 | void caps_print_filter(pid_t pid); |
diff --git a/src/firejail/main.c b/src/firejail/main.c index ff7b762cd..111a1d751 100644 --- a/src/firejail/main.c +++ b/src/firejail/main.c | |||
@@ -1186,8 +1186,7 @@ int main(int argc, char **argv) { | |||
1186 | if (!arg_caps_list) | 1186 | if (!arg_caps_list) |
1187 | errExit("strdup"); | 1187 | errExit("strdup"); |
1188 | // verify caps list and exit if problems | 1188 | // verify caps list and exit if problems |
1189 | if (caps_check_list(arg_caps_list, NULL)) | 1189 | caps_check_list(arg_caps_list, NULL); |
1190 | return 1; | ||
1191 | } | 1190 | } |
1192 | else if (strncmp(argv[i], "--caps.keep=", 12) == 0) { | 1191 | else if (strncmp(argv[i], "--caps.keep=", 12) == 0) { |
1193 | arg_caps_keep = 1; | 1192 | arg_caps_keep = 1; |
@@ -1195,8 +1194,7 @@ int main(int argc, char **argv) { | |||
1195 | if (!arg_caps_list) | 1194 | if (!arg_caps_list) |
1196 | errExit("strdup"); | 1195 | errExit("strdup"); |
1197 | // verify caps list and exit if problems | 1196 | // verify caps list and exit if problems |
1198 | if (caps_check_list(arg_caps_list, NULL)) | 1197 | caps_check_list(arg_caps_list, NULL); |
1199 | return 1; | ||
1200 | } | 1198 | } |
1201 | 1199 | ||
1202 | 1200 | ||
diff --git a/src/firejail/profile.c b/src/firejail/profile.c index 688fa9609..abb8bd9b6 100644 --- a/src/firejail/profile.c +++ b/src/firejail/profile.c | |||
@@ -570,8 +570,7 @@ int profile_check_line(char *ptr, int lineno, const char *fname) { | |||
570 | if (!arg_caps_list) | 570 | if (!arg_caps_list) |
571 | errExit("strdup"); | 571 | errExit("strdup"); |
572 | // verify caps list and exit if problems | 572 | // verify caps list and exit if problems |
573 | if (caps_check_list(arg_caps_list, NULL)) | 573 | caps_check_list(arg_caps_list, NULL); |
574 | exit(1); | ||
575 | return 0; | 574 | return 0; |
576 | } | 575 | } |
577 | 576 | ||
@@ -582,8 +581,7 @@ int profile_check_line(char *ptr, int lineno, const char *fname) { | |||
582 | if (!arg_caps_list) | 581 | if (!arg_caps_list) |
583 | errExit("strdup"); | 582 | errExit("strdup"); |
584 | // verify caps list and exit if problems | 583 | // verify caps list and exit if problems |
585 | if (caps_check_list(arg_caps_list, NULL)) | 584 | caps_check_list(arg_caps_list, NULL); |
586 | exit(1); | ||
587 | return 0; | 585 | return 0; |
588 | } | 586 | } |
589 | 587 | ||