]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/selinux-util.c
bus: stop using EDEADLOCK
[thirdparty/systemd.git] / src / shared / selinux-util.c
CommitLineData
cad45ba1
LP
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
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
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
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
66b6d9d5 22#include <errno.h>
66b6d9d5
WC
23#include <malloc.h>
24#include <sys/un.h>
a07e9cfb 25
66b6d9d5
WC
26#ifdef HAVE_SELINUX
27#include <selinux/selinux.h>
28#include <selinux/label.h>
29#include <selinux/context.h>
30#endif
31
32#include "strv.h"
33#include "path-util.h"
cad45ba1
LP
34#include "selinux-util.h"
35
0b6018f3 36#ifdef HAVE_SELINUX
66b6d9d5
WC
37DEFINE_TRIVIAL_CLEANUP_FUNC(security_context_t, freecon);
38DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
0b6018f3 39
66b6d9d5
WC
40#define _cleanup_security_context_free_ _cleanup_(freeconp)
41#define _cleanup_context_free_ _cleanup_(context_freep)
0b6018f3 42
6baa7db0 43static int cached_use = -1;
66b6d9d5 44static struct selabel_handle *label_hnd = NULL;
66cedb30
LP
45
46#define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
66b6d9d5 47#endif
cad45ba1 48
6baa7db0 49bool mac_selinux_use(void) {
66b6d9d5 50#ifdef HAVE_SELINUX
6baa7db0
LP
51 if (cached_use < 0)
52 cached_use = is_selinux_enabled() > 0;
cad45ba1 53
6baa7db0 54 return cached_use;
66b6d9d5
WC
55#else
56 return false;
57#endif
cad45ba1
LP
58}
59
6baa7db0 60void mac_selinux_retest(void) {
66b6d9d5 61#ifdef HAVE_SELINUX
6baa7db0 62 cached_use = -1;
66b6d9d5 63#endif
cad45ba1 64}
0b6018f3 65
cc56fafe 66int mac_selinux_init(const char *prefix) {
66b6d9d5 67 int r = 0;
d682b3a7 68
66b6d9d5
WC
69#ifdef HAVE_SELINUX
70 usec_t before_timestamp, after_timestamp;
71 struct mallinfo before_mallinfo, after_mallinfo;
72
6baa7db0 73 if (!mac_selinux_use())
66b6d9d5
WC
74 return 0;
75
76 if (label_hnd)
77 return 0;
78
79 before_mallinfo = mallinfo();
80 before_timestamp = now(CLOCK_MONOTONIC);
81
82 if (prefix) {
83 struct selinux_opt options[] = {
84 { .type = SELABEL_OPT_SUBSET, .value = prefix },
85 };
86
87 label_hnd = selabel_open(SELABEL_CTX_FILE, options, ELEMENTSOF(options));
88 } else
89 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
90
91 if (!label_hnd) {
66cedb30 92 log_enforcing("Failed to initialize SELinux context: %m");
66b6d9d5
WC
93 r = security_getenforce() == 1 ? -errno : 0;
94 } else {
95 char timespan[FORMAT_TIMESPAN_MAX];
96 int l;
97
98 after_timestamp = now(CLOCK_MONOTONIC);
99 after_mallinfo = mallinfo();
100
101 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
102
103 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
104 format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
105 (l+1023)/1024);
106 }
107#endif
108
109 return r;
d682b3a7
LP
110}
111
ecabcf8b
LP
112void mac_selinux_finish(void) {
113
114#ifdef HAVE_SELINUX
115 if (!label_hnd)
116 return;
117
118 selabel_close(label_hnd);
119#endif
120}
121
cc56fafe 122int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
66b6d9d5
WC
123
124#ifdef HAVE_SELINUX
125 struct stat st;
ecabcf8b 126 int r;
66b6d9d5 127
5dfc5461
LP
128 assert(path);
129
130 /* if mac_selinux_init() wasn't called before we are a NOOP */
66b6d9d5
WC
131 if (!label_hnd)
132 return 0;
133
134 r = lstat(path, &st);
5dfc5461
LP
135 if (r >= 0) {
136 _cleanup_security_context_free_ security_context_t fcon = NULL;
137
66b6d9d5
WC
138 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
139
140 /* If there's no label to set, then exit without warning */
141 if (r < 0 && errno == ENOENT)
142 return 0;
143
5dfc5461 144 if (r >= 0) {
66b6d9d5 145 r = lsetfilecon(path, fcon);
66b6d9d5
WC
146
147 /* If the FS doesn't support labels, then exit without warning */
148 if (r < 0 && errno == ENOTSUP)
149 return 0;
150 }
151 }
152
153 if (r < 0) {
154 /* Ignore ENOENT in some cases */
155 if (ignore_enoent && errno == ENOENT)
156 return 0;
157
158 if (ignore_erofs && errno == EROFS)
159 return 0;
160
ecabcf8b
LP
161 log_enforcing("Unable to fix SELinux security context of %s: %m", path);
162 if (security_getenforce() == 1)
163 return -errno;
66b6d9d5
WC
164 }
165#endif
166
ecabcf8b 167 return 0;
66b6d9d5
WC
168}
169
ecabcf8b 170int mac_selinux_apply(const char *path, const char *label) {
66b6d9d5
WC
171
172#ifdef HAVE_SELINUX
ecabcf8b
LP
173 assert(path);
174 assert(label);
66b6d9d5 175
ecabcf8b
LP
176 if (!mac_selinux_use())
177 return 0;
178
179 if (setfilecon(path, (security_context_t) label) < 0) {
180 log_enforcing("Failed to set SELinux security context %s on path %s: %m", label, path);
181 if (security_getenforce() == 1)
182 return -errno;
183 }
66b6d9d5 184#endif
ecabcf8b 185 return 0;
d682b3a7
LP
186}
187
cc56fafe 188int mac_selinux_get_create_label_from_exe(const char *exe, char **label) {
7f416dae 189 int r = -EOPNOTSUPP;
66b6d9d5
WC
190
191#ifdef HAVE_SELINUX
1ec220bc 192 _cleanup_security_context_free_ security_context_t mycon = NULL, fcon = NULL;
66b6d9d5
WC
193 security_class_t sclass;
194
7f416dae
LP
195 assert(exe);
196 assert(label);
197
198 if (!mac_selinux_use())
199 return -EOPNOTSUPP;
66b6d9d5
WC
200
201 r = getcon(&mycon);
202 if (r < 0)
7f416dae 203 return -errno;
66b6d9d5
WC
204
205 r = getfilecon(exe, &fcon);
206 if (r < 0)
7f416dae 207 return -errno;
66b6d9d5
WC
208
209 sclass = string_to_security_class("process");
210 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
7f416dae
LP
211 if (r < 0)
212 return -errno;
66b6d9d5
WC
213#endif
214
215 return r;
216}
217
cc56fafe 218int mac_selinux_get_our_label(char **label) {
66b6d9d5
WC
219 int r = -EOPNOTSUPP;
220
7f416dae
LP
221 assert(label);
222
66b6d9d5 223#ifdef HAVE_SELINUX
7f416dae
LP
224 if (!mac_selinux_use())
225 return -EOPNOTSUPP;
66b6d9d5 226
7f416dae 227 r = getcon(label);
66b6d9d5 228 if (r < 0)
7f416dae 229 return -errno;
0b6018f3 230#endif
66b6d9d5
WC
231
232 return r;
233}
234
9008e1ac 235int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label) {
66b6d9d5
WC
236 int r = -EOPNOTSUPP;
237
238#ifdef HAVE_SELINUX
7f416dae 239 _cleanup_security_context_free_ security_context_t mycon = NULL, peercon = NULL, fcon = NULL;
66b6d9d5
WC
240 _cleanup_context_free_ context_t pcon = NULL, bcon = NULL;
241 security_class_t sclass;
66b6d9d5
WC
242 const char *range = NULL;
243
244 assert(socket_fd >= 0);
245 assert(exe);
246 assert(label);
247
7f416dae
LP
248 if (!mac_selinux_use())
249 return -EOPNOTSUPP;
250
66b6d9d5 251 r = getcon(&mycon);
7f416dae
LP
252 if (r < 0)
253 return -errno;
66b6d9d5
WC
254
255 r = getpeercon(socket_fd, &peercon);
7f416dae
LP
256 if (r < 0)
257 return -errno;
66b6d9d5 258
9008e1ac 259 if (!exec_label) {
66b6d9d5
WC
260 /* If there is no context set for next exec let's use context
261 of target executable */
262 r = getfilecon(exe, &fcon);
7f416dae
LP
263 if (r < 0)
264 return -errno;
66b6d9d5
WC
265 }
266
267 bcon = context_new(mycon);
7f416dae
LP
268 if (!bcon)
269 return -ENOMEM;
66b6d9d5
WC
270
271 pcon = context_new(peercon);
7f416dae
LP
272 if (!pcon)
273 return -ENOMEM;
66b6d9d5
WC
274
275 range = context_range_get(pcon);
7f416dae
LP
276 if (!range)
277 return -errno;
66b6d9d5
WC
278
279 r = context_range_set(bcon, range);
7f416dae
LP
280 if (r)
281 return -errno;
66b6d9d5
WC
282
283 freecon(mycon);
284 mycon = strdup(context_str(bcon));
7f416dae
LP
285 if (!mycon)
286 return -ENOMEM;
66b6d9d5
WC
287
288 sclass = string_to_security_class("process");
7f416dae
LP
289 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
290 if (r < 0)
291 return -errno;
66b6d9d5 292#endif
7f416dae 293
66b6d9d5
WC
294 return r;
295}
296
ecabcf8b
LP
297void mac_selinux_free(char *label) {
298
299#ifdef HAVE_SELINUX
300 if (!mac_selinux_use())
301 return;
302
303 freecon((security_context_t) label);
304#endif
305}
306
307int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
66b6d9d5
WC
308 int r = 0;
309
310#ifdef HAVE_SELINUX
1ec220bc 311 _cleanup_security_context_free_ security_context_t filecon = NULL;
66b6d9d5 312
ecabcf8b
LP
313 assert(path);
314
66cedb30 315 if (!label_hnd)
66b6d9d5
WC
316 return 0;
317
c34255bd
LP
318 if (path_is_absolute(path))
319 r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
320 else {
321 _cleanup_free_ char *newpath;
322
323 newpath = path_make_absolute_cwd(path);
324 if (!newpath)
325 return -ENOMEM;
326
a07e9cfb 327 r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode);
c34255bd
LP
328 }
329
2d58aa46
MS
330 /* No context specified by the policy? Proceed without setting it. */
331 if (r < 0 && errno == ENOENT)
332 return 0;
333
334 if (r < 0)
66b6d9d5 335 r = -errno;
2d58aa46 336 else {
66b6d9d5
WC
337 r = setfscreatecon(filecon);
338 if (r < 0) {
ecabcf8b 339 log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path);
66b6d9d5
WC
340 r = -errno;
341 }
66b6d9d5
WC
342 }
343
344 if (r < 0 && security_getenforce() == 0)
345 r = 0;
346#endif
347
348 return r;
349}
350
ecabcf8b 351void mac_selinux_create_file_clear(void) {
66b6d9d5
WC
352
353#ifdef HAVE_SELINUX
354 PROTECT_ERRNO;
355
6baa7db0 356 if (!mac_selinux_use())
66b6d9d5
WC
357 return;
358
359 setfscreatecon(NULL);
360#endif
361}
362
ecabcf8b 363int mac_selinux_create_socket_prepare(const char *label) {
66b6d9d5
WC
364
365#ifdef HAVE_SELINUX
6baa7db0 366 if (!mac_selinux_use())
ecabcf8b 367 return 0;
66b6d9d5 368
ecabcf8b
LP
369 assert(label);
370
371 if (setsockcreatecon((security_context_t) label) < 0) {
372 log_enforcing("Failed to set SELinux security context %s for sockets: %m", label);
373
374 if (security_getenforce() == 1)
375 return -errno;
376 }
66b6d9d5 377#endif
ecabcf8b
LP
378
379 return 0;
66b6d9d5
WC
380}
381
ecabcf8b 382void mac_selinux_create_socket_clear(void) {
66b6d9d5
WC
383
384#ifdef HAVE_SELINUX
ecabcf8b
LP
385 PROTECT_ERRNO;
386
6baa7db0 387 if (!mac_selinux_use())
66b6d9d5
WC
388 return;
389
ecabcf8b 390 setsockcreatecon(NULL);
66b6d9d5
WC
391#endif
392}
393
cc56fafe 394int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
66b6d9d5
WC
395
396 /* Binds a socket and label its file system object according to the SELinux policy */
397
398#ifdef HAVE_SELINUX
1ec220bc 399 _cleanup_security_context_free_ security_context_t fcon = NULL;
66b6d9d5
WC
400 const struct sockaddr_un *un;
401 char *path;
402 int r;
403
404 assert(fd >= 0);
405 assert(addr);
406 assert(addrlen >= sizeof(sa_family_t));
407
ecabcf8b 408 if (!label_hnd)
66b6d9d5
WC
409 goto skipped;
410
411 /* Filter out non-local sockets */
412 if (addr->sa_family != AF_UNIX)
413 goto skipped;
414
415 /* Filter out anonymous sockets */
416 if (addrlen < sizeof(sa_family_t) + 1)
417 goto skipped;
418
419 /* Filter out abstract namespace sockets */
420 un = (const struct sockaddr_un*) addr;
421 if (un->sun_path[0] == 0)
422 goto skipped;
423
424 path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
425
426 if (path_is_absolute(path))
427 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
428 else {
429 _cleanup_free_ char *newpath;
430
431 newpath = path_make_absolute_cwd(path);
432 if (!newpath)
433 return -ENOMEM;
434
435 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
436 }
437
438 if (r == 0)
439 r = setfscreatecon(fcon);
440
441 if (r < 0 && errno != ENOENT) {
ecabcf8b 442 log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path);
66b6d9d5
WC
443
444 if (security_getenforce() == 1) {
445 r = -errno;
446 goto finish;
447 }
448 }
449
450 r = bind(fd, addr, addrlen);
451 if (r < 0)
452 r = -errno;
453
454finish:
455 setfscreatecon(NULL);
66b6d9d5
WC
456 return r;
457
458skipped:
459#endif
460 return bind(fd, addr, addrlen) < 0 ? -errno : 0;
461}