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) {
57 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
59 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
60 "Failed to initialize SELinux context: %m");
61 r = security_getenforce() == 1 ? -errno : 0;
68 int label_fix(const char *path, bool ignore_enoent) {
73 security_context_t fcon;
75 if (!use_selinux() || !label_hnd)
80 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
82 /* If there's no label to set, then exit without warning */
83 if (r < 0 && errno == ENOENT)
87 r = setfilecon(path, fcon);
90 /* If the FS doesn't support labels, then exit without warning */
91 if (r < 0 && errno == ENOTSUP)
94 /* Ignore ENOENT in some cases */
95 if (r < 0 && ignore_enoent && errno == ENOENT)
101 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
102 "Unable to fix label of %s: %m", path);
103 r = security_getenforce() == 1 ? -errno : 0;
110 void label_finish(void) {
113 if (use_selinux() && label_hnd)
114 selabel_close(label_hnd);
118 int label_get_socket_label_from_exe(const char *exe, char **label) {
123 security_context_t mycon = NULL, fcon = NULL;
124 security_class_t sclass;
126 if (!use_selinux()) {
135 r = getfilecon(exe, &fcon);
139 sclass = string_to_security_class("process");
140 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
142 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
145 if (r < 0 && security_getenforce() == 1)
155 int label_fifofile_set(const char *path) {
159 security_context_t filecon = NULL;
161 if (!use_selinux() || !label_hnd)
164 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) {
165 if ((r = setfscreatecon(filecon)) < 0) {
166 log_error("Failed to set SELinux file context on %s: %m", path);
173 if (r < 0 && security_getenforce() == 0)
180 int label_symlinkfile_set(const char *path) {
184 security_context_t filecon = NULL;
186 if (!use_selinux() || !label_hnd)
189 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFLNK)) == 0) {
190 if ((r = setfscreatecon(filecon)) < 0) {
191 log_error("Failed to set SELinux file context on %s: %m", path);
198 if (r < 0 && security_getenforce() == 0)
205 int label_socket_set(const char *label) {
211 if (setsockcreatecon((security_context_t) label) < 0) {
212 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
213 "Failed to set SELinux context (%s) on socket: %m", label);
215 if (security_getenforce() == 1)
223 void label_file_clear(void) {
229 setfscreatecon(NULL);
233 void label_socket_clear(void) {
239 setsockcreatecon(NULL);
243 void label_free(const char *label) {
249 freecon((security_context_t) label);
257 /* Creates a directory and labels it according to the SELinux policy */
261 security_context_t fcon = NULL;
263 if (use_selinux() && label_hnd) {
265 if (path_is_absolute(path))
266 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
268 char *newpath = NULL;
270 if (!(newpath = path_make_absolute_cwd(path)))
273 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
278 r = setfscreatecon(fcon);
280 if (r < 0 && errno != ENOENT) {
281 log_error("Failed to set security context %s for %s: %m", fcon, path);
284 if (security_getenforce() == 1)
289 if ((r = mkdir(path, mode)) < 0)
293 if (use_selinux() && label_hnd) {
294 setfscreatecon(NULL);
300 return mkdir(path, mode);