2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-10-27 05:47:02 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2010-10-27 05:47:02 +02:00
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_SELINUX
|
2010-10-27 05:47:02 +02:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "log.h"
|
2010-10-27 05:47:02 +02:00
|
|
|
#include "macro.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "selinux-setup.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "selinux-util.h"
|
|
|
|
#include "string-util.h"
|
2019-03-27 11:32:41 +01:00
|
|
|
#include "time-util.h"
|
2010-10-27 05:47:02 +02:00
|
|
|
#include "util.h"
|
2011-07-28 23:52:23 +02:00
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_SELINUX
|
2015-08-17 23:23:31 +02:00
|
|
|
_printf_(2,3)
|
2012-07-10 19:28:26 +02:00
|
|
|
static int null_log(int type, const char *fmt, ...) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-28 02:02:00 +02:00
|
|
|
#endif
|
2012-07-10 19:28:26 +02:00
|
|
|
|
2014-10-24 14:15:25 +02:00
|
|
|
int mac_selinux_setup(bool *loaded_policy) {
|
2010-10-27 05:47:02 +02:00
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_SELINUX
|
2014-04-26 00:26:33 +02:00
|
|
|
int enforce = 0;
|
|
|
|
usec_t before_load, after_load;
|
2016-07-15 18:44:02 +02:00
|
|
|
char *con;
|
2014-04-26 00:26:33 +02:00
|
|
|
int r;
|
2018-03-21 11:48:40 +01:00
|
|
|
static const union selinux_callback cb = {
|
|
|
|
.func_log = null_log,
|
|
|
|
};
|
|
|
|
|
core: let selinux_setup() load policy more than once
When you switch-root into a new root that has SELinux policy, you're
supposed to to run selinux_init_load_policy() to set up SELinux and load
policy. Normally this gets handled by selinux_setup().
But if SELinux was already initialized, selinux_setup() skips loading
policy and returns 0. So if you load policy normally, and then you
switch-root to a new root that has new policy, selinux_setup() never
loads the new policy. What gives?
As far as I can tell, this check is an artifact of how selinux_setup()
worked when it was first written (see commit c4dcdb9 / systemd v12):
* when systemd starts, run selinux_setup()
* if selinux_setup() loads policy OK, restart systemd
So the "if policy already loaded, skip load and return 0" check was
there to prevent an infinite re-exec loop.
Modern systemd only calls selinux_setup() on initial load and after
switch-root, and selinux_setup() no longer restarts systemd, so we don't
need that check to guard against the infinite loop anymore.
So: this patch removes the "return 0", thus allowing selinux_setup() to
actually perform SELinux setup after switch-root.
We still want to check to see if SELinux is initialized, because if
selinux_init_load_policy() fails *but* SELinux is initialized that means
we still have (old) policy active. So we don't need to halt if
enforce=1.
2014-04-26 00:26:34 +02:00
|
|
|
bool initialized = false;
|
2014-04-26 00:26:33 +02:00
|
|
|
|
|
|
|
assert(loaded_policy);
|
|
|
|
|
|
|
|
/* Turn off all of SELinux' own logging, we want to do that */
|
|
|
|
selinux_set_callback(SELINUX_CB_LOG, cb);
|
|
|
|
|
|
|
|
/* Don't load policy in the initrd if we don't appear to have
|
|
|
|
* it. For the real root, we check below if we've already
|
|
|
|
* loaded policy, and return gracefully.
|
|
|
|
*/
|
|
|
|
if (in_initrd() && access(selinux_path(), F_OK) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Already initialized by somebody else? */
|
|
|
|
r = getcon_raw(&con);
|
2020-08-03 18:19:49 +02:00
|
|
|
/* getcon_raw can return 0, and still give us a NULL pointer if
|
|
|
|
* /proc/self/attr/current is empty. SELinux guarantees this won't
|
|
|
|
* happen, but that file isn't specific to SELinux, and may be provided
|
|
|
|
* by some other arbitrary LSM with different semantics. */
|
2020-07-23 19:54:23 +02:00
|
|
|
if (r == 0 && con) {
|
2014-04-26 00:26:33 +02:00
|
|
|
initialized = !streq(con, "kernel");
|
|
|
|
freecon(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we have no fds open while loading the policy and
|
|
|
|
* transitioning */
|
|
|
|
log_close();
|
|
|
|
|
|
|
|
/* Now load the policy */
|
|
|
|
before_load = now(CLOCK_MONOTONIC);
|
|
|
|
r = selinux_init_load_policy(&enforce);
|
|
|
|
if (r == 0) {
|
2015-09-23 19:55:49 +02:00
|
|
|
_cleanup_(mac_selinux_freep) char *label = NULL;
|
2014-04-26 00:26:33 +02:00
|
|
|
char timespan[FORMAT_TIMESPAN_MAX];
|
|
|
|
|
2014-10-23 17:34:30 +02:00
|
|
|
mac_selinux_retest();
|
2014-04-26 00:26:33 +02:00
|
|
|
|
|
|
|
/* Transition to the new context */
|
2014-10-23 10:23:46 +02:00
|
|
|
r = mac_selinux_get_create_label_from_exe(SYSTEMD_BINARY_PATH, &label);
|
2015-09-23 19:55:49 +02:00
|
|
|
if (r < 0 || !label) {
|
2014-04-26 00:26:33 +02:00
|
|
|
log_open();
|
|
|
|
log_error("Failed to compute init label, ignoring.");
|
|
|
|
} else {
|
2016-03-04 02:58:28 +01:00
|
|
|
r = setcon_raw(label);
|
2014-04-26 00:26:33 +02:00
|
|
|
|
|
|
|
log_open();
|
|
|
|
if (r < 0)
|
|
|
|
log_error("Failed to transition into init label '%s', ignoring.", label);
|
|
|
|
}
|
|
|
|
|
|
|
|
after_load = now(CLOCK_MONOTONIC);
|
|
|
|
|
|
|
|
log_info("Successfully loaded SELinux policy in %s.",
|
|
|
|
format_timespan(timespan, sizeof(timespan), after_load - before_load, 0));
|
|
|
|
|
|
|
|
*loaded_policy = true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
if (enforce > 0) {
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!initialized)
|
|
|
|
return log_emergency_errno(SYNTHETIC_ERRNO(EIO),
|
|
|
|
"Failed to load SELinux policy.");
|
core: let selinux_setup() load policy more than once
When you switch-root into a new root that has SELinux policy, you're
supposed to to run selinux_init_load_policy() to set up SELinux and load
policy. Normally this gets handled by selinux_setup().
But if SELinux was already initialized, selinux_setup() skips loading
policy and returns 0. So if you load policy normally, and then you
switch-root to a new root that has new policy, selinux_setup() never
loads the new policy. What gives?
As far as I can tell, this check is an artifact of how selinux_setup()
worked when it was first written (see commit c4dcdb9 / systemd v12):
* when systemd starts, run selinux_setup()
* if selinux_setup() loads policy OK, restart systemd
So the "if policy already loaded, skip load and return 0" check was
there to prevent an infinite re-exec loop.
Modern systemd only calls selinux_setup() on initial load and after
switch-root, and selinux_setup() no longer restarts systemd, so we don't
need that check to guard against the infinite loop anymore.
So: this patch removes the "return 0", thus allowing selinux_setup() to
actually perform SELinux setup after switch-root.
We still want to check to see if SELinux is initialized, because if
selinux_init_load_policy() fails *but* SELinux is initialized that means
we still have (old) policy active. So we don't need to halt if
enforce=1.
2014-04-26 00:26:34 +02:00
|
|
|
|
|
|
|
log_warning("Failed to load new SELinux policy. Continuing with old policy.");
|
2014-04-26 00:26:33 +02:00
|
|
|
} else
|
|
|
|
log_debug("Unable to load SELinux policy. Ignoring.");
|
|
|
|
}
|
2010-10-27 05:47:02 +02:00
|
|
|
#endif
|
|
|
|
|
2014-04-26 00:26:33 +02:00
|
|
|
return 0;
|
2010-10-27 05:47:02 +02:00
|
|
|
}
|