]>
git.proxmox.com Git - 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
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <selinux/selinux.h>
31 #include "selinux-setup.h"
32 #include "selinux-util.h"
33 #include "string-util.h"
38 static int null_log(int type
, const char *fmt
, ...) {
43 int mac_selinux_setup(bool *loaded_policy
) {
47 usec_t before_load
, after_load
;
50 union selinux_callback cb
;
51 bool initialized
= false;
53 assert(loaded_policy
);
55 /* Turn off all of SELinux' own logging, we want to do that */
56 cb
.func_log
= null_log
;
57 selinux_set_callback(SELINUX_CB_LOG
, cb
);
59 /* Don't load policy in the initrd if we don't appear to have
60 * it. For the real root, we check below if we've already
61 * loaded policy, and return gracefully.
63 if (in_initrd() && access(selinux_path(), F_OK
) < 0)
66 /* Already initialized by somebody else? */
69 initialized
= !streq(con
, "kernel");
73 /* Make sure we have no fds open while loading the policy and
77 /* Now load the policy */
78 before_load
= now(CLOCK_MONOTONIC
);
79 r
= selinux_init_load_policy(&enforce
);
81 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
82 char timespan
[FORMAT_TIMESPAN_MAX
];
86 /* Transition to the new context */
87 r
= mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH
, &label
);
88 if (r
< 0 || !label
) {
90 log_error("Failed to compute init label, ignoring.");
92 r
= setcon_raw(label
);
96 log_error("Failed to transition into init label '%s', ignoring.", label
);
99 after_load
= now(CLOCK_MONOTONIC
);
101 log_info("Successfully loaded SELinux policy in %s.",
102 format_timespan(timespan
, sizeof(timespan
), after_load
- before_load
, 0));
104 *loaded_policy
= true;
111 log_emergency("Failed to load SELinux policy.");
115 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
117 log_debug("Unable to load SELinux policy. Ignoring.");