chiark / gitweb /
selinux: clean up selinux label function naming
[elogind.git] / src / shared / selinux-util.c
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
22 #include <errno.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <sys/un.h>
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"
34 #include "selinux-util.h"
35
36 #ifdef HAVE_SELINUX
37 DEFINE_TRIVIAL_CLEANUP_FUNC(security_context_t, freecon);
38 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
39
40 #define _cleanup_security_context_free_ _cleanup_(freeconp)
41 #define _cleanup_context_free_ _cleanup_(context_freep)
42
43 static int cached_use = -1;
44 static struct selabel_handle *label_hnd = NULL;
45
46 #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
47 #endif
48
49 bool mac_selinux_use(void) {
50 #ifdef HAVE_SELINUX
51         if (cached_use < 0)
52                 cached_use = is_selinux_enabled() > 0;
53
54         return cached_use;
55 #else
56         return false;
57 #endif
58 }
59
60 void mac_selinux_retest(void) {
61 #ifdef HAVE_SELINUX
62         cached_use = -1;
63 #endif
64 }
65
66 int mac_selinux_init(const char *prefix) {
67         int r = 0;
68
69 #ifdef HAVE_SELINUX
70         usec_t before_timestamp, after_timestamp;
71         struct mallinfo before_mallinfo, after_mallinfo;
72
73         if (!mac_selinux_use())
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) {
92                 log_enforcing("Failed to initialize SELinux context: %m");
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;
110 }
111
112 void 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
122 int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
123
124 #ifdef HAVE_SELINUX
125         struct stat st;
126         int r;
127
128         assert(path);
129
130         /* if mac_selinux_init() wasn't called before we are a NOOP */
131         if (!label_hnd)
132                 return 0;
133
134         r = lstat(path, &st);
135         if (r >= 0) {
136                 _cleanup_security_context_free_ security_context_t fcon = NULL;
137
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
144                 if (r >= 0) {
145                         r = lsetfilecon(path, fcon);
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
161                 log_enforcing("Unable to fix SELinux security context of %s: %m", path);
162                 if (security_getenforce() == 1)
163                         return -errno;
164         }
165 #endif
166
167         return 0;
168 }
169
170 int mac_selinux_apply(const char *path, const char *label) {
171
172 #ifdef HAVE_SELINUX
173         assert(path);
174         assert(label);
175
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         }
184 #endif
185         return 0;
186 }
187
188 int mac_selinux_get_create_label_from_exe(const char *exe, char **label) {
189         int r = -EOPNOTSUPP;
190
191 #ifdef HAVE_SELINUX
192         _cleanup_security_context_free_ security_context_t mycon = NULL, fcon = NULL;
193         security_class_t sclass;
194
195         assert(exe);
196         assert(label);
197
198         if (!mac_selinux_use())
199                 return -EOPNOTSUPP;
200
201         r = getcon(&mycon);
202         if (r < 0)
203                 return -errno;
204
205         r = getfilecon(exe, &fcon);
206         if (r < 0)
207                 return -errno;
208
209         sclass = string_to_security_class("process");
210         r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
211         if (r < 0)
212                 return -errno;
213 #endif
214
215         return r;
216 }
217
218 int mac_selinux_get_our_label(char **label) {
219         int r = -EOPNOTSUPP;
220
221         assert(label);
222
223 #ifdef HAVE_SELINUX
224         if (!mac_selinux_use())
225                 return -EOPNOTSUPP;
226
227         r = getcon(label);
228         if (r < 0)
229                 return -errno;
230 #endif
231
232         return r;
233 }
234
235 int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, char **label) {
236         int r = -EOPNOTSUPP;
237
238 #ifdef HAVE_SELINUX
239         _cleanup_security_context_free_ security_context_t mycon = NULL, peercon = NULL, fcon = NULL;
240         _cleanup_context_free_ context_t pcon = NULL, bcon = NULL;
241         security_class_t sclass;
242         const char *range = NULL;
243
244         assert(socket_fd >= 0);
245         assert(exe);
246         assert(label);
247
248         if (!mac_selinux_use())
249                 return -EOPNOTSUPP;
250
251         r = getcon(&mycon);
252         if (r < 0)
253                 return -errno;
254
255         r = getpeercon(socket_fd, &peercon);
256         if (r < 0)
257                 return -errno;
258
259         r = getexeccon(&fcon);
260         if (r < 0)
261                 return -errno;
262
263         if (!fcon) {
264                 /* If there is no context set for next exec let's use context
265                    of target executable */
266                 r = getfilecon(exe, &fcon);
267                 if (r < 0)
268                         return -errno;
269         }
270
271         bcon = context_new(mycon);
272         if (!bcon)
273                 return -ENOMEM;
274
275         pcon = context_new(peercon);
276         if (!pcon)
277                 return -ENOMEM;
278
279         range = context_range_get(pcon);
280         if (!range)
281                 return -errno;
282
283         r = context_range_set(bcon, range);
284         if (r)
285                 return -errno;
286
287         freecon(mycon);
288         mycon = strdup(context_str(bcon));
289         if (!mycon)
290                 return -ENOMEM;
291
292         sclass = string_to_security_class("process");
293         r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
294         if (r < 0)
295                 return -errno;
296 #endif
297
298         return r;
299 }
300
301 void mac_selinux_free(char *label) {
302
303 #ifdef HAVE_SELINUX
304         if (!mac_selinux_use())
305                 return;
306
307         freecon((security_context_t) label);
308 #endif
309 }
310
311 int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
312         int r = 0;
313
314 #ifdef HAVE_SELINUX
315         _cleanup_security_context_free_ security_context_t filecon = NULL;
316
317         assert(path);
318
319         if (!label_hnd)
320                 return 0;
321
322         r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
323         if (r < 0 && errno != ENOENT)
324                 r = -errno;
325         else if (r == 0) {
326                 r = setfscreatecon(filecon);
327                 if (r < 0) {
328                         log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path);
329                         r = -errno;
330                 }
331         }
332
333         if (r < 0 && security_getenforce() == 0)
334                 r = 0;
335 #endif
336
337         return r;
338 }
339
340 void mac_selinux_create_file_clear(void) {
341
342 #ifdef HAVE_SELINUX
343         PROTECT_ERRNO;
344
345         if (!mac_selinux_use())
346                 return;
347
348         setfscreatecon(NULL);
349 #endif
350 }
351
352 int mac_selinux_create_socket_prepare(const char *label) {
353
354 #ifdef HAVE_SELINUX
355         if (!mac_selinux_use())
356                 return 0;
357
358         assert(label);
359
360         if (setsockcreatecon((security_context_t) label) < 0) {
361                 log_enforcing("Failed to set SELinux security context %s for sockets: %m", label);
362
363                 if (security_getenforce() == 1)
364                         return -errno;
365         }
366 #endif
367
368         return 0;
369 }
370
371 void mac_selinux_create_socket_clear(void) {
372
373 #ifdef HAVE_SELINUX
374         PROTECT_ERRNO;
375
376         if (!mac_selinux_use())
377                 return;
378
379         setsockcreatecon(NULL);
380 #endif
381 }
382
383 int mac_selinux_mkdir(const char *path, mode_t mode) {
384
385         /* Creates a directory and labels it according to the SELinux policy */
386
387 #ifdef HAVE_SELINUX
388         _cleanup_security_context_free_ security_context_t fcon = NULL;
389         int r;
390
391         assert(path);
392
393         if (!label_hnd)
394                 goto skipped;
395
396         if (path_is_absolute(path))
397                 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFDIR);
398         else {
399                 _cleanup_free_ char *newpath;
400
401                 newpath = path_make_absolute_cwd(path);
402                 if (!newpath)
403                         return -ENOMEM;
404
405                 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFDIR);
406         }
407
408         if (r == 0)
409                 r = setfscreatecon(fcon);
410
411         if (r < 0 && errno != ENOENT) {
412                 log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path);
413
414                 if (security_getenforce() == 1) {
415                         r = -errno;
416                         goto finish;
417                 }
418         }
419
420         r = mkdir(path, mode);
421         if (r < 0)
422                 r = -errno;
423
424 finish:
425         setfscreatecon(NULL);
426         return r;
427
428 skipped:
429 #endif
430         return mkdir(path, mode) < 0 ? -errno : 0;
431 }
432
433 int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
434
435         /* Binds a socket and label its file system object according to the SELinux policy */
436
437 #ifdef HAVE_SELINUX
438         _cleanup_security_context_free_ security_context_t fcon = NULL;
439         const struct sockaddr_un *un;
440         char *path;
441         int r;
442
443         assert(fd >= 0);
444         assert(addr);
445         assert(addrlen >= sizeof(sa_family_t));
446
447         if (!label_hnd)
448                 goto skipped;
449
450         /* Filter out non-local sockets */
451         if (addr->sa_family != AF_UNIX)
452                 goto skipped;
453
454         /* Filter out anonymous sockets */
455         if (addrlen < sizeof(sa_family_t) + 1)
456                 goto skipped;
457
458         /* Filter out abstract namespace sockets */
459         un = (const struct sockaddr_un*) addr;
460         if (un->sun_path[0] == 0)
461                 goto skipped;
462
463         path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
464
465         if (path_is_absolute(path))
466                 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
467         else {
468                 _cleanup_free_ char *newpath;
469
470                 newpath = path_make_absolute_cwd(path);
471                 if (!newpath)
472                         return -ENOMEM;
473
474                 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
475         }
476
477         if (r == 0)
478                 r = setfscreatecon(fcon);
479
480         if (r < 0 && errno != ENOENT) {
481                 log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path);
482
483                 if (security_getenforce() == 1) {
484                         r = -errno;
485                         goto finish;
486                 }
487         }
488
489         r = bind(fd, addr, addrlen);
490         if (r < 0)
491                 r = -errno;
492
493 finish:
494         setfscreatecon(NULL);
495         return r;
496
497 skipped:
498 #endif
499         return bind(fd, addr, addrlen) < 0 ? -errno : 0;
500 }