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_cached = -1;
39 if (use_selinux_cached < 0)
40 use_selinux_cached = is_selinux_enabled() > 0;
42 return use_selinux_cached;
47 int label_init(void) {
51 usec_t before_timestamp, after_timestamp;
52 struct mallinfo before_mallinfo, after_mallinfo;
60 before_mallinfo = mallinfo();
61 before_timestamp = 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 timespan[FORMAT_TIMESPAN_MAX];
72 after_timestamp = now(CLOCK_MONOTONIC);
73 after_mallinfo = mallinfo();
75 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
77 log_info("Successfully loaded SELinux database in %s, size on heap is %iK.",
78 format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp),
86 int label_fix(const char *path, bool ignore_enoent) {
91 security_context_t fcon;
93 if (!use_selinux() || !label_hnd)
98 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
100 /* If there's no label to set, then exit without warning */
101 if (r < 0 && errno == ENOENT)
105 r = setfilecon(path, fcon);
108 /* If the FS doesn't support labels, then exit without warning */
109 if (r < 0 && errno == ENOTSUP)
115 /* Ignore ENOENT in some cases */
116 if (ignore_enoent && errno == ENOENT)
119 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
120 "Unable to fix label of %s: %m", path);
121 r = security_getenforce() == 1 ? -errno : 0;
128 void label_finish(void) {
131 if (use_selinux() && label_hnd)
132 selabel_close(label_hnd);
136 int label_get_create_label_from_exe(const char *exe, char **label) {
141 security_context_t mycon = NULL, fcon = NULL;
142 security_class_t sclass;
144 if (!use_selinux()) {
153 r = getfilecon(exe, &fcon);
157 sclass = string_to_security_class("process");
158 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
160 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
163 if (r < 0 && security_getenforce() == 1)
173 int label_fifofile_set(const char *path) {
177 security_context_t filecon = NULL;
179 if (!use_selinux() || !label_hnd)
182 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) {
183 if ((r = setfscreatecon(filecon)) < 0) {
184 log_error("Failed to set SELinux file context on %s: %m", path);
191 if (r < 0 && security_getenforce() == 0)
198 int label_symlinkfile_set(const char *path) {
202 security_context_t filecon = NULL;
204 if (!use_selinux() || !label_hnd)
207 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFLNK)) == 0) {
208 if ((r = setfscreatecon(filecon)) < 0) {
209 log_error("Failed to set SELinux file context on %s: %m", path);
216 if (r < 0 && security_getenforce() == 0)
223 int label_socket_set(const char *label) {
229 if (setsockcreatecon((security_context_t) label) < 0) {
230 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
231 "Failed to set SELinux context (%s) on socket: %m", label);
233 if (security_getenforce() == 1)
241 void label_file_clear(void) {
247 setfscreatecon(NULL);
251 void label_socket_clear(void) {
257 setsockcreatecon(NULL);
261 void label_free(const char *label) {
267 freecon((security_context_t) label);
275 /* Creates a directory and labels it according to the SELinux policy */
279 security_context_t fcon = NULL;
281 if (use_selinux() && label_hnd) {
283 if (path_is_absolute(path))
284 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
286 char *newpath = NULL;
288 if (!(newpath = path_make_absolute_cwd(path)))
291 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
296 r = setfscreatecon(fcon);
298 if (r < 0 && errno != ENOENT) {
299 log_error("Failed to set security context %s for %s: %m", fcon, path);
302 if (security_getenforce() == 1)
307 if ((r = mkdir(path, mode)) < 0)
311 if (use_selinux() && label_hnd) {
312 setfscreatecon(NULL);
318 return mkdir(path, mode);