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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
27 #include <sys/types.h>
34 #include "path-util.h"
37 #include "selinux-util.h"
38 #include <selinux/selinux.h>
39 #include <selinux/label.h>
41 static struct selabel_handle *label_hnd = NULL;
45 int label_init(const char *prefix) {
49 usec_t before_timestamp, after_timestamp;
50 struct mallinfo before_mallinfo, after_mallinfo;
58 before_mallinfo = mallinfo();
59 before_timestamp = now(CLOCK_MONOTONIC);
62 struct selinux_opt options[] = {
63 { .type = SELABEL_OPT_SUBSET, .value = prefix },
66 label_hnd = selabel_open(SELABEL_CTX_FILE, options, ELEMENTSOF(options));
68 label_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
71 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
72 "Failed to initialize SELinux context: %m");
73 r = security_getenforce() == 1 ? -errno : 0;
75 char timespan[FORMAT_TIMESPAN_MAX];
78 after_timestamp = now(CLOCK_MONOTONIC);
79 after_mallinfo = mallinfo();
81 l = after_mallinfo.uordblks > before_mallinfo.uordblks ? after_mallinfo.uordblks - before_mallinfo.uordblks : 0;
83 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
84 format_timespan(timespan, sizeof(timespan), after_timestamp - before_timestamp, 0),
92 int label_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
97 security_context_t fcon;
99 if (!use_selinux() || !label_hnd)
102 r = lstat(path, &st);
104 r = selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode);
106 /* If there's no label to set, then exit without warning */
107 if (r < 0 && errno == ENOENT)
111 r = lsetfilecon(path, fcon);
114 /* If the FS doesn't support labels, then exit without warning */
115 if (r < 0 && errno == ENOTSUP)
121 /* Ignore ENOENT in some cases */
122 if (ignore_enoent && errno == ENOENT)
125 if (ignore_erofs && errno == EROFS)
128 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
129 "Unable to fix label of %s: %m", path);
130 r = security_getenforce() == 1 ? -errno : 0;
137 void label_finish(void) {
140 if (use_selinux() && label_hnd)
141 selabel_close(label_hnd);
145 int label_get_create_label_from_exe(const char *exe, char **label) {
150 security_context_t mycon = NULL, fcon = NULL;
151 security_class_t sclass;
153 if (!use_selinux()) {
162 r = getfilecon(exe, &fcon);
166 sclass = string_to_security_class("process");
167 r = security_compute_create(mycon, fcon, sclass, (security_context_t *) label);
169 log_debug("SELinux Socket context for %s will be set to %s", exe, *label);
172 if (r < 0 && security_getenforce() == 1)
182 int label_context_set(const char *path, mode_t mode) {
186 security_context_t filecon = NULL;
188 if (!use_selinux() || !label_hnd)
191 r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
192 if (r < 0 && errno != ENOENT)
195 r = setfscreatecon(filecon);
197 log_error("Failed to set SELinux file context on %s: %m", path);
204 if (r < 0 && security_getenforce() == 0)
211 int label_socket_set(const char *label) {
217 if (setsockcreatecon((security_context_t) label) < 0) {
218 log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG,
219 "Failed to set SELinux context (%s) on socket: %m", label);
221 if (security_getenforce() == 1)
229 void label_context_clear(void) {
235 setfscreatecon(NULL);
239 void label_socket_clear(void) {
245 setsockcreatecon(NULL);
249 void label_free(const char *label) {
255 freecon((security_context_t) label);
259 int label_mkdir(const char *path, mode_t mode) {
261 /* Creates a directory and labels it according to the SELinux policy */
264 security_context_t fcon = NULL;
266 if (!use_selinux() || !label_hnd)
269 if (path_is_absolute(path))
270 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFDIR);
274 newpath = path_make_absolute_cwd(path);
278 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFDIR);
283 r = setfscreatecon(fcon);
285 if (r < 0 && errno != ENOENT) {
286 log_error("Failed to set security context %s for %s: %m", fcon, path);
288 if (security_getenforce() == 1) {
294 r = mkdir(path, mode);
299 setfscreatecon(NULL);
306 return mkdir(path, mode) < 0 ? -errno : 0;
309 int label_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
311 /* Binds a socket and label its file system object according to the SELinux policy */
315 security_context_t fcon = NULL;
316 const struct sockaddr_un *un;
321 assert(addrlen >= sizeof(sa_family_t));
323 if (!use_selinux() || !label_hnd)
326 /* Filter out non-local sockets */
327 if (addr->sa_family != AF_UNIX)
330 /* Filter out anonymous sockets */
331 if (addrlen < sizeof(sa_family_t) + 1)
334 /* Filter out abstract namespace sockets */
335 un = (const struct sockaddr_un*) addr;
336 if (un->sun_path[0] == 0)
339 path = strndup(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
343 if (path_is_absolute(path))
344 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
348 newpath = path_make_absolute_cwd(path);
355 r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
360 r = setfscreatecon(fcon);
362 if (r < 0 && errno != ENOENT) {
363 log_error("Failed to set security context %s for %s: %m", fcon, path);
365 if (security_getenforce() == 1) {
371 r = bind(fd, addr, addrlen);
376 setfscreatecon(NULL);
384 return bind(fd, addr, addrlen) < 0 ? -errno : 0;
387 int label_apply(const char *path, const char *label) {
394 r = setfilecon(path, (char *)label);