]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/capability-util.c
Merge pull request #10221 from lucaswerkmeister/bash-completion
[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 "fileio.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "parse-util.h"
17 #include "user-util.h"
18 #include "util.h"
19
20 int have_effective_cap(int value) {
21 _cleanup_cap_free_ cap_t cap;
22 cap_flag_value_t fv;
23
24 cap = cap_get_proc();
25 if (!cap)
26 return -errno;
27
28 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
29 return -errno;
30 else
31 return fv == CAP_SET;
32 }
33
34 unsigned long cap_last_cap(void) {
35 static thread_local unsigned long saved;
36 static thread_local bool valid = false;
37 _cleanup_free_ char *content = NULL;
38 unsigned long p = 0;
39 int r;
40
41 if (valid)
42 return saved;
43
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 */
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 }
80
81 int 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
103 int 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
136 int capability_bounding_set_drop(uint64_t keep, bool right_now) {
137 _cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
138 cap_flag_value_t fv;
139 unsigned long i;
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
147 before_cap = cap_get_proc();
148 if (!before_cap)
149 return -errno;
150
151 if (cap_get_flag(before_cap, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
152 return -errno;
153
154 if (fv != CAP_SET) {
155 _cleanup_cap_free_ cap_t temp_cap = NULL;
156 static const cap_value_t v = CAP_SETPCAP;
157
158 temp_cap = cap_dup(before_cap);
159 if (!temp_cap)
160 return -errno;
161
162 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0)
163 return -errno;
164
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. */
170 }
171
172 after_cap = cap_dup(before_cap);
173 if (!after_cap)
174 return -errno;
175
176 for (i = 0; i <= cap_last_cap(); i++) {
177 cap_value_t v;
178
179 if ((keep & (UINT64_C(1) << i)))
180 continue;
181
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)
190 goto finish;
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 }
201
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) {
207 r = -errno;
208 goto finish;
209 }
210 }
211 }
212
213 r = 0;
214
215 finish:
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 }
221
222 return r;
223 }
224
225 static int drop_from_file(const char *fn, uint64_t keep) {
226 _cleanup_free_ char *p = NULL;
227 uint64_t current, after;
228 uint32_t hi, lo;
229 int r, k;
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);
239 if (k != 2)
240 return -EIO;
241
242 current = (uint64_t) lo | ((uint64_t) hi << 32ULL);
243 after = current & keep;
244
245 if (current == after)
246 return 0;
247
248 lo = (unsigned) (after & 0xFFFFFFFFULL);
249 hi = (unsigned) ((after >> 32ULL) & 0xFFFFFFFFULL);
250
251 return write_string_filef(fn, WRITE_STRING_FILE_CREATE, "%u %u", lo, hi);
252 }
253
254 int capability_bounding_set_drop_usermode(uint64_t keep) {
255 int r;
256
257 r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep);
258 if (r < 0)
259 return r;
260
261 r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep);
262 if (r < 0)
263 return r;
264
265 return r;
266 }
267
268 int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
269 _cleanup_cap_free_ cap_t d = NULL;
270 unsigned i, j = 0;
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
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() */
288 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
289 return log_error_errno(errno, "Failed to enable keep capabilities flag: %m");
290
291 if (setresuid(uid, uid, uid) < 0)
292 return log_error_errno(errno, "Failed to change user ID: %m");
293
294 if (prctl(PR_SET_KEEPCAPS, 0) < 0)
295 return log_error_errno(errno, "Failed to disable keep capabilities flag: %m");
296
297 /* Drop all caps from the bounding set, except the ones we want */
298 r = capability_bounding_set_drop(keep_capabilities, true);
299 if (r < 0)
300 return log_error_errno(r, "Failed to drop capabilities: %m");
301
302 /* Now upgrade the permitted caps we still kept to effective caps */
303 d = cap_init();
304 if (!d)
305 return log_oom();
306
307 if (keep_capabilities) {
308 cap_value_t bits[u64log2(keep_capabilities) + 1];
309
310 for (i = 0; i < ELEMENTSOF(bits); i++)
311 if (keep_capabilities & (1ULL << i))
312 bits[j++] = i;
313
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)));
318
319 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
320 cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0)
321 return log_error_errno(errno, "Failed to enable capabilities bits: %m");
322
323 if (cap_set_proc(d) < 0)
324 return log_error_errno(errno, "Failed to increase capabilities: %m");
325 }
326
327 return 0;
328 }
329
330 int 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 }
347
348 bool 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 }
362
363 int capability_quintet_enforce(const CapabilityQuintet *q) {
364 _cleanup_cap_free_ cap_t c = NULL;
365 int r;
366
367 if (q->ambient != (uint64_t) -1) {
368 unsigned long i;
369 bool changed = false;
370
371 c = cap_get_proc();
372 if (!c)
373 return -errno;
374
375 /* In order to raise the ambient caps set we first need to raise the matching inheritable + permitted
376 * cap */
377 for (i = 0; i <= cap_last_cap(); i++) {
378 uint64_t m = UINT64_C(1) << i;
379 cap_value_t cv = (cap_value_t) i;
380 cap_flag_value_t old_value_inheritable, old_value_permitted;
381
382 if ((q->ambient & m) == 0)
383 continue;
384
385 if (cap_get_flag(c, cv, CAP_INHERITABLE, &old_value_inheritable) < 0)
386 return -errno;
387 if (cap_get_flag(c, cv, CAP_PERMITTED, &old_value_permitted) < 0)
388 return -errno;
389
390 if (old_value_inheritable == CAP_SET && old_value_permitted == CAP_SET)
391 continue;
392
393 if (cap_set_flag(c, CAP_INHERITABLE, 1, &cv, CAP_SET) < 0)
394 return -errno;
395
396 if (cap_set_flag(c, CAP_PERMITTED, 1, &cv, CAP_SET) < 0)
397 return -errno;
398
399 changed = true;
400 }
401
402 if (changed)
403 if (cap_set_proc(c) < 0)
404 return -errno;
405
406 r = capability_ambient_set_apply(q->ambient, false);
407 if (r < 0)
408 return r;
409 }
410
411 if (q->inheritable != (uint64_t) -1 || q->permitted != (uint64_t) -1 || q->effective != (uint64_t) -1) {
412 bool changed = false;
413 unsigned long i;
414
415 if (!c) {
416 c = cap_get_proc();
417 if (!c)
418 return -errno;
419 }
420
421 for (i = 0; i <= cap_last_cap(); i++) {
422 uint64_t m = UINT64_C(1) << i;
423 cap_value_t cv = (cap_value_t) i;
424
425 if (q->inheritable != (uint64_t) -1) {
426 cap_flag_value_t old_value, new_value;
427
428 if (cap_get_flag(c, cv, CAP_INHERITABLE, &old_value) < 0)
429 return -errno;
430
431 new_value = (q->inheritable & m) ? CAP_SET : CAP_CLEAR;
432
433 if (old_value != new_value) {
434 changed = true;
435
436 if (cap_set_flag(c, CAP_INHERITABLE, 1, &cv, new_value) < 0)
437 return -errno;
438 }
439 }
440
441 if (q->permitted != (uint64_t) -1) {
442 cap_flag_value_t old_value, new_value;
443
444 if (cap_get_flag(c, cv, CAP_PERMITTED, &old_value) < 0)
445 return -errno;
446
447 new_value = (q->permitted & m) ? CAP_SET : CAP_CLEAR;
448
449 if (old_value != new_value) {
450 changed = true;
451
452 if (cap_set_flag(c, CAP_PERMITTED, 1, &cv, new_value) < 0)
453 return -errno;
454 }
455 }
456
457 if (q->effective != (uint64_t) -1) {
458 cap_flag_value_t old_value, new_value;
459
460 if (cap_get_flag(c, cv, CAP_EFFECTIVE, &old_value) < 0)
461 return -errno;
462
463 new_value = (q->effective & m) ? CAP_SET : CAP_CLEAR;
464
465 if (old_value != new_value) {
466 changed = true;
467
468 if (cap_set_flag(c, CAP_EFFECTIVE, 1, &cv, new_value) < 0)
469 return -errno;
470 }
471 }
472 }
473
474 if (changed)
475 if (cap_set_proc(c) < 0)
476 return -errno;
477 }
478
479 if (q->bounding != (uint64_t) -1) {
480 r = capability_bounding_set_drop(q->bounding, false);
481 if (r < 0)
482 return r;
483 }
484
485 return 0;
486 }