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/>.
28 #include <selinux/selinux.h>
29 #include <selinux/label.h>
30 #include <selinux/context.h>
34 #include "path-util.h"
35 #include "selinux-util.h"
38 DEFINE_TRIVIAL_CLEANUP_FUNC(security_context_t
, freecon
);
39 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t
, context_free
);
41 #define _cleanup_security_context_free_ _cleanup_(freeconp)
42 #define _cleanup_context_free_ _cleanup_(context_freep)
44 static int cached_use
= -1;
45 static struct selabel_handle
*label_hnd
= NULL
;
47 #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
50 bool mac_selinux_use(void) {
53 cached_use
= is_selinux_enabled() > 0;
61 void mac_selinux_retest(void) {
67 int mac_selinux_init(const char *prefix
) {
71 usec_t before_timestamp
, after_timestamp
;
72 struct mallinfo before_mallinfo
, after_mallinfo
;
74 if (!mac_selinux_use())
80 before_mallinfo
= mallinfo();
81 before_timestamp
= now(CLOCK_MONOTONIC
);
84 struct selinux_opt options
[] = {
85 { .type
= SELABEL_OPT_SUBSET
, .value
= prefix
},
88 label_hnd
= selabel_open(SELABEL_CTX_FILE
, options
, ELEMENTSOF(options
));
90 label_hnd
= selabel_open(SELABEL_CTX_FILE
, NULL
, 0);
93 log_enforcing("Failed to initialize SELinux context: %m");
94 r
= security_getenforce() == 1 ? -errno
: 0;
96 char timespan
[FORMAT_TIMESPAN_MAX
];
99 after_timestamp
= now(CLOCK_MONOTONIC
);
100 after_mallinfo
= mallinfo();
102 l
= after_mallinfo
.uordblks
> before_mallinfo
.uordblks
? after_mallinfo
.uordblks
- before_mallinfo
.uordblks
: 0;
104 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
105 format_timespan(timespan
, sizeof(timespan
), after_timestamp
- before_timestamp
, 0),
113 void mac_selinux_finish(void) {
119 selabel_close(label_hnd
);
123 int mac_selinux_fix(const char *path
, bool ignore_enoent
, bool ignore_erofs
) {
131 /* if mac_selinux_init() wasn't called before we are a NOOP */
135 r
= lstat(path
, &st
);
137 _cleanup_security_context_free_ security_context_t fcon
= NULL
;
139 r
= selabel_lookup_raw(label_hnd
, &fcon
, path
, st
.st_mode
);
141 /* If there's no label to set, then exit without warning */
142 if (r
< 0 && errno
== ENOENT
)
146 r
= lsetfilecon(path
, fcon
);
148 /* If the FS doesn't support labels, then exit without warning */
149 if (r
< 0 && errno
== ENOTSUP
)
155 /* Ignore ENOENT in some cases */
156 if (ignore_enoent
&& errno
== ENOENT
)
159 if (ignore_erofs
&& errno
== EROFS
)
162 log_enforcing("Unable to fix SELinux security context of %s: %m", path
);
163 if (security_getenforce() == 1)
171 int mac_selinux_apply(const char *path
, const char *label
) {
177 if (!mac_selinux_use())
180 if (setfilecon(path
, (security_context_t
) label
) < 0) {
181 log_enforcing("Failed to set SELinux security context %s on path %s: %m", label
, path
);
182 if (security_getenforce() == 1)
189 int mac_selinux_get_create_label_from_exe(const char *exe
, char **label
) {
193 _cleanup_security_context_free_ security_context_t mycon
= NULL
, fcon
= NULL
;
194 security_class_t sclass
;
199 if (!mac_selinux_use())
206 r
= getfilecon(exe
, &fcon
);
210 sclass
= string_to_security_class("process");
211 r
= security_compute_create(mycon
, fcon
, sclass
, (security_context_t
*) label
);
219 int mac_selinux_get_our_label(char **label
) {
225 if (!mac_selinux_use())
236 int mac_selinux_get_child_mls_label(int socket_fd
, const char *exe
, const char *exec_label
, char **label
) {
240 _cleanup_security_context_free_ security_context_t mycon
= NULL
, peercon
= NULL
, fcon
= NULL
;
241 _cleanup_context_free_ context_t pcon
= NULL
, bcon
= NULL
;
242 security_class_t sclass
;
243 const char *range
= NULL
;
245 assert(socket_fd
>= 0);
249 if (!mac_selinux_use())
256 r
= getpeercon(socket_fd
, &peercon
);
261 /* If there is no context set for next exec let's use context
262 of target executable */
263 r
= getfilecon(exe
, &fcon
);
268 bcon
= context_new(mycon
);
272 pcon
= context_new(peercon
);
276 range
= context_range_get(pcon
);
280 r
= context_range_set(bcon
, range
);
285 mycon
= strdup(context_str(bcon
));
289 sclass
= string_to_security_class("process");
290 r
= security_compute_create(mycon
, fcon
, sclass
, (security_context_t
*) label
);
298 void mac_selinux_free(char *label
) {
301 if (!mac_selinux_use())
304 freecon((security_context_t
) label
);
308 int mac_selinux_create_file_prepare(const char *path
, mode_t mode
) {
312 _cleanup_security_context_free_ security_context_t filecon
= NULL
;
319 if (path_is_absolute(path
))
320 r
= selabel_lookup_raw(label_hnd
, &filecon
, path
, mode
);
322 _cleanup_free_
char *newpath
;
324 newpath
= path_make_absolute_cwd(path
);
328 r
= selabel_lookup_raw(label_hnd
, &filecon
, newpath
, mode
);
331 /* No context specified by the policy? Proceed without setting it. */
332 if (r
< 0 && errno
== ENOENT
)
338 r
= setfscreatecon(filecon
);
340 log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon
, path
);
345 if (r
< 0 && security_getenforce() == 0)
352 void mac_selinux_create_file_clear(void) {
357 if (!mac_selinux_use())
360 setfscreatecon(NULL
);
364 int mac_selinux_create_socket_prepare(const char *label
) {
367 if (!mac_selinux_use())
372 if (setsockcreatecon((security_context_t
) label
) < 0) {
373 log_enforcing("Failed to set SELinux security context %s for sockets: %m", label
);
375 if (security_getenforce() == 1)
383 void mac_selinux_create_socket_clear(void) {
388 if (!mac_selinux_use())
391 setsockcreatecon(NULL
);
395 int mac_selinux_bind(int fd
, const struct sockaddr
*addr
, socklen_t addrlen
) {
397 /* Binds a socket and label its file system object according to the SELinux policy */
400 _cleanup_security_context_free_ security_context_t fcon
= NULL
;
401 const struct sockaddr_un
*un
;
407 assert(addrlen
>= sizeof(sa_family_t
));
412 /* Filter out non-local sockets */
413 if (addr
->sa_family
!= AF_UNIX
)
416 /* Filter out anonymous sockets */
417 if (addrlen
< sizeof(sa_family_t
) + 1)
420 /* Filter out abstract namespace sockets */
421 un
= (const struct sockaddr_un
*) addr
;
422 if (un
->sun_path
[0] == 0)
425 path
= strndupa(un
->sun_path
, addrlen
- offsetof(struct sockaddr_un
, sun_path
));
427 if (path_is_absolute(path
))
428 r
= selabel_lookup_raw(label_hnd
, &fcon
, path
, S_IFSOCK
);
430 _cleanup_free_
char *newpath
;
432 newpath
= path_make_absolute_cwd(path
);
436 r
= selabel_lookup_raw(label_hnd
, &fcon
, newpath
, S_IFSOCK
);
440 r
= setfscreatecon(fcon
);
442 if (r
< 0 && errno
!= ENOENT
) {
443 log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon
, path
);
445 if (security_getenforce() == 1) {
451 r
= bind(fd
, addr
, addrlen
);
456 setfscreatecon(NULL
);
461 return bind(fd
, addr
, addrlen
) < 0 ? -errno
: 0;