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