1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include <selinux/context.h>
14 #include <selinux/label.h>
15 #include <selinux/selinux.h>
18 #include "alloc-util.h"
22 #include "path-util.h"
23 #include "selinux-util.h"
24 #include "stdio-util.h"
25 #include "time-util.h"
29 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon
);
30 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t
, context_free
);
32 #define _cleanup_freecon_ _cleanup_(freeconp)
33 #define _cleanup_context_free_ _cleanup_(context_freep)
35 static int cached_use
= -1;
36 static struct selabel_handle
*label_hnd
= NULL
;
38 #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
39 #define log_enforcing_errno(r, ...) log_full_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, r, __VA_ARGS__)
42 bool mac_selinux_use(void) {
45 cached_use
= is_selinux_enabled() > 0;
53 void mac_selinux_retest(void) {
59 int mac_selinux_init(void) {
63 usec_t before_timestamp
, after_timestamp
;
64 struct mallinfo before_mallinfo
, after_mallinfo
;
69 if (!mac_selinux_use())
72 before_mallinfo
= mallinfo();
73 before_timestamp
= now(CLOCK_MONOTONIC
);
75 label_hnd
= selabel_open(SELABEL_CTX_FILE
, NULL
, 0);
77 log_enforcing_errno(errno
, "Failed to initialize SELinux context: %m");
78 r
= security_getenforce() == 1 ? -errno
: 0;
80 char timespan
[FORMAT_TIMESPAN_MAX
];
83 after_timestamp
= now(CLOCK_MONOTONIC
);
84 after_mallinfo
= mallinfo();
86 l
= after_mallinfo
.uordblks
> before_mallinfo
.uordblks
? after_mallinfo
.uordblks
- before_mallinfo
.uordblks
: 0;
88 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
89 format_timespan(timespan
, sizeof(timespan
), after_timestamp
- before_timestamp
, 0),
97 void mac_selinux_finish(void) {
103 selabel_close(label_hnd
);
108 int mac_selinux_fix(const char *path
, LabelFixFlags flags
) {
111 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
112 _cleanup_freecon_
char* fcon
= NULL
;
113 _cleanup_close_
int fd
= -1;
119 /* if mac_selinux_init() wasn't called before we are a NOOP */
123 /* Open the file as O_PATH, to pin it while we determine and adjust the label */
124 fd
= open(path
, O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
126 if ((flags
& LABEL_IGNORE_ENOENT
) && errno
== ENOENT
)
132 if (fstat(fd
, &st
) < 0)
135 if (selabel_lookup_raw(label_hnd
, &fcon
, path
, st
.st_mode
) < 0) {
138 /* If there's no label to set, then exit without warning */
145 xsprintf(procfs_path
, "/proc/self/fd/%i", fd
);
146 if (setfilecon_raw(procfs_path
, fcon
) < 0) {
147 _cleanup_freecon_
char *oldcon
= NULL
;
151 /* If the FS doesn't support labels, then exit without warning */
152 if (r
== -EOPNOTSUPP
)
155 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
156 if (r
== -EROFS
&& (flags
& LABEL_IGNORE_EROFS
))
159 /* If the old label is identical to the new one, suppress any kind of error */
160 if (getfilecon_raw(procfs_path
, &oldcon
) >= 0 && streq(fcon
, oldcon
))
169 log_enforcing_errno(r
, "Unable to fix SELinux security context of %s: %m", path
);
170 if (security_getenforce() == 1)
177 int mac_selinux_apply(const char *path
, const char *label
) {
180 if (!mac_selinux_use())
186 if (setfilecon(path
, label
) < 0) {
187 log_enforcing_errno(errno
, "Failed to set SELinux security context %s on path %s: %m", label
, path
);
188 if (security_getenforce() > 0)
195 int mac_selinux_get_create_label_from_exe(const char *exe
, char **label
) {
199 _cleanup_freecon_
char *mycon
= NULL
, *fcon
= NULL
;
200 security_class_t sclass
;
205 if (!mac_selinux_use())
208 r
= getcon_raw(&mycon
);
212 r
= getfilecon_raw(exe
, &fcon
);
216 sclass
= string_to_security_class("process");
217 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
225 int mac_selinux_get_our_label(char **label
) {
231 if (!mac_selinux_use())
234 r
= getcon_raw(label
);
242 int mac_selinux_get_child_mls_label(int socket_fd
, const char *exe
, const char *exec_label
, char **label
) {
246 _cleanup_freecon_
char *mycon
= NULL
, *peercon
= NULL
, *fcon
= NULL
;
247 _cleanup_context_free_ context_t pcon
= NULL
, bcon
= NULL
;
248 security_class_t sclass
;
249 const char *range
= NULL
;
251 assert(socket_fd
>= 0);
255 if (!mac_selinux_use())
258 r
= getcon_raw(&mycon
);
262 r
= getpeercon_raw(socket_fd
, &peercon
);
267 /* If there is no context set for next exec let's use context
268 of target executable */
269 r
= getfilecon_raw(exe
, &fcon
);
274 bcon
= context_new(mycon
);
278 pcon
= context_new(peercon
);
282 range
= context_range_get(pcon
);
286 r
= context_range_set(bcon
, range
);
291 mycon
= strdup(context_str(bcon
));
295 sclass
= string_to_security_class("process");
296 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
304 char* mac_selinux_free(char *label
) {
310 if (!mac_selinux_use())
319 int mac_selinux_create_file_prepare(const char *path
, mode_t mode
) {
322 _cleanup_freecon_
char *filecon
= NULL
;
330 if (path_is_absolute(path
))
331 r
= selabel_lookup_raw(label_hnd
, &filecon
, path
, mode
);
333 _cleanup_free_
char *newpath
= NULL
;
335 r
= path_make_absolute_cwd(path
, &newpath
);
339 r
= selabel_lookup_raw(label_hnd
, &filecon
, newpath
, mode
);
343 /* No context specified by the policy? Proceed without setting it. */
347 log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", path
);
349 if (setfscreatecon_raw(filecon
) >= 0)
350 return 0; /* Success! */
352 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", filecon
, path
);
355 if (security_getenforce() > 0)
362 void mac_selinux_create_file_clear(void) {
367 if (!mac_selinux_use())
370 setfscreatecon_raw(NULL
);
374 int mac_selinux_create_socket_prepare(const char *label
) {
377 if (!mac_selinux_use())
382 if (setsockcreatecon(label
) < 0) {
383 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for sockets: %m", label
);
385 if (security_getenforce() == 1)
393 void mac_selinux_create_socket_clear(void) {
398 if (!mac_selinux_use())
401 setsockcreatecon_raw(NULL
);
405 int mac_selinux_bind(int fd
, const struct sockaddr
*addr
, socklen_t addrlen
) {
407 /* Binds a socket and label its file system object according to the SELinux policy */
410 _cleanup_freecon_
char *fcon
= NULL
;
411 const struct sockaddr_un
*un
;
412 bool context_changed
= false;
418 assert(addrlen
>= sizeof(sa_family_t
));
423 /* Filter out non-local sockets */
424 if (addr
->sa_family
!= AF_UNIX
)
427 /* Filter out anonymous sockets */
428 if (addrlen
< offsetof(struct sockaddr_un
, sun_path
) + 1)
431 /* Filter out abstract namespace sockets */
432 un
= (const struct sockaddr_un
*) addr
;
433 if (un
->sun_path
[0] == 0)
436 path
= strndupa(un
->sun_path
, addrlen
- offsetof(struct sockaddr_un
, sun_path
));
438 if (path_is_absolute(path
))
439 r
= selabel_lookup_raw(label_hnd
, &fcon
, path
, S_IFSOCK
);
441 _cleanup_free_
char *newpath
= NULL
;
443 r
= path_make_absolute_cwd(path
, &newpath
);
447 r
= selabel_lookup_raw(label_hnd
, &fcon
, newpath
, S_IFSOCK
);
451 /* No context specified by the policy? Proceed without setting it */
455 log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", path
);
456 if (security_getenforce() > 0)
460 if (setfscreatecon_raw(fcon
) < 0) {
461 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", fcon
, path
);
462 if (security_getenforce() > 0)
465 context_changed
= true;
468 r
= bind(fd
, addr
, addrlen
) < 0 ? -errno
: 0;
471 setfscreatecon_raw(NULL
);
477 if (bind(fd
, addr
, addrlen
) < 0)