]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/selinux-setup.c
selinux: avoid inclusion of null_log without selinux
[thirdparty/systemd.git] / src / core / selinux-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #ifdef HAVE_SELINUX
29 #include <selinux/selinux.h>
30 #endif
31
32 #include "selinux-setup.h"
33 #include "mount-setup.h"
34 #include "macro.h"
35 #include "util.h"
36 #include "log.h"
37 #include "label.h"
38
39 #ifdef HAVE_SELINUX
40 static int null_log(int type, const char *fmt, ...) {
41 return 0;
42 }
43 #endif
44
45 int selinux_setup(bool *loaded_policy) {
46
47 #ifdef HAVE_SELINUX
48 int enforce = 0;
49 usec_t before_load, after_load;
50 security_context_t con;
51 int r;
52 union selinux_callback cb;
53
54 assert(loaded_policy);
55
56 /* Turn off all of SELinux' own logging, we want to do that */
57 cb.func_log = null_log;
58 selinux_set_callback(SELINUX_CB_LOG, cb);
59
60 /* Make sure getcon() works, which needs /proc and /sys */
61 mount_setup_early();
62
63 /* Already initialized by somebody else? */
64 r = getcon_raw(&con);
65 if (r == 0) {
66 bool initialized;
67
68 initialized = !streq(con, "kernel");
69 freecon(con);
70
71 if (initialized)
72 return 0;
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
83 if (r == 0) {
84 char timespan[FORMAT_TIMESPAN_MAX];
85 char *label;
86
87 label_retest_selinux();
88
89 /* Transition to the new context */
90 r = label_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
91 if (r < 0 || label == NULL) {
92 log_open();
93 log_error("Failed to compute init label, ignoring.");
94 } else {
95 r = setcon(label);
96
97 log_open();
98 if (r < 0)
99 log_error("Failed to transition into init label '%s', ignoring.", label);
100
101 label_free(label);
102 }
103
104 after_load = now(CLOCK_MONOTONIC);
105
106 log_info("Successfully loaded SELinux policy in %s.",
107 format_timespan(timespan, sizeof(timespan), after_load - before_load));
108
109 *loaded_policy = true;
110
111 } else {
112 log_open();
113
114 if (enforce > 0) {
115 log_error("Failed to load SELinux policy. Freezing.");
116 return -EIO;
117 } else
118 log_debug("Unable to load SELinux policy. Ignoring.");
119 }
120 #endif
121
122 return 0;
123 }