]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/selinux-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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? */
53 /* getcon_raw can return 0, and still give us a NULL pointer if
54 * /proc/self/attr/current is empty. SELinux guarantees this won't
55 * happen, but that file isn't specific to SELinux, and may be provided
56 * by some other arbitrary LSM with different semantics. */
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 return log_emergency_errno(SYNTHETIC_ERRNO(EIO
),
101 "Failed to load SELinux policy.");
103 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
105 log_debug("Unable to load SELinux policy. Ignoring.");