1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
12 #include <sys/capability.h>
13 #include <sys/prctl.h>
16 #include "alloc-util.h"
17 #include "capability-util.h"
21 #include "parse-util.h"
22 #include "user-util.h"
25 #if 0 /// UNNEEDED by elogind
26 int have_effective_cap(int value) {
27 _cleanup_cap_free_ cap_t cap;
34 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
41 unsigned long cap_last_cap(void) {
42 static thread_local unsigned long saved;
43 static thread_local bool valid = false;
44 _cleanup_free_ char *content = NULL;
51 /* available since linux-3.2 */
52 r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
54 r = safe_atolu(content, &p);
62 /* fall back to syscall-probing for pre linux-3.2 */
63 p = (unsigned long) CAP_LAST_CAP;
65 if (prctl(PR_CAPBSET_READ, p) < 0) {
67 /* Hmm, look downwards, until we find one that
69 for (p--; p > 0; p --)
70 if (prctl(PR_CAPBSET_READ, p) >= 0)
75 /* Hmm, look upwards, until we find one that doesn't
78 if (prctl(PR_CAPBSET_READ, p+1) < 0)
88 #if 0 /// UNNEEDED by elogind
89 int capability_update_inherited_set(cap_t caps, uint64_t set) {
92 /* Add capabilities in the set to the inherited caps. Do not apply
95 for (i = 0; i < cap_last_cap(); i++) {
97 if (set & (UINT64_C(1) << i)) {
102 /* Make the capability inheritable. */
103 if (cap_set_flag(caps, CAP_INHERITABLE, 1, &v, CAP_SET) < 0)
111 int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
113 _cleanup_cap_free_ cap_t caps = NULL;
115 /* Add the capabilities to the ambient set. */
119 caps = cap_get_proc();
123 r = capability_update_inherited_set(caps, set);
127 if (cap_set_proc(caps) < 0)
131 for (i = 0; i < cap_last_cap(); i++) {
133 if (set & (UINT64_C(1) << i)) {
135 /* Add the capability to the ambient set. */
136 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0)
144 int capability_bounding_set_drop(uint64_t keep, bool right_now) {
145 _cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
150 /* If we are run as PID 1 we will lack CAP_SETPCAP by default
151 * in the effective set (yes, the kernel drops that when
152 * executing init!), so get it back temporarily so that we can
153 * call PR_CAPBSET_DROP. */
155 before_cap = cap_get_proc();
159 if (cap_get_flag(before_cap, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
163 _cleanup_cap_free_ cap_t temp_cap = NULL;
164 static const cap_value_t v = CAP_SETPCAP;
166 temp_cap = cap_dup(before_cap);
170 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0)
173 if (cap_set_proc(temp_cap) < 0)
174 log_debug_errno(errno, "Can't acquire effective CAP_SETPCAP bit, ignoring: %m");
176 /* If we didn't manage to acquire the CAP_SETPCAP bit, we continue anyway, after all this just means
177 * we'll fail later, when we actually intend to drop some capabilities. */
180 after_cap = cap_dup(before_cap);
184 for (i = 0; i <= cap_last_cap(); i++) {
187 if ((keep & (UINT64_C(1) << i)))
190 /* Drop it from the bounding set */
191 if (prctl(PR_CAPBSET_DROP, i) < 0) {
194 /* If dropping the capability failed, let's see if we didn't have it in the first place. If so,
195 * continue anyway, as dropping a capability we didn't have in the first place doesn't really
197 if (prctl(PR_CAPBSET_READ, i) != 0)
202 /* Also drop it from the inheritable set, so
203 * that anything we exec() loses the
204 * capability for good. */
205 if (cap_set_flag(after_cap, CAP_INHERITABLE, 1, &v, CAP_CLEAR) < 0) {
210 /* If we shall apply this right now drop it
211 * also from our own capability sets. */
213 if (cap_set_flag(after_cap, CAP_PERMITTED, 1, &v, CAP_CLEAR) < 0 ||
214 cap_set_flag(after_cap, CAP_EFFECTIVE, 1, &v, CAP_CLEAR) < 0) {
224 if (cap_set_proc(after_cap) < 0) {
225 /* If there are no actual changes anyway then let's ignore this error. */
226 if (cap_compare(before_cap, after_cap) != 0)
233 static int drop_from_file(const char *fn, uint64_t keep) {
234 _cleanup_free_ char *p = NULL;
235 uint64_t current, after;
239 r = read_one_line_file(fn, &p);
243 assert_cc(sizeof(hi) == sizeof(unsigned));
244 assert_cc(sizeof(lo) == sizeof(unsigned));
246 k = sscanf(p, "%u %u", &lo, &hi);
250 current = (uint64_t) lo | ((uint64_t) hi << 32ULL);
251 after = current & keep;
253 if (current == after)
256 lo = (unsigned) (after & 0xFFFFFFFFULL);
257 hi = (unsigned) ((after >> 32ULL) & 0xFFFFFFFFULL);
259 return write_string_filef(fn, WRITE_STRING_FILE_CREATE, "%u %u", lo, hi);
262 int capability_bounding_set_drop_usermode(uint64_t keep) {
265 r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep);
269 r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep);
276 int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
277 _cleanup_cap_free_ cap_t d = NULL;
281 /* Unfortunately we cannot leave privilege dropping to PID 1
282 * here, since we want to run as user but want to keep some
283 * capabilities. Since file capabilities have been introduced
284 * this cannot be done across exec() anymore, unless our
285 * binary has the capability configured in the file system,
286 * which we want to avoid. */
288 if (setresgid(gid, gid, gid) < 0)
289 return log_error_errno(errno, "Failed to change group ID: %m");
291 r = maybe_setgroups(0, NULL);
293 return log_error_errno(r, "Failed to drop auxiliary groups list: %m");
295 /* Ensure we keep the permitted caps across the setresuid() */
296 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
297 return log_error_errno(errno, "Failed to enable keep capabilities flag: %m");
299 if (setresuid(uid, uid, uid) < 0)
300 return log_error_errno(errno, "Failed to change user ID: %m");
302 if (prctl(PR_SET_KEEPCAPS, 0) < 0)
303 return log_error_errno(errno, "Failed to disable keep capabilities flag: %m");
305 /* Drop all caps from the bounding set, except the ones we want */
306 r = capability_bounding_set_drop(keep_capabilities, true);
308 return log_error_errno(r, "Failed to drop capabilities: %m");
310 /* Now upgrade the permitted caps we still kept to effective caps */
315 if (keep_capabilities) {
316 cap_value_t bits[u64log2(keep_capabilities) + 1];
318 for (i = 0; i < ELEMENTSOF(bits); i++)
319 if (keep_capabilities & (1ULL << i))
322 /* use enough bits */
323 assert(i == 64 || (keep_capabilities >> i) == 0);
324 /* don't use too many bits */
325 assert(keep_capabilities & (1ULL << (i - 1)));
327 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
328 cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0)
329 return log_error_errno(errno, "Failed to enable capabilities bits: %m");
331 if (cap_set_proc(d) < 0)
332 return log_error_errno(errno, "Failed to increase capabilities: %m");
338 int drop_capability(cap_value_t cv) {
339 _cleanup_cap_free_ cap_t tmp_cap = NULL;
341 tmp_cap = cap_get_proc();
345 if ((cap_set_flag(tmp_cap, CAP_INHERITABLE, 1, &cv, CAP_CLEAR) < 0) ||
346 (cap_set_flag(tmp_cap, CAP_PERMITTED, 1, &cv, CAP_CLEAR) < 0) ||
347 (cap_set_flag(tmp_cap, CAP_EFFECTIVE, 1, &cv, CAP_CLEAR) < 0))
350 if (cap_set_proc(tmp_cap) < 0)
356 bool ambient_capabilities_supported(void) {
357 static int cache = -1;
362 /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
365 cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 ||
366 !IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS);