| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <linux/capability.h> /* IWYU pragma: export */ |
| 5 |
|
| 6 |
#include "basic-forward.h" |
| 7 |
|
| 8 |
/* Special marker used when storing a capabilities mask as "unset". This would need to be updated as soon as |
| 9 |
* Linux learns more than 63 caps. */ |
| 10 |
#define CAP_MASK_UNSET UINT64_MAX |
| 11 |
|
| 12 |
/* All possible capabilities bits on */ |
| 13 |
#define CAP_MASK_ALL UINT64_C(0x7fffffffffffffff) |
| 14 |
|
| 15 |
/* The largest capability we can deal with, given we want to be able to store cap masks in uint64_t but still |
| 16 |
* be able to use UINT64_MAX as indicator for "not set". The latter makes capability 63 unavailable. */ |
| 17 |
#define CAP_LIMIT 62 |
| 18 |
assert_cc(CAP_LAST_CAP <= CAP_LIMIT); |
| 19 |
|
| 20 |
/* Identical to linux/capability.h's CAP_TO_MASK(), but uses an unsigned 1U instead of a signed 1 for shifting left, in |
| 21 |
* order to avoid complaints about shifting a signed int left by 31 bits, which would make it negative. */ |
| 22 |
#define CAP_TO_MASK_CORRECTED(x) (1U << ((x) & 31U)) |
| 23 |
|
| 24 |
typedef struct CapabilityQuintet { |
| 25 |
/* Stores all five types of capabilities in one go. */ |
| 26 |
uint64_t effective; |
| 27 |
uint64_t bounding; |
| 28 |
uint64_t inheritable; |
| 29 |
uint64_t permitted; |
| 30 |
uint64_t ambient; |
| 31 |
} CapabilityQuintet; |
| 32 |
|
| 33 |
#define CAPABILITY_QUINTET_NULL \ |
| 34 |
(const CapabilityQuintet) { \ |
| 35 |
CAP_MASK_UNSET, \ |
| 36 |
CAP_MASK_UNSET, \ |
| 37 |
CAP_MASK_UNSET, \ |
| 38 |
CAP_MASK_UNSET, \ |
| 39 |
CAP_MASK_UNSET, \ |
| 40 |
} |
| 41 |
|
| 42 |
static inline bool capability_is_set(uint64_t v) { |
| 43 |
return v != CAP_MASK_UNSET; |
| 44 |
} |
| 45 |
|
| 46 |
int capability_get(CapabilityQuintet *ret); |
| 47 |
|
| 48 |
unsigned cap_last_cap(void); |
| 49 |
int have_effective_cap(unsigned cap); |
| 50 |
int have_inheritable_cap(unsigned cap); |
| 51 |
int capability_gain_cap_setpcap(void); |
| 52 |
int capability_bounding_set_drop(uint64_t keep, bool right_now); |
| 53 |
int capability_bounding_set_drop_usermode(uint64_t keep); |
| 54 |
|
| 55 |
int capability_ambient_set_apply(uint64_t set, bool also_inherit); |
| 56 |
|
| 57 |
int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities); |
| 58 |
|
| 59 |
int drop_capability(unsigned cap); |
| 60 |
int keep_capability(unsigned cap); |
| 61 |
|
| 62 |
static inline uint64_t all_capabilities(void) { |
| 63 |
return UINT64_MAX >> (63 - cap_last_cap()); |
| 64 |
} |
| 65 |
|
| 66 |
static inline bool cap_test_all(uint64_t caps) { |
| 67 |
return FLAGS_SET(caps, all_capabilities()); |
| 68 |
} |
| 69 |
|
| 70 |
static inline bool capability_quintet_is_set(const CapabilityQuintet *q) { |
| 71 |
return capability_is_set(q->effective) || |
| 72 |
capability_is_set(q->bounding) || |
| 73 |
capability_is_set(q->inheritable) || |
| 74 |
capability_is_set(q->permitted) || |
| 75 |
capability_is_set(q->ambient); |
| 76 |
} |
| 77 |
|
| 78 |
static inline bool capability_quintet_is_fully_set(const CapabilityQuintet *q) { |
| 79 |
return capability_is_set(q->effective) && |
| 80 |
capability_is_set(q->bounding) && |
| 81 |
capability_is_set(q->inheritable) && |
| 82 |
capability_is_set(q->permitted) && |
| 83 |
capability_is_set(q->ambient); |
| 84 |
} |
| 85 |
|
| 86 |
static inline bool capability_quintet_equal(const CapabilityQuintet *a, const CapabilityQuintet *b) { |
| 87 |
return a->effective == b->effective && |
| 88 |
a->bounding == b->bounding && |
| 89 |
a->inheritable == b->inheritable && |
| 90 |
a->permitted == b->permitted && |
| 91 |
a->ambient == b->ambient; |
| 92 |
} |
| 93 |
|
| 94 |
/* Mangles the specified caps quintet taking the current bounding set into account: |
| 95 |
* drops all caps from all five sets if our bounding set doesn't allow them. |
| 96 |
* Returns true if the quintet was modified. */ |
| 97 |
bool capability_quintet_mangle(CapabilityQuintet *q); |
| 98 |
|
| 99 |
int capability_quintet_enforce(const CapabilityQuintet *q); |
| 100 |
|
| 101 |
int capability_get_ambient(uint64_t *ret); |
| 102 |
|
| 103 |
int pidref_get_capability(const PidRef *pidref, CapabilityQuintet *ret); |