chiark / gitweb /
911f62b351a1feae28e69c4042d668f94a230369
[elogind.git] / src / basic / capability-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <grp.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/capability.h>
13 #include <sys/prctl.h>
14 #include <unistd.h>
15
16 #include "alloc-util.h"
17 #include "capability-util.h"
18 #include "fileio.h"
19 #include "log.h"
20 #include "macro.h"
21 #include "parse-util.h"
22 #include "user-util.h"
23 #include "util.h"
24
25 #if 0 /// UNNEEDED by elogind
26 int have_effective_cap(int value) {
27         _cleanup_cap_free_ cap_t cap;
28         cap_flag_value_t fv;
29
30         cap = cap_get_proc();
31         if (!cap)
32                 return -errno;
33
34         if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
35                 return -errno;
36         else
37                 return fv == CAP_SET;
38 }
39 #endif // 0
40
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;
45         unsigned long p = 0;
46         int r;
47
48         if (valid)
49                 return saved;
50
51         /* available since linux-3.2 */
52         r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
53         if (r >= 0) {
54                 r = safe_atolu(content, &p);
55                 if (r >= 0) {
56                         saved = p;
57                         valid = true;
58                         return p;
59                 }
60         }
61
62         /* fall back to syscall-probing for pre linux-3.2 */
63         p = (unsigned long) CAP_LAST_CAP;
64
65         if (prctl(PR_CAPBSET_READ, p) < 0) {
66
67                 /* Hmm, look downwards, until we find one that
68                  * works */
69                 for (p--; p > 0; p --)
70                         if (prctl(PR_CAPBSET_READ, p) >= 0)
71                                 break;
72
73         } else {
74
75                 /* Hmm, look upwards, until we find one that doesn't
76                  * work */
77                 for (;; p++)
78                         if (prctl(PR_CAPBSET_READ, p+1) < 0)
79                                 break;
80         }
81
82         saved = p;
83         valid = true;
84
85         return p;
86 }
87
88 #if 0 /// UNNEEDED by elogind
89 int capability_update_inherited_set(cap_t caps, uint64_t set) {
90         unsigned long i;
91
92         /* Add capabilities in the set to the inherited caps. Do not apply
93          * them yet. */
94
95         for (i = 0; i < cap_last_cap(); i++) {
96
97                 if (set & (UINT64_C(1) << i)) {
98                         cap_value_t v;
99
100                         v = (cap_value_t) i;
101
102                         /* Make the capability inheritable. */
103                         if (cap_set_flag(caps, CAP_INHERITABLE, 1, &v, CAP_SET) < 0)
104                                 return -errno;
105                 }
106         }
107
108         return 0;
109 }
110
111 int capability_ambient_set_apply(uint64_t set, bool also_inherit) {
112         unsigned long i;
113         _cleanup_cap_free_ cap_t caps = NULL;
114
115         /* Add the capabilities to the ambient set. */
116
117         if (also_inherit) {
118                 int r;
119                 caps = cap_get_proc();
120                 if (!caps)
121                         return -errno;
122
123                 r = capability_update_inherited_set(caps, set);
124                 if (r < 0)
125                         return -errno;
126
127                 if (cap_set_proc(caps) < 0)
128                         return -errno;
129         }
130
131         for (i = 0; i < cap_last_cap(); i++) {
132
133                 if (set & (UINT64_C(1) << i)) {
134
135                         /* Add the capability to the ambient set. */
136                         if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0)
137                                 return -errno;
138                 }
139         }
140
141         return 0;
142 }
143
144 int capability_bounding_set_drop(uint64_t keep, bool right_now) {
145         _cleanup_cap_free_ cap_t before_cap = NULL, after_cap = NULL;
146         cap_flag_value_t fv;
147         unsigned long i;
148         int r;
149
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. */
154
155         before_cap = cap_get_proc();
156         if (!before_cap)
157                 return -errno;
158
159         if (cap_get_flag(before_cap, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
160                 return -errno;
161
162         if (fv != CAP_SET) {
163                 _cleanup_cap_free_ cap_t temp_cap = NULL;
164                 static const cap_value_t v = CAP_SETPCAP;
165
166                 temp_cap = cap_dup(before_cap);
167                 if (!temp_cap)
168                         return -errno;
169
170                 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0)
171                         return -errno;
172
173                 if (cap_set_proc(temp_cap) < 0)
174                         log_debug_errno(errno, "Can't acquire effective CAP_SETPCAP bit, ignoring: %m");
175
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. */
178         }
179
180         after_cap = cap_dup(before_cap);
181         if (!after_cap)
182                 return -errno;
183
184         for (i = 0; i <= cap_last_cap(); i++) {
185                 cap_value_t v;
186
187                 if ((keep & (UINT64_C(1) << i)))
188                         continue;
189
190                 /* Drop it from the bounding set */
191                 if (prctl(PR_CAPBSET_DROP, i) < 0) {
192                         r = -errno;
193
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
196                          * matter anyway. */
197                         if (prctl(PR_CAPBSET_READ, i) != 0)
198                                 goto finish;
199                 }
200                 v = (cap_value_t) i;
201
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) {
206                         r = -errno;
207                         goto finish;
208                 }
209
210                 /* If we shall apply this right now drop it
211                  * also from our own capability sets. */
212                 if (right_now) {
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) {
215                                 r = -errno;
216                                 goto finish;
217                         }
218                 }
219         }
220
221         r = 0;
222
223 finish:
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)
227                         r = -errno;
228         }
229
230         return r;
231 }
232
233 static int drop_from_file(const char *fn, uint64_t keep) {
234         _cleanup_free_ char *p = NULL;
235         uint64_t current, after;
236         uint32_t hi, lo;
237         int r, k;
238
239         r = read_one_line_file(fn, &p);
240         if (r < 0)
241                 return r;
242
243         assert_cc(sizeof(hi) == sizeof(unsigned));
244         assert_cc(sizeof(lo) == sizeof(unsigned));
245
246         k = sscanf(p, "%u %u", &lo, &hi);
247         if (k != 2)
248                 return -EIO;
249
250         current = (uint64_t) lo | ((uint64_t) hi << 32ULL);
251         after = current & keep;
252
253         if (current == after)
254                 return 0;
255
256         lo = (unsigned) (after & 0xFFFFFFFFULL);
257         hi = (unsigned) ((after >> 32ULL) & 0xFFFFFFFFULL);
258
259         return write_string_filef(fn, WRITE_STRING_FILE_CREATE, "%u %u", lo, hi);
260 }
261
262 int capability_bounding_set_drop_usermode(uint64_t keep) {
263         int r;
264
265         r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", keep);
266         if (r < 0)
267                 return r;
268
269         r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", keep);
270         if (r < 0)
271                 return r;
272
273         return r;
274 }
275
276 int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
277         _cleanup_cap_free_ cap_t d = NULL;
278         unsigned i, j = 0;
279         int r;
280
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. */
287
288         if (setresgid(gid, gid, gid) < 0)
289                 return log_error_errno(errno, "Failed to change group ID: %m");
290
291         r = maybe_setgroups(0, NULL);
292         if (r < 0)
293                 return log_error_errno(r, "Failed to drop auxiliary groups list: %m");
294
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");
298
299         if (setresuid(uid, uid, uid) < 0)
300                 return log_error_errno(errno, "Failed to change user ID: %m");
301
302         if (prctl(PR_SET_KEEPCAPS, 0) < 0)
303                 return log_error_errno(errno, "Failed to disable keep capabilities flag: %m");
304
305         /* Drop all caps from the bounding set, except the ones we want */
306         r = capability_bounding_set_drop(keep_capabilities, true);
307         if (r < 0)
308                 return log_error_errno(r, "Failed to drop capabilities: %m");
309
310         /* Now upgrade the permitted caps we still kept to effective caps */
311         d = cap_init();
312         if (!d)
313                 return log_oom();
314
315         if (keep_capabilities) {
316                 cap_value_t bits[u64log2(keep_capabilities) + 1];
317
318                 for (i = 0; i < ELEMENTSOF(bits); i++)
319                         if (keep_capabilities & (1ULL << i))
320                                 bits[j++] = i;
321
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)));
326
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");
330
331                 if (cap_set_proc(d) < 0)
332                         return log_error_errno(errno, "Failed to increase capabilities: %m");
333         }
334
335         return 0;
336 }
337
338 int drop_capability(cap_value_t cv) {
339         _cleanup_cap_free_ cap_t tmp_cap = NULL;
340
341         tmp_cap = cap_get_proc();
342         if (!tmp_cap)
343                 return -errno;
344
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))
348                 return -errno;
349
350         if (cap_set_proc(tmp_cap) < 0)
351                 return -errno;
352
353         return 0;
354 }
355
356 bool ambient_capabilities_supported(void) {
357         static int cache = -1;
358
359         if (cache >= 0)
360                 return cache;
361
362         /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
363          * available. */
364
365         cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 ||
366                 !IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS);
367
368         return cache;
369 }
370 #endif // 0