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