1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
16 #include <selinux/context.h>
17 #include <selinux/label.h>
18 #include <selinux/selinux.h>
21 #include "alloc-util.h"
25 #include "path-util.h"
26 #include "selinux-util.h"
27 #include "stdio-util.h"
28 #include "time-util.h"
32 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon
);
33 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t
, context_free
);
35 #define _cleanup_freecon_ _cleanup_(freeconp)
36 #define _cleanup_context_free_ _cleanup_(context_freep)
38 static int cached_use
= -1;
39 static struct selabel_handle
*label_hnd
= NULL
;
41 #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
42 #define log_enforcing_errno(r, ...) log_full_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, r, __VA_ARGS__)
45 bool mac_selinux_use(void) {
48 cached_use
= is_selinux_enabled() > 0;
56 void mac_selinux_retest(void) {
62 int mac_selinux_init(void) {
66 usec_t before_timestamp
, after_timestamp
;
67 struct mallinfo before_mallinfo
, after_mallinfo
;
72 if (!mac_selinux_use())
75 before_mallinfo
= mallinfo();
76 before_timestamp
= now(CLOCK_MONOTONIC
);
78 label_hnd
= selabel_open(SELABEL_CTX_FILE
, NULL
, 0);
80 log_enforcing_errno(errno
, "Failed to initialize SELinux context: %m");
81 r
= security_getenforce() == 1 ? -errno
: 0;
83 char timespan
[FORMAT_TIMESPAN_MAX
];
86 after_timestamp
= now(CLOCK_MONOTONIC
);
87 after_mallinfo
= mallinfo();
89 l
= after_mallinfo
.uordblks
> before_mallinfo
.uordblks
? after_mallinfo
.uordblks
- before_mallinfo
.uordblks
: 0;
91 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
92 format_timespan(timespan
, sizeof(timespan
), after_timestamp
- before_timestamp
, 0),
100 void mac_selinux_finish(void) {
106 selabel_close(label_hnd
);
111 int mac_selinux_fix(const char *path
, LabelFixFlags flags
) {
114 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
115 _cleanup_freecon_
char* fcon
= NULL
;
116 _cleanup_close_
int fd
= -1;
122 /* if mac_selinux_init() wasn't called before we are a NOOP */
126 /* Open the file as O_PATH, to pin it while we determine and adjust the label */
127 fd
= open(path
, O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
129 if ((flags
& LABEL_IGNORE_ENOENT
) && errno
== ENOENT
)
135 if (fstat(fd
, &st
) < 0)
138 if (selabel_lookup_raw(label_hnd
, &fcon
, path
, st
.st_mode
) < 0) {
141 /* If there's no label to set, then exit without warning */
148 xsprintf(procfs_path
, "/proc/self/fd/%i", fd
);
149 if (setfilecon_raw(procfs_path
, fcon
) < 0) {
150 _cleanup_freecon_
char *oldcon
= NULL
;
154 /* If the FS doesn't support labels, then exit without warning */
155 if (r
== -EOPNOTSUPP
)
158 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
159 if (r
== -EROFS
&& (flags
& LABEL_IGNORE_EROFS
))
162 /* If the old label is identical to the new one, suppress any kind of error */
163 if (getfilecon_raw(procfs_path
, &oldcon
) >= 0 && streq(fcon
, oldcon
))
172 log_enforcing_errno(r
, "Unable to fix SELinux security context of %s: %m", path
);
173 if (security_getenforce() == 1)
180 int mac_selinux_apply(const char *path
, const char *label
) {
183 if (!mac_selinux_use())
189 if (setfilecon(path
, label
) < 0) {
190 log_enforcing_errno(errno
, "Failed to set SELinux security context %s on path %s: %m", label
, path
);
191 if (security_getenforce() > 0)
198 int mac_selinux_get_create_label_from_exe(const char *exe
, char **label
) {
202 _cleanup_freecon_
char *mycon
= NULL
, *fcon
= NULL
;
203 security_class_t sclass
;
208 if (!mac_selinux_use())
211 r
= getcon_raw(&mycon
);
215 r
= getfilecon_raw(exe
, &fcon
);
219 sclass
= string_to_security_class("process");
220 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
228 int mac_selinux_get_our_label(char **label
) {
234 if (!mac_selinux_use())
237 r
= getcon_raw(label
);
245 int mac_selinux_get_child_mls_label(int socket_fd
, const char *exe
, const char *exec_label
, char **label
) {
249 _cleanup_freecon_
char *mycon
= NULL
, *peercon
= NULL
, *fcon
= NULL
;
250 _cleanup_context_free_ context_t pcon
= NULL
, bcon
= NULL
;
251 security_class_t sclass
;
252 const char *range
= NULL
;
254 assert(socket_fd
>= 0);
258 if (!mac_selinux_use())
261 r
= getcon_raw(&mycon
);
265 r
= getpeercon_raw(socket_fd
, &peercon
);
270 /* If there is no context set for next exec let's use context
271 of target executable */
272 r
= getfilecon_raw(exe
, &fcon
);
277 bcon
= context_new(mycon
);
281 pcon
= context_new(peercon
);
285 range
= context_range_get(pcon
);
289 r
= context_range_set(bcon
, range
);
294 mycon
= strdup(context_str(bcon
));
298 sclass
= string_to_security_class("process");
299 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
307 char* mac_selinux_free(char *label
) {
313 if (!mac_selinux_use())
322 int mac_selinux_create_file_prepare(const char *path
, mode_t mode
) {
325 _cleanup_freecon_
char *filecon
= NULL
;
333 if (path_is_absolute(path
))
334 r
= selabel_lookup_raw(label_hnd
, &filecon
, path
, mode
);
336 _cleanup_free_
char *newpath
= NULL
;
338 r
= path_make_absolute_cwd(path
, &newpath
);
342 r
= selabel_lookup_raw(label_hnd
, &filecon
, newpath
, mode
);
346 /* No context specified by the policy? Proceed without setting it. */
350 log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", path
);
352 if (setfscreatecon_raw(filecon
) >= 0)
353 return 0; /* Success! */
355 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", filecon
, path
);
358 if (security_getenforce() > 0)
365 void mac_selinux_create_file_clear(void) {
370 if (!mac_selinux_use())
373 setfscreatecon_raw(NULL
);
377 int mac_selinux_create_socket_prepare(const char *label
) {
380 if (!mac_selinux_use())
385 if (setsockcreatecon(label
) < 0) {
386 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for sockets: %m", label
);
388 if (security_getenforce() == 1)
396 void mac_selinux_create_socket_clear(void) {
401 if (!mac_selinux_use())
404 setsockcreatecon_raw(NULL
);
408 int mac_selinux_bind(int fd
, const struct sockaddr
*addr
, socklen_t addrlen
) {
410 /* Binds a socket and label its file system object according to the SELinux policy */
413 _cleanup_freecon_
char *fcon
= NULL
;
414 const struct sockaddr_un
*un
;
415 bool context_changed
= false;
421 assert(addrlen
>= sizeof(sa_family_t
));
426 /* Filter out non-local sockets */
427 if (addr
->sa_family
!= AF_UNIX
)
430 /* Filter out anonymous sockets */
431 if (addrlen
< offsetof(struct sockaddr_un
, sun_path
) + 1)
434 /* Filter out abstract namespace sockets */
435 un
= (const struct sockaddr_un
*) addr
;
436 if (un
->sun_path
[0] == 0)
439 path
= strndupa(un
->sun_path
, addrlen
- offsetof(struct sockaddr_un
, sun_path
));
441 if (path_is_absolute(path
))
442 r
= selabel_lookup_raw(label_hnd
, &fcon
, path
, S_IFSOCK
);
444 _cleanup_free_
char *newpath
= NULL
;
446 r
= path_make_absolute_cwd(path
, &newpath
);
450 r
= selabel_lookup_raw(label_hnd
, &fcon
, newpath
, S_IFSOCK
);
454 /* No context specified by the policy? Proceed without setting it */
458 log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", path
);
459 if (security_getenforce() > 0)
463 if (setfscreatecon_raw(fcon
) < 0) {
464 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", fcon
, path
);
465 if (security_getenforce() > 0)
468 context_changed
= true;
471 r
= bind(fd
, addr
, addrlen
) < 0 ? -errno
: 0;
474 setfscreatecon_raw(NULL
);
480 if (bind(fd
, addr
, addrlen
) < 0)