aboutsummaryrefslogtreecommitdiffstats
path: root/src/faudit/caps.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/faudit/caps.c')
-rw-r--r--src/faudit/caps.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/faudit/caps.c b/src/faudit/caps.c
new file mode 100644
index 000000000..364cfcd03
--- /dev/null
+++ b/src/faudit/caps.c
@@ -0,0 +1,77 @@
1/*
2 * Copyright (C) 2014-2016 Firejail Authors
3 *
4 * This file is part of firejail project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20#include "faudit.h"
21#include <linux/capability.h>
22
23#define MAXBUF 4098
24static int extract_caps(uint64_t *val) {
25 FILE *fp = fopen("/proc/self/status", "r");
26 if (!fp)
27 return 1;
28
29 char buf[MAXBUF];
30 while (fgets(buf, MAXBUF, fp)) {
31 if (strncmp(buf, "CapBnd:\t", 8) == 0) {
32 char *ptr = buf + 8;
33 unsigned long long tmp;
34 sscanf(ptr, "%llx", &tmp);
35 *val = tmp;
36 fclose(fp);
37 return 0;
38 }
39 }
40
41 fclose(fp);
42 return 1;
43}
44
45// return 1 if the capability is in tbe map
46static int check_capability(uint64_t map, int cap) {
47 int i;
48 uint64_t mask = 1ULL;
49
50 for (i = 0; i < 64; i++, mask <<= 1) {
51 if ((i == cap) && (mask & map))
52 return 1;
53 }
54
55 return 0;
56}
57
58void caps(void) {
59 uint64_t caps_val;
60
61 if (extract_caps(&caps_val)) {
62 printf("SKIP: cannot extract capabilities on this platform\n");
63 return;
64 }
65
66 if (caps_val) {
67 printf("BAD: the capability map is %llx, it should be all zero\n", (unsigned long long) caps_val);
68
69 if (check_capability(caps_val, CAP_SYS_ADMIN))
70 printf("UGLY: CAP_SYS_ADMIN is enabled\n");
71 if (check_capability(caps_val, CAP_SYS_BOOT))
72 printf("UGLY: CAP_SYS_BOOT is enabled\n");
73 }
74 else
75 printf("GOOD: all capabilities are disabled\n");
76}
77