]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/selinux-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
13 #include <selinux/selinux.h>
18 #include "selinux-setup.h"
19 #include "selinux-util.h"
20 #include "string-util.h"
25 static int null_log(int type
, const char *fmt
, ...) {
30 int mac_selinux_setup(bool *loaded_policy
) {
34 usec_t before_load
, after_load
;
37 static const union selinux_callback cb
= {
41 bool initialized
= false;
43 assert(loaded_policy
);
45 /* Turn off all of SELinux' own logging, we want to do that */
46 selinux_set_callback(SELINUX_CB_LOG
, cb
);
48 /* Don't load policy in the initrd if we don't appear to have
49 * it. For the real root, we check below if we've already
50 * loaded policy, and return gracefully.
52 if (in_initrd() && access(selinux_path(), F_OK
) < 0)
55 /* Already initialized by somebody else? */
58 initialized
= !streq(con
, "kernel");
62 /* Make sure we have no fds open while loading the policy and
66 /* Now load the policy */
67 before_load
= now(CLOCK_MONOTONIC
);
68 r
= selinux_init_load_policy(&enforce
);
70 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
71 char timespan
[FORMAT_TIMESPAN_MAX
];
75 /* Transition to the new context */
76 r
= mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH
, &label
);
77 if (r
< 0 || !label
) {
79 log_error("Failed to compute init label, ignoring.");
81 r
= setcon_raw(label
);
85 log_error("Failed to transition into init label '%s', ignoring.", label
);
88 after_load
= now(CLOCK_MONOTONIC
);
90 log_info("Successfully loaded SELinux policy in %s.",
91 format_timespan(timespan
, sizeof(timespan
), after_load
- before_load
, 0));
93 *loaded_policy
= true;
100 log_emergency("Failed to load SELinux policy.");
104 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
106 log_debug("Unable to load SELinux policy. Ignoring.");