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/>.
30 #include <selinux/selinux.h>
31 #include <selinux/label.h>
33 static struct selabel_handle *label_hnd = NULL;
35 static inline bool use_selinux(void) {
36 static int use_selinux_ind = -1;
38 if (use_selinux_ind < 0)
39 use_selinux_ind = is_selinux_enabled() > 0;
41 return use_selinux_ind;
46 int label_init(void) {
58 n = now(CLOCK_MONOTONIC);
59 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
61 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
62 "Failed to initialize SELinux context: %m");
63 r = security_getenforce() == 1 ? -errno : 0;
65 char buf[FORMAT_TIMESPAN_MAX];
67 n = now(CLOCK_MONOTONIC) - n;
68 log_info("Successfully loaded SELinux database in %s.",
69 format_timespan(buf, sizeof(buf), n));
77 int label_fix(const char *path, bool ignore_enoent) {
82 security_context_t fcon;
84 if (!use_selinux() || !label_hnd)
89 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
91 /* If there's no label to set, then exit without warning */
92 if (r < 0 && errno == ENOENT)
96 r = setfilecon(path, fcon);
99 /* If the FS doesn't support labels, then exit without warning */
100 if (r < 0 && errno == ENOTSUP)
106 /* Ignore ENOENT in some cases */
107 if (ignore_enoent && errno == ENOENT)
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;
119 void label_finish(void) {
122 if (use_selinux() && label_hnd)
123 selabel_close(label_hnd);
127 int label_get_socket_label_from_exe(const char *exe, char **label) {
132 security_context_t mycon = NULL, fcon = NULL;
133 security_class_t sclass;
135 if (!use_selinux()) {
144 r = getfilecon(exe, &fcon);
148 sclass = string_to_security_class("process");
149 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
151 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
154 if (r < 0 && security_getenforce() == 1)
164 int label_fifofile_set(const char *path) {
168 security_context_t filecon = NULL;
170 if (!use_selinux() || !label_hnd)
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);
182 if (r < 0 && security_getenforce() == 0)
189 int label_symlinkfile_set(const char *path) {
193 security_context_t filecon = NULL;
195 if (!use_selinux() || !label_hnd)
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);
207 if (r < 0 && security_getenforce() == 0)
214 int label_socket_set(const char *label) {
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);
224 if (security_getenforce() == 1)
232 void label_file_clear(void) {
238 setfscreatecon(NULL);
242 void label_socket_clear(void) {
248 setsockcreatecon(NULL);
252 void label_free(const char *label) {
258 freecon((security_context_t) label);
266 /* Creates a directory and labels it according to the SELinux policy */
270 security_context_t fcon = NULL;
272 if (use_selinux() && label_hnd) {
274 if (path_is_absolute(path))
275 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
277 char *newpath = NULL;
279 if (!(newpath = path_make_absolute_cwd(path)))
282 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
287 r = setfscreatecon(fcon);
289 if (r < 0 && errno != ENOENT) {
290 log_error("Failed to set security context %s for %s: %m", fcon, path);
293 if (security_getenforce() == 1)
298 if ((r = mkdir(path, mode)) < 0)
302 if (use_selinux() && label_hnd) {
303 setfscreatecon(NULL);
309 return mkdir(path, mode);