]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/selinux-setup.c
b8a94a52ab6d21ca2bd844f83277c63eba297714
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"
16 #include "time-util.h"
21 static int null_log(int type
, const char *fmt
, ...) {
26 int mac_selinux_setup(bool *loaded_policy
) {
30 usec_t before_load
, after_load
;
33 static const union selinux_callback cb
= {
37 bool initialized
= false;
39 assert(loaded_policy
);
41 /* Turn off all of SELinux' own logging, we want to do that */
42 selinux_set_callback(SELINUX_CB_LOG
, cb
);
44 /* Don't load policy in the initrd if we don't appear to have
45 * it. For the real root, we check below if we've already
46 * loaded policy, and return gracefully.
48 if (in_initrd() && access(selinux_path(), F_OK
) < 0)
51 /* Already initialized by somebody else? */
54 initialized
= !streq(con
, "kernel");
58 /* Make sure we have no fds open while loading the policy and
62 /* Now load the policy */
63 before_load
= now(CLOCK_MONOTONIC
);
64 r
= selinux_init_load_policy(&enforce
);
66 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
67 char timespan
[FORMAT_TIMESPAN_MAX
];
71 /* Transition to the new context */
72 r
= mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH
, &label
);
73 if (r
< 0 || !label
) {
75 log_error("Failed to compute init label, ignoring.");
77 r
= setcon_raw(label
);
81 log_error("Failed to transition into init label '%s', ignoring.", label
);
84 after_load
= now(CLOCK_MONOTONIC
);
86 log_info("Successfully loaded SELinux policy in %s.",
87 format_timespan(timespan
, sizeof(timespan
), after_load
- before_load
, 0));
89 *loaded_policy
= true;
96 log_emergency("Failed to load SELinux policy.");
100 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
102 log_debug("Unable to load SELinux policy. Ignoring.");