]>
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 | ||
c4dcdb9f | 22 | #include <errno.h> |
07630cea LP |
23 | #include <stdio.h> |
24 | #include <unistd.h> | |
c4dcdb9f LP |
25 | |
26 | #ifdef HAVE_SELINUX | |
27 | #include <selinux/selinux.h> | |
28 | #endif | |
29 | ||
07630cea | 30 | #include "log.h" |
c4dcdb9f | 31 | #include "macro.h" |
07630cea LP |
32 | #include "selinux-util.h" |
33 | #include "string-util.h" | |
c4dcdb9f | 34 | #include "util.h" |
07630cea | 35 | #include "selinux-setup.h" |
0b3325e7 | 36 | |
f8e9f2cc | 37 | #ifdef HAVE_SELINUX |
82613b14 | 38 | _printf_(2,3) |
dbc655d5 LP |
39 | static int null_log(int type, const char *fmt, ...) { |
40 | return 0; | |
41 | } | |
f8e9f2cc | 42 | #endif |
dbc655d5 | 43 | |
8a188de9 | 44 | int mac_selinux_setup(bool *loaded_policy) { |
c4dcdb9f | 45 | |
c4dcdb9f | 46 | #ifdef HAVE_SELINUX |
4ab72d6f WW |
47 | int enforce = 0; |
48 | usec_t before_load, after_load; | |
49 | security_context_t con; | |
50 | int r; | |
51 | union selinux_callback cb; | |
68d3acac | 52 | bool initialized = false; |
4ab72d6f WW |
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 | /* Don't load policy in the initrd if we don't appear to have | |
61 | * it. For the real root, we check below if we've already | |
62 | * loaded policy, and return gracefully. | |
63 | */ | |
64 | if (in_initrd() && access(selinux_path(), F_OK) < 0) | |
65 | return 0; | |
66 | ||
67 | /* Already initialized by somebody else? */ | |
68 | r = getcon_raw(&con); | |
69 | if (r == 0) { | |
4ab72d6f WW |
70 | initialized = !streq(con, "kernel"); |
71 | freecon(con); | |
4ab72d6f WW |
72 | } |
73 | ||
74 | /* Make sure we have no fds open while loading the policy and | |
75 | * transitioning */ | |
76 | log_close(); | |
77 | ||
78 | /* Now load the policy */ | |
79 | before_load = now(CLOCK_MONOTONIC); | |
80 | r = selinux_init_load_policy(&enforce); | |
81 | if (r == 0) { | |
710a6b50 | 82 | _cleanup_(mac_selinux_freep) char *label = NULL; |
4ab72d6f | 83 | char timespan[FORMAT_TIMESPAN_MAX]; |
4ab72d6f | 84 | |
6baa7db0 | 85 | mac_selinux_retest(); |
4ab72d6f WW |
86 | |
87 | /* Transition to the new context */ | |
cc56fafe | 88 | r = mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label); |
710a6b50 | 89 | if (r < 0 || !label) { |
4ab72d6f WW |
90 | log_open(); |
91 | log_error("Failed to compute init label, ignoring."); | |
92 | } else { | |
93 | r = setcon(label); | |
94 | ||
95 | log_open(); | |
96 | if (r < 0) | |
97 | log_error("Failed to transition into init label '%s', ignoring.", label); | |
4ab72d6f WW |
98 | } |
99 | ||
100 | after_load = now(CLOCK_MONOTONIC); | |
101 | ||
102 | log_info("Successfully loaded SELinux policy in %s.", | |
103 | format_timespan(timespan, sizeof(timespan), after_load - before_load, 0)); | |
104 | ||
105 | *loaded_policy = true; | |
106 | ||
107 | } else { | |
108 | log_open(); | |
109 | ||
110 | if (enforce > 0) { | |
68d3acac | 111 | if (!initialized) { |
cb6531be | 112 | log_emergency("Failed to load SELinux policy."); |
68d3acac WW |
113 | return -EIO; |
114 | } | |
115 | ||
116 | log_warning("Failed to load new SELinux policy. Continuing with old policy."); | |
4ab72d6f WW |
117 | } else |
118 | log_debug("Unable to load SELinux policy. Ignoring."); | |
119 | } | |
c4dcdb9f LP |
120 | #endif |
121 | ||
4ab72d6f | 122 | return 0; |
c4dcdb9f | 123 | } |