]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/selinux-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <selinux/selinux.h>
13 #include "selinux-setup.h"
14 #include "selinux-util.h"
15 #include "string-util.h"
20 static int null_log(int type
, const char *fmt
, ...) {
25 int mac_selinux_setup(bool *loaded_policy
) {
29 usec_t before_load
, after_load
;
32 static const union selinux_callback cb
= {
36 bool initialized
= false;
38 assert(loaded_policy
);
40 /* Turn off all of SELinux' own logging, we want to do that */
41 selinux_set_callback(SELINUX_CB_LOG
, cb
);
43 /* Don't load policy in the initrd if we don't appear to have
44 * it. For the real root, we check below if we've already
45 * loaded policy, and return gracefully.
47 if (in_initrd() && access(selinux_path(), F_OK
) < 0)
50 /* Already initialized by somebody else? */
53 initialized
= !streq(con
, "kernel");
57 /* Make sure we have no fds open while loading the policy and
61 /* Now load the policy */
62 before_load
= now(CLOCK_MONOTONIC
);
63 r
= selinux_init_load_policy(&enforce
);
65 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
66 char timespan
[FORMAT_TIMESPAN_MAX
];
70 /* Transition to the new context */
71 r
= mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH
, &label
);
72 if (r
< 0 || !label
) {
74 log_error("Failed to compute init label, ignoring.");
76 r
= setcon_raw(label
);
80 log_error("Failed to transition into init label '%s', ignoring.", label
);
83 after_load
= now(CLOCK_MONOTONIC
);
85 log_info("Successfully loaded SELinux policy in %s.",
86 format_timespan(timespan
, sizeof(timespan
), after_load
- before_load
, 0));
88 *loaded_policy
= true;
95 log_emergency("Failed to load SELinux policy.");
99 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
101 log_debug("Unable to load SELinux policy. Ignoring.");