chiark / gitweb /
selinux: use setcon() instead of reexec to apply selinux policy
[elogind.git] / src / 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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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 "macro.h"
34 #include "util.h"
35 #include "log.h"
36 #include "label.h"
37
38 int selinux_setup(bool *loaded_policy) {
39
40 #ifdef HAVE_SELINUX
41        int enforce = 0;
42        usec_t before_load, after_load;
43        security_context_t con;
44        int r;
45
46        assert(loaded_policy);
47
48        /* Already initialized by somebody else? */
49        r = getcon_raw(&con);
50        if (r == 0) {
51                bool initialized;
52
53                initialized = !streq(con, "kernel");
54                freecon(con);
55
56                if (initialized)
57                        return 0;
58        }
59
60        /* Make sure we have no fds open while loading the policy and
61         * transitioning */
62        log_close();
63
64        /* Now load the policy */
65        before_load = now(CLOCK_MONOTONIC);
66        r = selinux_init_load_policy(&enforce);
67
68        if (r == 0) {
69                char timespan[FORMAT_TIMESPAN_MAX];
70                char *label;
71
72                /* Transition to the new context */
73                r = label_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
74                if (r < 0) {
75                        log_open();
76                        log_error("Failed to compute init label, ignoring.");
77                } else {
78                        r = setcon(label);
79
80                        log_open();
81                        if (r < 0)
82                                log_error("Failed to transition into init label '%s', ignoring.", label);
83
84                        label_free(label);
85                }
86
87                after_load = now(CLOCK_MONOTONIC);
88
89                log_info("Successfully loaded SELinux policy in %s.",
90                          format_timespan(timespan, sizeof(timespan), after_load - before_load));
91
92                *loaded_policy = true;
93
94        } else {
95                if (enforce > 0) {
96                        log_error("Failed to load SELinux policy.");
97                        return -EIO;
98                } else
99                        log_debug("Unable to load SELinux policy.");
100        }
101 #endif
102
103        return 0;
104 }