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