1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
31 #include <selinux/selinux.h>
32 #include <selinux/label.h>
34 static struct selabel_handle *label_hnd = NULL;
36 static inline bool use_selinux(void) {
37 static int use_selinux_ind = -1;
39 if (use_selinux_ind < 0)
40 use_selinux_ind = is_selinux_enabled() > 0;
42 return use_selinux_ind;
47 int label_init(void) {
52 struct mallinfo before;
61 n = now(CLOCK_MONOTONIC);
63 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
65 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
66 "Failed to initialize SELinux context: %m");
67 r = security_getenforce() == 1 ? -errno : 0;
69 char buf[FORMAT_TIMESPAN_MAX];
70 struct mallinfo after;
73 n = now(CLOCK_MONOTONIC) - n;
76 l = after.uordblks > before.uordblks ? after.uordblks - before.uordblks : 0;
78 log_info("Successfully loaded SELinux database in %s, size on heap is %iK.",
79 format_timespan(buf, sizeof(buf), n),
87 int label_fix(const char *path, bool ignore_enoent) {
92 security_context_t fcon;
94 if (!use_selinux() || !label_hnd)
99 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
101 /* If there's no label to set, then exit without warning */
102 if (r < 0 && errno == ENOENT)
106 r = setfilecon(path, fcon);
109 /* If the FS doesn't support labels, then exit without warning */
110 if (r < 0 && errno == ENOTSUP)
116 /* Ignore ENOENT in some cases */
117 if (ignore_enoent && errno == ENOENT)
120 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
121 "Unable to fix label of %s: %m", path);
122 r = security_getenforce() == 1 ? -errno : 0;
129 void label_finish(void) {
132 if (use_selinux() && label_hnd)
133 selabel_close(label_hnd);
137 int label_get_socket_label_from_exe(const char *exe, char **label) {
142 security_context_t mycon = NULL, fcon = NULL;
143 security_class_t sclass;
145 if (!use_selinux()) {
154 r = getfilecon(exe, &fcon);
158 sclass = string_to_security_class("process");
159 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
161 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
164 if (r < 0 && security_getenforce() == 1)
174 int label_fifofile_set(const char *path) {
178 security_context_t filecon = NULL;
180 if (!use_selinux() || !label_hnd)
183 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) {
184 if ((r = setfscreatecon(filecon)) < 0) {
185 log_error("Failed to set SELinux file context on %s: %m", path);
192 if (r < 0 && security_getenforce() == 0)
199 int label_symlinkfile_set(const char *path) {
203 security_context_t filecon = NULL;
205 if (!use_selinux() || !label_hnd)
208 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFLNK)) == 0) {
209 if ((r = setfscreatecon(filecon)) < 0) {
210 log_error("Failed to set SELinux file context on %s: %m", path);
217 if (r < 0 && security_getenforce() == 0)
224 int label_socket_set(const char *label) {
230 if (setsockcreatecon((security_context_t) label) < 0) {
231 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
232 "Failed to set SELinux context (%s) on socket: %m", label);
234 if (security_getenforce() == 1)
242 void label_file_clear(void) {
248 setfscreatecon(NULL);
252 void label_socket_clear(void) {
258 setsockcreatecon(NULL);
262 void label_free(const char *label) {
268 freecon((security_context_t) label);
276 /* Creates a directory and labels it according to the SELinux policy */
280 security_context_t fcon = NULL;
282 if (use_selinux() && label_hnd) {
284 if (path_is_absolute(path))
285 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
287 char *newpath = NULL;
289 if (!(newpath = path_make_absolute_cwd(path)))
292 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
297 r = setfscreatecon(fcon);
299 if (r < 0 && errno != ENOENT) {
300 log_error("Failed to set security context %s for %s: %m", fcon, path);
303 if (security_getenforce() == 1)
308 if ((r = mkdir(path, mode)) < 0)
312 if (use_selinux() && label_hnd) {
313 setfscreatecon(NULL);
319 return mkdir(path, mode);