1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
10 #include "capability-util.h"
14 #include "logarithm.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
30 if (cap_get_flag(cap
, value
, CAP_EFFECTIVE
, &fv
) < 0)
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
;
46 /* available since linux-3.2 */
47 r
= read_one_line_file("/proc/sys/kernel/cap_last_cap", &content
);
49 r
= safe_atolu(content
, &p
);
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
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 */
73 if (prctl(PR_CAPBSET_READ
, p
) >= 0)
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)
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
;
100 if (cap_set_flag(caps
, CAP_INHERITABLE
, 1, &v
, flag
) < 0)
107 int capability_ambient_set_apply(uint64_t set
, bool also_inherit
) {
108 _cleanup_cap_free_ cap_t caps
= NULL
;
111 /* Remove capabilities requested in ambient set, but not in the bounding set */
112 for (unsigned i
= 0; i
<= cap_last_cap(); i
++) {
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 */
132 caps
= cap_get_proc();
136 r
= capability_update_inherited_set(caps
, set
);
140 if (cap_set_proc(caps
) < 0)
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)
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);
159 if (prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_LOWER
, i
, 0, 0) < 0)
168 int capability_gain_cap_setpcap(cap_t
*ret_before_caps
) {
169 _cleanup_cap_free_ cap_t caps
= NULL
;
171 caps
= cap_get_proc();
175 if (cap_get_flag(caps
, CAP_SETPCAP
, CAP_EFFECTIVE
, &fv
) < 0)
179 _cleanup_cap_free_ cap_t temp_cap
= NULL
;
180 static const cap_value_t v
= CAP_SETPCAP
;
182 temp_cap
= cap_dup(caps
);
186 if (cap_set_flag(temp_cap
, CAP_EFFECTIVE
, 1, &v
, CAP_SET
) < 0)
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. */
196 /* Return the capabilities as they have been before setting CAP_SETPCAP */
197 *ret_before_caps
= TAKE_PTR(caps
);
202 int capability_bounding_set_drop(uint64_t keep
, bool right_now
) {
203 _cleanup_cap_free_ cap_t before_cap
= NULL
, after_cap
= NULL
;
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
);
215 after_cap
= cap_dup(before_cap
);
219 for (unsigned i
= 0; i
<= cap_last_cap(); i
++) {
222 if ((keep
& (UINT64_C(1) << i
)))
225 /* Drop it from the bounding set */
226 if (prctl(PR_CAPBSET_DROP
, i
) < 0) {
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
232 if (prctl(PR_CAPBSET_READ
, i
) != 0)
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) {
245 /* If we shall apply this right now drop it
246 * also from our own capability sets. */
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) {
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)
268 static int drop_from_file(const char *fn
, uint64_t keep
) {
269 _cleanup_free_
char *p
= NULL
;
270 uint64_t current
, after
;
274 r
= read_one_line_file(fn
, &p
);
278 k
= sscanf(p
, "%" PRIu32
" %" PRIu32
, &lo
, &hi
);
282 current
= (uint64_t) lo
| ((uint64_t) hi
<< 32);
283 after
= current
& keep
;
285 if (current
== after
)
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
) {
297 r
= drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep
);
301 r
= drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep
);
308 int drop_privileges(uid_t uid
, gid_t gid
, uint64_t keep_capabilities
) {
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
);
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);
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
;
351 for (i
= 0; i
< ELEMENTSOF(bits
); i
++)
352 if (keep_capabilities
& (1ULL << 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");
371 int drop_capability(cap_value_t cv
) {
372 _cleanup_cap_free_ cap_t tmp_cap
= NULL
;
374 tmp_cap
= cap_get_proc();
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))
383 if (cap_set_proc(tmp_cap
) < 0)
389 bool ambient_capabilities_supported(void) {
390 static int cache
= -1;
395 /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
398 cache
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_IS_SET
, CAP_KILL
, 0, 0) >= 0 ||
399 !IN_SET(errno
, EINVAL
, EOPNOTSUPP
, ENOSYS
);
404 bool capability_quintet_mangle(CapabilityQuintet
*q
) {
405 uint64_t combined
, drop
= 0;
406 bool ambient_supported
;
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
))
421 if (prctl(PR_CAPBSET_READ
, i
) > 0)
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
)
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
;
444 if (q
->ambient
!= CAP_MASK_UNSET
) {
445 bool changed
= false;
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)
461 if (cap_get_flag(c
, cv
, CAP_INHERITABLE
, &old_value_inheritable
) < 0)
463 if (cap_get_flag(c
, cv
, CAP_PERMITTED
, &old_value_permitted
) < 0)
466 if (old_value_inheritable
== CAP_SET
&& old_value_permitted
== CAP_SET
)
469 if (cap_set_flag(c
, CAP_INHERITABLE
, 1, &cv
, CAP_SET
) < 0)
471 if (cap_set_flag(c
, CAP_PERMITTED
, 1, &cv
, CAP_SET
) < 0)
478 if (cap_set_proc(c
) < 0)
481 r
= capability_ambient_set_apply(q
->ambient
, false);
486 if (q
->inheritable
!= CAP_MASK_UNSET
|| q
->permitted
!= CAP_MASK_UNSET
|| q
->effective
!= CAP_MASK_UNSET
) {
487 bool changed
= false;
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. */
512 new_value
= (q
->inheritable
& m
) ? CAP_SET
: CAP_CLEAR
;
514 if (old_value
!= new_value
) {
517 if (cap_set_flag(c
, CAP_INHERITABLE
, 1, &cv
, new_value
) < 0)
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) {
532 new_value
= (q
->permitted
& m
) ? CAP_SET
: CAP_CLEAR
;
534 if (old_value
!= new_value
) {
537 if (cap_set_flag(c
, CAP_PERMITTED
, 1, &cv
, new_value
) < 0)
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) {
552 new_value
= (q
->effective
& m
) ? CAP_SET
: CAP_CLEAR
;
554 if (old_value
!= new_value
) {
557 if (cap_set_flag(c
, CAP_EFFECTIVE
, 1, &cv
, new_value
) < 0)
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
);
573 if (cap_set_flag(modified
, CAP_PERMITTED
, 1, &cv
, CAP_SET
) < 0)
575 if (cap_set_flag(modified
, CAP_EFFECTIVE
, 1, &cv
, CAP_SET
) < 0)
578 if (cap_compare(modified
, c
) == 0) {
579 /* No change? then drop this nonsense again */
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)
594 if (q
->bounding
!= CAP_MASK_UNSET
) {
595 r
= capability_bounding_set_drop(q
->bounding
, false);
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
605 if (cap_set_proc(c
) < 0)
611 int capability_get_ambient(uint64_t *ret
) {
617 if (!ambient_capabilities_supported()) {
622 for (unsigned i
= 0; i
<= cap_last_cap(); i
++) {
623 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_IS_SET
, i
, 0, 0);
628 a
|= UINT64_C(1) << i
;