]>
Commit | Line | Data |
---|---|---|
f47781d8 MP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
e735f4d4 | 20 | #include <sys/prctl.h> |
f47781d8 | 21 | |
db2df898 MP |
22 | #include "alloc-util.h" |
23 | #include "cap-list.h" | |
24 | #include "capability-util.h" | |
25 | #include "fileio.h" | |
26 | #include "parse-util.h" | |
27 | #include "util.h" | |
28 | ||
e735f4d4 MP |
29 | /* verify the capability parser */ |
30 | static void test_cap_list(void) { | |
f47781d8 MP |
31 | int i; |
32 | ||
33 | assert_se(!capability_to_name(-1)); | |
e735f4d4 | 34 | assert_se(!capability_to_name(capability_list_length())); |
f47781d8 | 35 | |
e735f4d4 | 36 | for (i = 0; i < capability_list_length(); i++) { |
f47781d8 MP |
37 | const char *n; |
38 | ||
39 | assert_se(n = capability_to_name(i)); | |
40 | assert_se(capability_from_name(n) == i); | |
41 | printf("%s = %i\n", n, i); | |
42 | } | |
43 | ||
44 | assert_se(capability_from_name("asdfbsd") == -EINVAL); | |
45 | assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ); | |
e735f4d4 MP |
46 | assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ); |
47 | assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ); | |
f47781d8 MP |
48 | assert_se(capability_from_name("0") == 0); |
49 | assert_se(capability_from_name("15") == 15); | |
50 | assert_se(capability_from_name("-1") == -EINVAL); | |
51 | ||
e735f4d4 MP |
52 | for (i = 0; i < capability_list_length(); i++) { |
53 | _cleanup_cap_free_charp_ char *a = NULL; | |
54 | const char *b; | |
55 | unsigned u; | |
56 | ||
57 | assert_se(a = cap_to_name(i)); | |
58 | ||
59 | /* quit the loop as soon as libcap starts returning | |
60 | * numeric ids, formatted as strings */ | |
61 | if (safe_atou(a, &u) >= 0) | |
62 | break; | |
63 | ||
64 | assert_se(b = capability_to_name(i)); | |
65 | ||
66 | printf("%s vs. %s\n", a, b); | |
67 | ||
68 | assert_se(strcasecmp(a, b) == 0); | |
69 | } | |
70 | } | |
71 | ||
72 | /* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ | |
73 | static void test_last_cap_file(void) { | |
74 | _cleanup_free_ char *content = NULL; | |
75 | unsigned long val = 0; | |
76 | int r; | |
77 | ||
78 | r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); | |
79 | assert_se(r >= 0); | |
80 | ||
81 | r = safe_atolu(content, &val); | |
82 | assert_se(r >= 0); | |
83 | assert_se(val != 0); | |
84 | assert_se(val == cap_last_cap()); | |
85 | } | |
86 | ||
87 | /* verify cap_last_cap() against syscall probing */ | |
88 | static void test_last_cap_probe(void) { | |
89 | unsigned long p = (unsigned long)CAP_LAST_CAP; | |
90 | ||
91 | if (prctl(PR_CAPBSET_READ, p) < 0) { | |
92 | for (p--; p > 0; p --) | |
93 | if (prctl(PR_CAPBSET_READ, p) >= 0) | |
94 | break; | |
95 | } else { | |
96 | for (;; p++) | |
97 | if (prctl(PR_CAPBSET_READ, p+1) < 0) | |
98 | break; | |
99 | } | |
100 | ||
101 | assert_se(p != 0); | |
102 | assert_se(p == cap_last_cap()); | |
103 | } | |
104 | ||
105 | int main(int argc, char *argv[]) { | |
106 | test_cap_list(); | |
107 | test_last_cap_file(); | |
108 | test_last_cap_probe(); | |
109 | ||
f47781d8 MP |
110 | return 0; |
111 | } |