]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/capability-util.c
cgroup-util: add mask definitions for sets of controllers supported by cgroupsv1...
[thirdparty/systemd.git] / src / basic / capability-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d7832d2c 2
d7832d2c 3#include <errno.h>
3ffd4af2 4#include <grp.h>
d7832d2c 5#include <stdio.h>
11c3a366 6#include <stdlib.h>
d7832d2c
KS
7#include <sys/capability.h>
8#include <sys/prctl.h>
3ffd4af2 9#include <unistd.h>
d7832d2c 10
b5efdb8a 11#include "alloc-util.h"
430f0182 12#include "capability-util.h"
3ffd4af2
LP
13#include "fileio.h"
14#include "log.h"
d7832d2c 15#include "macro.h"
6bedfcbb 16#include "parse-util.h"
36d85478 17#include "user-util.h"
d7832d2c 18#include "util.h"
d7832d2c
KS
19
20int have_effective_cap(int value) {
5ce70e5b 21 _cleanup_cap_free_ cap_t cap;
d7832d2c 22 cap_flag_value_t fv;
d7832d2c 23
ec8927ca
LP
24 cap = cap_get_proc();
25 if (!cap)
d7832d2c
KS
26 return -errno;
27
28 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
5ce70e5b 29 return -errno;
d7832d2c 30 else
5ce70e5b 31 return fv == CAP_SET;
d7832d2c
KS
32}
33
34unsigned long cap_last_cap(void) {
ec202eae
SL
35 static thread_local unsigned long saved;
36 static thread_local bool valid = false;
80b43783 37 _cleanup_free_ char *content = NULL;
a7f7d1bd 38 unsigned long p = 0;
80b43783 39 int r;
d7832d2c
KS
40
41 if (valid)
42 return saved;
43
80b43783
DH
44 /* available since linux-3.2 */
45 r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
46 if (r >= 0) {
47 r = safe_atolu(content, &p);
48 if (r >= 0) {
49 saved = p;
50 valid = true;
51 return p;
52 }
53 }
54
55 /* fall back to syscall-probing for pre linux-3.2 */
d7832d2c
KS
56 p = (unsigned long) CAP_LAST_CAP;
57
58 if (prctl(PR_CAPBSET_READ, p) < 0) {
59
60 /* Hmm, look downwards, until we find one that
61 * works */
62 for (p--; p > 0; p --)
63 if (prctl(PR_CAPBSET_READ, p) >= 0)
64 break;
65
66 } else {
67
68 /* Hmm, look upwards, until we find one that doesn't
69 * work */
70 for (;; p++)
71 if (prctl(PR_CAPBSET_READ, p+1) < 0)
72 break;
73 }
74
75 saved = p;
76 valid = true;
77
78 return p;
79}
ec8927ca 80
755d4b67
IP
81int capability_update_inherited_set(cap_t caps, uint64_t set) {
82 unsigned long i;
83
84 /* Add capabilities in the set to the inherited caps. Do not apply
85 * them yet. */
86
87 for (i = 0; i < cap_last_cap(); i++) {
88
89 if (set & (UINT64_C(1) << i)) {
90 cap_value_t v;
91
92 v = (cap_value_t) i;
93
94 /* Make the capability inheritable. */
95 if (cap_set_flag(caps, CAP_INHERITABLE, 1, &v, CAP_SET) < 0)
96 return -errno;
97 }
98 }
99
100 return 0;
101}
102
103int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
104 unsigned long i;
105 _cleanup_cap_free_ cap_t caps = NULL;
106
107 /* Add the capabilities to the ambient set. */
108
109 if (also_inherit) {
110 int r;
111 caps = cap_get_proc();
112 if (!caps)
113 return -errno;
114
115 r = capability_update_inherited_set(caps, set);
116 if (r < 0)
117 return -errno;
118
119 if (cap_set_proc(caps) < 0)
120 return -errno;
121 }
122
123 for (i = 0; i < cap_last_cap(); i++) {
124
125 if (set & (UINT64_C(1) << i)) {
126
127 /* Add the capability to the ambient set. */
128 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0)
129 return -errno;
130 }
131 }
132
133 return 0;
134}
135
a103496c 136int capability_bounding_set_drop(uint64_t keep, bool right_now) {
6067611a 137 _cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
ec8927ca 138 cap_flag_value_t fv;
6a010ac9 139 unsigned long i;
ec8927ca
LP
140 int r;
141
142 /* If we are run as PID 1 we will lack CAP_SETPCAP by default
143 * in the effective set (yes, the kernel drops that when
144 * executing init!), so get it back temporarily so that we can
145 * call PR_CAPBSET_DROP. */
146
6067611a
LP
147 before_cap = cap_get_proc();
148 if (!before_cap)
ec8927ca
LP
149 return -errno;
150
6067611a 151 if (cap_get_flag(before_cap, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
ec8927ca 152 return -errno;
ec8927ca
LP
153
154 if (fv != CAP_SET) {
6a010ac9 155 _cleanup_cap_free_ cap_t temp_cap = NULL;
ec8927ca
LP
156 static const cap_value_t v = CAP_SETPCAP;
157
6067611a
LP
158 temp_cap = cap_dup(before_cap);
159 if (!temp_cap)
160 return -errno;
ec8927ca 161
6067611a
LP
162 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0)
163 return -errno;
ec8927ca 164
6067611a
LP
165 if (cap_set_proc(temp_cap) < 0)
166 log_debug_errno(errno, "Can't acquire effective CAP_SETPCAP bit, ignoring: %m");
167
168 /* If we didn't manage to acquire the CAP_SETPCAP bit, we continue anyway, after all this just means
169 * we'll fail later, when we actually intend to drop some capabilities. */
ec8927ca
LP
170 }
171
6067611a
LP
172 after_cap = cap_dup(before_cap);
173 if (!after_cap)
174 return -errno;
175
ec8927ca 176 for (i = 0; i <= cap_last_cap(); i++) {
6067611a 177 cap_value_t v;
ec8927ca 178
6067611a
LP
179 if ((keep & (UINT64_C(1) << i)))
180 continue;
ec8927ca 181
6067611a
LP
182 /* Drop it from the bounding set */
183 if (prctl(PR_CAPBSET_DROP, i) < 0) {
184 r = -errno;
185
186 /* If dropping the capability failed, let's see if we didn't have it in the first place. If so,
187 * continue anyway, as dropping a capability we didn't have in the first place doesn't really
188 * matter anyway. */
189 if (prctl(PR_CAPBSET_READ, i) != 0)
ec8927ca 190 goto finish;
6067611a
LP
191 }
192 v = (cap_value_t) i;
193
194 /* Also drop it from the inheritable set, so
195 * that anything we exec() loses the
196 * capability for good. */
197 if (cap_set_flag(after_cap, CAP_INHERITABLE, 1, &v, CAP_CLEAR) < 0) {
198 r = -errno;
199 goto finish;
200 }
ec8927ca 201
6067611a
LP
202 /* If we shall apply this right now drop it
203 * also from our own capability sets. */
204 if (right_now) {
205 if (cap_set_flag(after_cap, CAP_PERMITTED, 1, &v, CAP_CLEAR) < 0 ||
206 cap_set_flag(after_cap, CAP_EFFECTIVE, 1, &v, CAP_CLEAR) < 0) {
ec8927ca
LP
207 r = -errno;
208 goto finish;
209 }
ec8927ca
LP
210 }
211 }
212
213 r = 0;
214
215finish:
6067611a
LP
216 if (cap_set_proc(after_cap) < 0) {
217 /* If there are no actual changes anyway then let's ignore this error. */
218 if (cap_compare(before_cap, after_cap) != 0)
219 r = -errno;
220 }
ec8927ca
LP
221
222 return r;
223}
939b8f14 224
a103496c 225static int drop_from_file(const char *fn, uint64_t keep) {
3130fca5 226 _cleanup_free_ char *p = NULL;
939b8f14 227 uint64_t current, after;
3130fca5
LP
228 uint32_t hi, lo;
229 int r, k;
939b8f14
LP
230
231 r = read_one_line_file(fn, &p);
232 if (r < 0)
233 return r;
234
235 assert_cc(sizeof(hi) == sizeof(unsigned));
236 assert_cc(sizeof(lo) == sizeof(unsigned));
237
238 k = sscanf(p, "%u %u", &lo, &hi);
939b8f14
LP
239 if (k != 2)
240 return -EIO;
241
242 current = (uint64_t) lo | ((uint64_t) hi << 32ULL);
a103496c 243 after = current & keep;
939b8f14
LP
244
245 if (current == after)
246 return 0;
247
248 lo = (unsigned) (after & 0xFFFFFFFFULL);
249 hi = (unsigned) ((after >> 32ULL) & 0xFFFFFFFFULL);
250
3130fca5 251 return write_string_filef(fn, WRITE_STRING_FILE_CREATE, "%u %u", lo, hi);
939b8f14
LP
252}
253
a103496c 254int capability_bounding_set_drop_usermode(uint64_t keep) {
939b8f14
LP
255 int r;
256
a103496c 257 r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep);
939b8f14
LP
258 if (r < 0)
259 return r;
260
a103496c 261 r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep);
939b8f14
LP
262 if (r < 0)
263 return r;
264
265 return r;
266}
966bff26 267
ed617ec2 268int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
966bff26 269 _cleanup_cap_free_ cap_t d = NULL;
f11943c5 270 unsigned i, j = 0;
966bff26
LP
271 int r;
272
273 /* Unfortunately we cannot leave privilege dropping to PID 1
274 * here, since we want to run as user but want to keep some
275 * capabilities. Since file capabilities have been introduced
276 * this cannot be done across exec() anymore, unless our
277 * binary has the capability configured in the file system,
278 * which we want to avoid. */
279
4a62c710
MS
280 if (setresgid(gid, gid, gid) < 0)
281 return log_error_errno(errno, "Failed to change group ID: %m");
966bff26 282
97f0e76f
LP
283 r = maybe_setgroups(0, NULL);
284 if (r < 0)
285 return log_error_errno(r, "Failed to drop auxiliary groups list: %m");
966bff26 286
51ddf615
ZJS
287 /* Ensure we keep the permitted caps across the setresuid() */
288 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
4a62c710 289 return log_error_errno(errno, "Failed to enable keep capabilities flag: %m");
966bff26 290
b1c05b98 291 if (setresuid(uid, uid, uid) < 0)
4a62c710 292 return log_error_errno(errno, "Failed to change user ID: %m");
966bff26 293
4a62c710
MS
294 if (prctl(PR_SET_KEEPCAPS, 0) < 0)
295 return log_error_errno(errno, "Failed to disable keep capabilities flag: %m");
966bff26 296
f11943c5 297 /* Drop all caps from the bounding set, except the ones we want */
a103496c 298 r = capability_bounding_set_drop(keep_capabilities, true);
f647962d
MS
299 if (r < 0)
300 return log_error_errno(r, "Failed to drop capabilities: %m");
966bff26 301
f11943c5 302 /* Now upgrade the permitted caps we still kept to effective caps */
966bff26
LP
303 d = cap_init();
304 if (!d)
305 return log_oom();
306
51ddf615 307 if (keep_capabilities) {
057255fb 308 cap_value_t bits[u64log2(keep_capabilities) + 1];
6a010ac9 309
7d328b54 310 for (i = 0; i < ELEMENTSOF(bits); i++)
51ddf615
ZJS
311 if (keep_capabilities & (1ULL << i))
312 bits[j++] = i;
057255fb 313
2c9fc266
ZJS
314 /* use enough bits */
315 assert(i == 64 || (keep_capabilities >> i) == 0);
316 /* don't use too many bits */
317 assert(keep_capabilities & (1ULL << (i - 1)));
966bff26 318
51ddf615 319 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
e1427b13
MS
320 cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0)
321 return log_error_errno(errno, "Failed to enable capabilities bits: %m");
51ddf615
ZJS
322
323 if (cap_set_proc(d) < 0)
324 return log_error_errno(errno, "Failed to increase capabilities: %m");
325 }
966bff26
LP
326
327 return 0;
328}
dd5ae4c3
PK
329
330int drop_capability(cap_value_t cv) {
331 _cleanup_cap_free_ cap_t tmp_cap = NULL;
332
333 tmp_cap = cap_get_proc();
334 if (!tmp_cap)
335 return -errno;
336
337 if ((cap_set_flag(tmp_cap, CAP_INHERITABLE, 1, &cv, CAP_CLEAR) < 0) ||
338 (cap_set_flag(tmp_cap, CAP_PERMITTED, 1, &cv, CAP_CLEAR) < 0) ||
339 (cap_set_flag(tmp_cap, CAP_EFFECTIVE, 1, &cv, CAP_CLEAR) < 0))
340 return -errno;
341
342 if (cap_set_proc(tmp_cap) < 0)
343 return -errno;
344
345 return 0;
346}
39f608e4
LP
347
348bool ambient_capabilities_supported(void) {
349 static int cache = -1;
350
351 if (cache >= 0)
352 return cache;
353
354 /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
355 * available. */
356
357 cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 ||
358 !IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS);
359
360 return cache;
361}