]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/selinux-setup.c
build-sys: prefer using ln --relative -s where appropriate
[thirdparty/systemd.git] / src / core / selinux-setup.c
CommitLineData
c4dcdb9f
LP
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
5430f7f2
LP
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
c4dcdb9f
LP
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
5430f7f2 16 Lesser General Public License for more details.
c4dcdb9f 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
c4dcdb9f
LP
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"
cad45ba1
LP
33#include "selinux-util.h"
34#include "label.h"
4ef31082 35#include "mount-setup.h"
c4dcdb9f
LP
36#include "macro.h"
37#include "util.h"
38#include "log.h"
0b3325e7 39
f8e9f2cc 40#ifdef HAVE_SELINUX
dbc655d5
LP
41static int null_log(int type, const char *fmt, ...) {
42 return 0;
43}
f8e9f2cc 44#endif
dbc655d5 45
0b3325e7 46int selinux_setup(bool *loaded_policy) {
c4dcdb9f 47
c4dcdb9f
LP
48#ifdef HAVE_SELINUX
49 int enforce = 0;
0b3325e7 50 usec_t before_load, after_load;
0843f2d6 51 security_context_t con;
0b3325e7 52 int r;
dbc655d5 53 union selinux_callback cb;
0b3325e7
LP
54
55 assert(loaded_policy);
c4dcdb9f 56
dbc655d5
LP
57 /* Turn off all of SELinux' own logging, we want to do that */
58 cb.func_log = null_log;
59 selinux_set_callback(SELINUX_CB_LOG, cb);
60
37f78db2
CW
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
0b3325e7
LP
68 /* Already initialized by somebody else? */
69 r = getcon_raw(&con);
70 if (r == 0) {
0843f2d6
LP
71 bool initialized;
72
73 initialized = !streq(con, "kernel");
74 freecon(con);
75
76 if (initialized)
77 return 0;
78 }
c4dcdb9f 79
0b3325e7
LP
80 /* Make sure we have no fds open while loading the policy and
81 * transitioning */
82 log_close();
1829dc9d 83
0b3325e7
LP
84 /* Now load the policy */
85 before_load = now(CLOCK_MONOTONIC);
86 r = selinux_init_load_policy(&enforce);
0b3325e7
LP
87 if (r == 0) {
88 char timespan[FORMAT_TIMESPAN_MAX];
89 char *label;
c4dcdb9f 90
cad45ba1 91 retest_selinux();
4d4c7486 92
0b3325e7
LP
93 /* Transition to the new context */
94 r = label_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
4ef31082 95 if (r < 0 || label == NULL) {
0b3325e7
LP
96 log_open();
97 log_error("Failed to compute init label, ignoring.");
98 } else {
99 r = setcon(label);
c4dcdb9f 100
0b3325e7
LP
101 log_open();
102 if (r < 0)
103 log_error("Failed to transition into init label '%s', ignoring.", label);
c4dcdb9f 104
0b3325e7
LP
105 label_free(label);
106 }
107
108 after_load = now(CLOCK_MONOTONIC);
1829dc9d 109
0b3325e7 110 log_info("Successfully loaded SELinux policy in %s.",
2fa4092c 111 format_timespan(timespan, sizeof(timespan), after_load - before_load, 0));
0b3325e7
LP
112
113 *loaded_policy = true;
114
115 } else {
878587bd
LP
116 log_open();
117
25bafad6 118 if (enforce > 0) {
878587bd 119 log_error("Failed to load SELinux policy. Freezing.");
c4dcdb9f 120 return -EIO;
0b3325e7 121 } else
878587bd 122 log_debug("Unable to load SELinux policy. Ignoring.");
c4dcdb9f
LP
123 }
124#endif
125
126 return 0;
127}