]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/selinux-setup.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / core / selinux-setup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c4dcdb9f 2
c4dcdb9f 3#include <errno.h>
07630cea
LP
4#include <stdio.h>
5#include <unistd.h>
c4dcdb9f 6
349cc4a5 7#if HAVE_SELINUX
c4dcdb9f
LP
8#include <selinux/selinux.h>
9#endif
10
07630cea 11#include "log.h"
c4dcdb9f 12#include "macro.h"
cf0fbc49 13#include "selinux-setup.h"
07630cea
LP
14#include "selinux-util.h"
15#include "string-util.h"
ca78ad1d 16#include "time-util.h"
c4dcdb9f 17#include "util.h"
0b3325e7 18
349cc4a5 19#if HAVE_SELINUX
82613b14 20_printf_(2,3)
dbc655d5
LP
21static int null_log(int type, const char *fmt, ...) {
22 return 0;
23}
f8e9f2cc 24#endif
dbc655d5 25
8a188de9 26int mac_selinux_setup(bool *loaded_policy) {
c4dcdb9f 27
349cc4a5 28#if HAVE_SELINUX
4ab72d6f
WW
29 int enforce = 0;
30 usec_t before_load, after_load;
2ed96880 31 char *con;
4ab72d6f 32 int r;
2062ada7
LP
33 static const union selinux_callback cb = {
34 .func_log = null_log,
35 };
36
68d3acac 37 bool initialized = false;
4ab72d6f
WW
38
39 assert(loaded_policy);
40
41 /* Turn off all of SELinux' own logging, we want to do that */
4ab72d6f
WW
42 selinux_set_callback(SELINUX_CB_LOG, cb);
43
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.
47 */
48 if (in_initrd() && access(selinux_path(), F_OK) < 0)
49 return 0;
50
51 /* Already initialized by somebody else? */
52 r = getcon_raw(&con);
a119185c
AR
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. */
199a8922 57 if (r == 0 && con) {
4ab72d6f
WW
58 initialized = !streq(con, "kernel");
59 freecon(con);
4ab72d6f
WW
60 }
61
62 /* Make sure we have no fds open while loading the policy and
63 * transitioning */
64 log_close();
65
66 /* Now load the policy */
67 before_load = now(CLOCK_MONOTONIC);
68 r = selinux_init_load_policy(&enforce);
69 if (r == 0) {
710a6b50 70 _cleanup_(mac_selinux_freep) char *label = NULL;
4ab72d6f 71 char timespan[FORMAT_TIMESPAN_MAX];
4ab72d6f 72
6baa7db0 73 mac_selinux_retest();
4ab72d6f
WW
74
75 /* Transition to the new context */
cc56fafe 76 r = mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
710a6b50 77 if (r < 0 || !label) {
4ab72d6f
WW
78 log_open();
79 log_error("Failed to compute init label, ignoring.");
80 } else {
a1d2de07 81 r = setcon_raw(label);
4ab72d6f
WW
82
83 log_open();
84 if (r < 0)
85 log_error("Failed to transition into init label '%s', ignoring.", label);
4ab72d6f
WW
86 }
87
88 after_load = now(CLOCK_MONOTONIC);
89
90 log_info("Successfully loaded SELinux policy in %s.",
91 format_timespan(timespan, sizeof(timespan), after_load - before_load, 0));
92
93 *loaded_policy = true;
94
95 } else {
96 log_open();
97
98 if (enforce > 0) {
d7a0f1f4
FS
99 if (!initialized)
100 return log_emergency_errno(SYNTHETIC_ERRNO(EIO),
101 "Failed to load SELinux policy.");
68d3acac
WW
102
103 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
4ab72d6f
WW
104 } else
105 log_debug("Unable to load SELinux policy. Ignoring.");
106 }
c4dcdb9f
LP
107#endif
108
4ab72d6f 109 return 0;
c4dcdb9f 110}