]>
Commit | Line | Data |
---|---|---|
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 |
41 | static int null_log(int type, const char *fmt, ...) { |
42 | return 0; | |
43 | } | |
f8e9f2cc | 44 | #endif |
dbc655d5 | 45 | |
0b3325e7 | 46 | int selinux_setup(bool *loaded_policy) { |
c4dcdb9f | 47 | |
c4dcdb9f | 48 | #ifdef HAVE_SELINUX |
4ab72d6f WW |
49 | int enforce = 0; |
50 | usec_t before_load, after_load; | |
51 | security_context_t con; | |
52 | int r; | |
53 | union selinux_callback cb; | |
68d3acac | 54 | bool initialized = false; |
4ab72d6f WW |
55 | |
56 | assert(loaded_policy); | |
57 | ||
58 | /* Turn off all of SELinux' own logging, we want to do that */ | |
59 | cb.func_log = null_log; | |
60 | selinux_set_callback(SELINUX_CB_LOG, cb); | |
61 | ||
62 | /* Don't load policy in the initrd if we don't appear to have | |
63 | * it. For the real root, we check below if we've already | |
64 | * loaded policy, and return gracefully. | |
65 | */ | |
66 | if (in_initrd() && access(selinux_path(), F_OK) < 0) | |
67 | return 0; | |
68 | ||
69 | /* Already initialized by somebody else? */ | |
70 | r = getcon_raw(&con); | |
71 | if (r == 0) { | |
4ab72d6f WW |
72 | initialized = !streq(con, "kernel"); |
73 | freecon(con); | |
4ab72d6f WW |
74 | } |
75 | ||
76 | /* Make sure we have no fds open while loading the policy and | |
77 | * transitioning */ | |
78 | log_close(); | |
79 | ||
80 | /* Now load the policy */ | |
81 | before_load = now(CLOCK_MONOTONIC); | |
82 | r = selinux_init_load_policy(&enforce); | |
83 | if (r == 0) { | |
84 | char timespan[FORMAT_TIMESPAN_MAX]; | |
85 | char *label; | |
86 | ||
87 | 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, 0)); | |
108 | ||
109 | *loaded_policy = true; | |
110 | ||
111 | } else { | |
112 | log_open(); | |
113 | ||
114 | if (enforce > 0) { | |
68d3acac WW |
115 | if (!initialized) { |
116 | log_error("Failed to load SELinux policy. Freezing."); | |
117 | return -EIO; | |
118 | } | |
119 | ||
120 | log_warning("Failed to load new SELinux policy. Continuing with old policy."); | |
4ab72d6f WW |
121 | } else |
122 | log_debug("Unable to load SELinux policy. Ignoring."); | |
123 | } | |
c4dcdb9f LP |
124 | #endif |
125 | ||
4ab72d6f | 126 | return 0; |
c4dcdb9f | 127 | } |