]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
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 Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 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 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5eef597e | 22 | #include <errno.h> |
5eef597e MP |
23 | #include <malloc.h> |
24 | #include <sys/un.h> | |
25 | ||
26 | #ifdef HAVE_SELINUX | |
27 | #include <selinux/selinux.h> | |
28 | #include <selinux/label.h> | |
29 | #include <selinux/context.h> | |
30 | #endif | |
31 | ||
32 | #include "strv.h" | |
33 | #include "path-util.h" | |
663996b3 MS |
34 | #include "selinux-util.h" |
35 | ||
36 | #ifdef HAVE_SELINUX | |
5eef597e MP |
37 | DEFINE_TRIVIAL_CLEANUP_FUNC(security_context_t, freecon); |
38 | DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free); | |
663996b3 | 39 | |
5eef597e MP |
40 | #define _cleanup_security_context_free_ _cleanup_(freeconp) |
41 | #define _cleanup_context_free_ _cleanup_(context_freep) | |
663996b3 | 42 | |
5eef597e MP |
43 | static int cached_use = -1; |
44 | static struct selabel_handle *label_hnd = NULL; | |
663996b3 | 45 | |
5eef597e MP |
46 | #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__) |
47 | #endif | |
663996b3 | 48 | |
5eef597e MP |
49 | bool mac_selinux_use(void) { |
50 | #ifdef HAVE_SELINUX | |
51 | if (cached_use < 0) | |
52 | cached_use = is_selinux_enabled() > 0; | |
663996b3 | 53 | |
5eef597e MP |
54 | return cached_use; |
55 | #else | |
56 | return false; | |
57 | #endif | |
663996b3 MS |
58 | } |
59 | ||
5eef597e MP |
60 | void mac_selinux_retest(void) { |
61 | #ifdef HAVE_SELINUX | |
62 | cached_use = -1; | |
63 | #endif | |
663996b3 MS |
64 | } |
65 | ||
5eef597e MP |
66 | int mac_selinux_init(const char *prefix) { |
67 | int r = 0; | |
60f067b4 | 68 | |
5eef597e MP |
69 | #ifdef HAVE_SELINUX |
70 | usec_t before_timestamp, after_timestamp; | |
71 | struct mallinfo before_mallinfo, after_mallinfo; | |
72 | ||
73 | if (!mac_selinux_use()) | |
74 | return 0; | |
75 | ||
76 | if (label_hnd) | |
77 | return 0; | |
78 | ||
79 | before_mallinfo = mallinfo(); | |
80 | before_timestamp = now(CLOCK_MONOTONIC); | |
81 | ||
82 | if (prefix) { | |
83 | struct selinux_opt options[] = { | |
84 | { .type = SELABEL_OPT_SUBSET, .value = prefix }, | |
85 | }; | |
86 | ||
87 | label_hnd = selabel_open(SELABEL_CTX_FILE, options, ELEMENTSOF(options)); | |
88 | } else | |
89 | label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0); | |
90 | ||
91 | if (!label_hnd) { | |
92 | log_enforcing("Failed to initialize SELinux context: %m"); | |
93 | r = security_getenforce() == 1 ? -errno : 0; | |
94 | } else { | |
95 | char timespan[FORMAT_TIMESPAN_MAX]; | |
96 | int l; | |
97 | ||
98 | after_timestamp = now(CLOCK_MONOTONIC); | |
99 | after_mallinfo = mallinfo(); | |
100 | ||
101 | l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0; | |
102 | ||
103 | log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.", | |
104 | format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0), | |
105 | (l+1023)/1024); | |
106 | } | |
107 | #endif | |
108 | ||
109 | return r; | |
110 | } | |
111 | ||
112 | void mac_selinux_finish(void) { | |
113 | ||
114 | #ifdef HAVE_SELINUX | |
115 | if (!label_hnd) | |
116 | return; | |
117 | ||
118 | selabel_close(label_hnd); | |
e3bff60a | 119 | label_hnd = NULL; |
5eef597e MP |
120 | #endif |
121 | } | |
122 | ||
123 | int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) { | |
124 | ||
125 | #ifdef HAVE_SELINUX | |
126 | struct stat st; | |
127 | int r; | |
128 | ||
129 | assert(path); | |
130 | ||
131 | /* if mac_selinux_init() wasn't called before we are a NOOP */ | |
132 | if (!label_hnd) | |
133 | return 0; | |
134 | ||
135 | r = lstat(path, &st); | |
136 | if (r >= 0) { | |
137 | _cleanup_security_context_free_ security_context_t fcon = NULL; | |
138 | ||
139 | r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode); | |
140 | ||
141 | /* If there's no label to set, then exit without warning */ | |
142 | if (r < 0 && errno == ENOENT) | |
143 | return 0; | |
144 | ||
145 | if (r >= 0) { | |
146 | r = lsetfilecon(path, fcon); | |
147 | ||
148 | /* If the FS doesn't support labels, then exit without warning */ | |
e3bff60a | 149 | if (r < 0 && errno == EOPNOTSUPP) |
5eef597e MP |
150 | return 0; |
151 | } | |
152 | } | |
153 | ||
154 | if (r < 0) { | |
155 | /* Ignore ENOENT in some cases */ | |
156 | if (ignore_enoent && errno == ENOENT) | |
157 | return 0; | |
158 | ||
159 | if (ignore_erofs && errno == EROFS) | |
160 | return 0; | |
161 | ||
162 | log_enforcing("Unable to fix SELinux security context of %s: %m", path); | |
163 | if (security_getenforce() == 1) | |
164 | return -errno; | |
165 | } | |
166 | #endif | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | int mac_selinux_apply(const char *path, const char *label) { | |
172 | ||
173 | #ifdef HAVE_SELINUX | |
174 | assert(path); | |
175 | assert(label); | |
176 | ||
177 | if (!mac_selinux_use()) | |
178 | return 0; | |
179 | ||
180 | if (setfilecon(path, (security_context_t) label) < 0) { | |
181 | log_enforcing("Failed to set SELinux security context %s on path %s: %m", label, path); | |
182 | if (security_getenforce() == 1) | |
183 | return -errno; | |
184 | } | |
185 | #endif | |
186 | return 0; | |
187 | } | |
188 | ||
189 | int mac_selinux_get_create_label_from_exe(const char *exe, char **label) { | |
190 | int r = -EOPNOTSUPP; | |
191 | ||
192 | #ifdef HAVE_SELINUX | |
193 | _cleanup_security_context_free_ security_context_t mycon = NULL, fcon = NULL; | |
194 | security_class_t sclass; | |
195 | ||
196 | assert(exe); | |
197 | assert(label); | |
198 | ||
199 | if (!mac_selinux_use()) | |
200 | return -EOPNOTSUPP; | |
201 | ||
d9dfd233 | 202 | r = getcon_raw(&mycon); |
5eef597e MP |
203 | if (r < 0) |
204 | return -errno; | |
205 | ||
d9dfd233 | 206 | r = getfilecon_raw(exe, &fcon); |
5eef597e MP |
207 | if (r < 0) |
208 | return -errno; | |
209 | ||
210 | sclass = string_to_security_class("process"); | |
211 | r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label); | |
212 | if (r < 0) | |
213 | return -errno; | |
214 | #endif | |
215 | ||
216 | return r; | |
217 | } | |
218 | ||
219 | int mac_selinux_get_our_label(char **label) { | |
220 | int r = -EOPNOTSUPP; | |
221 | ||
222 | assert(label); | |
223 | ||
224 | #ifdef HAVE_SELINUX | |
225 | if (!mac_selinux_use()) | |
226 | return -EOPNOTSUPP; | |
227 | ||
d9dfd233 | 228 | r = getcon_raw(label); |
5eef597e MP |
229 | if (r < 0) |
230 | return -errno; | |
231 | #endif | |
232 | ||
233 | return r; | |
234 | } | |
235 | ||
f47781d8 | 236 | int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label) { |
5eef597e MP |
237 | int r = -EOPNOTSUPP; |
238 | ||
239 | #ifdef HAVE_SELINUX | |
240 | _cleanup_security_context_free_ security_context_t mycon = NULL, peercon = NULL, fcon = NULL; | |
241 | _cleanup_context_free_ context_t pcon = NULL, bcon = NULL; | |
242 | security_class_t sclass; | |
243 | const char *range = NULL; | |
244 | ||
245 | assert(socket_fd >= 0); | |
246 | assert(exe); | |
247 | assert(label); | |
248 | ||
249 | if (!mac_selinux_use()) | |
250 | return -EOPNOTSUPP; | |
251 | ||
d9dfd233 | 252 | r = getcon_raw(&mycon); |
5eef597e MP |
253 | if (r < 0) |
254 | return -errno; | |
255 | ||
256 | r = getpeercon(socket_fd, &peercon); | |
257 | if (r < 0) | |
258 | return -errno; | |
259 | ||
f47781d8 | 260 | if (!exec_label) { |
5eef597e MP |
261 | /* If there is no context set for next exec let's use context |
262 | of target executable */ | |
d9dfd233 | 263 | r = getfilecon_raw(exe, &fcon); |
5eef597e MP |
264 | if (r < 0) |
265 | return -errno; | |
266 | } | |
267 | ||
268 | bcon = context_new(mycon); | |
269 | if (!bcon) | |
270 | return -ENOMEM; | |
271 | ||
272 | pcon = context_new(peercon); | |
273 | if (!pcon) | |
274 | return -ENOMEM; | |
275 | ||
276 | range = context_range_get(pcon); | |
277 | if (!range) | |
278 | return -errno; | |
279 | ||
280 | r = context_range_set(bcon, range); | |
281 | if (r) | |
282 | return -errno; | |
283 | ||
284 | freecon(mycon); | |
285 | mycon = strdup(context_str(bcon)); | |
286 | if (!mycon) | |
287 | return -ENOMEM; | |
288 | ||
289 | sclass = string_to_security_class("process"); | |
290 | r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label); | |
291 | if (r < 0) | |
292 | return -errno; | |
293 | #endif | |
294 | ||
295 | return r; | |
296 | } | |
297 | ||
6300502b | 298 | char* mac_selinux_free(char *label) { |
5eef597e MP |
299 | |
300 | #ifdef HAVE_SELINUX | |
6300502b MP |
301 | if (!label) |
302 | return NULL; | |
303 | ||
5eef597e | 304 | if (!mac_selinux_use()) |
6300502b MP |
305 | return NULL; |
306 | ||
5eef597e MP |
307 | |
308 | freecon((security_context_t) label); | |
309 | #endif | |
6300502b MP |
310 | |
311 | return NULL; | |
5eef597e MP |
312 | } |
313 | ||
314 | int mac_selinux_create_file_prepare(const char *path, mode_t mode) { | |
315 | int r = 0; | |
316 | ||
317 | #ifdef HAVE_SELINUX | |
318 | _cleanup_security_context_free_ security_context_t filecon = NULL; | |
319 | ||
320 | assert(path); | |
321 | ||
322 | if (!label_hnd) | |
323 | return 0; | |
324 | ||
325 | if (path_is_absolute(path)) | |
326 | r = selabel_lookup_raw(label_hnd, &filecon, path, mode); | |
327 | else { | |
328 | _cleanup_free_ char *newpath; | |
329 | ||
330 | newpath = path_make_absolute_cwd(path); | |
331 | if (!newpath) | |
332 | return -ENOMEM; | |
333 | ||
334 | r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode); | |
335 | } | |
336 | ||
f47781d8 MP |
337 | /* No context specified by the policy? Proceed without setting it. */ |
338 | if (r < 0 && errno == ENOENT) | |
339 | return 0; | |
340 | ||
341 | if (r < 0) | |
5eef597e | 342 | r = -errno; |
f47781d8 | 343 | else { |
5eef597e MP |
344 | r = setfscreatecon(filecon); |
345 | if (r < 0) { | |
346 | log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path); | |
347 | r = -errno; | |
348 | } | |
349 | } | |
350 | ||
351 | if (r < 0 && security_getenforce() == 0) | |
352 | r = 0; | |
353 | #endif | |
354 | ||
355 | return r; | |
356 | } | |
357 | ||
358 | void mac_selinux_create_file_clear(void) { | |
359 | ||
360 | #ifdef HAVE_SELINUX | |
361 | PROTECT_ERRNO; | |
362 | ||
363 | if (!mac_selinux_use()) | |
364 | return; | |
365 | ||
366 | setfscreatecon(NULL); | |
367 | #endif | |
60f067b4 JS |
368 | } |
369 | ||
5eef597e MP |
370 | int mac_selinux_create_socket_prepare(const char *label) { |
371 | ||
372 | #ifdef HAVE_SELINUX | |
373 | if (!mac_selinux_use()) | |
374 | return 0; | |
375 | ||
376 | assert(label); | |
377 | ||
378 | if (setsockcreatecon((security_context_t) label) < 0) { | |
379 | log_enforcing("Failed to set SELinux security context %s for sockets: %m", label); | |
380 | ||
381 | if (security_getenforce() == 1) | |
382 | return -errno; | |
383 | } | |
384 | #endif | |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
389 | void mac_selinux_create_socket_clear(void) { | |
390 | ||
391 | #ifdef HAVE_SELINUX | |
392 | PROTECT_ERRNO; | |
393 | ||
394 | if (!mac_selinux_use()) | |
395 | return; | |
396 | ||
397 | setsockcreatecon(NULL); | |
398 | #endif | |
60f067b4 JS |
399 | } |
400 | ||
5eef597e MP |
401 | int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { |
402 | ||
403 | /* Binds a socket and label its file system object according to the SELinux policy */ | |
404 | ||
405 | #ifdef HAVE_SELINUX | |
406 | _cleanup_security_context_free_ security_context_t fcon = NULL; | |
407 | const struct sockaddr_un *un; | |
408 | char *path; | |
409 | int r; | |
410 | ||
411 | assert(fd >= 0); | |
412 | assert(addr); | |
413 | assert(addrlen >= sizeof(sa_family_t)); | |
414 | ||
415 | if (!label_hnd) | |
416 | goto skipped; | |
417 | ||
418 | /* Filter out non-local sockets */ | |
419 | if (addr->sa_family != AF_UNIX) | |
420 | goto skipped; | |
421 | ||
422 | /* Filter out anonymous sockets */ | |
423 | if (addrlen < sizeof(sa_family_t) + 1) | |
424 | goto skipped; | |
425 | ||
426 | /* Filter out abstract namespace sockets */ | |
427 | un = (const struct sockaddr_un*) addr; | |
428 | if (un->sun_path[0] == 0) | |
429 | goto skipped; | |
430 | ||
431 | path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path)); | |
432 | ||
433 | if (path_is_absolute(path)) | |
434 | r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK); | |
435 | else { | |
436 | _cleanup_free_ char *newpath; | |
437 | ||
438 | newpath = path_make_absolute_cwd(path); | |
439 | if (!newpath) | |
440 | return -ENOMEM; | |
441 | ||
442 | r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK); | |
443 | } | |
444 | ||
445 | if (r == 0) | |
446 | r = setfscreatecon(fcon); | |
447 | ||
448 | if (r < 0 && errno != ENOENT) { | |
449 | log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path); | |
450 | ||
451 | if (security_getenforce() == 1) { | |
452 | r = -errno; | |
453 | goto finish; | |
454 | } | |
455 | } | |
456 | ||
457 | r = bind(fd, addr, addrlen); | |
458 | if (r < 0) | |
459 | r = -errno; | |
460 | ||
461 | finish: | |
462 | setfscreatecon(NULL); | |
463 | return r; | |
464 | ||
465 | skipped: | |
663996b3 | 466 | #endif |
5eef597e MP |
467 | return bind(fd, addr, addrlen) < 0 ? -errno : 0; |
468 | } |