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/>.
29 #include <selinux/selinux.h>
32 #include "selinux-setup.h"
33 #include "selinux-util.h"
35 #include "mount-setup.h"
41 static int null_log(int type, const char *fmt, ...) {
46 int selinux_setup(bool *loaded_policy) {
50 usec_t before_load, after_load;
51 security_context_t con;
53 union selinux_callback cb;
54 bool initialized = false;
56 assert(loaded_policy);
58 /* Turn off all of SELinux' own logging, we want to do that */
59 cb.func_log = null_log;
60 selinux_set_callback(SELINUX_CB_LOG, cb);
62 /* Don't load policy in the initrd if we don't appear to have
63 * it. For the real root, we check below if we've already
64 * loaded policy, and return gracefully.
66 if (in_initrd() && access(selinux_path(), F_OK) < 0)
69 /* Already initialized by somebody else? */
72 initialized = !streq(con, "kernel");
76 /* Make sure we have no fds open while loading the policy and
80 /* Now load the policy */
81 before_load = now(CLOCK_MONOTONIC);
82 r = selinux_init_load_policy(&enforce);
84 char timespan[FORMAT_TIMESPAN_MAX];
89 /* Transition to the new context */
90 r = label_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
91 if (r < 0 || label == NULL) {
93 log_error("Failed to compute init label, ignoring.");
99 log_error("Failed to transition into init label '%s', ignoring.", label);
104 after_load = now(CLOCK_MONOTONIC);
106 log_info("Successfully loaded SELinux policy in %s.",
107 format_timespan(timespan, sizeof(timespan), after_load - before_load, 0));
109 *loaded_policy = true;
116 log_error("Failed to load SELinux policy. Freezing.");
120 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
122 log_debug("Unable to load SELinux policy. Ignoring.");