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