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) {
54 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
56 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
57 "Failed to initialize SELinux context: %m");
58 r = security_getenforce() == 1 ? -errno : 0;
65 int label_fix(const char *path) {
70 security_context_t fcon;
72 if (!use_selinux() || !label_hnd)
77 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
80 r = setfilecon(path, fcon);
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;
94 void label_finish(void) {
97 if (use_selinux() && label_hnd)
98 selabel_close(label_hnd);
102 int label_get_socket_label_from_exe(const char *exe, char **label) {
107 security_context_t mycon = NULL, fcon = NULL;
108 security_class_t sclass;
110 if (!use_selinux()) {
119 r = getfilecon(exe, &fcon);
123 sclass = string_to_security_class("process");
124 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
126 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
129 if (r < 0 && security_getenforce() == 1)
139 int label_fifofile_set(const char *path) {
143 security_context_t filecon = NULL;
145 if (!use_selinux() || !label_hnd)
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);
157 if (r < 0 && security_getenforce() == 0)
164 int label_socket_set(const char *label) {
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);
174 if (security_getenforce() == 1)
182 void label_file_clear(void) {
188 setfscreatecon(NULL);
192 void label_socket_clear(void) {
198 setsockcreatecon(NULL);
202 void label_free(const char *label) {
208 freecon((security_context_t) label);
216 /* Creates a directory and labels it according to the SELinux policy */
220 security_context_t fcon = NULL;
222 if (use_selinux() && label_hnd) {
225 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
227 char *cwd = NULL, *newpath = NULL;
229 cwd = get_current_dir_name();
231 if (cwd || asprintf(&newpath, "%s/%s", cwd, path) < 0) {
236 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
242 r = setfscreatecon(fcon);
244 if (r < 0 && errno != ENOENT) {
245 log_error("Failed to set security context %s for %s: %m", fcon, path);
248 if (security_getenforce() == 1)
253 if ((r = mkdir(path, mode)) < 0)
257 if (use_selinux() && label_hnd) {
258 setfscreatecon(NULL);
264 return mkdir(path, mode);