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;
44 static int label_get_file_label_from_path(
48 security_context_t *fcon) {
50 security_context_t dir_con = NULL;
51 security_class_t sclass;
54 r = getfilecon(path, &dir_con);
59 if ((sclass = string_to_security_class(class)) != 0)
60 r = security_compute_create((security_context_t) label, dir_con, sclass, fcon);
71 int label_init(void) {
79 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
81 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
82 "Failed to initialize SELinux context: %m");
83 r = (security_getenforce() == 1) ? -errno : 0;
90 int label_fix(const char *path) {
95 security_context_t fcon;
97 if (!use_selinux() || !label_hnd)
100 r = lstat(path, &st);
102 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
105 r = setfilecon(path, fcon);
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 *label, const char *path) {
168 security_context_t filecon = NULL;
170 if (!use_selinux() || !label)
173 if (((r = label_get_file_label_from_path(label, path, "fifo_file", &filecon)) == 0)) {
174 if ((r = setfscreatecon(filecon)) < 0) {
175 log_error("Failed to set SELinux file context (%s) on %s: %m", label, path);
182 if (r < 0 && security_getenforce() == 0)
189 int label_socket_set(const char *label) {
195 if (setsockcreatecon((security_context_t) label) < 0) {
196 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
197 "Failed to set SELinux context (%s) on socket: %m", label);
199 if (security_getenforce() == 1)
207 void label_file_clear(void) {
213 setfscreatecon(NULL);
217 void label_socket_clear(void) {
223 setsockcreatecon(NULL);
227 void label_free(const char *label) {
233 freecon((security_context_t) label);
241 /* Creates a directory and labels it according to the SELinux policy */
245 security_context_t fcon = NULL;
247 if (use_selinux() && label_hnd) {
250 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
252 char *cwd = NULL, *newpath = NULL;
254 cwd = get_current_dir_name();
256 if (cwd || asprintf(&newpath, "%s/%s", cwd, path) < 0) {
261 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
267 r = setfscreatecon(fcon);
269 if (r < 0 && errno != ENOENT) {
270 log_error("Failed to set security context %s for %s: %m", fcon, path);
273 if (security_getenforce() == 1)
278 if ((r = mkdir(path, mode)) < 0)
282 if (use_selinux() && label_hnd) {
283 setfscreatecon(NULL);
289 return mkdir(path, mode);