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