]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
e51bc1a2 LP |
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> | |
455a946f | 25 | #include <malloc.h> |
f13e30d2 LP |
26 | #include <sys/socket.h> |
27 | #include <sys/un.h> | |
e51bc1a2 LP |
28 | |
29 | #include "label.h" | |
30 | #include "util.h" | |
31 | ||
32 | #ifdef HAVE_SELINUX | |
33 | #include <selinux/selinux.h> | |
34 | #include <selinux/label.h> | |
35 | ||
36 | static struct selabel_handle *label_hnd = NULL; | |
37 | ||
4d4c7486 LP |
38 | static int use_selinux_cached = -1; |
39 | ||
e51bc1a2 | 40 | static inline bool use_selinux(void) { |
e51bc1a2 | 41 | |
4ef31082 LP |
42 | if (use_selinux_cached < 0) |
43 | use_selinux_cached = is_selinux_enabled() > 0; | |
e51bc1a2 | 44 | |
4ef31082 | 45 | return use_selinux_cached; |
e51bc1a2 LP |
46 | } |
47 | ||
4d4c7486 LP |
48 | void label_retest_selinux(void) { |
49 | use_selinux_cached = -1; | |
50 | } | |
51 | ||
e51bc1a2 LP |
52 | #endif |
53 | ||
54 | int label_init(void) { | |
55 | int r = 0; | |
56 | ||
57 | #ifdef HAVE_SELINUX | |
189583d7 LP |
58 | usec_t before_timestamp, after_timestamp; |
59 | struct mallinfo before_mallinfo, after_mallinfo; | |
e51bc1a2 LP |
60 | |
61 | if (!use_selinux()) | |
62 | return 0; | |
63 | ||
c4dcdb9f LP |
64 | if (label_hnd) |
65 | return 0; | |
66 | ||
189583d7 LP |
67 | before_mallinfo = mallinfo(); |
68 | before_timestamp = now(CLOCK_MONOTONIC); | |
455a946f | 69 | |
e51bc1a2 LP |
70 | label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0); |
71 | if (!label_hnd) { | |
72 | log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, | |
73 | "Failed to initialize SELinux context: %m"); | |
049f8642 | 74 | r = security_getenforce() == 1 ? -errno : 0; |
871e5809 | 75 | } else { |
189583d7 | 76 | char timespan[FORMAT_TIMESPAN_MAX]; |
455a946f | 77 | int l; |
871e5809 | 78 | |
189583d7 LP |
79 | after_timestamp = now(CLOCK_MONOTONIC); |
80 | after_mallinfo = mallinfo(); | |
871e5809 | 81 | |
189583d7 | 82 | l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0; |
455a946f LP |
83 | |
84 | log_info("Successfully loaded SELinux database in %s, size on heap is %iK.", | |
189583d7 LP |
85 | format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp), |
86 | (l+1023)/1024); | |
455a946f | 87 | } |
e51bc1a2 LP |
88 | #endif |
89 | ||
90 | return r; | |
91 | } | |
92 | ||
c904f64d | 93 | int label_fix(const char *path, bool ignore_enoent) { |
e51bc1a2 LP |
94 | int r = 0; |
95 | ||
96 | #ifdef HAVE_SELINUX | |
97 | struct stat st; | |
98 | security_context_t fcon; | |
99 | ||
100 | if (!use_selinux() || !label_hnd) | |
101 | return 0; | |
102 | ||
103 | r = lstat(path, &st); | |
104 | if (r == 0) { | |
105 | r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode); | |
106 | ||
5a33f657 LP |
107 | /* If there's no label to set, then exit without warning */ |
108 | if (r < 0 && errno == ENOENT) | |
109 | return 0; | |
110 | ||
e51bc1a2 LP |
111 | if (r == 0) { |
112 | r = setfilecon(path, fcon); | |
113 | freecon(fcon); | |
d2dfce17 LP |
114 | |
115 | /* If the FS doesn't support labels, then exit without warning */ | |
116 | if (r < 0 && errno == ENOTSUP) | |
117 | return 0; | |
e51bc1a2 LP |
118 | } |
119 | } | |
5a33f657 | 120 | |
e51bc1a2 | 121 | if (r < 0) { |
b4bd5144 LP |
122 | /* Ignore ENOENT in some cases */ |
123 | if (ignore_enoent && errno == ENOENT) | |
124 | return 0; | |
125 | ||
e51bc1a2 LP |
126 | log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, |
127 | "Unable to fix label of %s: %m", path); | |
049f8642 | 128 | r = security_getenforce() == 1 ? -errno : 0; |
e51bc1a2 LP |
129 | } |
130 | #endif | |
131 | ||
132 | return r; | |
133 | } | |
134 | ||
135 | void label_finish(void) { | |
136 | ||
137 | #ifdef HAVE_SELINUX | |
138 | if (use_selinux() && label_hnd) | |
139 | selabel_close(label_hnd); | |
140 | #endif | |
141 | } | |
142 | ||
189583d7 | 143 | int label_get_create_label_from_exe(const char *exe, char **label) { |
e51bc1a2 LP |
144 | |
145 | int r = 0; | |
146 | ||
147 | #ifdef HAVE_SELINUX | |
148 | security_context_t mycon = NULL, fcon = NULL; | |
149 | security_class_t sclass; | |
150 | ||
151 | if (!use_selinux()) { | |
152 | *label = NULL; | |
153 | return 0; | |
154 | } | |
155 | ||
156 | r = getcon(&mycon); | |
157 | if (r < 0) | |
158 | goto fail; | |
159 | ||
160 | r = getfilecon(exe, &fcon); | |
161 | if (r < 0) | |
162 | goto fail; | |
163 | ||
164 | sclass = string_to_security_class("process"); | |
165 | r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label); | |
166 | if (r == 0) | |
167 | log_debug("SELinux Socket context for %s will be set to %s", exe, *label); | |
168 | ||
169 | fail: | |
170 | if (r < 0 && security_getenforce() == 1) | |
171 | r = -errno; | |
172 | ||
173 | freecon(mycon); | |
174 | freecon(fcon); | |
175 | #endif | |
176 | ||
177 | return r; | |
178 | } | |
179 | ||
049f8642 | 180 | int label_fifofile_set(const char *path) { |
e51bc1a2 LP |
181 | int r = 0; |
182 | ||
183 | #ifdef HAVE_SELINUX | |
184 | security_context_t filecon = NULL; | |
185 | ||
049f8642 | 186 | if (!use_selinux() || !label_hnd) |
e51bc1a2 LP |
187 | return 0; |
188 | ||
049f8642 | 189 | if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) { |
e51bc1a2 | 190 | if ((r = setfscreatecon(filecon)) < 0) { |
049f8642 | 191 | log_error("Failed to set SELinux file context on %s: %m", path); |
e51bc1a2 LP |
192 | r = -errno; |
193 | } | |
194 | ||
195 | freecon(filecon); | |
196 | } | |
197 | ||
198 | if (r < 0 && security_getenforce() == 0) | |
199 | r = 0; | |
200 | #endif | |
201 | ||
202 | return r; | |
203 | } | |
204 | ||
5c0532d1 LP |
205 | int label_symlinkfile_set(const char *path) { |
206 | int r = 0; | |
207 | ||
208 | #ifdef HAVE_SELINUX | |
209 | security_context_t filecon = NULL; | |
210 | ||
211 | if (!use_selinux() || !label_hnd) | |
212 | return 0; | |
213 | ||
214 | if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFLNK)) == 0) { | |
215 | if ((r = setfscreatecon(filecon)) < 0) { | |
216 | log_error("Failed to set SELinux file context on %s: %m", path); | |
217 | r = -errno; | |
218 | } | |
219 | ||
220 | freecon(filecon); | |
221 | } | |
222 | ||
223 | if (r < 0 && security_getenforce() == 0) | |
224 | r = 0; | |
225 | #endif | |
226 | ||
227 | return r; | |
228 | } | |
229 | ||
e51bc1a2 LP |
230 | int label_socket_set(const char *label) { |
231 | ||
232 | #ifdef HAVE_SELINUX | |
233 | if (!use_selinux()) | |
234 | return 0; | |
235 | ||
236 | if (setsockcreatecon((security_context_t) label) < 0) { | |
237 | log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, | |
238 | "Failed to set SELinux context (%s) on socket: %m", label); | |
239 | ||
240 | if (security_getenforce() == 1) | |
241 | return -errno; | |
242 | } | |
243 | #endif | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
248 | void label_file_clear(void) { | |
249 | ||
250 | #ifdef HAVE_SELINUX | |
251 | if (!use_selinux()) | |
252 | return; | |
253 | ||
254 | setfscreatecon(NULL); | |
255 | #endif | |
256 | } | |
257 | ||
258 | void label_socket_clear(void) { | |
259 | ||
260 | #ifdef HAVE_SELINUX | |
261 | if (!use_selinux()) | |
262 | return; | |
263 | ||
264 | setsockcreatecon(NULL); | |
265 | #endif | |
266 | } | |
267 | ||
268 | void label_free(const char *label) { | |
269 | ||
270 | #ifdef HAVE_SELINUX | |
271 | if (!use_selinux()) | |
272 | return; | |
273 | ||
274 | freecon((security_context_t) label); | |
275 | #endif | |
276 | } | |
277 | ||
f13e30d2 | 278 | int label_mkdir(const char *path, mode_t mode) { |
e51bc1a2 LP |
279 | |
280 | /* Creates a directory and labels it according to the SELinux policy */ | |
281 | ||
282 | #ifdef HAVE_SELINUX | |
283 | int r; | |
284 | security_context_t fcon = NULL; | |
285 | ||
f13e30d2 LP |
286 | if (!use_selinux() || label_hnd) |
287 | goto skipped; | |
e51bc1a2 | 288 | |
f13e30d2 LP |
289 | if (path_is_absolute(path)) |
290 | r = selabel_lookup_raw(label_hnd, &fcon, path, mode); | |
291 | else { | |
292 | char *newpath; | |
e51bc1a2 | 293 | |
f13e30d2 LP |
294 | newpath = path_make_absolute_cwd(path); |
295 | if (!newpath) | |
296 | return -ENOMEM; | |
e51bc1a2 | 297 | |
f13e30d2 LP |
298 | r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode); |
299 | free(newpath); | |
300 | } | |
301 | ||
302 | if (r == 0) | |
303 | r = setfscreatecon(fcon); | |
e51bc1a2 | 304 | |
f13e30d2 LP |
305 | if (r < 0 && errno != ENOENT) { |
306 | log_error("Failed to set security context %s for %s: %m", fcon, path); | |
e51bc1a2 | 307 | |
f13e30d2 | 308 | if (security_getenforce() == 1) { |
e51bc1a2 | 309 | r = -errno; |
f13e30d2 | 310 | goto finish; |
e51bc1a2 LP |
311 | } |
312 | } | |
313 | ||
f13e30d2 LP |
314 | r = mkdir(path, mode); |
315 | if (r < 0) | |
e51bc1a2 LP |
316 | r = -errno; |
317 | ||
318 | finish: | |
f13e30d2 LP |
319 | setfscreatecon(NULL); |
320 | freecon(fcon); | |
321 | ||
322 | return r; | |
323 | ||
324 | skipped: | |
325 | #endif | |
326 | return mkdir(path, mode) < 0 ? -errno : 0; | |
327 | } | |
328 | ||
329 | int label_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { | |
330 | ||
331 | /* Binds a socket and label its file system object according to the SELinux policy */ | |
332 | ||
333 | #ifdef HAVE_SELINUX | |
334 | int r; | |
335 | security_context_t fcon = NULL; | |
336 | const struct sockaddr_un *un; | |
337 | char *path = NULL; | |
338 | ||
339 | assert(fd >= 0); | |
340 | assert(addr); | |
341 | assert(addrlen >= sizeof(sa_family_t)); | |
342 | ||
343 | if (!use_selinux() || !label_hnd) | |
344 | goto skipped; | |
345 | ||
346 | /* Filter out non-local sockets */ | |
347 | if (addr->sa_family != AF_UNIX) | |
348 | goto skipped; | |
349 | ||
350 | /* Filter out anonymous sockets */ | |
351 | if (addrlen < sizeof(sa_family_t) + 1) | |
352 | goto skipped; | |
353 | ||
354 | /* Filter out abstract namespace sockets */ | |
355 | un = (const struct sockaddr_un*) addr; | |
356 | if (un->sun_path[0] == 0) | |
357 | goto skipped; | |
358 | ||
359 | path = strndup(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path)); | |
360 | if (!path) | |
361 | return -ENOMEM; | |
362 | ||
363 | if (path_is_absolute(path)) | |
364 | r = selabel_lookup_raw(label_hnd, &fcon, path, 0777); | |
365 | else { | |
366 | char *newpath; | |
367 | ||
368 | newpath = path_make_absolute_cwd(path); | |
369 | ||
370 | if (!newpath) { | |
371 | free(path); | |
372 | return -ENOMEM; | |
373 | } | |
374 | ||
375 | r = selabel_lookup_raw(label_hnd, &fcon, newpath, 0777); | |
376 | free(newpath); | |
e51bc1a2 LP |
377 | } |
378 | ||
f13e30d2 LP |
379 | if (r == 0) |
380 | r = setfscreatecon(fcon); | |
381 | ||
382 | if (r < 0 && errno != ENOENT) { | |
383 | log_error("Failed to set security context %s for %s: %m", fcon, path); | |
384 | ||
385 | if (security_getenforce() == 1) { | |
386 | r = -errno; | |
387 | goto finish; | |
388 | } | |
389 | } | |
390 | ||
391 | r = bind(fd, addr, addrlen); | |
392 | if (r < 0) | |
393 | r = -errno; | |
394 | ||
395 | finish: | |
396 | setfscreatecon(NULL); | |
397 | freecon(fcon); | |
398 | free(path); | |
399 | ||
e51bc1a2 | 400 | return r; |
f13e30d2 LP |
401 | |
402 | skipped: | |
e51bc1a2 | 403 | #endif |
f13e30d2 | 404 | return bind(fd, addr, addrlen) < 0 ? -errno : 0; |
e51bc1a2 | 405 | } |