]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/capability.c
man: document ARM root partition types
[thirdparty/systemd.git] / src / shared / capability.c
CommitLineData
d7832d2c
KS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
d7832d2c
KS
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 16 Lesser General Public License for more details.
d7832d2c 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
d7832d2c
KS
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <assert.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
26#include <stdio.h>
27#include <sys/types.h>
28#include <stdarg.h>
29#include <ctype.h>
30#include <sys/capability.h>
31#include <sys/prctl.h>
32
33#include "macro.h"
34#include "capability.h"
35#include "util.h"
36#include "log.h"
a5c32cff 37#include "fileio.h"
d7832d2c
KS
38
39int have_effective_cap(int value) {
5ce70e5b 40 _cleanup_cap_free_ cap_t cap;
d7832d2c 41 cap_flag_value_t fv;
d7832d2c 42
ec8927ca
LP
43 cap = cap_get_proc();
44 if (!cap)
d7832d2c
KS
45 return -errno;
46
47 if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0)
5ce70e5b 48 return -errno;
d7832d2c 49 else
5ce70e5b 50 return fv == CAP_SET;
d7832d2c
KS
51}
52
53unsigned long cap_last_cap(void) {
ec202eae
SL
54 static thread_local unsigned long saved;
55 static thread_local bool valid = false;
d7832d2c
KS
56 unsigned long p;
57
58 if (valid)
59 return saved;
60
61 p = (unsigned long) CAP_LAST_CAP;
62
63 if (prctl(PR_CAPBSET_READ, p) < 0) {
64
65 /* Hmm, look downwards, until we find one that
66 * works */
67 for (p--; p > 0; p --)
68 if (prctl(PR_CAPBSET_READ, p) >= 0)
69 break;
70
71 } else {
72
73 /* Hmm, look upwards, until we find one that doesn't
74 * work */
75 for (;; p++)
76 if (prctl(PR_CAPBSET_READ, p+1) < 0)
77 break;
78 }
79
80 saved = p;
81 valid = true;
82
83 return p;
84}
ec8927ca
LP
85
86int capability_bounding_set_drop(uint64_t drop, bool right_now) {
87 unsigned long i;
5ce70e5b 88 _cleanup_cap_free_ cap_t after_cap = NULL, temp_cap = NULL;
ec8927ca
LP
89 cap_flag_value_t fv;
90 int r;
91
92 /* If we are run as PID 1 we will lack CAP_SETPCAP by default
93 * in the effective set (yes, the kernel drops that when
94 * executing init!), so get it back temporarily so that we can
95 * call PR_CAPBSET_DROP. */
96
97 after_cap = cap_get_proc();
98 if (!after_cap)
99 return -errno;
100
5ce70e5b 101 if (cap_get_flag(after_cap, CAP_SETPCAP, CAP_EFFECTIVE, &fv) < 0)
ec8927ca 102 return -errno;
ec8927ca
LP
103
104 if (fv != CAP_SET) {
105 static const cap_value_t v = CAP_SETPCAP;
106
107 temp_cap = cap_dup(after_cap);
108 if (!temp_cap) {
109 r = -errno;
110 goto finish;
111 }
112
113 if (cap_set_flag(temp_cap, CAP_EFFECTIVE, 1, &v, CAP_SET) < 0) {
114 r = -errno;
115 goto finish;
116 }
117
118 if (cap_set_proc(temp_cap) < 0) {
119 r = -errno;
120 goto finish;
121 }
122 }
123
124 for (i = 0; i <= cap_last_cap(); i++) {
125
126 if (drop & ((uint64_t) 1ULL << (uint64_t) i)) {
127 cap_value_t v;
128
129 /* Drop it from the bounding set */
130 if (prctl(PR_CAPBSET_DROP, i) < 0) {
131 r = -errno;
132 goto finish;
133 }
693eb9a2 134 v = (cap_value_t) i;
ec8927ca
LP
135
136 /* Also drop it from the inheritable set, so
137 * that anything we exec() loses the
138 * capability for good. */
139 if (cap_set_flag(after_cap, CAP_INHERITABLE, 1, &v, CAP_CLEAR) < 0) {
140 r = -errno;
141 goto finish;
142 }
143
144 /* If we shall apply this right now drop it
145 * also from our own capability sets. */
146 if (right_now) {
147 if (cap_set_flag(after_cap, CAP_PERMITTED, 1, &v, CAP_CLEAR) < 0 ||
148 cap_set_flag(after_cap, CAP_EFFECTIVE, 1, &v, CAP_CLEAR) < 0) {
149 r = -errno;
150 goto finish;
151 }
152 }
153 }
154 }
155
156 r = 0;
157
158finish:
5ce70e5b 159 cap_set_proc(after_cap);
ec8927ca
LP
160
161 return r;
162}
939b8f14
LP
163
164static int drop_from_file(const char *fn, uint64_t drop) {
165 int r, k;
166 uint32_t hi, lo;
167 uint64_t current, after;
168 char *p;
169
170 r = read_one_line_file(fn, &p);
171 if (r < 0)
172 return r;
173
174 assert_cc(sizeof(hi) == sizeof(unsigned));
175 assert_cc(sizeof(lo) == sizeof(unsigned));
176
177 k = sscanf(p, "%u %u", &lo, &hi);
178 free(p);
179
180 if (k != 2)
181 return -EIO;
182
183 current = (uint64_t) lo | ((uint64_t) hi << 32ULL);
184 after = current & ~drop;
185
186 if (current == after)
187 return 0;
188
189 lo = (unsigned) (after & 0xFFFFFFFFULL);
190 hi = (unsigned) ((after >> 32ULL) & 0xFFFFFFFFULL);
191
192 if (asprintf(&p, "%u %u", lo, hi) < 0)
193 return -ENOMEM;
194
574d5f2d 195 r = write_string_file(fn, p);
939b8f14
LP
196 free(p);
197
198 return r;
199}
200
201int capability_bounding_set_drop_usermode(uint64_t drop) {
202 int r;
203
204 r = drop_from_file("/proc/sys/kernel/usermodehelper/inheritable", drop);
205 if (r < 0)
206 return r;
207
208 r = drop_from_file("/proc/sys/kernel/usermodehelper/bset", drop);
209 if (r < 0)
210 return r;
211
212 return r;
213}