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 int use_selinux_cached = -1;
38 static inline bool use_selinux(void) {
40 if (use_selinux_cached < 0)
41 use_selinux_cached = is_selinux_enabled() > 0;
43 return use_selinux_cached;
46 void label_retest_selinux(void) {
47 use_selinux_cached = -1;
52 int label_init(void) {
56 usec_t before_timestamp, after_timestamp;
57 struct mallinfo before_mallinfo, after_mallinfo;
65 before_mallinfo = mallinfo();
66 before_timestamp = now(CLOCK_MONOTONIC);
68 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
70 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
71 "Failed to initialize SELinux context: %m");
72 r = security_getenforce() == 1 ? -errno : 0;
74 char timespan[FORMAT_TIMESPAN_MAX];
77 after_timestamp = now(CLOCK_MONOTONIC);
78 after_mallinfo = mallinfo();
80 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
82 log_info("Successfully loaded SELinux database in %s, size on heap is %iK.",
83 format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp),
91 int label_fix(const char *path, bool ignore_enoent) {
96 security_context_t fcon;
98 if (!use_selinux() || !label_hnd)
101 r = lstat(path, &st);
103 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
105 /* If there's no label to set, then exit without warning */
106 if (r < 0 && errno == ENOENT)
110 r = setfilecon(path, fcon);
113 /* If the FS doesn't support labels, then exit without warning */
114 if (r < 0 && errno == ENOTSUP)
120 /* Ignore ENOENT in some cases */
121 if (ignore_enoent && errno == ENOENT)
124 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
125 "Unable to fix label of %s: %m", path);
126 r = security_getenforce() == 1 ? -errno : 0;
133 void label_finish(void) {
136 if (use_selinux() && label_hnd)
137 selabel_close(label_hnd);
141 int label_get_create_label_from_exe(const char *exe, char **label) {
146 security_context_t mycon = NULL, fcon = NULL;
147 security_class_t sclass;
149 if (!use_selinux()) {
158 r = getfilecon(exe, &fcon);
162 sclass = string_to_security_class("process");
163 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
165 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
168 if (r < 0 && security_getenforce() == 1)
178 int label_fifofile_set(const char *path) {
182 security_context_t filecon = NULL;
184 if (!use_selinux() || !label_hnd)
187 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFIFO)) == 0) {
188 if ((r = setfscreatecon(filecon)) < 0) {
189 log_error("Failed to set SELinux file context on %s: %m", path);
196 if (r < 0 && security_getenforce() == 0)
203 int label_symlinkfile_set(const char *path) {
207 security_context_t filecon = NULL;
209 if (!use_selinux() || !label_hnd)
212 if ((r = selabel_lookup_raw(label_hnd, &filecon, path, S_IFLNK)) == 0) {
213 if ((r = setfscreatecon(filecon)) < 0) {
214 log_error("Failed to set SELinux file context on %s: %m", path);
221 if (r < 0 && security_getenforce() == 0)
228 int label_socket_set(const char *label) {
234 if (setsockcreatecon((security_context_t) label) < 0) {
235 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
236 "Failed to set SELinux context (%s) on socket: %m", label);
238 if (security_getenforce() == 1)
246 void label_file_clear(void) {
252 setfscreatecon(NULL);
256 void label_socket_clear(void) {
262 setsockcreatecon(NULL);
266 void label_free(const char *label) {
272 freecon((security_context_t) label);
280 /* Creates a directory and labels it according to the SELinux policy */
284 security_context_t fcon = NULL;
286 if (use_selinux() && label_hnd) {
288 if (path_is_absolute(path))
289 r = selabel_lookup_raw(label_hnd, &fcon, path, mode);
291 char *newpath = NULL;
293 if (!(newpath = path_make_absolute_cwd(path)))
296 r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode);
301 r = setfscreatecon(fcon);
303 if (r < 0 && errno != ENOENT) {
304 log_error("Failed to set security context %s for %s: %m", fcon, path);
307 if (security_getenforce() == 1)
312 if ((r = mkdir(path, mode)) < 0)
316 if (use_selinux() && label_hnd) {
317 setfscreatecon(NULL);
323 return mkdir(path, mode);