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