udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / capability-util.c
blobe033e35cdbeba7fbd73d654b1e1edcb8f15ee95d
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/prctl.h>
7 #include <unistd.h>
9 #include "alloc-util.h"
10 #include "capability-util.h"
11 #include "cap-list.h"
12 #include "fileio.h"
13 #include "log.h"
14 #include "logarithm.h"
15 #include "macro.h"
16 #include "missing_prctl.h"
17 #include "missing_threads.h"
18 #include "parse-util.h"
19 #include "user-util.h"
21 int have_effective_cap(int value) {
22 _cleanup_cap_free_ cap_t cap = NULL;
23 cap_flag_value_t fv = CAP_CLEAR; /* To avoid false-positive use-of-uninitialized-value error reported
24 * by fuzzers. */
26 cap = cap_get_proc();
27 if (!cap)
28 return -errno;
30 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
31 return -errno;
33 return fv == CAP_SET;
36 unsigned cap_last_cap(void) {
37 static thread_local unsigned saved;
38 static thread_local bool valid = false;
39 _cleanup_free_ char *content = NULL;
40 unsigned long p = 0;
41 int r;
43 if (valid)
44 return saved;
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) {
52 if (p > CAP_LIMIT) /* Safety for the future: if one day the kernel learns more than
53 * 64 caps, then we are in trouble (since we, as much userspace
54 * and kernel space store capability masks in uint64_t types). We
55 * also want to use UINT64_MAX as marker for "unset". Hence let's
56 * hence protect ourselves against that and always cap at 62 for
57 * now. */
58 p = CAP_LIMIT;
60 saved = p;
61 valid = true;
62 return p;
66 /* fall back to syscall-probing for pre linux-3.2 */
67 p = (unsigned long) MIN(CAP_LAST_CAP, CAP_LIMIT);
69 if (prctl(PR_CAPBSET_READ, p) < 0) {
71 /* Hmm, look downwards, until we find one that works */
72 for (p--; p > 0; p--)
73 if (prctl(PR_CAPBSET_READ, p) >= 0)
74 break;
76 } else {
78 /* Hmm, look upwards, until we find one that doesn't work */
79 for (; p < CAP_LIMIT; p++)
80 if (prctl(PR_CAPBSET_READ, p+1) < 0)
81 break;
84 saved = p;
85 valid = true;
87 return p;
90 int capability_update_inherited_set(cap_t caps, uint64_t set) {
91 /* Add capabilities in the set to the inherited caps, drops capabilities not in the set.
92 * Do not apply them yet. */
94 for (unsigned i = 0; i <= cap_last_cap(); i++) {
95 cap_flag_value_t flag = set & (UINT64_C(1) << i) ? CAP_SET : CAP_CLEAR;
96 cap_value_t v;
98 v = (cap_value_t) i;
100 if (cap_set_flag(caps, CAP_INHERITABLE, 1, &v, flag) < 0)
101 return -errno;
104 return 0;
107 int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
108 _cleanup_cap_free_ cap_t caps = NULL;
109 int r;
111 /* Remove capabilities requested in ambient set, but not in the bounding set */
112 for (unsigned i = 0; i <= cap_last_cap(); i++) {
113 if (set == 0)
114 break;
116 if (FLAGS_SET(set, (UINT64_C(1) << i)) && prctl(PR_CAPBSET_READ, i) != 1) {
117 log_debug("Ambient capability %s requested but missing from bounding set,"
118 " suppressing automatically.", capability_to_name(i));
119 set &= ~(UINT64_C(1) << i);
123 /* Add the capabilities to the ambient set (an possibly also the inheritable set) */
125 /* Check that we can use PR_CAP_AMBIENT or quit early. */
126 if (!ambient_capabilities_supported())
127 return (set & all_capabilities()) == 0 ?
128 0 : -EOPNOTSUPP; /* if actually no ambient caps are to be set, be silent,
129 * otherwise fail recognizably */
131 if (also_inherit) {
132 caps = cap_get_proc();
133 if (!caps)
134 return -errno;
136 r = capability_update_inherited_set(caps, set);
137 if (r < 0)
138 return -errno;
140 if (cap_set_proc(caps) < 0)
141 return -errno;
144 for (unsigned i = 0; i <= cap_last_cap(); i++) {
146 if (set & (UINT64_C(1) << i)) {
148 /* Add the capability to the ambient set. */
149 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0)
150 return -errno;
151 } else {
153 /* Drop the capability so we don't inherit capabilities we didn't ask for. */
154 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0);
155 if (r < 0)
156 return -errno;
158 if (r)
159 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, i, 0, 0) < 0)
160 return -errno;
165 return 0;
168 int capability_gain_cap_setpcap(cap_t *ret_before_caps) {
169 _cleanup_cap_free_ cap_t caps = NULL;
170 cap_flag_value_t fv;
171 caps = cap_get_proc();
172 if (!caps)
173 return -errno;
175 if (cap_get_flag(caps, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
176 return -errno;
178 if (fv != CAP_SET) {
179 _cleanup_cap_free_ cap_t temp_cap = NULL;
180 static const cap_value_t v = CAP_SETPCAP;
182 temp_cap = cap_dup(caps);
183 if (!temp_cap)
184 return -errno;
186 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0)
187 return -errno;
189 if (cap_set_proc(temp_cap) < 0)
190 log_debug_errno(errno, "Can't acquire effective CAP_SETPCAP bit, ignoring: %m");
192 /* If we didn't manage to acquire the CAP_SETPCAP bit, we continue anyway, after all this just means
193 * we'll fail later, when we actually intend to drop some capabilities or try to set securebits. */
195 if (ret_before_caps)
196 /* Return the capabilities as they have been before setting CAP_SETPCAP */
197 *ret_before_caps = TAKE_PTR(caps);
199 return 0;
202 int capability_bounding_set_drop(uint64_t keep, bool right_now) {
203 _cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
204 int r;
206 /* If we are run as PID 1 we will lack CAP_SETPCAP by default
207 * in the effective set (yes, the kernel drops that when
208 * executing init!), so get it back temporarily so that we can
209 * call PR_CAPBSET_DROP. */
211 r = capability_gain_cap_setpcap(&before_cap);
212 if (r < 0)
213 return r;
215 after_cap = cap_dup(before_cap);
216 if (!after_cap)
217 return -errno;
219 for (unsigned i = 0; i <= cap_last_cap(); i++) {
220 cap_value_t v;
222 if ((keep & (UINT64_C(1) << i)))
223 continue;
225 /* Drop it from the bounding set */
226 if (prctl(PR_CAPBSET_DROP, i) < 0) {
227 r = -errno;
229 /* If dropping the capability failed, let's see if we didn't have it in the first place. If so,
230 * continue anyway, as dropping a capability we didn't have in the first place doesn't really
231 * matter anyway. */
232 if (prctl(PR_CAPBSET_READ, i) != 0)
233 goto finish;
235 v = (cap_value_t) i;
237 /* Also drop it from the inheritable set, so
238 * that anything we exec() loses the
239 * capability for good. */
240 if (cap_set_flag(after_cap, CAP_INHERITABLE, 1, &v, CAP_CLEAR) < 0) {
241 r = -errno;
242 goto finish;
245 /* If we shall apply this right now drop it
246 * also from our own capability sets. */
247 if (right_now) {
248 if (cap_set_flag(after_cap, CAP_PERMITTED, 1, &v, CAP_CLEAR) < 0 ||
249 cap_set_flag(after_cap, CAP_EFFECTIVE, 1, &v, CAP_CLEAR) < 0) {
250 r = -errno;
251 goto finish;
256 r = 0;
258 finish:
259 if (cap_set_proc(after_cap) < 0) {
260 /* If there are no actual changes anyway then let's ignore this error. */
261 if (cap_compare(before_cap, after_cap) != 0)
262 r = -errno;
265 return r;
268 static int drop_from_file(const char *fn, uint64_t keep) {
269 _cleanup_free_ char *p = NULL;
270 uint64_t current, after;
271 uint32_t hi, lo;
272 int r, k;
274 r = read_one_line_file(fn, &p);
275 if (r < 0)
276 return r;
278 k = sscanf(p, "%" PRIu32 " %" PRIu32, &lo, &hi);
279 if (k != 2)
280 return -EIO;
282 current = (uint64_t) lo | ((uint64_t) hi << 32);
283 after = current & keep;
285 if (current == after)
286 return 0;
288 lo = after & UINT32_MAX;
289 hi = (after >> 32) & UINT32_MAX;
291 return write_string_filef(fn, 0, "%" PRIu32 " %" PRIu32, lo, hi);
294 int capability_bounding_set_drop_usermode(uint64_t keep) {
295 int r;
297 r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep);
298 if (r < 0)
299 return r;
301 r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep);
302 if (r < 0)
303 return r;
305 return r;
308 int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
309 int r;
311 /* Unfortunately we cannot leave privilege dropping to PID 1 here, since we want to run as user but
312 * want to keep some capabilities. Since file capabilities have been introduced this cannot be done
313 * across exec() anymore, unless our binary has the capability configured in the file system, which
314 * we want to avoid. */
316 if (setresgid(gid, gid, gid) < 0)
317 return log_error_errno(errno, "Failed to change group ID: %m");
319 r = maybe_setgroups(0, NULL);
320 if (r < 0)
321 return log_error_errno(r, "Failed to drop auxiliary groups list: %m");
323 /* Ensure we keep the permitted caps across the setresuid(). Note that we do this even if we actually
324 * don't want to keep any capabilities, since we want to be able to drop them from the bounding set
325 * too, and we can only do that if we have capabilities. */
326 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
327 return log_error_errno(errno, "Failed to enable keep capabilities flag: %m");
329 if (setresuid(uid, uid, uid) < 0)
330 return log_error_errno(errno, "Failed to change user ID: %m");
332 if (prctl(PR_SET_KEEPCAPS, 0) < 0)
333 return log_error_errno(errno, "Failed to disable keep capabilities flag: %m");
335 /* Drop all caps from the bounding set (as well as the inheritable/permitted/effective sets), except
336 * the ones we want to keep */
337 r = capability_bounding_set_drop(keep_capabilities, true);
338 if (r < 0)
339 return log_error_errno(r, "Failed to drop capabilities: %m");
341 /* Now upgrade the permitted caps we still kept to effective caps */
342 if (keep_capabilities != 0) {
343 cap_value_t bits[log2u64(keep_capabilities) + 1];
344 _cleanup_cap_free_ cap_t d = NULL;
345 unsigned i, j = 0;
347 d = cap_init();
348 if (!d)
349 return log_oom();
351 for (i = 0; i < ELEMENTSOF(bits); i++)
352 if (keep_capabilities & (1ULL << i))
353 bits[j++] = i;
355 /* use enough bits */
356 assert(i == 64 || (keep_capabilities >> i) == 0);
357 /* don't use too many bits */
358 assert(keep_capabilities & (UINT64_C(1) << (i - 1)));
360 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
361 cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0)
362 return log_error_errno(errno, "Failed to enable capabilities bits: %m");
364 if (cap_set_proc(d) < 0)
365 return log_error_errno(errno, "Failed to increase capabilities: %m");
368 return 0;
371 int drop_capability(cap_value_t cv) {
372 _cleanup_cap_free_ cap_t tmp_cap = NULL;
374 tmp_cap = cap_get_proc();
375 if (!tmp_cap)
376 return -errno;
378 if ((cap_set_flag(tmp_cap, CAP_INHERITABLE, 1, &cv, CAP_CLEAR) < 0) ||
379 (cap_set_flag(tmp_cap, CAP_PERMITTED, 1, &cv, CAP_CLEAR) < 0) ||
380 (cap_set_flag(tmp_cap, CAP_EFFECTIVE, 1, &cv, CAP_CLEAR) < 0))
381 return -errno;
383 if (cap_set_proc(tmp_cap) < 0)
384 return -errno;
386 return 0;
389 bool ambient_capabilities_supported(void) {
390 static int cache = -1;
392 if (cache >= 0)
393 return cache;
395 /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
396 * available. */
398 cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 ||
399 !IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS);
401 return cache;
404 bool capability_quintet_mangle(CapabilityQuintet *q) {
405 uint64_t combined, drop = 0;
406 bool ambient_supported;
408 assert(q);
410 combined = q->effective | q->bounding | q->inheritable | q->permitted;
412 ambient_supported = q->ambient != CAP_MASK_UNSET;
413 if (ambient_supported)
414 combined |= q->ambient;
416 for (unsigned i = 0; i <= cap_last_cap(); i++) {
417 unsigned long bit = UINT64_C(1) << i;
418 if (!FLAGS_SET(combined, bit))
419 continue;
421 if (prctl(PR_CAPBSET_READ, i) > 0)
422 continue;
424 drop |= bit;
426 log_debug("Not in the current bounding set: %s", capability_to_name(i));
429 q->effective &= ~drop;
430 q->bounding &= ~drop;
431 q->inheritable &= ~drop;
432 q->permitted &= ~drop;
434 if (ambient_supported)
435 q->ambient &= ~drop;
437 return drop != 0; /* Let the caller know we changed something */
440 int capability_quintet_enforce(const CapabilityQuintet *q) {
441 _cleanup_cap_free_ cap_t c = NULL, modified = NULL;
442 int r;
444 if (q->ambient != CAP_MASK_UNSET) {
445 bool changed = false;
447 c = cap_get_proc();
448 if (!c)
449 return -errno;
451 /* In order to raise the ambient caps set we first need to raise the matching
452 * inheritable + permitted cap */
453 for (unsigned i = 0; i <= cap_last_cap(); i++) {
454 uint64_t m = UINT64_C(1) << i;
455 cap_value_t cv = (cap_value_t) i;
456 cap_flag_value_t old_value_inheritable, old_value_permitted;
458 if ((q->ambient & m) == 0)
459 continue;
461 if (cap_get_flag(c, cv, CAP_INHERITABLE, &old_value_inheritable) < 0)
462 return -errno;
463 if (cap_get_flag(c, cv, CAP_PERMITTED, &old_value_permitted) < 0)
464 return -errno;
466 if (old_value_inheritable == CAP_SET && old_value_permitted == CAP_SET)
467 continue;
469 if (cap_set_flag(c, CAP_INHERITABLE, 1, &cv, CAP_SET) < 0)
470 return -errno;
471 if (cap_set_flag(c, CAP_PERMITTED, 1, &cv, CAP_SET) < 0)
472 return -errno;
474 changed = true;
477 if (changed)
478 if (cap_set_proc(c) < 0)
479 return -errno;
481 r = capability_ambient_set_apply(q->ambient, false);
482 if (r < 0)
483 return r;
486 if (q->inheritable != CAP_MASK_UNSET || q->permitted != CAP_MASK_UNSET || q->effective != CAP_MASK_UNSET) {
487 bool changed = false;
489 if (!c) {
490 c = cap_get_proc();
491 if (!c)
492 return -errno;
495 for (unsigned i = 0; i <= cap_last_cap(); i++) {
496 uint64_t m = UINT64_C(1) << i;
497 cap_value_t cv = (cap_value_t) i;
499 if (q->inheritable != CAP_MASK_UNSET) {
500 cap_flag_value_t old_value, new_value;
502 if (cap_get_flag(c, cv, CAP_INHERITABLE, &old_value) < 0) {
503 if (errno == EINVAL) /* If the kernel knows more caps than this
504 * version of libcap, then this will return
505 * EINVAL. In that case, simply ignore it,
506 * pretend it doesn't exist. */
507 continue;
509 return -errno;
512 new_value = (q->inheritable & m) ? CAP_SET : CAP_CLEAR;
514 if (old_value != new_value) {
515 changed = true;
517 if (cap_set_flag(c, CAP_INHERITABLE, 1, &cv, new_value) < 0)
518 return -errno;
522 if (q->permitted != CAP_MASK_UNSET) {
523 cap_flag_value_t old_value, new_value;
525 if (cap_get_flag(c, cv, CAP_PERMITTED, &old_value) < 0) {
526 if (errno == EINVAL)
527 continue;
529 return -errno;
532 new_value = (q->permitted & m) ? CAP_SET : CAP_CLEAR;
534 if (old_value != new_value) {
535 changed = true;
537 if (cap_set_flag(c, CAP_PERMITTED, 1, &cv, new_value) < 0)
538 return -errno;
542 if (q->effective != CAP_MASK_UNSET) {
543 cap_flag_value_t old_value, new_value;
545 if (cap_get_flag(c, cv, CAP_EFFECTIVE, &old_value) < 0) {
546 if (errno == EINVAL)
547 continue;
549 return -errno;
552 new_value = (q->effective & m) ? CAP_SET : CAP_CLEAR;
554 if (old_value != new_value) {
555 changed = true;
557 if (cap_set_flag(c, CAP_EFFECTIVE, 1, &cv, new_value) < 0)
558 return -errno;
563 if (changed) {
564 /* In order to change the bounding caps, we need to keep CAP_SETPCAP for a bit
565 * longer. Let's add it to our list hence for now. */
566 if (q->bounding != CAP_MASK_UNSET) {
567 cap_value_t cv = CAP_SETPCAP;
569 modified = cap_dup(c);
570 if (!modified)
571 return -ENOMEM;
573 if (cap_set_flag(modified, CAP_PERMITTED, 1, &cv, CAP_SET) < 0)
574 return -errno;
575 if (cap_set_flag(modified, CAP_EFFECTIVE, 1, &cv, CAP_SET) < 0)
576 return -errno;
578 if (cap_compare(modified, c) == 0) {
579 /* No change? then drop this nonsense again */
580 cap_free(modified);
581 modified = NULL;
585 /* Now, let's enforce the caps for the first time. Note that this is where we acquire
586 * caps in any of the sets we currently don't have. We have to do this before
587 * dropping the bounding caps below, since at that point we can never acquire new
588 * caps in inherited/permitted/effective anymore, but only lose them. */
589 if (cap_set_proc(modified ?: c) < 0)
590 return -errno;
594 if (q->bounding != CAP_MASK_UNSET) {
595 r = capability_bounding_set_drop(q->bounding, false);
596 if (r < 0)
597 return r;
600 /* If needed, let's now set the caps again, this time in the final version, which differs from what
601 * we have already set only in the CAP_SETPCAP bit, which we needed for dropping the bounding
602 * bits. This call only undoes bits and doesn't acquire any which means the bounding caps don't
603 * matter. */
604 if (modified)
605 if (cap_set_proc(c) < 0)
606 return -errno;
608 return 0;
611 int capability_get_ambient(uint64_t *ret) {
612 uint64_t a = 0;
613 int r;
615 assert(ret);
617 if (!ambient_capabilities_supported()) {
618 *ret = 0;
619 return 0;
622 for (unsigned i = 0; i <= cap_last_cap(); i++) {
623 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0);
624 if (r < 0)
625 return -errno;
627 if (r)
628 a |= UINT64_C(1) << i;
632 *ret = a;
633 return 1;