chiark / gitweb /
5157b790d284de06a50306d1216c7c31999edb12
[elogind.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         usec_t n;
51
52         if (!use_selinux())
53                 return 0;
54
55         if (label_hnd)
56                 return 0;
57
58         n = now(CLOCK_MONOTONIC);
59         label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
60         if (!label_hnd) {
61                 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
62                          "Failed to initialize SELinux context: %m");
63                 r = security_getenforce() == 1 ? -errno : 0;
64         } else  {
65                 char buf[FORMAT_TIMESPAN_MAX];
66
67                 n = now(CLOCK_MONOTONIC) - n;
68                 log_info("Successfully loaded SELinux database in %s.",
69                          format_timespan(buf, sizeof(buf), n));
70         }
71
72 #endif
73
74         return r;
75 }
76
77 int label_fix(const char *path, bool ignore_enoent) {
78         int r = 0;
79
80 #ifdef HAVE_SELINUX
81         struct stat st;
82         security_context_t fcon;
83
84         if (!use_selinux() || !label_hnd)
85                 return 0;
86
87         r = lstat(path, &st);
88         if (r == 0) {
89                 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
90
91                 /* If there's no label to set, then exit without warning */
92                 if (r < 0 && errno == ENOENT)
93                         return 0;
94
95                 if (r == 0) {
96                         r = setfilecon(path, fcon);
97                         freecon(fcon);
98
99                         /* If the FS doesn't support labels, then exit without warning */
100                         if (r < 0 && errno == ENOTSUP)
101                                 return 0;
102                 }
103         }
104
105         if (r < 0) {
106                 /* Ignore ENOENT in some cases */
107                 if (ignore_enoent && errno == ENOENT)
108                         return 0;
109
110                 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
111                          "Unable to fix label of %s: %m", path);
112                 r = security_getenforce() == 1 ? -errno : 0;
113         }
114 #endif
115
116         return r;
117 }
118
119 void label_finish(void) {
120
121 #ifdef HAVE_SELINUX
122         if (use_selinux() && label_hnd)
123                 selabel_close(label_hnd);
124 #endif
125 }
126
127 int label_get_socket_label_from_exe(const char *exe, char **label) {
128
129         int r = 0;
130
131 #ifdef HAVE_SELINUX
132         security_context_t mycon = NULL, fcon = NULL;
133         security_class_t sclass;
134
135         if (!use_selinux()) {
136                 *label = NULL;
137                 return 0;
138         }
139
140         r = getcon(&mycon);
141         if (r < 0)
142                 goto fail;
143
144         r = getfilecon(exe, &fcon);
145         if (r < 0)
146                 goto fail;
147
148         sclass = string_to_security_class("process");
149         r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
150         if (r == 0)
151                 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
152
153 fail:
154         if (r < 0 && security_getenforce() == 1)
155                 r = -errno;
156
157         freecon(mycon);
158         freecon(fcon);
159 #endif
160
161         return r;
162 }
163
164 int label_fifofile_set(const char *path) {
165         int r = 0;
166
167 #ifdef HAVE_SELINUX
168         security_context_t filecon = NULL;
169
170         if (!use_selinux() || !label_hnd)
171                 return 0;
172
173         if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) {
174                 if ((r = setfscreatecon(filecon)) < 0) {
175                         log_error("Failed to set SELinux file context on %s: %m", path);
176                         r = -errno;
177                 }
178
179                 freecon(filecon);
180         }
181
182         if (r < 0 && security_getenforce() == 0)
183                 r = 0;
184 #endif
185
186         return r;
187 }
188
189 int label_symlinkfile_set(const char *path) {
190         int r = 0;
191
192 #ifdef HAVE_SELINUX
193         security_context_t filecon = NULL;
194
195         if (!use_selinux() || !label_hnd)
196                 return 0;
197
198         if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFLNK)) == 0) {
199                 if ((r = setfscreatecon(filecon)) < 0) {
200                         log_error("Failed to set SELinux file context on %s: %m", path);
201                         r = -errno;
202                 }
203
204                 freecon(filecon);
205         }
206
207         if (r < 0 && security_getenforce() == 0)
208                 r = 0;
209 #endif
210
211         return r;
212 }
213
214 int label_socket_set(const char *label) {
215
216 #ifdef HAVE_SELINUX
217         if (!use_selinux())
218                 return 0;
219
220         if (setsockcreatecon((security_context_t) label) < 0) {
221                 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
222                          "Failed to set SELinux context (%s) on socket: %m", label);
223
224                 if (security_getenforce() == 1)
225                         return -errno;
226         }
227 #endif
228
229         return 0;
230 }
231
232 void label_file_clear(void) {
233
234 #ifdef HAVE_SELINUX
235         if (!use_selinux())
236                 return;
237
238         setfscreatecon(NULL);
239 #endif
240 }
241
242 void label_socket_clear(void) {
243
244 #ifdef HAVE_SELINUX
245         if (!use_selinux())
246                 return;
247
248         setsockcreatecon(NULL);
249 #endif
250 }
251
252 void label_free(const char *label) {
253
254 #ifdef HAVE_SELINUX
255         if (!use_selinux())
256                 return;
257
258         freecon((security_context_t) label);
259 #endif
260 }
261
262 int label_mkdir(
263         const char *path,
264         mode_t mode) {
265
266         /* Creates a directory and labels it according to the SELinux policy */
267
268 #ifdef HAVE_SELINUX
269         int r;
270         security_context_t fcon = NULL;
271
272         if (use_selinux() && label_hnd) {
273
274                 if (path_is_absolute(path))
275                         r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
276                 else {
277                         char *newpath = NULL;
278
279                         if (!(newpath = path_make_absolute_cwd(path)))
280                                 return -ENOMEM;
281
282                         r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
283                         free(newpath);
284                 }
285
286                 if (r == 0)
287                         r = setfscreatecon(fcon);
288
289                 if (r < 0 && errno != ENOENT) {
290                         log_error("Failed to set security context %s for %s: %m", fcon, path);
291                         r = -errno;
292
293                         if (security_getenforce() == 1)
294                                 goto finish;
295                 }
296         }
297
298         if ((r = mkdir(path, mode)) < 0)
299                 r = -errno;
300
301 finish:
302         if (use_selinux() && label_hnd) {
303                 setfscreatecon(NULL);
304                 freecon(fcon);
305         }
306
307         return r;
308 #else
309         return mkdir(path, mode);
310 #endif
311 }