Debian Codegraph @

src/test/test-capability-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4 #include <pwd.h>
5 #include <stdlib.h>
6 #include <sys/prctl.h>
7 #include <sys/socket.h>
8 #include <unistd.h>
9 #include "pidref.h"
10
11 #define TEST_CAPABILITY_C
12
13 #include "alloc-util.h"
14 #include "capability-list.h"
15 #include "capability-util.h"
16 #include "errno-util.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "parse-util.h"
20 #include "process-util.h"
21 #include "tests.h"
22
23 static uid_t test_uid = -1;
24 static gid_t test_gid = -1;
25 static bool run_ambient = false;
26
27 #if HAS_FEATURE_ADDRESS_SANITIZER
28 /* Keep CAP_SYS_PTRACE when running under Address Sanitizer */
29 static const uint64_t test_flags = UINT64_C(1) << CAP_SYS_PTRACE;
30 #else
31 /* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */
32 static const uint64_t test_flags = UINT64_C(1) << CAP_DAC_OVERRIDE;
33 #endif
34
35 static void show_capabilities(void) {
36 _cleanup_free_ char *e = NULL, *p = NULL, *i = NULL;
37 CapabilityQuintet q;
38
39 ASSERT_OK(capability_get(&q));
40 ASSERT_OK(capability_set_to_string(q.effective, &e));
41 ASSERT_OK(capability_set_to_string(q.permitted, &p));
42 ASSERT_OK(capability_set_to_string(q.inheritable, &i));
43
44 log_info("Capabilities:e=%s p=%s, i=%s", e, p, i);
45 }
46
47 /* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */
48 TEST(last_cap_file) {
49 _cleanup_free_ char *content = NULL;
50 unsigned long val = 0;
51 int r;
52
53 r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
54 if (ERRNO_IS_NEG_PRIVILEGE(r))
55 return (void) log_tests_skipped_errno(r, "Failed to /proc/sys/kernel/cap_last_cap");
56 ASSERT_OK(r);
57
58 ASSERT_OK(safe_atolu(content, &val));
59 ASSERT_NE(val, 0UL);
60 ASSERT_EQ(val, cap_last_cap());
61 }
62
63 /* verify cap_last_cap() against syscall probing */
64 TEST(last_cap_probe) {
65 unsigned long p = (unsigned long)CAP_LAST_CAP;
66
67 if (prctl(PR_CAPBSET_READ, p) < 0) {
68 for (p--; p > 0; p--)
69 if (prctl(PR_CAPBSET_READ, p) >= 0)
70 break;
71 } else {
72 for (;; p++)
73 if (prctl(PR_CAPBSET_READ, p+1) < 0)
74 break;
75 }
76
77 ASSERT_NE(p, 0UL);
78 ASSERT_EQ(p, cap_last_cap());
79 }
80
81 static void test_drop_privileges_keep_net_raw(void) {
82 int sock;
83
84 sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
85 ASSERT_OK_ERRNO(sock);
86 safe_close(sock);
87
88 ASSERT_OK(drop_privileges(test_uid, test_gid, test_flags | (1ULL << CAP_NET_RAW)));
89 ASSERT_EQ(getuid(), test_uid);
90 ASSERT_EQ(getgid(), test_gid);
91 show_capabilities();
92
93 sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
94 ASSERT_OK(sock);
95 safe_close(sock);
96 }
97
98 static void test_drop_privileges_dontkeep_net_raw(void) {
99 int sock;
100
101 sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
102 ASSERT_OK(sock);
103 safe_close(sock);
104
105 ASSERT_OK(drop_privileges(test_uid, test_gid, test_flags));
106 ASSERT_EQ(getuid(), test_uid);
107 ASSERT_EQ(getgid(), test_gid);
108 show_capabilities();
109
110 sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
111 ASSERT_LT(sock, 0);
112 }
113
114 static void test_drop_privileges_fail(void) {
115 ASSERT_OK(drop_privileges(test_uid, test_gid, test_flags));
116 ASSERT_EQ(getuid(), test_uid);
117 ASSERT_EQ(getgid(), test_gid);
118
119 ASSERT_FAIL(drop_privileges(test_uid, test_gid, test_flags));
120 ASSERT_FAIL(drop_privileges(0, 0, test_flags));
121 }
122
123 TEST(drop_privileges) {
124 int r;
125
126 if (getuid() != 0)
127 return (void) log_tests_skipped("not running as root");
128 if (userns_has_single_user())
129 return (void) log_tests_skipped("running in single-user user namespace");
130
131 r = ASSERT_OK(pidref_safe_fork("test-cap", FORK_WAIT|FORK_DEATHSIG_SIGKILL|FORK_LOG, /* ret= */ NULL));
132 if (r == 0) {
133 test_drop_privileges_fail();
134 _exit(EXIT_SUCCESS);
135 }
136
137 if (have_effective_cap(CAP_NET_RAW) <= 0) /* The remaining two tests only work if we have CAP_NET_RAW
138 * in the first place. If we are run in some restricted
139 * container environment we might not. */
140 return;
141
142 r = ASSERT_OK(pidref_safe_fork("test-cap", FORK_WAIT|FORK_DEATHSIG_SIGKILL|FORK_LOG, /* ret= */ NULL));
143 if (r == 0) {
144 test_drop_privileges_keep_net_raw();
145 _exit(EXIT_SUCCESS);
146 }
147
148 r = ASSERT_OK(pidref_safe_fork("test-cap", FORK_WAIT|FORK_DEATHSIG_SIGKILL|FORK_LOG, /* ret= */ NULL));
149 if (r == 0) {
150 test_drop_privileges_dontkeep_net_raw();
151 _exit(EXIT_SUCCESS);
152 }
153 }
154
155 static void test_have_effective_cap_impl(void) {
156 ASSERT_GT(have_effective_cap(CAP_KILL), 0);
157 ASSERT_GT(have_effective_cap(CAP_CHOWN), 0);
158
159 ASSERT_OK(drop_privileges(test_uid, test_gid, test_flags | (1ULL << CAP_KILL)));
160 ASSERT_EQ(getuid(), test_uid);
161 ASSERT_EQ(getgid(), test_gid);
162
163 ASSERT_GT(have_effective_cap(CAP_KILL), 0);
164 ASSERT_EQ(have_effective_cap(CAP_CHOWN), 0);
165 }
166
167 TEST(have_effective_cap) {
168 int r;
169
170 if (getuid() != 0)
171 return (void) log_tests_skipped("not running as root");
172 if (userns_has_single_user())
173 return (void) log_tests_skipped("running in single-user user namespace");
174
175 r = ASSERT_OK(pidref_safe_fork("test-cap", FORK_WAIT|FORK_DEATHSIG_SIGKILL|FORK_LOG, /* ret= */ NULL));
176 if (r == 0) {
177 test_have_effective_cap_impl();
178 _exit(EXIT_SUCCESS);
179 }
180 }
181
182 static void test_apply_ambient_caps_impl(void) {
183 ASSERT_OK_EQ_ERRNO(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0), 0);
184
185 ASSERT_OK(capability_ambient_set_apply(UINT64_C(1) << CAP_CHOWN, /* also_inherit= */ true));
186 ASSERT_OK_POSITIVE(have_inheritable_cap(CAP_CHOWN));
187
188 ASSERT_OK_EQ_ERRNO(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0), 1);
189
190 ASSERT_OK(capability_ambient_set_apply(0, /* also_inherit= */ true));
191 ASSERT_OK_ZERO(have_inheritable_cap(CAP_CHOWN));
192
193 ASSERT_OK_EQ_ERRNO(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0), 0);
194 }
195
196 TEST(apply_ambient_caps) {
197 int r;
198
199 if (getuid() != 0)
200 return (void) log_tests_skipped("not running as root");
201 if (!run_ambient)
202 return (void) log_tests_skipped("ambient caps not supported");
203
204 r = ASSERT_OK(pidref_safe_fork("test-cap", FORK_WAIT|FORK_DEATHSIG_SIGKILL|FORK_LOG, /* ret= */ NULL));
205 if (r == 0) {
206 test_apply_ambient_caps_impl();
207 _exit(EXIT_SUCCESS);
208 }
209 }
210
211 TEST(ensure_cap_64_bit) {
212 _cleanup_free_ char *content = NULL;
213 unsigned long p = 0;
214 int r;
215
216 r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
217 if (ERRNO_IS_NEG_PRIVILEGE(r))
218 return (void) log_tests_skipped_errno(r, "Failed to /proc/sys/kernel/cap_last_cap");
219 ASSERT_OK(r);
220
221 ASSERT_OK(safe_atolu(content, &p));
222
223 /* If caps don't fit into 64-bit anymore, we have a problem, fail the test. Moreover, we use
224 * UINT64_MAX as unset, hence it must be smaller than or equals to 62 (CAP_LIMIT). */
225 ASSERT_LE(p, (unsigned long) CAP_LIMIT);
226 }
227
228 TEST(capability_get_ambient) {
229 uint64_t c;
230 int r;
231
232 if (getuid() != 0)
233 return (void) log_tests_skipped("not running as root");
234
235 ASSERT_OK(capability_get_ambient(&c));
236
237 r = prctl(PR_CAPBSET_READ, CAP_MKNOD);
238 if (r <= 0)
239 return (void) log_tests_skipped("Lacking CAP_MKNOD, skipping getambient test.");
240 r = prctl(PR_CAPBSET_READ, CAP_LINUX_IMMUTABLE);
241 if (r <= 0)
242 return (void) log_tests_skipped("Lacking CAP_LINUX_IMMUTABLE, skipping getambient test.");
243
244 r = ASSERT_OK(pidref_safe_fork(
245 "(getambient)",
246 FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_WAIT|FORK_LOG,
247 /* ret= */ NULL));
248
249 if (r == 0) {
250 int x, y;
251 /* child */
252 ASSERT_OK(capability_get_ambient(&c));
253
254 x = capability_ambient_set_apply(
255 (UINT64_C(1) << CAP_MKNOD)|
256 (UINT64_C(1) << CAP_LINUX_IMMUTABLE),
257 /* also_inherit= */ true);
258 ASSERT_TRUE(x >= 0 || ERRNO_IS_PRIVILEGE(x));
259
260 ASSERT_OK(capability_get_ambient(&c));
261 ASSERT_TRUE(x < 0 || FLAGS_SET(c, UINT64_C(1) << CAP_MKNOD));
262 ASSERT_TRUE(x < 0 || FLAGS_SET(c, UINT64_C(1) << CAP_LINUX_IMMUTABLE));
263 ASSERT_TRUE(x < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_SETPCAP));
264
265 y = capability_bounding_set_drop(
266 ((UINT64_C(1) << CAP_LINUX_IMMUTABLE)|
267 (UINT64_C(1) << CAP_SETPCAP)),
268 /* right_now= */ true);
269 ASSERT_TRUE(y >= 0 || ERRNO_IS_PRIVILEGE(y));
270
271 ASSERT_OK(capability_get_ambient(&c));
272 ASSERT_TRUE(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_MKNOD));
273 ASSERT_TRUE(x < 0 || y < 0 || FLAGS_SET(c, UINT64_C(1) << CAP_LINUX_IMMUTABLE));
274 ASSERT_TRUE(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_SETPCAP));
275
276 y = capability_bounding_set_drop(
277 (UINT64_C(1) << CAP_SETPCAP),
278 /* right_now= */ true);
279 ASSERT_TRUE(y >= 0 || ERRNO_IS_PRIVILEGE(y));
280
281 ASSERT_OK(capability_get_ambient(&c));
282 ASSERT_TRUE(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_MKNOD));
283 ASSERT_TRUE(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_LINUX_IMMUTABLE));
284 ASSERT_TRUE(x < 0 || y < 0 || !FLAGS_SET(c, UINT64_C(1) << CAP_SETPCAP));
285
286 _exit(EXIT_SUCCESS);
287 }
288 }
289
290 TEST(pidref_get_capability) {
291 CapabilityQuintet q = CAPABILITY_QUINTET_NULL;
292
293 if (getuid() != 0)
294 return (void) log_tests_skipped("not running as root");
295
296 ASSERT_OK(pidref_get_capability(&PIDREF_MAKE_FROM_PID(getpid_cached()), &q));
297
298 ASSERT_NE(q.effective, CAP_MASK_UNSET);
299 ASSERT_NE(q.inheritable, CAP_MASK_UNSET);
300 ASSERT_NE(q.permitted, CAP_MASK_UNSET);
301 ASSERT_NE(q.effective, CAP_MASK_UNSET);
302 ASSERT_NE(q.ambient, CAP_MASK_UNSET);
303 }
304
305 static int intro(void) {
306 /* Try to set up nobody user/ambient caps for tests that need them.
307 * Not finding nobody is non-fatal — those tests will skip themselves. */
308 struct passwd *nobody = getpwnam(NOBODY_USER_NAME);
309 if (nobody) {
310 test_uid = nobody->pw_uid;
311 test_gid = nobody->pw_gid;
312 }
313
314 int r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
315 /* There's support for PR_CAP_AMBIENT if the prctl() call succeeded or error code was something else
316 * than EINVAL. The EINVAL check should be good enough to rule out false positives. */
317 run_ambient = r >= 0 || errno != EINVAL;
318
319 if (getuid() == 0)
320 show_capabilities();
321
322 return EXIT_SUCCESS;
323 }
324
325 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);