chiark / gitweb /
41c8297f9b53e6aa9a54b60b6a9ebd65988bad2a
[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(char*, freecon);
45 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
46
47 #define _cleanup_freecon_ _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_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, errno, __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 void mac_selinux_retest(void) {
78 #ifdef HAVE_SELINUX
79         cached_use = -1;
80 #endif
81 }
82
83 int mac_selinux_init(void) {
84         int r = 0;
85
86 #ifdef HAVE_SELINUX
87         usec_t before_timestamp, after_timestamp;
88         struct mallinfo before_mallinfo, after_mallinfo;
89
90         if (label_hnd)
91                 return 0;
92
93         if (!mac_selinux_use())
94                 return 0;
95
96         before_mallinfo = mallinfo();
97         before_timestamp = now(CLOCK_MONOTONIC);
98
99         label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
100         if (!label_hnd) {
101                 log_enforcing("Failed to initialize SELinux context: %m");
102                 r = security_getenforce() == 1 ? -errno : 0;
103         } else  {
104                 char timespan[FORMAT_TIMESPAN_MAX];
105                 int l;
106
107                 after_timestamp = now(CLOCK_MONOTONIC);
108                 after_mallinfo = mallinfo();
109
110                 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
111
112                 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
113                           format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
114                           (l+1023)/1024);
115         }
116 #endif
117
118         return r;
119 }
120
121 void mac_selinux_finish(void) {
122
123 #ifdef HAVE_SELINUX
124         if (!label_hnd)
125                 return;
126
127         selabel_close(label_hnd);
128         label_hnd = NULL;
129 #endif
130 }
131
132 int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
133
134 #ifdef HAVE_SELINUX
135         struct stat st;
136         int r;
137
138         assert(path);
139
140         /* if mac_selinux_init() wasn't called before we are a NOOP */
141         if (!label_hnd)
142                 return 0;
143
144         r = lstat(path, &st);
145         if (r >= 0) {
146                 _cleanup_freecon_ char* fcon = NULL;
147
148                 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
149
150                 /* If there's no label to set, then exit without warning */
151                 if (r < 0 && errno == ENOENT)
152                         return 0;
153
154                 if (r >= 0) {
155                         r = lsetfilecon_raw(path, fcon);
156
157                         /* If the FS doesn't support labels, then exit without warning */
158                         if (r < 0 && errno == EOPNOTSUPP)
159                                 return 0;
160                 }
161         }
162
163         if (r < 0) {
164                 /* Ignore ENOENT in some cases */
165                 if (ignore_enoent && errno == ENOENT)
166                         return 0;
167
168                 if (ignore_erofs && errno == EROFS)
169                         return 0;
170
171                 log_enforcing("Unable to fix SELinux security context of %s: %m", path);
172                 if (security_getenforce() == 1)
173                         return -errno;
174         }
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 #ifdef 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("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 #ifdef 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_have())
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 #ifdef HAVE_SELINUX
235         if (!mac_selinux_have())
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 #ifdef 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_have())
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 #ifdef HAVE_SELINUX
311         if (!label)
312                 return NULL;
313
314         if (!mac_selinux_have())
315                 return NULL;
316
317
318         freecon(label);
319 #endif
320
321         return NULL;
322 }
323 #endif // 0
324
325 int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
326
327 #ifdef HAVE_SELINUX
328         _cleanup_freecon_ char *filecon = NULL;
329         int r;
330
331         assert(path);
332
333         if (!label_hnd)
334                 return 0;
335
336         if (path_is_absolute(path))
337                 r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
338         else {
339                 _cleanup_free_ char *newpath = NULL;
340
341                 r = path_make_absolute_cwd(path, &newpath);
342                 if (r < 0)
343                         return r;
344
345                 r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode);
346         }
347
348         if (r < 0) {
349                 /* No context specified by the policy? Proceed without setting it. */
350                 if (errno == ENOENT)
351                         return 0;
352
353                 log_enforcing("Failed to determine SELinux security context for %s: %m", path);
354         } else {
355                 if (setfscreatecon_raw(filecon) >= 0)
356                         return 0; /* Success! */
357
358                 log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path);
359         }
360
361         if (security_getenforce() > 0)
362                 return -errno;
363
364 #endif
365         return 0;
366 }
367
368 void mac_selinux_create_file_clear(void) {
369
370 #ifdef HAVE_SELINUX
371         PROTECT_ERRNO;
372
373         if (!mac_selinux_use())
374                 return;
375
376         setfscreatecon_raw(NULL);
377 #endif
378 }
379
380 #if 0 /// UNNEEDED by elogind
381 int mac_selinux_create_socket_prepare(const char *label) {
382
383 #ifdef HAVE_SELINUX
384         if (!mac_selinux_use())
385                 return 0;
386
387         assert(label);
388
389         if (setsockcreatecon(label) < 0) {
390                 log_enforcing("Failed to set SELinux security context %s for sockets: %m", label);
391
392                 if (security_getenforce() == 1)
393                         return -errno;
394         }
395 #endif
396
397         return 0;
398 }
399
400 void mac_selinux_create_socket_clear(void) {
401
402 #ifdef HAVE_SELINUX
403         PROTECT_ERRNO;
404
405         if (!mac_selinux_use())
406                 return;
407
408         setsockcreatecon_raw(NULL);
409 #endif
410 }
411
412 int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
413
414         /* Binds a socket and label its file system object according to the SELinux policy */
415
416 #ifdef HAVE_SELINUX
417         _cleanup_freecon_ char *fcon = NULL;
418         const struct sockaddr_un *un;
419         bool context_changed = false;
420         char *path;
421         int r;
422
423         assert(fd >= 0);
424         assert(addr);
425         assert(addrlen >= sizeof(sa_family_t));
426
427         if (!label_hnd)
428                 goto skipped;
429
430         /* Filter out non-local sockets */
431         if (addr->sa_family != AF_UNIX)
432                 goto skipped;
433
434         /* Filter out anonymous sockets */
435         if (addrlen < offsetof(struct sockaddr_un, sun_path) + 1)
436                 goto skipped;
437
438         /* Filter out abstract namespace sockets */
439         un = (const struct sockaddr_un*) addr;
440         if (un->sun_path[0] == 0)
441                 goto skipped;
442
443         path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
444
445         if (path_is_absolute(path))
446                 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
447         else {
448                 _cleanup_free_ char *newpath = NULL;
449
450                 r = path_make_absolute_cwd(path, &newpath);
451                 if (r < 0)
452                         return r;
453
454                 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
455         }
456
457         if (r < 0) {
458                 /* No context specified by the policy? Proceed without setting it */
459                 if (errno == ENOENT)
460                         goto skipped;
461
462                 log_enforcing("Failed to determine SELinux security context for %s: %m", path);
463                 if (security_getenforce() > 0)
464                         return -errno;
465
466         } else {
467                 if (setfscreatecon_raw(fcon) < 0) {
468                         log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path);
469                         if (security_getenforce() > 0)
470                                 return -errno;
471                 } else
472                         context_changed = true;
473         }
474
475         r = bind(fd, addr, addrlen) < 0 ? -errno : 0;
476
477         if (context_changed)
478                 setfscreatecon_raw(NULL);
479
480         return r;
481
482 skipped:
483 #endif
484         if (bind(fd, addr, addrlen) < 0)
485                 return -errno;
486
487         return 0;
488 }
489 #endif // 0