chiark / gitweb /
cdeeb3e2aa879649c98cef8ceed1c986e331b763
[elogind.git] / src / basic / selinux-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 <malloc.h>
10 #include <stddef.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <sys/un.h>
15 #include <syslog.h>
16
17 #if HAVE_SELINUX
18 #include <selinux/context.h>
19 #include <selinux/label.h>
20 #include <selinux/selinux.h>
21 #endif
22
23 #include "alloc-util.h"
24 //#include "fd-util.h"
25 #include "log.h"
26 #include "macro.h"
27 #include "path-util.h"
28 #include "selinux-util.h"
29 //#include "stdio-util.h"
30 #include "time-util.h"
31 #include "util.h"
32
33 #if HAVE_SELINUX
34 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon);
35 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
36
37 #define _cleanup_freecon_ _cleanup_(freeconp)
38 #define _cleanup_context_free_ _cleanup_(context_freep)
39
40 static int cached_use = -1;
41 static struct selabel_handle *label_hnd = NULL;
42
43 #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
44 #define log_enforcing_errno(r, ...) log_full_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, r, __VA_ARGS__)
45 #endif
46
47 bool mac_selinux_use(void) {
48 #if HAVE_SELINUX
49         if (cached_use < 0)
50                 cached_use = is_selinux_enabled() > 0;
51
52         return cached_use;
53 #else
54         return false;
55 #endif
56 }
57
58 void mac_selinux_retest(void) {
59 #if HAVE_SELINUX
60         cached_use = -1;
61 #endif
62 }
63
64 int mac_selinux_init(void) {
65         int r = 0;
66
67 #if HAVE_SELINUX
68         usec_t before_timestamp, after_timestamp;
69         struct mallinfo before_mallinfo, after_mallinfo;
70
71         if (label_hnd)
72                 return 0;
73
74         if (!mac_selinux_use())
75                 return 0;
76
77         before_mallinfo = mallinfo();
78         before_timestamp = now(CLOCK_MONOTONIC);
79
80         label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
81         if (!label_hnd) {
82                 log_enforcing_errno(errno, "Failed to initialize SELinux context: %m");
83                 r = security_getenforce() == 1 ? -errno : 0;
84         } else  {
85                 char timespan[FORMAT_TIMESPAN_MAX];
86                 int l;
87
88                 after_timestamp = now(CLOCK_MONOTONIC);
89                 after_mallinfo = mallinfo();
90
91                 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
92
93                 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
94                           format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
95                           (l+1023)/1024);
96         }
97 #endif
98
99         return r;
100 }
101
102 void mac_selinux_finish(void) {
103
104 #if HAVE_SELINUX
105         if (!label_hnd)
106                 return;
107
108         selabel_close(label_hnd);
109         label_hnd = NULL;
110 #endif
111 }
112
113 int mac_selinux_fix(const char *path, LabelFixFlags flags) {
114
115 #if HAVE_SELINUX
116         char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
117         _cleanup_freecon_ char* fcon = NULL;
118         _cleanup_close_ int fd = -1;
119         struct stat st;
120         int r;
121
122         assert(path);
123
124         /* if mac_selinux_init() wasn't called before we are a NOOP */
125         if (!label_hnd)
126                 return 0;
127
128         /* Open the file as O_PATH, to pin it while we determine and adjust the label */
129         fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
130         if (fd < 0) {
131                 if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT)
132                         return 0;
133
134                 return -errno;
135         }
136
137         if (fstat(fd, &st) < 0)
138                 return -errno;
139
140         if (selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode) < 0) {
141                 r = -errno;
142
143                 /* If there's no label to set, then exit without warning */
144                 if (r == -ENOENT)
145                         return 0;
146
147                 goto fail;
148         }
149
150         xsprintf(procfs_path, "/proc/self/fd/%i", fd);
151         if (setfilecon_raw(procfs_path, fcon) < 0) {
152                 _cleanup_freecon_ char *oldcon = NULL;
153
154                 r = -errno;
155
156                 /* If the FS doesn't support labels, then exit without warning */
157                 if (r == -EOPNOTSUPP)
158                         return 0;
159
160                 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
161                 if (r == -EROFS && (flags & LABEL_IGNORE_EROFS))
162                         return 0;
163
164                 /* If the old label is identical to the new one, suppress any kind of error */
165                 if (getfilecon_raw(procfs_path, &oldcon) >= 0 && streq(fcon, oldcon))
166                         return 0;
167
168                 goto fail;
169         }
170
171         return 0;
172
173 fail:
174         log_enforcing_errno(r, "Unable to fix SELinux security context of %s: %m", path);
175         if (security_getenforce() == 1)
176                 return r;
177 #endif
178
179         return 0;
180 }
181
182 #if 0 /// UNNEDED by elogind
183 int mac_selinux_apply(const char *path, const char *label) {
184
185 #if HAVE_SELINUX
186         if (!mac_selinux_use())
187                 return 0;
188
189         assert(path);
190         assert(label);
191
192         if (setfilecon(path, label) < 0) {
193                 log_enforcing_errno(errno, "Failed to set SELinux security context %s on path %s: %m", label, path);
194                 if (security_getenforce() > 0)
195                         return -errno;
196         }
197 #endif
198         return 0;
199 }
200
201 int mac_selinux_get_create_label_from_exe(const char *exe, char **label) {
202         int r = -EOPNOTSUPP;
203
204 #if HAVE_SELINUX
205         _cleanup_freecon_ char *mycon = NULL, *fcon = NULL;
206         security_class_t sclass;
207
208         assert(exe);
209         assert(label);
210
211         if (!mac_selinux_use())
212                 return -EOPNOTSUPP;
213
214         r = getcon_raw(&mycon);
215         if (r < 0)
216                 return -errno;
217
218         r = getfilecon_raw(exe, &fcon);
219         if (r < 0)
220                 return -errno;
221
222         sclass = string_to_security_class("process");
223         r = security_compute_create_raw(mycon, fcon, sclass, label);
224         if (r < 0)
225                 return -errno;
226 #endif
227
228         return r;
229 }
230
231 int mac_selinux_get_our_label(char **label) {
232         int r = -EOPNOTSUPP;
233
234         assert(label);
235
236 #if HAVE_SELINUX
237         if (!mac_selinux_use())
238                 return -EOPNOTSUPP;
239
240         r = getcon_raw(label);
241         if (r < 0)
242                 return -errno;
243 #endif
244
245         return r;
246 }
247
248 int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label) {
249         int r = -EOPNOTSUPP;
250
251 #if HAVE_SELINUX
252         _cleanup_freecon_ char *mycon = NULL, *peercon = NULL, *fcon = NULL;
253         _cleanup_context_free_ context_t pcon = NULL, bcon = NULL;
254         security_class_t sclass;
255         const char *range = NULL;
256
257         assert(socket_fd >= 0);
258         assert(exe);
259         assert(label);
260
261         if (!mac_selinux_use())
262                 return -EOPNOTSUPP;
263
264         r = getcon_raw(&mycon);
265         if (r < 0)
266                 return -errno;
267
268         r = getpeercon_raw(socket_fd, &peercon);
269         if (r < 0)
270                 return -errno;
271
272         if (!exec_label) {
273                 /* If there is no context set for next exec let's use context
274                    of target executable */
275                 r = getfilecon_raw(exe, &fcon);
276                 if (r < 0)
277                         return -errno;
278         }
279
280         bcon = context_new(mycon);
281         if (!bcon)
282                 return -ENOMEM;
283
284         pcon = context_new(peercon);
285         if (!pcon)
286                 return -ENOMEM;
287
288         range = context_range_get(pcon);
289         if (!range)
290                 return -errno;
291
292         r = context_range_set(bcon, range);
293         if (r)
294                 return -errno;
295
296         freecon(mycon);
297         mycon = strdup(context_str(bcon));
298         if (!mycon)
299                 return -ENOMEM;
300
301         sclass = string_to_security_class("process");
302         r = security_compute_create_raw(mycon, fcon, sclass, label);
303         if (r < 0)
304                 return -errno;
305 #endif
306
307         return r;
308 }
309
310 char* mac_selinux_free(char *label) {
311
312 #if HAVE_SELINUX
313         if (!label)
314                 return NULL;
315
316         if (!mac_selinux_use())
317                 return NULL;
318
319         freecon(label);
320 #endif
321
322         return NULL;
323 }
324 #endif // 0
325
326 int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
327
328 #if HAVE_SELINUX
329         _cleanup_freecon_ char *filecon = NULL;
330         int r;
331
332         assert(path);
333
334         if (!label_hnd)
335                 return 0;
336
337         if (path_is_absolute(path))
338                 r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
339         else {
340                 _cleanup_free_ char *newpath = NULL;
341
342                 r = path_make_absolute_cwd(path, &newpath);
343                 if (r < 0)
344                         return r;
345
346                 r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode);
347         }
348
349         if (r < 0) {
350                 /* No context specified by the policy? Proceed without setting it. */
351                 if (errno == ENOENT)
352                         return 0;
353
354                 log_enforcing_errno(errno, "Failed to determine SELinux security context for %s: %m", path);
355         } else {
356                 if (setfscreatecon_raw(filecon) >= 0)
357                         return 0; /* Success! */
358
359                 log_enforcing_errno(errno, "Failed to set SELinux security context %s for %s: %m", filecon, path);
360         }
361
362         if (security_getenforce() > 0)
363                 return -errno;
364
365 #endif
366         return 0;
367 }
368
369 void mac_selinux_create_file_clear(void) {
370
371 #if HAVE_SELINUX
372         PROTECT_ERRNO;
373
374         if (!mac_selinux_use())
375                 return;
376
377         setfscreatecon_raw(NULL);
378 #endif
379 }
380
381 #if 0 /// UNNEEDED by elogind
382 int mac_selinux_create_socket_prepare(const char *label) {
383
384 #if HAVE_SELINUX
385         if (!mac_selinux_use())
386                 return 0;
387
388         assert(label);
389
390         if (setsockcreatecon(label) < 0) {
391                 log_enforcing_errno(errno, "Failed to set SELinux security context %s for sockets: %m", label);
392
393                 if (security_getenforce() == 1)
394                         return -errno;
395         }
396 #endif
397
398         return 0;
399 }
400
401 void mac_selinux_create_socket_clear(void) {
402
403 #if HAVE_SELINUX
404         PROTECT_ERRNO;
405
406         if (!mac_selinux_use())
407                 return;
408
409         setsockcreatecon_raw(NULL);
410 #endif
411 }
412
413 int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
414
415         /* Binds a socket and label its file system object according to the SELinux policy */
416
417 #if HAVE_SELINUX
418         _cleanup_freecon_ char *fcon = NULL;
419         const struct sockaddr_un *un;
420         bool context_changed = false;
421         char *path;
422         int r;
423
424         assert(fd >= 0);
425         assert(addr);
426         assert(addrlen >= sizeof(sa_family_t));
427
428         if (!label_hnd)
429                 goto skipped;
430
431         /* Filter out non-local sockets */
432         if (addr->sa_family != AF_UNIX)
433                 goto skipped;
434
435         /* Filter out anonymous sockets */
436         if (addrlen < offsetof(struct sockaddr_un, sun_path) + 1)
437                 goto skipped;
438
439         /* Filter out abstract namespace sockets */
440         un = (const struct sockaddr_un*) addr;
441         if (un->sun_path[0] == 0)
442                 goto skipped;
443
444         path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
445
446         if (path_is_absolute(path))
447                 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
448         else {
449                 _cleanup_free_ char *newpath = NULL;
450
451                 r = path_make_absolute_cwd(path, &newpath);
452                 if (r < 0)
453                         return r;
454
455                 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
456         }
457
458         if (r < 0) {
459                 /* No context specified by the policy? Proceed without setting it */
460                 if (errno == ENOENT)
461                         goto skipped;
462
463                 log_enforcing_errno(errno, "Failed to determine SELinux security context for %s: %m", path);
464                 if (security_getenforce() > 0)
465                         return -errno;
466
467         } else {
468                 if (setfscreatecon_raw(fcon) < 0) {
469                         log_enforcing_errno(errno, "Failed to set SELinux security context %s for %s: %m", fcon, path);
470                         if (security_getenforce() > 0)
471                                 return -errno;
472                 } else
473                         context_changed = true;
474         }
475
476         r = bind(fd, addr, addrlen) < 0 ? -errno : 0;
477
478         if (context_changed)
479                 setfscreatecon_raw(NULL);
480
481         return r;
482
483 skipped:
484 #endif
485         if (bind(fd, addr, addrlen) < 0)
486                 return -errno;
487
488         return 0;
489 }
490 #endif // 0