Merge pull request #5883 from garyttierney/fix-selinux

selinux: enable labeling and access checks for unprivileged users
This commit is contained in:
Lennart Poettering
2017-05-17 11:39:44 +02:00
committed by GitHub
11 changed files with 24 additions and 27 deletions
+5 -15
View File
@@ -53,7 +53,7 @@ static struct selabel_handle *label_hnd = NULL;
#define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
#endif
bool mac_selinux_have(void) {
bool mac_selinux_use(void) {
#ifdef HAVE_SELINUX
if (cached_use < 0)
cached_use = is_selinux_enabled() > 0;
@@ -64,16 +64,6 @@ bool mac_selinux_have(void) {
#endif
}
bool mac_selinux_use(void) {
if (!mac_selinux_have())
return false;
/* Never try to configure SELinux features if we aren't
* root */
return getuid() == 0;
}
void mac_selinux_retest(void) {
#ifdef HAVE_SELINUX
cached_use = -1;
@@ -205,7 +195,7 @@ int mac_selinux_get_create_label_from_exe(const char *exe, char **label) {
assert(exe);
assert(label);
if (!mac_selinux_have())
if (!mac_selinux_use())
return -EOPNOTSUPP;
r = getcon_raw(&mycon);
@@ -231,7 +221,7 @@ int mac_selinux_get_our_label(char **label) {
assert(label);
#ifdef HAVE_SELINUX
if (!mac_selinux_have())
if (!mac_selinux_use())
return -EOPNOTSUPP;
r = getcon_raw(label);
@@ -255,7 +245,7 @@ int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *
assert(exe);
assert(label);
if (!mac_selinux_have())
if (!mac_selinux_use())
return -EOPNOTSUPP;
r = getcon_raw(&mycon);
@@ -310,7 +300,7 @@ char* mac_selinux_free(char *label) {
if (!label)
return NULL;
if (!mac_selinux_have())
if (!mac_selinux_use())
return NULL;
-1
View File
@@ -26,7 +26,6 @@
#include "macro.h"
bool mac_selinux_use(void);
bool mac_selinux_have(void);
void mac_selinux_retest(void);
int mac_selinux_init(void);
+8
View File
@@ -27,6 +27,7 @@
#include <libaudit.h>
#include <stdbool.h>
#include "capability-util.h"
#include "fd-util.h"
#include "log.h"
#include "util.h"
@@ -37,6 +38,13 @@ static int audit_fd;
int get_audit_fd(void) {
if (!initialized) {
if (have_effective_cap(CAP_AUDIT_WRITE) == 0) {
audit_fd = -EPERM;
initialized = true;
return audit_fd;
}
audit_fd = audit_open();
if (audit_fd < 0) {
+1 -1
View File
@@ -479,7 +479,7 @@ int server_open_native_socket(Server*s) {
return log_error_errno(errno, "SO_PASSCRED failed: %m");
#ifdef HAVE_SELINUX
if (mac_selinux_have()) {
if (mac_selinux_use()) {
r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
if (r < 0)
log_warning_errno(errno, "SO_PASSSEC failed: %m");
+1 -1
View File
@@ -918,7 +918,7 @@ static void dispatch_message_real(
}
#ifdef HAVE_SELINUX
if (mac_selinux_have()) {
if (mac_selinux_use()) {
if (label) {
x = alloca(strlen("_SELINUX_CONTEXT=") + label_len + 1);
+1 -1
View File
@@ -494,7 +494,7 @@ static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
if (r < 0)
return log_error_errno(r, "Failed to determine peer credentials: %m");
if (mac_selinux_have()) {
if (mac_selinux_use()) {
r = getpeersec(fd, &stream->label);
if (r < 0 && r != -EOPNOTSUPP)
(void) log_warning_errno(r, "Failed to determine peer security context: %m");
+1 -1
View File
@@ -410,7 +410,7 @@ int server_open_syslog_socket(Server *s) {
return log_error_errno(errno, "SO_PASSCRED failed: %m");
#ifdef HAVE_SELINUX
if (mac_selinux_have()) {
if (mac_selinux_use()) {
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
if (r < 0)
log_warning_errno(errno, "SO_PASSSEC failed: %m");
+1 -1
View File
@@ -607,7 +607,7 @@ static void bus_get_peercred(sd_bus *b) {
b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
/* Get the SELinux context of the peer */
if (mac_selinux_have()) {
if (mac_selinux_use()) {
r = getpeersec(b->input_fd, &b->label);
if (r < 0 && r != -EOPNOTSUPP)
log_debug_errno(r, "Failed to determine peer security context: %m");
+1 -1
View File
@@ -235,7 +235,7 @@ static int condition_test_security(Condition *c) {
assert(c->type == CONDITION_SECURITY);
if (streq(c->parameter, "selinux"))
return mac_selinux_have();
return mac_selinux_use();
if (streq(c->parameter, "smack"))
return mac_smack_use();
if (streq(c->parameter, "apparmor"))
+1 -1
View File
@@ -243,7 +243,7 @@ static void test_condition_test_security(void) {
condition = condition_new(CONDITION_SECURITY, "selinux", false, true);
assert_se(condition);
assert_se(condition_test(condition) != mac_selinux_have());
assert_se(condition_test(condition) != mac_selinux_use());
condition_free(condition);
condition = condition_new(CONDITION_SECURITY, "ima", false, false);
+4 -4
View File
@@ -35,16 +35,16 @@ static void test_testing(void) {
b = mac_selinux_use();
log_info("mac_selinux_use → %s", yes_no(b));
b = mac_selinux_have();
log_info("mac_selinux_have → %s", yes_no(b));
b = mac_selinux_use();
log_info("mac_selinux_use → %s", yes_no(b));
mac_selinux_retest();
b = mac_selinux_use();
log_info("mac_selinux_use → %s", yes_no(b));
b = mac_selinux_have();
log_info("mac_selinux_have → %s", yes_no(b));
b = mac_selinux_use();
log_info("mac_selinux_use → %s", yes_no(b));
}
static void test_loading(void) {