1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <sys/socket.h>
27 #include <sys/types.h>
30 #include <sys/xattr.h>
32 #include <selinux/selinux.h>
33 #include <selinux/label.h>
34 #include <selinux/context.h>
40 #include "path-util.h"
41 #include "selinux-util.h"
42 #include "smack-util.h"
45 DEFINE_TRIVIAL_CLEANUP_FUNC(security_context_t, freecon);
46 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
48 #define _cleanup_security_context_free_ _cleanup_(freeconp)
49 #define _cleanup_context_free_ _cleanup_(context_freep)
51 static struct selabel_handle *label_hnd = NULL;
54 static int smack_relabel_in_dev(const char *path) {
62 * Path must be in /dev and must exist
64 if (!path_startswith(path, "/dev"))
72 * Label directories and character devices "*".
74 * Don't change anything else.
76 if (S_ISDIR(sb.st_mode))
77 label = SMACK_STAR_LABEL;
78 else if (S_ISLNK(sb.st_mode))
79 label = SMACK_FLOOR_LABEL;
80 else if (S_ISCHR(sb.st_mode))
81 label = SMACK_STAR_LABEL;
85 r = setxattr(path, "security.SMACK64", label, strlen(label), 0);
87 log_error("Smack relabeling \"%s\" %m", path);
95 int label_init(const char *prefix) {
99 usec_t before_timestamp, after_timestamp;
100 struct mallinfo before_mallinfo, after_mallinfo;
108 before_mallinfo = mallinfo();
109 before_timestamp = now(CLOCK_MONOTONIC);
112 struct selinux_opt options[] = {
113 { .type = SELABEL_OPT_SUBSET, .value = prefix },
116 label_hnd = selabel_open(SELABEL_CTX_FILE, options, ELEMENTSOF(options));
118 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
121 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
122 "Failed to initialize SELinux context: %m");
123 r = security_getenforce() == 1 ? -errno : 0;
125 char timespan[FORMAT_TIMESPAN_MAX];
128 after_timestamp = now(CLOCK_MONOTONIC);
129 after_mallinfo = mallinfo();
131 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
133 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
134 format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
142 static int label_fix_selinux(const char *path, bool ignore_enoent, bool ignore_erofs) {
147 security_context_t fcon;
152 r = lstat(path, &st);
154 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
156 /* If there's no label to set, then exit without warning */
157 if (r < 0 && errno == ENOENT)
161 r = lsetfilecon(path, fcon);
164 /* If the FS doesn't support labels, then exit without warning */
165 if (r < 0 && errno == ENOTSUP)
171 /* Ignore ENOENT in some cases */
172 if (ignore_enoent && errno == ENOENT)
175 if (ignore_erofs && errno == EROFS)
178 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
179 "Unable to fix label of %s: %m", path);
180 r = security_getenforce() == 1 ? -errno : 0;
187 int label_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
191 r = label_fix_selinux(path, ignore_enoent, ignore_erofs);
197 r = smack_relabel_in_dev(path);
205 void label_finish(void) {
212 selabel_close(label_hnd);
216 int label_get_create_label_from_exe(const char *exe, char **label) {
221 security_context_t mycon = NULL, fcon = NULL;
222 security_class_t sclass;
224 if (!use_selinux()) {
233 r = getfilecon(exe, &fcon);
237 sclass = string_to_security_class("process");
238 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
240 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
243 if (r < 0 && security_getenforce() == 1)
253 int label_get_our_label(char **label) {
268 int label_get_child_mls_label(int socket_fd, const char *exe, char **label) {
273 _cleanup_security_context_free_ security_context_t mycon = NULL, peercon = NULL, fcon = NULL, ret = NULL;
274 _cleanup_context_free_ context_t pcon = NULL, bcon = NULL;
275 security_class_t sclass;
277 const char *range = NULL;
279 assert(socket_fd >= 0);
289 r = getpeercon(socket_fd, &peercon);
295 r = getexeccon(&fcon);
302 /* If there is no context set for next exec let's use context
303 of target executable */
304 r = getfilecon(exe, &fcon);
311 bcon = context_new(mycon);
317 pcon = context_new(peercon);
323 range = context_range_get(pcon);
329 r = context_range_set(bcon, range);
336 mycon = context_str(bcon);
342 sclass = string_to_security_class("process");
343 r = security_compute_create(mycon, fcon, sclass, &ret);
353 if (r < 0 && security_getenforce() == 1)
359 int label_context_set(const char *path, mode_t mode) {
363 security_context_t filecon = NULL;
365 if (!use_selinux() || !label_hnd)
368 r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
369 if (r < 0 && errno != ENOENT)
372 r = setfscreatecon(filecon);
374 log_error("Failed to set SELinux file context on %s: %m", path);
381 if (r < 0 && security_getenforce() == 0)
388 int label_socket_set(const char *label) {
394 if (setsockcreatecon((security_context_t) label) < 0) {
395 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
396 "Failed to set SELinux context (%s) on socket: %m", label);
398 if (security_getenforce() == 1)
406 void label_context_clear(void) {
414 setfscreatecon(NULL);
418 void label_socket_clear(void) {
426 setsockcreatecon(NULL);
430 void label_free(const char *label) {
436 freecon((security_context_t) label);
440 static int label_mkdir_selinux(const char *path, mode_t mode) {
444 /* Creates a directory and labels it according to the SELinux policy */
445 security_context_t fcon = NULL;
450 if (path_is_absolute(path))
451 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFDIR);
453 _cleanup_free_ char *newpath;
455 newpath = path_make_absolute_cwd(path);
459 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFDIR);
463 r = setfscreatecon(fcon);
465 if (r < 0 && errno != ENOENT) {
466 log_error("Failed to set security context %s for %s: %m", fcon, path);
468 if (security_getenforce() == 1) {
474 r = mkdir(path, mode);
479 setfscreatecon(NULL);
486 int label_mkdir(const char *path, mode_t mode) {
490 r = label_mkdir_selinux(path, mode);
496 r = mkdir(path, mode);
497 if (r < 0 && errno != EEXIST)
500 r = smack_relabel_in_dev(path);
505 r = mkdir(path, mode);
506 if (r < 0 && errno != EEXIST)
512 int label_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
514 /* Binds a socket and label its file system object according to the SELinux policy */
517 security_context_t fcon = NULL;
518 const struct sockaddr_un *un;
524 assert(addrlen >= sizeof(sa_family_t));
526 if (!use_selinux() || !label_hnd)
529 /* Filter out non-local sockets */
530 if (addr->sa_family != AF_UNIX)
533 /* Filter out anonymous sockets */
534 if (addrlen < sizeof(sa_family_t) + 1)
537 /* Filter out abstract namespace sockets */
538 un = (const struct sockaddr_un*) addr;
539 if (un->sun_path[0] == 0)
542 path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
544 if (path_is_absolute(path))
545 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
547 _cleanup_free_ char *newpath;
549 newpath = path_make_absolute_cwd(path);
553 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
557 r = setfscreatecon(fcon);
559 if (r < 0 && errno != ENOENT) {
560 log_error("Failed to set security context %s for %s: %m", fcon, path);
562 if (security_getenforce() == 1) {
568 r = bind(fd, addr, addrlen);
573 setfscreatecon(NULL);
580 return bind(fd, addr, addrlen) < 0 ? -errno : 0;
583 int label_apply(const char *path, const char *label) {
590 r = setfilecon(path, (char *)label);