1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/types.h>
15 #include <selinux/avc.h>
16 #include <selinux/context.h>
17 #include <selinux/label.h>
18 #include <selinux/selinux.h>
21 #include "alloc-util.h"
22 #include "errno-util.h"
26 #include "path-util.h"
27 #include "selinux-util.h"
28 #include "stdio-util.h"
29 #include "time-util.h"
32 DEFINE_TRIVIAL_CLEANUP_FUNC(context_t
, context_free
);
33 #define _cleanup_context_free_ _cleanup_(context_freep)
35 static int mac_selinux_reload(int seqno
);
37 static int cached_use
= -1;
38 static int cached_enforcing
= -1;
39 static struct selabel_handle
*label_hnd
= NULL
;
41 #define log_enforcing(...) log_full(mac_selinux_enforcing() ? LOG_ERR : LOG_WARNING, __VA_ARGS__)
43 #define log_enforcing_errno(error, ...) \
45 bool _enforcing = mac_selinux_enforcing(); \
46 int _level = _enforcing ? LOG_ERR : LOG_WARNING; \
49 int _r = (log_get_max_level() >= LOG_PRI(_level)) \
50 ? log_internal_realm(_level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
52 _enforcing ? _r : 0; \
56 bool mac_selinux_use(void) {
58 if (_unlikely_(cached_use
< 0)) {
59 cached_use
= is_selinux_enabled() > 0;
60 log_debug("SELinux enabled state cached to: %s", cached_use
? "enabled" : "disabled");
69 bool mac_selinux_enforcing(void) {
71 if (_unlikely_(cached_enforcing
< 0)) {
72 cached_enforcing
= security_getenforce();
73 if (cached_enforcing
< 0) {
74 log_debug_errno(errno
, "Failed to get SELinux enforced status, continuing in enforcing mode: %m");
75 return true; /* treat failure as enforcing mode */
78 log_debug("SELinux enforcing state cached to: %s", cached_enforcing
? "enforcing" : "permissive");
81 return cached_enforcing
> 0;
87 void mac_selinux_retest(void) {
90 cached_enforcing
= -1;
95 static int setenforce_callback(int enforcing
) {
96 cached_enforcing
= enforcing
;
98 log_debug("SELinux enforcing state updated to: %s", cached_enforcing
? "enforcing" : "permissive");
104 int mac_selinux_init(void) {
106 usec_t before_timestamp
, after_timestamp
;
107 struct mallinfo before_mallinfo
, after_mallinfo
;
108 char timespan
[FORMAT_TIMESPAN_MAX
];
111 selinux_set_callback(SELINUX_CB_POLICYLOAD
, (union selinux_callback
) mac_selinux_reload
);
112 selinux_set_callback(SELINUX_CB_SETENFORCE
, (union selinux_callback
) setenforce_callback
);
117 if (!mac_selinux_use())
120 before_mallinfo
= mallinfo();
121 before_timestamp
= now(CLOCK_MONOTONIC
);
123 label_hnd
= selabel_open(SELABEL_CTX_FILE
, NULL
, 0);
125 return log_enforcing_errno(errno
, "Failed to initialize SELinux labeling handle: %m");
127 after_timestamp
= now(CLOCK_MONOTONIC
);
128 after_mallinfo
= mallinfo();
130 l
= after_mallinfo
.uordblks
> before_mallinfo
.uordblks
? after_mallinfo
.uordblks
- before_mallinfo
.uordblks
: 0;
132 log_debug("Successfully loaded SELinux database in %s, size on heap is %iK.",
133 format_timespan(timespan
, sizeof(timespan
), after_timestamp
- before_timestamp
, 0),
140 void mac_selinux_finish(void) {
146 selabel_close(label_hnd
);
152 static int mac_selinux_reload(int seqno
) {
153 struct selabel_handle
*backup_label_hnd
;
158 backup_label_hnd
= TAKE_PTR(label_hnd
);
160 /* try to initialize new handle
161 * on success close backup
162 * on failure restore backup */
163 if (mac_selinux_init() == 0)
164 selabel_close(backup_label_hnd
);
166 label_hnd
= backup_label_hnd
;
172 int mac_selinux_fix_container(const char *path
, const char *inside_path
, LabelFixFlags flags
) {
175 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
176 _cleanup_freecon_
char* fcon
= NULL
;
177 _cleanup_close_
int fd
= -1;
183 /* if mac_selinux_init() wasn't called before we are a NOOP */
187 /* Open the file as O_PATH, to pin it while we determine and adjust the label */
188 fd
= open(path
, O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
190 if ((flags
& LABEL_IGNORE_ENOENT
) && errno
== ENOENT
)
196 if (fstat(fd
, &st
) < 0)
199 /* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */
200 (void) avc_netlink_check_nb();
202 if (selabel_lookup_raw(label_hnd
, &fcon
, inside_path
, st
.st_mode
) < 0) {
205 /* If there's no label to set, then exit without warning */
212 xsprintf(procfs_path
, "/proc/self/fd/%i", fd
);
213 if (setfilecon_raw(procfs_path
, fcon
) < 0) {
214 _cleanup_freecon_
char *oldcon
= NULL
;
218 /* If the FS doesn't support labels, then exit without warning */
219 if (r
== -EOPNOTSUPP
)
222 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
223 if (r
== -EROFS
&& (flags
& LABEL_IGNORE_EROFS
))
226 /* If the old label is identical to the new one, suppress any kind of error */
227 if (getfilecon_raw(procfs_path
, &oldcon
) >= 0 && streq(fcon
, oldcon
))
236 return log_enforcing_errno(r
, "Unable to fix SELinux security context of %s (%s): %m", path
, inside_path
);
242 int mac_selinux_apply(const char *path
, const char *label
) {
245 if (!mac_selinux_use())
251 if (setfilecon(path
, label
) < 0)
252 return log_enforcing_errno(errno
, "Failed to set SELinux security context %s on path %s: %m", label
, path
);
257 int mac_selinux_get_create_label_from_exe(const char *exe
, char **label
) {
259 _cleanup_freecon_
char *mycon
= NULL
, *fcon
= NULL
;
260 security_class_t sclass
;
266 if (!mac_selinux_use())
269 r
= getcon_raw(&mycon
);
273 r
= getfilecon_raw(exe
, &fcon
);
277 sclass
= string_to_security_class("process");
281 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
291 int mac_selinux_get_our_label(char **label
) {
297 if (!mac_selinux_use())
300 r
= getcon_raw(label
);
310 int mac_selinux_get_child_mls_label(int socket_fd
, const char *exe
, const char *exec_label
, char **label
) {
312 _cleanup_freecon_
char *mycon
= NULL
, *peercon
= NULL
, *fcon
= NULL
;
313 _cleanup_context_free_ context_t pcon
= NULL
, bcon
= NULL
;
314 security_class_t sclass
;
315 const char *range
= NULL
;
318 assert(socket_fd
>= 0);
322 if (!mac_selinux_use())
325 r
= getcon_raw(&mycon
);
329 r
= getpeercon_raw(socket_fd
, &peercon
);
334 /* If there is no context set for next exec let's use context
335 of target executable */
336 r
= getfilecon_raw(exe
, &fcon
);
341 bcon
= context_new(mycon
);
345 pcon
= context_new(peercon
);
349 range
= context_range_get(pcon
);
353 r
= context_range_set(bcon
, range
);
358 mycon
= strdup(context_str(bcon
));
362 sclass
= string_to_security_class("process");
366 r
= security_compute_create_raw(mycon
, fcon
, sclass
, label
);
376 char* mac_selinux_free(char *label
) {
388 static int selinux_create_file_prepare_abspath(const char *abspath
, mode_t mode
) {
389 _cleanup_freecon_
char *filecon
= NULL
;
393 assert(path_is_absolute(abspath
));
395 /* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */
396 (void) avc_netlink_check_nb();
398 r
= selabel_lookup_raw(label_hnd
, &filecon
, abspath
, mode
);
400 /* No context specified by the policy? Proceed without setting it. */
404 return log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", abspath
);
407 if (setfscreatecon_raw(filecon
) < 0)
408 return log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", filecon
, abspath
);
414 int mac_selinux_create_file_prepare_at(int dirfd
, const char *path
, mode_t mode
) {
416 _cleanup_free_
char *abspath
= NULL
;
425 if (!path_is_absolute(path
)) {
426 _cleanup_free_
char *p
= NULL
;
428 if (dirfd
== AT_FDCWD
)
431 r
= fd_get_path(dirfd
, &p
);
435 path
= abspath
= path_join(p
, path
);
440 return selinux_create_file_prepare_abspath(path
, mode
);
446 int mac_selinux_create_file_prepare(const char *path
, mode_t mode
) {
450 _cleanup_free_
char *abspath
= NULL
;
457 r
= path_make_absolute_cwd(path
, &abspath
);
461 return selinux_create_file_prepare_abspath(abspath
, mode
);
467 void mac_selinux_create_file_clear(void) {
472 if (!mac_selinux_use())
475 setfscreatecon_raw(NULL
);
479 int mac_selinux_create_socket_prepare(const char *label
) {
484 if (!mac_selinux_use())
487 if (setsockcreatecon(label
) < 0)
488 return log_enforcing_errno(errno
, "Failed to set SELinux security context %s for sockets: %m", label
);
494 void mac_selinux_create_socket_clear(void) {
499 if (!mac_selinux_use())
502 setsockcreatecon_raw(NULL
);
506 int mac_selinux_bind(int fd
, const struct sockaddr
*addr
, socklen_t addrlen
) {
508 /* Binds a socket and label its file system object according to the SELinux policy */
511 _cleanup_freecon_
char *fcon
= NULL
;
512 const struct sockaddr_un
*un
;
513 bool context_changed
= false;
519 assert(addrlen
>= sizeof(sa_family_t
));
524 /* Filter out non-local sockets */
525 if (addr
->sa_family
!= AF_UNIX
)
528 /* Filter out anonymous sockets */
529 if (addrlen
< offsetof(struct sockaddr_un
, sun_path
) + 1)
532 /* Filter out abstract namespace sockets */
533 un
= (const struct sockaddr_un
*) addr
;
534 if (un
->sun_path
[0] == 0)
537 path
= strndupa(un
->sun_path
, addrlen
- offsetof(struct sockaddr_un
, sun_path
));
539 /* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */
540 (void) avc_netlink_check_nb();
542 if (path_is_absolute(path
))
543 r
= selabel_lookup_raw(label_hnd
, &fcon
, path
, S_IFSOCK
);
545 _cleanup_free_
char *newpath
= NULL
;
547 r
= path_make_absolute_cwd(path
, &newpath
);
551 r
= selabel_lookup_raw(label_hnd
, &fcon
, newpath
, S_IFSOCK
);
555 /* No context specified by the policy? Proceed without setting it */
559 r
= log_enforcing_errno(errno
, "Failed to determine SELinux security context for %s: %m", path
);
563 if (setfscreatecon_raw(fcon
) < 0) {
564 r
= log_enforcing_errno(errno
, "Failed to set SELinux security context %s for %s: %m", fcon
, path
);
568 context_changed
= true;
571 r
= bind(fd
, addr
, addrlen
) < 0 ? -errno
: 0;
574 (void) setfscreatecon_raw(NULL
);
580 if (bind(fd
, addr
, addrlen
) < 0)