]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/label.c
selinux: automatically load policy if the initrd hasn't done this for us yet
[thirdparty/systemd.git] / src / label.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 <errno.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include "label.h"
27 #include "util.h"
28
29 #ifdef HAVE_SELINUX
30 #include <selinux/selinux.h>
31 #include <selinux/label.h>
32
33 static struct selabel_handle *label_hnd = NULL;
34
35 static inline bool use_selinux(void) {
36 static int use_selinux_ind = -1;
37
38 if (use_selinux_ind < 0)
39 use_selinux_ind = is_selinux_enabled() > 0;
40
41 return use_selinux_ind;
42 }
43
44 #endif
45
46 int label_init(void) {
47 int r = 0;
48
49 #ifdef HAVE_SELINUX
50
51 if (!use_selinux())
52 return 0;
53
54 if (label_hnd)
55 return 0;
56
57 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
58 if (!label_hnd) {
59 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
60 "Failed to initialize SELinux context: %m");
61 r = security_getenforce() == 1 ? -errno : 0;
62 }
63 #endif
64
65 return r;
66 }
67
68 int label_fix(const char *path) {
69 int r = 0;
70
71 #ifdef HAVE_SELINUX
72 struct stat st;
73 security_context_t fcon;
74
75 if (!use_selinux() || !label_hnd)
76 return 0;
77
78 r = lstat(path, &st);
79 if (r == 0) {
80 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
81
82 /* If there's no label to set, then exit without warning */
83 if (r < 0 && errno == ENOENT)
84 return 0;
85
86 if (r == 0) {
87 r = setfilecon(path, fcon);
88 freecon(fcon);
89
90 /* If the FS doesn't support labels, then exit without warning */
91 if (r < 0 && errno == ENOTSUP)
92 return 0;
93 }
94 }
95
96 if (r < 0) {
97 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
98 "Unable to fix label of %s: %m", path);
99 r = security_getenforce() == 1 ? -errno : 0;
100 }
101 #endif
102
103 return r;
104 }
105
106 void label_finish(void) {
107
108 #ifdef HAVE_SELINUX
109 if (use_selinux() && label_hnd)
110 selabel_close(label_hnd);
111 #endif
112 }
113
114 int label_get_socket_label_from_exe(const char *exe, char **label) {
115
116 int r = 0;
117
118 #ifdef HAVE_SELINUX
119 security_context_t mycon = NULL, fcon = NULL;
120 security_class_t sclass;
121
122 if (!use_selinux()) {
123 *label = NULL;
124 return 0;
125 }
126
127 r = getcon(&mycon);
128 if (r < 0)
129 goto fail;
130
131 r = getfilecon(exe, &fcon);
132 if (r < 0)
133 goto fail;
134
135 sclass = string_to_security_class("process");
136 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
137 if (r == 0)
138 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
139
140 fail:
141 if (r < 0 && security_getenforce() == 1)
142 r = -errno;
143
144 freecon(mycon);
145 freecon(fcon);
146 #endif
147
148 return r;
149 }
150
151 int label_fifofile_set(const char *path) {
152 int r = 0;
153
154 #ifdef HAVE_SELINUX
155 security_context_t filecon = NULL;
156
157 if (!use_selinux() || !label_hnd)
158 return 0;
159
160 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) {
161 if ((r = setfscreatecon(filecon)) < 0) {
162 log_error("Failed to set SELinux file context on %s: %m", path);
163 r = -errno;
164 }
165
166 freecon(filecon);
167 }
168
169 if (r < 0 && security_getenforce() == 0)
170 r = 0;
171 #endif
172
173 return r;
174 }
175
176 int label_socket_set(const char *label) {
177
178 #ifdef HAVE_SELINUX
179 if (!use_selinux())
180 return 0;
181
182 if (setsockcreatecon((security_context_t) label) < 0) {
183 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
184 "Failed to set SELinux context (%s) on socket: %m", label);
185
186 if (security_getenforce() == 1)
187 return -errno;
188 }
189 #endif
190
191 return 0;
192 }
193
194 void label_file_clear(void) {
195
196 #ifdef HAVE_SELINUX
197 if (!use_selinux())
198 return;
199
200 setfscreatecon(NULL);
201 #endif
202 }
203
204 void label_socket_clear(void) {
205
206 #ifdef HAVE_SELINUX
207 if (!use_selinux())
208 return;
209
210 setsockcreatecon(NULL);
211 #endif
212 }
213
214 void label_free(const char *label) {
215
216 #ifdef HAVE_SELINUX
217 if (!use_selinux())
218 return;
219
220 freecon((security_context_t) label);
221 #endif
222 }
223
224 int label_mkdir(
225 const char *path,
226 mode_t mode) {
227
228 /* Creates a directory and labels it according to the SELinux policy */
229
230 #ifdef HAVE_SELINUX
231 int r;
232 security_context_t fcon = NULL;
233
234 if (use_selinux() && label_hnd) {
235
236 if (path[0] == '/')
237 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
238 else {
239 char *cwd = NULL, *newpath = NULL;
240
241 cwd = get_current_dir_name();
242
243 if (cwd || asprintf(&newpath, "%s/%s", cwd, path) < 0) {
244 free(cwd);
245 return -errno;
246 }
247
248 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
249 free(cwd);
250 free(newpath);
251 }
252
253 if (r == 0)
254 r = setfscreatecon(fcon);
255
256 if (r < 0 && errno != ENOENT) {
257 log_error("Failed to set security context %s for %s: %m", fcon, path);
258 r = -errno;
259
260 if (security_getenforce() == 1)
261 goto finish;
262 }
263 }
264
265 if ((r = mkdir(path, mode)) < 0)
266 r = -errno;
267
268 finish:
269 if (use_selinux() && label_hnd) {
270 setfscreatecon(NULL);
271 freecon(fcon);
272 }
273
274 return r;
275 #else
276 return mkdir(path, mode);
277 #endif
278 }