]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/selinux-setup.c
7b69b4c4c63b0759ef47e4b469f0ee3ffb8371f1
[thirdparty/systemd.git] / src / core / selinux-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <unistd.h>
24
25 #if HAVE_SELINUX
26 #include <selinux/selinux.h>
27 #endif
28
29 #include "log.h"
30 #include "macro.h"
31 #include "selinux-setup.h"
32 #include "selinux-util.h"
33 #include "string-util.h"
34 #include "util.h"
35
36 #if HAVE_SELINUX
37 _printf_(2,3)
38 static int null_log(int type, const char *fmt, ...) {
39 return 0;
40 }
41 #endif
42
43 int mac_selinux_setup(bool *loaded_policy) {
44
45 #if HAVE_SELINUX
46 int enforce = 0;
47 usec_t before_load, after_load;
48 char *con;
49 int r;
50 static const union selinux_callback cb = {
51 .func_log = null_log,
52 };
53
54 bool initialized = false;
55
56 assert(loaded_policy);
57
58 /* Turn off all of SELinux' own logging, we want to do that */
59 selinux_set_callback(SELINUX_CB_LOG, cb);
60
61 /* Don't load policy in the initrd if we don't appear to have
62 * it. For the real root, we check below if we've already
63 * loaded policy, and return gracefully.
64 */
65 if (in_initrd() && access(selinux_path(), F_OK) < 0)
66 return 0;
67
68 /* Already initialized by somebody else? */
69 r = getcon_raw(&con);
70 if (r == 0) {
71 initialized = !streq(con, "kernel");
72 freecon(con);
73 }
74
75 /* Make sure we have no fds open while loading the policy and
76 * transitioning */
77 log_close();
78
79 /* Now load the policy */
80 before_load = now(CLOCK_MONOTONIC);
81 r = selinux_init_load_policy(&enforce);
82 if (r == 0) {
83 _cleanup_(mac_selinux_freep) char *label = NULL;
84 char timespan[FORMAT_TIMESPAN_MAX];
85
86 mac_selinux_retest();
87
88 /* Transition to the new context */
89 r = mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
90 if (r < 0 || !label) {
91 log_open();
92 log_error("Failed to compute init label, ignoring.");
93 } else {
94 r = setcon_raw(label);
95
96 log_open();
97 if (r < 0)
98 log_error("Failed to transition into init label '%s', ignoring.", label);
99 }
100
101 after_load = now(CLOCK_MONOTONIC);
102
103 log_info("Successfully loaded SELinux policy in %s.",
104 format_timespan(timespan, sizeof(timespan), after_load - before_load, 0));
105
106 *loaded_policy = true;
107
108 } else {
109 log_open();
110
111 if (enforce > 0) {
112 if (!initialized) {
113 log_emergency("Failed to load SELinux policy.");
114 return -EIO;
115 }
116
117 log_warning("Failed to load new SELinux policy. Continuing with old policy.");
118 } else
119 log_debug("Unable to load SELinux policy. Ignoring.");
120 }
121 #endif
122
123 return 0;
124 }