]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/capability-util.c
Merge pull request #12750 from keszybz/tmpfiles-c-copy
[thirdparty/systemd.git] / src / basic / capability-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <grp.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/capability.h>
8 #include <sys/prctl.h>
9 #include <unistd.h>
10
11 #include "alloc-util.h"
12 #include "capability-util.h"
13 #include "cap-list.h"
14 #include "fileio.h"
15 #include "log.h"
16 #include "macro.h"
17 #include "missing_prctl.h"
18 #include "parse-util.h"
19 #include "user-util.h"
20 #include "util.h"
21
22 int have_effective_cap(int value) {
23 _cleanup_cap_free_ cap_t cap;
24 cap_flag_value_t fv;
25
26 cap = cap_get_proc();
27 if (!cap)
28 return -errno;
29
30 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
31 return -errno;
32
33 return fv == CAP_SET;
34 }
35
36 unsigned long cap_last_cap(void) {
37 static thread_local unsigned long saved;
38 static thread_local bool valid = false;
39 _cleanup_free_ char *content = NULL;
40 unsigned long p = 0;
41 int r;
42
43 if (valid)
44 return saved;
45
46 /* available since linux-3.2 */
47 r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
48 if (r >= 0) {
49 r = safe_atolu(content, &p);
50 if (r >= 0) {
51
52 if (p > 63) /* Safety for the future: if one day the kernel learns more than 64 caps,
53 * then we are in trouble (since we, as much userspace and kernel space
54 * store capability masks in uint64_t types). Let's hence protect
55 * ourselves against that and always cap at 63 for now. */
56 p = 63;
57
58 saved = p;
59 valid = true;
60 return p;
61 }
62 }
63
64 /* fall back to syscall-probing for pre linux-3.2 */
65 p = MIN((unsigned long) CAP_LAST_CAP, 63U);
66
67 if (prctl(PR_CAPBSET_READ, p) < 0) {
68
69 /* Hmm, look downwards, until we find one that works */
70 for (p--; p > 0; p --)
71 if (prctl(PR_CAPBSET_READ, p) >= 0)
72 break;
73
74 } else {
75
76 /* Hmm, look upwards, until we find one that doesn't work */
77 for (; p < 63; p++)
78 if (prctl(PR_CAPBSET_READ, p+1) < 0)
79 break;
80 }
81
82 saved = p;
83 valid = true;
84
85 return p;
86 }
87
88 int capability_update_inherited_set(cap_t caps, uint64_t set) {
89 unsigned long i;
90
91 /* Add capabilities in the set to the inherited caps. Do not apply
92 * them yet. */
93
94 for (i = 0; i <= cap_last_cap(); i++) {
95
96 if (set & (UINT64_C(1) << i)) {
97 cap_value_t v;
98
99 v = (cap_value_t) i;
100
101 /* Make the capability inheritable. */
102 if (cap_set_flag(caps, CAP_INHERITABLE, 1, &v, CAP_SET) < 0)
103 return -errno;
104 }
105 }
106
107 return 0;
108 }
109
110 int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
111 _cleanup_cap_free_ cap_t caps = NULL;
112 unsigned long i;
113 int r;
114
115 /* Add the capabilities to the ambient set. */
116
117 if (also_inherit) {
118 caps = cap_get_proc();
119 if (!caps)
120 return -errno;
121
122 r = capability_update_inherited_set(caps, set);
123 if (r < 0)
124 return -errno;
125
126 if (cap_set_proc(caps) < 0)
127 return -errno;
128 }
129
130 for (i = 0; i <= cap_last_cap(); i++) {
131
132 if (set & (UINT64_C(1) << i)) {
133
134 /* Add the capability to the ambient set. */
135 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0)
136 return -errno;
137 }
138 }
139
140 return 0;
141 }
142
143 int capability_bounding_set_drop(uint64_t keep, bool right_now) {
144 _cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
145 cap_flag_value_t fv;
146 unsigned long i;
147 int r;
148
149 /* If we are run as PID 1 we will lack CAP_SETPCAP by default
150 * in the effective set (yes, the kernel drops that when
151 * executing init!), so get it back temporarily so that we can
152 * call PR_CAPBSET_DROP. */
153
154 before_cap = cap_get_proc();
155 if (!before_cap)
156 return -errno;
157
158 if (cap_get_flag(before_cap, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
159 return -errno;
160
161 if (fv != CAP_SET) {
162 _cleanup_cap_free_ cap_t temp_cap = NULL;
163 static const cap_value_t v = CAP_SETPCAP;
164
165 temp_cap = cap_dup(before_cap);
166 if (!temp_cap)
167 return -errno;
168
169 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0)
170 return -errno;
171
172 if (cap_set_proc(temp_cap) < 0)
173 log_debug_errno(errno, "Can't acquire effective CAP_SETPCAP bit, ignoring: %m");
174
175 /* If we didn't manage to acquire the CAP_SETPCAP bit, we continue anyway, after all this just means
176 * we'll fail later, when we actually intend to drop some capabilities. */
177 }
178
179 after_cap = cap_dup(before_cap);
180 if (!after_cap)
181 return -errno;
182
183 for (i = 0; i <= cap_last_cap(); i++) {
184 cap_value_t v;
185
186 if ((keep & (UINT64_C(1) << i)))
187 continue;
188
189 /* Drop it from the bounding set */
190 if (prctl(PR_CAPBSET_DROP, i) < 0) {
191 r = -errno;
192
193 /* If dropping the capability failed, let's see if we didn't have it in the first place. If so,
194 * continue anyway, as dropping a capability we didn't have in the first place doesn't really
195 * matter anyway. */
196 if (prctl(PR_CAPBSET_READ, i) != 0)
197 goto finish;
198 }
199 v = (cap_value_t) i;
200
201 /* Also drop it from the inheritable set, so
202 * that anything we exec() loses the
203 * capability for good. */
204 if (cap_set_flag(after_cap, CAP_INHERITABLE, 1, &v, CAP_CLEAR) < 0) {
205 r = -errno;
206 goto finish;
207 }
208
209 /* If we shall apply this right now drop it
210 * also from our own capability sets. */
211 if (right_now) {
212 if (cap_set_flag(after_cap, CAP_PERMITTED, 1, &v, CAP_CLEAR) < 0 ||
213 cap_set_flag(after_cap, CAP_EFFECTIVE, 1, &v, CAP_CLEAR) < 0) {
214 r = -errno;
215 goto finish;
216 }
217 }
218 }
219
220 r = 0;
221
222 finish:
223 if (cap_set_proc(after_cap) < 0) {
224 /* If there are no actual changes anyway then let's ignore this error. */
225 if (cap_compare(before_cap, after_cap) != 0)
226 r = -errno;
227 }
228
229 return r;
230 }
231
232 static int drop_from_file(const char *fn, uint64_t keep) {
233 _cleanup_free_ char *p = NULL;
234 uint64_t current, after;
235 uint32_t hi, lo;
236 int r, k;
237
238 r = read_one_line_file(fn, &p);
239 if (r < 0)
240 return r;
241
242 k = sscanf(p, "%" PRIu32 " %" PRIu32, &lo, &hi);
243 if (k != 2)
244 return -EIO;
245
246 current = (uint64_t) lo | ((uint64_t) hi << 32);
247 after = current & keep;
248
249 if (current == after)
250 return 0;
251
252 lo = after & UINT32_C(0xFFFFFFFF);
253 hi = (after >> 32) & UINT32_C(0xFFFFFFFF);
254
255 return write_string_filef(fn, 0, "%" PRIu32 " %" PRIu32, lo, hi);
256 }
257
258 int capability_bounding_set_drop_usermode(uint64_t keep) {
259 int r;
260
261 r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep);
262 if (r < 0)
263 return r;
264
265 r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep);
266 if (r < 0)
267 return r;
268
269 return r;
270 }
271
272 int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
273 int r;
274
275 /* Unfortunately we cannot leave privilege dropping to PID 1 here, since we want to run as user but
276 * want to keep some capabilities. Since file capabilities have been introduced this cannot be done
277 * across exec() anymore, unless our binary has the capability configured in the file system, which
278 * we want to avoid. */
279
280 if (setresgid(gid, gid, gid) < 0)
281 return log_error_errno(errno, "Failed to change group ID: %m");
282
283 r = maybe_setgroups(0, NULL);
284 if (r < 0)
285 return log_error_errno(r, "Failed to drop auxiliary groups list: %m");
286
287 /* Ensure we keep the permitted caps across the setresuid(). Note that we do this even if we actually
288 * don't want to keep any capabilities, since we want to be able to drop them from the bounding set
289 * too, and we can only do that if we have capabilities. */
290 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
291 return log_error_errno(errno, "Failed to enable keep capabilities flag: %m");
292
293 if (setresuid(uid, uid, uid) < 0)
294 return log_error_errno(errno, "Failed to change user ID: %m");
295
296 if (prctl(PR_SET_KEEPCAPS, 0) < 0)
297 return log_error_errno(errno, "Failed to disable keep capabilities flag: %m");
298
299 /* Drop all caps from the bounding set (as well as the inheritable/permitted/effective sets), except
300 * the ones we want to keep */
301 r = capability_bounding_set_drop(keep_capabilities, true);
302 if (r < 0)
303 return log_error_errno(r, "Failed to drop capabilities: %m");
304
305 /* Now upgrade the permitted caps we still kept to effective caps */
306 if (keep_capabilities != 0) {
307 cap_value_t bits[u64log2(keep_capabilities) + 1];
308 _cleanup_cap_free_ cap_t d = NULL;
309 unsigned i, j = 0;
310
311 d = cap_init();
312 if (!d)
313 return log_oom();
314
315 for (i = 0; i < ELEMENTSOF(bits); i++)
316 if (keep_capabilities & (1ULL << i))
317 bits[j++] = i;
318
319 /* use enough bits */
320 assert(i == 64 || (keep_capabilities >> i) == 0);
321 /* don't use too many bits */
322 assert(keep_capabilities & (UINT64_C(1) << (i - 1)));
323
324 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
325 cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0)
326 return log_error_errno(errno, "Failed to enable capabilities bits: %m");
327
328 if (cap_set_proc(d) < 0)
329 return log_error_errno(errno, "Failed to increase capabilities: %m");
330 }
331
332 return 0;
333 }
334
335 int drop_capability(cap_value_t cv) {
336 _cleanup_cap_free_ cap_t tmp_cap = NULL;
337
338 tmp_cap = cap_get_proc();
339 if (!tmp_cap)
340 return -errno;
341
342 if ((cap_set_flag(tmp_cap, CAP_INHERITABLE, 1, &cv, CAP_CLEAR) < 0) ||
343 (cap_set_flag(tmp_cap, CAP_PERMITTED, 1, &cv, CAP_CLEAR) < 0) ||
344 (cap_set_flag(tmp_cap, CAP_EFFECTIVE, 1, &cv, CAP_CLEAR) < 0))
345 return -errno;
346
347 if (cap_set_proc(tmp_cap) < 0)
348 return -errno;
349
350 return 0;
351 }
352
353 bool ambient_capabilities_supported(void) {
354 static int cache = -1;
355
356 if (cache >= 0)
357 return cache;
358
359 /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
360 * available. */
361
362 cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 ||
363 !IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS);
364
365 return cache;
366 }
367
368 bool capability_quintet_mangle(CapabilityQuintet *q) {
369 unsigned long i;
370 uint64_t combined, drop = 0;
371 bool ambient_supported;
372
373 assert(q);
374
375 combined = q->effective | q->bounding | q->inheritable | q->permitted;
376
377 ambient_supported = q->ambient != (uint64_t) -1;
378 if (ambient_supported)
379 combined |= q->ambient;
380
381 for (i = 0; i <= cap_last_cap(); i++) {
382 unsigned long bit = UINT64_C(1) << i;
383 if (!FLAGS_SET(combined, bit))
384 continue;
385
386 if (prctl(PR_CAPBSET_READ, i) > 0)
387 continue;
388
389 drop |= bit;
390
391 log_debug("Not in the current bounding set: %s", capability_to_name(i));
392 }
393
394 q->effective &= ~drop;
395 q->bounding &= ~drop;
396 q->inheritable &= ~drop;
397 q->permitted &= ~drop;
398
399 if (ambient_supported)
400 q->ambient &= ~drop;
401
402 return drop != 0; /* Let the caller know we changed something */
403 }
404
405 int capability_quintet_enforce(const CapabilityQuintet *q) {
406 _cleanup_cap_free_ cap_t c = NULL, modified = NULL;
407 int r;
408
409 if (q->ambient != (uint64_t) -1) {
410 unsigned long i;
411 bool changed = false;
412
413 c = cap_get_proc();
414 if (!c)
415 return -errno;
416
417 /* In order to raise the ambient caps set we first need to raise the matching inheritable + permitted
418 * cap */
419 for (i = 0; i <= cap_last_cap(); i++) {
420 uint64_t m = UINT64_C(1) << i;
421 cap_value_t cv = (cap_value_t) i;
422 cap_flag_value_t old_value_inheritable, old_value_permitted;
423
424 if ((q->ambient & m) == 0)
425 continue;
426
427 if (cap_get_flag(c, cv, CAP_INHERITABLE, &old_value_inheritable) < 0)
428 return -errno;
429 if (cap_get_flag(c, cv, CAP_PERMITTED, &old_value_permitted) < 0)
430 return -errno;
431
432 if (old_value_inheritable == CAP_SET && old_value_permitted == CAP_SET)
433 continue;
434
435 if (cap_set_flag(c, CAP_INHERITABLE, 1, &cv, CAP_SET) < 0)
436 return -errno;
437 if (cap_set_flag(c, CAP_PERMITTED, 1, &cv, CAP_SET) < 0)
438 return -errno;
439
440 changed = true;
441 }
442
443 if (changed)
444 if (cap_set_proc(c) < 0)
445 return -errno;
446
447 r = capability_ambient_set_apply(q->ambient, false);
448 if (r < 0)
449 return r;
450 }
451
452 if (q->inheritable != (uint64_t) -1 || q->permitted != (uint64_t) -1 || q->effective != (uint64_t) -1) {
453 bool changed = false;
454 unsigned long i;
455
456 if (!c) {
457 c = cap_get_proc();
458 if (!c)
459 return -errno;
460 }
461
462 for (i = 0; i <= cap_last_cap(); i++) {
463 uint64_t m = UINT64_C(1) << i;
464 cap_value_t cv = (cap_value_t) i;
465
466 if (q->inheritable != (uint64_t) -1) {
467 cap_flag_value_t old_value, new_value;
468
469 if (cap_get_flag(c, cv, CAP_INHERITABLE, &old_value) < 0) {
470 if (errno == EINVAL) /* If the kernel knows more caps than this
471 * version of libcap, then this will return
472 * EINVAL. In that case, simply ignore it,
473 * pretend it doesn't exist. */
474 continue;
475
476 return -errno;
477 }
478
479 new_value = (q->inheritable & m) ? CAP_SET : CAP_CLEAR;
480
481 if (old_value != new_value) {
482 changed = true;
483
484 if (cap_set_flag(c, CAP_INHERITABLE, 1, &cv, new_value) < 0)
485 return -errno;
486 }
487 }
488
489 if (q->permitted != (uint64_t) -1) {
490 cap_flag_value_t old_value, new_value;
491
492 if (cap_get_flag(c, cv, CAP_PERMITTED, &old_value) < 0) {
493 if (errno == EINVAL)
494 continue;
495
496 return -errno;
497 }
498
499 new_value = (q->permitted & m) ? CAP_SET : CAP_CLEAR;
500
501 if (old_value != new_value) {
502 changed = true;
503
504 if (cap_set_flag(c, CAP_PERMITTED, 1, &cv, new_value) < 0)
505 return -errno;
506 }
507 }
508
509 if (q->effective != (uint64_t) -1) {
510 cap_flag_value_t old_value, new_value;
511
512 if (cap_get_flag(c, cv, CAP_EFFECTIVE, &old_value) < 0) {
513 if (errno == EINVAL)
514 continue;
515
516 return -errno;
517 }
518
519 new_value = (q->effective & m) ? CAP_SET : CAP_CLEAR;
520
521 if (old_value != new_value) {
522 changed = true;
523
524 if (cap_set_flag(c, CAP_EFFECTIVE, 1, &cv, new_value) < 0)
525 return -errno;
526 }
527 }
528 }
529
530 if (changed) {
531 /* In order to change the bounding caps, we need to keep CAP_SETPCAP for a bit
532 * longer. Let's add it to our list hence for now. */
533 if (q->bounding != (uint64_t) -1) {
534 cap_value_t cv = CAP_SETPCAP;
535
536 modified = cap_dup(c);
537 if (!modified)
538 return -ENOMEM;
539
540 if (cap_set_flag(modified, CAP_PERMITTED, 1, &cv, CAP_SET) < 0)
541 return -errno;
542 if (cap_set_flag(modified, CAP_EFFECTIVE, 1, &cv, CAP_SET) < 0)
543 return -errno;
544
545 if (cap_compare(modified, c) == 0) {
546 /* No change? then drop this nonsense again */
547 cap_free(modified);
548 modified = NULL;
549 }
550 }
551
552 /* Now, let's enforce the caps for the first time. Note that this is where we acquire
553 * caps in any of the sets we currently don't have. We have to do this before
554 * dropping the bounding caps below, since at that point we can never acquire new
555 * caps in inherited/permitted/effective anymore, but only lose them. */
556 if (cap_set_proc(modified ?: c) < 0)
557 return -errno;
558 }
559 }
560
561 if (q->bounding != (uint64_t) -1) {
562 r = capability_bounding_set_drop(q->bounding, false);
563 if (r < 0)
564 return r;
565 }
566
567 /* If needed, let's now set the caps again, this time in the final version, which differs from what
568 * we have already set only in the CAP_SETPCAP bit, which we needed for dropping the bounding
569 * bits. This call only undoes bits and doesn't acquire any which means the bounding caps don't
570 * matter. */
571 if (modified)
572 if (cap_set_proc(c) < 0)
573 return -errno;
574
575 return 0;
576 }