1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/types.h>
15 #include <selinux/context.h>
16 #include <selinux/label.h>
17 #include <selinux/selinux.h>
20 #include "alloc-util.h"
21 #include "errno-util.h"
25 #include "path-util.h"
26 #include "selinux-util.h"
27 #include "stdio-util.h"
28 #include "time-util.h"
31 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon
);
32 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t
, context_free
);
34 #define _cleanup_freecon_ _cleanup_(freeconp)
35 #define _cleanup_context_free_ _cleanup_(context_freep)
37 static int cached_use
= -1;
38 static struct selabel_handle
*label_hnd
= NULL
;
40 #define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
41 #define log_enforcing_errno(r, ...) log_full_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, r, __VA_ARGS__)
44 bool mac_selinux_use(void) {
47 cached_use
= is_selinux_enabled() > 0;
55 void mac_selinux_retest(void) {
61 int mac_selinux_init(void) {
65 usec_t before_timestamp
, after_timestamp
;
66 struct mallinfo before_mallinfo
, after_mallinfo
;
71 if (!mac_selinux_use())
74 before_mallinfo
= mallinfo();
75 before_timestamp
= now(CLOCK_MONOTONIC
);
77 label_hnd
= selabel_open(SELABEL_CTX_FILE
, NULL
, 0);
79 log_enforcing_errno(errno
, "Failed to initialize SELinux context: %m");
80 r
= security_getenforce() == 1 ? -errno
: 0;
82 char timespan
[FORMAT_TIMESPAN_MAX
];
85 after_timestamp
= now(CLOCK_MONOTONIC
);
86 after_mallinfo
= mallinfo();
88 l
= after_mallinfo
.uordblks
> before_mallinfo
.uordblks
? after_mallinfo
.uordblks
- before_mallinfo
.uordblks
: 0;
90 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
91 format_timespan(timespan
, sizeof(timespan
), after_timestamp
- before_timestamp
, 0),
99 void mac_selinux_finish(void) {
105 selabel_close(label_hnd
);
110 int mac_selinux_fix(const char *path
, LabelFixFlags flags
) {
113 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
114 _cleanup_freecon_
char* fcon
= NULL
;
115 _cleanup_close_
int fd
= -1;
121 /* if mac_selinux_init() wasn't called before we are a NOOP */
125 /* Open the file as O_PATH, to pin it while we determine and adjust the label */
126 fd
= open(path
, O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
128 if ((flags
& LABEL_IGNORE_ENOENT
) && errno
== ENOENT
)
134 if (fstat(fd
, &st
) < 0)
137 if (selabel_lookup_raw(label_hnd
, &fcon
, path
, st
.st_mode
) < 0) {
140 /* If there's no label to set, then exit without warning */
147 xsprintf(procfs_path
, "/proc/self/fd/%i", fd
);
148 if (setfilecon_raw(procfs_path
, fcon
) < 0) {
149 _cleanup_freecon_
char *oldcon
= NULL
;
153 /* If the FS doesn't support labels, then exit without warning */
154 if (r
== -EOPNOTSUPP
)
157 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
158 if (r
== -EROFS
&& (flags
& LABEL_IGNORE_EROFS
))
161 /* If the old label is identical to the new one, suppress any kind of error */
162 if (getfilecon_raw(procfs_path
, &oldcon
) >= 0 && streq(fcon
, oldcon
))
171 log_enforcing_errno(r
, "Unable to fix SELinux security context of %s: %m", path
);
172 if (security_getenforce() == 1)
179 int mac_selinux_apply(const char *path
, const char *label
) {
182 if (!mac_selinux_use())
188 if (setfilecon(path
, label
) < 0) {
189 log_enforcing_errno(errno
, "Failed to set SELinux security context %s on path %s: %m", label
, path
);
190 if (security_getenforce() > 0)
197 int mac_selinux_get_create_label_from_exe(const char *exe
, char **label
) {
201 _cleanup_freecon_
char *mycon
= NULL
, *fcon
= NULL
;
202 security_class_t sclass
;
207 if (!mac_selinux_use())
210 r
= getcon_raw(&mycon
);
214 r
= getfilecon_raw(exe
, &fcon
);
218 sclass
= string_to_security_class("process");
219 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
227 int mac_selinux_get_our_label(char **label
) {
233 if (!mac_selinux_use())
236 r
= getcon_raw(label
);
244 int mac_selinux_get_child_mls_label(int socket_fd
, const char *exe
, const char *exec_label
, char **label
) {
248 _cleanup_freecon_
char *mycon
= NULL
, *peercon
= NULL
, *fcon
= NULL
;
249 _cleanup_context_free_ context_t pcon
= NULL
, bcon
= NULL
;
250 security_class_t sclass
;
251 const char *range
= NULL
;
253 assert(socket_fd
>= 0);
257 if (!mac_selinux_use())
260 r
= getcon_raw(&mycon
);
264 r
= getpeercon_raw(socket_fd
, &peercon
);
269 /* If there is no context set for next exec let's use context
270 of target executable */
271 r
= getfilecon_raw(exe
, &fcon
);
276 bcon
= context_new(mycon
);
280 pcon
= context_new(peercon
);
284 range
= context_range_get(pcon
);
288 r
= context_range_set(bcon
, range
);
293 mycon
= strdup(context_str(bcon
));
297 sclass
= string_to_security_class("process");
298 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
306 char* mac_selinux_free(char *label
) {
312 if (!mac_selinux_use())
322 static int selinux_create_file_prepare_abspath(const char *abspath
, mode_t mode
) {
323 _cleanup_freecon_
char *filecon
= NULL
;
327 assert(path_is_absolute(abspath
));
329 r
= selabel_lookup_raw(label_hnd
, &filecon
, abspath
, mode
);
331 /* No context specified by the policy? Proceed without setting it. */
335 log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", abspath
);
337 if (setfscreatecon_raw(filecon
) >= 0)
338 return 0; /* Success! */
340 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", filecon
, abspath
);
343 if (security_getenforce() > 0)
350 int mac_selinux_create_file_prepare_at(int dirfd
, const char *path
, mode_t mode
) {
354 _cleanup_free_
char *abspath
= NULL
;
361 if (!path_is_absolute(path
)) {
362 _cleanup_free_
char *p
= NULL
;
364 if (dirfd
== AT_FDCWD
)
367 r
= fd_get_path(dirfd
, &p
);
371 path
= abspath
= path_join(p
, path
);
376 r
= selinux_create_file_prepare_abspath(path
, mode
);
381 int mac_selinux_create_file_prepare(const char *path
, mode_t mode
) {
385 _cleanup_free_
char *abspath
= NULL
;
392 r
= path_make_absolute_cwd(path
, &abspath
);
396 r
= selinux_create_file_prepare_abspath(abspath
, mode
);
401 void mac_selinux_create_file_clear(void) {
406 if (!mac_selinux_use())
409 setfscreatecon_raw(NULL
);
413 int mac_selinux_create_socket_prepare(const char *label
) {
416 if (!mac_selinux_use())
421 if (setsockcreatecon(label
) < 0) {
422 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for sockets: %m", label
);
424 if (security_getenforce() == 1)
432 void mac_selinux_create_socket_clear(void) {
437 if (!mac_selinux_use())
440 setsockcreatecon_raw(NULL
);
444 int mac_selinux_bind(int fd
, const struct sockaddr
*addr
, socklen_t addrlen
) {
446 /* Binds a socket and label its file system object according to the SELinux policy */
449 _cleanup_freecon_
char *fcon
= NULL
;
450 const struct sockaddr_un
*un
;
451 bool context_changed
= false;
457 assert(addrlen
>= sizeof(sa_family_t
));
462 /* Filter out non-local sockets */
463 if (addr
->sa_family
!= AF_UNIX
)
466 /* Filter out anonymous sockets */
467 if (addrlen
< offsetof(struct sockaddr_un
, sun_path
) + 1)
470 /* Filter out abstract namespace sockets */
471 un
= (const struct sockaddr_un
*) addr
;
472 if (un
->sun_path
[0] == 0)
475 path
= strndupa(un
->sun_path
, addrlen
- offsetof(struct sockaddr_un
, sun_path
));
477 if (path_is_absolute(path
))
478 r
= selabel_lookup_raw(label_hnd
, &fcon
, path
, S_IFSOCK
);
480 _cleanup_free_
char *newpath
= NULL
;
482 r
= path_make_absolute_cwd(path
, &newpath
);
486 r
= selabel_lookup_raw(label_hnd
, &fcon
, newpath
, S_IFSOCK
);
490 /* No context specified by the policy? Proceed without setting it */
494 log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", path
);
495 if (security_getenforce() > 0)
499 if (setfscreatecon_raw(fcon
) < 0) {
500 log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", fcon
, path
);
501 if (security_getenforce() > 0)
504 context_changed
= true;
507 r
= bind(fd
, addr
, addrlen
) < 0 ? -errno
: 0;
510 setfscreatecon_raw(NULL
);
516 if (bind(fd
, addr
, addrlen
) < 0)