| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-messages.h" |
| 6 |
|
| 7 |
#include "errno-util.h" |
| 8 |
#include "initrd-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "selinux-setup.h" |
| 11 |
#include "selinux-util.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "time-util.h" |
| 14 |
|
| 15 |
int mac_selinux_setup(bool *loaded_policy) { |
| 16 |
int r; |
| 17 |
|
| 18 |
assert(loaded_policy); |
| 19 |
|
| 20 |
r = DLOPEN_LIBSELINUX(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 21 |
if (r < 0) |
| 22 |
return 0; |
| 23 |
|
| 24 |
mac_selinux_disable_logging(); |
| 25 |
|
| 26 |
/* Don't load policy in the initrd if we don't appear to have it. For the real root, we check below |
| 27 |
* if we've already loaded policy, and return gracefully. */ |
| 28 |
if (in_initrd() && access(sym_selinux_path(), F_OK) < 0) { |
| 29 |
if (errno != ENOENT) |
| 30 |
log_warning_errno(errno, "Unable to check if %s exists, assuming it does not: %m", sym_selinux_path()); |
| 31 |
|
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
bool initialized = false; |
| 36 |
|
| 37 |
/* Already initialized by somebody else? |
| 38 |
* |
| 39 |
* Note: getcon_raw() can return 0, and still give us a NULL pointer if /proc/self/attr/current is |
| 40 |
* empty. SELinux guarantees this won't happen, but that file isn't specific to SELinux, and may be |
| 41 |
* provided by some other arbitrary LSM with different semantics. */ |
| 42 |
_cleanup_freecon_ char *con = NULL; |
| 43 |
if (sym_getcon_raw(&con) < 0) |
| 44 |
log_debug_errno(errno, "getcon_raw() failed, assuming SELinux is not initialized: %m"); |
| 45 |
else if (con) { |
| 46 |
initialized = !streq(con, "kernel"); |
| 47 |
log_debug("SELinux already initialized: %s", yes_no(initialized)); |
| 48 |
} |
| 49 |
|
| 50 |
/* Make sure we have no fds open while loading the policy and transitioning */ |
| 51 |
log_close(); |
| 52 |
|
| 53 |
/* Now load the policy */ |
| 54 |
usec_t before_load = now(CLOCK_MONOTONIC); |
| 55 |
int enforce = 0; |
| 56 |
if (sym_selinux_init_load_policy(&enforce) == 0) { /* NB: Apparently doesn't set useful errno! */ |
| 57 |
mac_selinux_retest(); |
| 58 |
|
| 59 |
/* Transition to the new context */ |
| 60 |
_cleanup_freecon_ char *label = NULL; |
| 61 |
r = mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label); |
| 62 |
if (r < 0) { |
| 63 |
log_open(); |
| 64 |
log_warning_errno(r, "Failed to compute init label, ignoring: %m"); |
| 65 |
} else { |
| 66 |
r = RET_NERRNO(sym_setcon_raw(label)); |
| 67 |
log_open(); |
| 68 |
if (r < 0) |
| 69 |
log_warning_errno(r, "Failed to transition into init label '%s', ignoring: %m", label); |
| 70 |
else |
| 71 |
log_debug("Successfully switched to calculated init label '%s'.", label); |
| 72 |
} |
| 73 |
|
| 74 |
usec_t after_load = now(CLOCK_MONOTONIC); |
| 75 |
log_info("Successfully loaded SELinux policy in %s.", |
| 76 |
FORMAT_TIMESPAN(after_load - before_load, 0)); |
| 77 |
|
| 78 |
*loaded_policy = true; |
| 79 |
} else { |
| 80 |
log_open(); |
| 81 |
|
| 82 |
if (enforce > 0) { |
| 83 |
if (!initialized) |
| 84 |
return log_struct_errno(LOG_EMERG, SYNTHETIC_ERRNO(EIO), |
| 85 |
LOG_MESSAGE("Failed to load SELinux policy."), |
| 86 |
LOG_MESSAGE_ID(SD_MESSAGE_SELINUX_FAILED_STR)); |
| 87 |
|
| 88 |
log_notice("Failed to load new SELinux policy. Continuing with old policy."); |
| 89 |
} else |
| 90 |
log_debug("Unable to load SELinux policy. Ignoring."); |
| 91 |
} |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |