chiark / gitweb /
security: rework selinux, smack, ima, apparmor detection logic
authorLennart Poettering <lennart@poettering.net>
Thu, 10 Oct 2013 14:35:44 +0000 (16:35 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 10 Oct 2013 14:35:44 +0000 (16:35 +0200)
Always cache the results, and bypass low-level security calls when the
respective subsystem is not enabled.

14 files changed:
Makefile.am
src/core/condition.c
src/core/socket.c
src/journal/journald-native.c
src/journal/journald-server.c
src/journal/journald-stream.c
src/journal/journald-syslog.c
src/shared/apparmor-util.c [new file with mode: 0644]
src/shared/apparmor-util.h [new file with mode: 0644]
src/shared/ima-util.c [new file with mode: 0644]
src/shared/ima-util.h [new file with mode: 0644]
src/shared/selinux-util.c
src/shared/smack-util.c
src/udev/udev-node.c

index be152e9b8d492ffc470826818afca0051e538263..12c7d7cacff51cc2cab0730a9e6f5e2a82a4dfa4 100644 (file)
@@ -725,7 +725,11 @@ libsystemd_shared_la_SOURCES = \
        src/shared/mkdir.c \
        src/shared/mkdir.h \
        src/shared/smack-util.c \
        src/shared/mkdir.c \
        src/shared/mkdir.h \
        src/shared/smack-util.c \
-       src/shared/smack-util.h
+       src/shared/smack-util.h \
+       src/shared/apparmor-util.c \
+       src/shared/apparmor-util.h \
+       src/shared/ima-util.c \
+       src/shared/ima-util.h
 
 #-------------------------------------------------------------------------------
 noinst_LTLIBRARIES += \
 
 #-------------------------------------------------------------------------------
 noinst_LTLIBRARIES += \
index 6c387450afdd5d2aea3d7798a55e17e0de3e5db7..c53d4066822cffd741412f85271cf8f294eaf329 100644 (file)
 #include <sys/statvfs.h>
 #include <fnmatch.h>
 
 #include <sys/statvfs.h>
 #include <fnmatch.h>
 
-#ifdef HAVE_SELINUX
-#include <selinux/selinux.h>
-#endif
-
 #include <systemd/sd-id128.h>
 #include "util.h"
 #include "condition.h"
 #include <systemd/sd-id128.h>
 #include "util.h"
 #include "condition.h"
 #include "path-util.h"
 #include "fileio.h"
 #include "unit.h"
 #include "path-util.h"
 #include "fileio.h"
 #include "unit.h"
+#include "smack-util.h"
+#include "apparmor-util.h"
+#include "ima-util.h"
+#include "selinux-util.h"
 
 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
         Condition *c;
 
 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
         Condition *c;
@@ -158,28 +158,15 @@ static bool test_virtualization(const char *parameter) {
         return v > 0 && streq(parameter, id);
 }
 
         return v > 0 && streq(parameter, id);
 }
 
-static bool test_apparmor_enabled(void) {
-        int r;
-        _cleanup_free_ char *p = NULL;
-
-        r = read_one_line_file("/sys/module/apparmor/parameters/enabled", &p);
-        if (r < 0)
-                return false;
-
-        return parse_boolean(p) > 0;
-}
-
 static bool test_security(const char *parameter) {
 static bool test_security(const char *parameter) {
-#ifdef HAVE_SELINUX
         if (streq(parameter, "selinux"))
         if (streq(parameter, "selinux"))
-                return is_selinux_enabled() > 0;
-#endif
+                return use_selinux();
         if (streq(parameter, "apparmor"))
         if (streq(parameter, "apparmor"))
-                return test_apparmor_enabled();
+                return use_apparmor();
         if (streq(parameter, "ima"))
         if (streq(parameter, "ima"))
-                return access("/sys/kernel/security/ima/", F_OK) == 0;
+                return use_ima();
         if (streq(parameter, "smack"))
         if (streq(parameter, "smack"))
-                return access("/sys/fs/smackfs", F_OK) == 0;
+                return use_smack();
         return false;
 }
 
         return false;
 }
 
index 6c0ac1a89864fefebff852f8e16de8990f434dda..345601f0aed052efd70458e2a5712caba1cac801 100644 (file)
@@ -775,12 +775,12 @@ static void socket_apply_socket_options(Socket *s, int fd) {
         }
 
 #ifdef HAVE_SMACK
         }
 
 #ifdef HAVE_SMACK
-        if (s->smack_ip_in)
+        if (s->smack_ip_in && use_smack())
                 if (fsetxattr(fd, "security.SMACK64IPIN", s->smack_ip_in, strlen(s->smack_ip_in), 0) < 0)
                         log_error_unit(UNIT(s)->id,
                                        "fsetxattr(\"security.SMACK64IPIN\"): %m");
 
                 if (fsetxattr(fd, "security.SMACK64IPIN", s->smack_ip_in, strlen(s->smack_ip_in), 0) < 0)
                         log_error_unit(UNIT(s)->id,
                                        "fsetxattr(\"security.SMACK64IPIN\"): %m");
 
-        if (s->smack_ip_out)
+        if (s->smack_ip_out && use_smack())
                 if (fsetxattr(fd, "security.SMACK64IPOUT", s->smack_ip_out, strlen(s->smack_ip_out), 0) < 0)
                         log_error_unit(UNIT(s)->id,
                                        "fsetxattr(\"security.SMACK64IPOUT\"): %m");
                 if (fsetxattr(fd, "security.SMACK64IPOUT", s->smack_ip_out, strlen(s->smack_ip_out), 0) < 0)
                         log_error_unit(UNIT(s)->id,
                                        "fsetxattr(\"security.SMACK64IPOUT\"): %m");
@@ -797,7 +797,7 @@ static void socket_apply_fifo_options(Socket *s, int fd) {
                                          "F_SETPIPE_SZ: %m");
 
 #ifdef HAVE_SMACK
                                          "F_SETPIPE_SZ: %m");
 
 #ifdef HAVE_SMACK
-        if (s->smack)
+        if (s->smack && use_smack())
                 if (fsetxattr(fd, "security.SMACK64", s->smack, strlen(s->smack), 0) < 0)
                         log_error_unit(UNIT(s)->id,
                                        "fsetxattr(\"security.SMACK64\"): %m");
                 if (fsetxattr(fd, "security.SMACK64", s->smack, strlen(s->smack), 0) < 0)
                         log_error_unit(UNIT(s)->id,
                                        "fsetxattr(\"security.SMACK64\"): %m");
index c50cf64f5ce26641795b3f27cd6db108e6d0e837..2c91cba16d81f1688671fc7f63b8f21ceed59488 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "socket-util.h"
 #include "path-util.h"
 
 #include "socket-util.h"
 #include "path-util.h"
+#include "selinux-util.h"
 #include "journald-server.h"
 #include "journald-native.h"
 #include "journald-kmsg.h"
 #include "journald-server.h"
 #include "journald-native.h"
 #include "journald-kmsg.h"
@@ -404,10 +405,12 @@ int server_open_native_socket(Server*s) {
         }
 
 #ifdef HAVE_SELINUX
         }
 
 #ifdef HAVE_SELINUX
-        one = 1;
-        r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
-        if (r < 0)
-                log_warning("SO_PASSSEC failed: %m");
+        if (use_selinux()) {
+                one = 1;
+                r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
+                if (r < 0)
+                        log_warning("SO_PASSSEC failed: %m");
+        }
 #endif
 
         one = 1;
 #endif
 
         one = 1;
index e03e413aeffba6fc337912d6d56bd77dfca5e9a4..9732e1b25ed4cec5d33eb6dc942b791d1e0de21f 100644 (file)
@@ -629,19 +629,21 @@ static void dispatch_message_real(
                 }
 
 #ifdef HAVE_SELINUX
                 }
 
 #ifdef HAVE_SELINUX
-                if (label) {
-                        x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
+                if (use_selinux()) {
+                        if (label) {
+                                x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
 
 
-                        *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
-                        IOVEC_SET_STRING(iovec[n++], x);
-                } else {
-                        security_context_t con;
+                                *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        } else {
+                                security_context_t con;
 
 
-                        if (getpidcon(ucred->pid, &con) >= 0) {
-                                x = strappenda("_SELINUX_CONTEXT=", con);
+                                if (getpidcon(ucred->pid, &con) >= 0) {
+                                        x = strappenda("_SELINUX_CONTEXT=", con);
 
 
-                                freecon(con);
-                                IOVEC_SET_STRING(iovec[n++], x);
+                                        freecon(con);
+                                        IOVEC_SET_STRING(iovec[n++], x);
+                                }
                         }
                 }
 #endif
                         }
                 }
 #endif
index 9c4efec9bcd4713f83285a0088ddbab259a149bb..543614aead0b676c69449c30e40662516d382b9d 100644 (file)
@@ -29,6 +29,7 @@
 #endif
 
 #include "socket-util.h"
 #endif
 
 #include "socket-util.h"
+#include "selinux-util.h"
 #include "journald-server.h"
 #include "journald-stream.h"
 #include "journald-syslog.h"
 #include "journald-server.h"
 #include "journald-stream.h"
 #include "journald-syslog.h"
@@ -381,8 +382,10 @@ int stdout_stream_new(Server *s) {
         }
 
 #ifdef HAVE_SELINUX
         }
 
 #ifdef HAVE_SELINUX
-        if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
-                log_error("Failed to determine peer security context: %m");
+        if (use_selinux()) {
+                if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
+                        log_error("Failed to determine peer security context: %m");
+        }
 #endif
 
         if (shutdown(fd, SHUT_WR) < 0) {
 #endif
 
         if (shutdown(fd, SHUT_WR) < 0) {
index c2770a53d0caf8791c4604cec44c7ad8cd1aa8f6..dc66ba8c8fa77ed580d21785582008ab87d9b548 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "systemd/sd-messages.h"
 #include "socket-util.h"
 
 #include "systemd/sd-messages.h"
 #include "socket-util.h"
+#include "selinux-util.h"
 #include "journald-server.h"
 #include "journald-syslog.h"
 #include "journald-kmsg.h"
 #include "journald-server.h"
 #include "journald-syslog.h"
 #include "journald-kmsg.h"
@@ -453,10 +454,12 @@ int server_open_syslog_socket(Server *s) {
         }
 
 #ifdef HAVE_SELINUX
         }
 
 #ifdef HAVE_SELINUX
-        one = 1;
-        r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
-        if (r < 0)
-                log_warning("SO_PASSSEC failed: %m");
+        if (use_selinux()) {
+                one = 1;
+                r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
+                if (r < 0)
+                        log_warning("SO_PASSSEC failed: %m");
+        }
 #endif
 
         one = 1;
 #endif
 
         one = 1;
diff --git a/src/shared/apparmor-util.c b/src/shared/apparmor-util.c
new file mode 100644 (file)
index 0000000..2b85da1
--- /dev/null
@@ -0,0 +1,41 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <unistd.h>
+
+#include "util.h"
+#include "fileio.h"
+#include "apparmor-util.h"
+
+static int use_apparmor_cached = -1;
+
+bool use_apparmor(void) {
+
+        if (use_apparmor_cached < 0) {
+                _cleanup_free_ char *p = NULL;
+
+                use_apparmor_cached =
+                        read_one_line_file("/sys/module/apparmor/parameters/enabled", &p) >= 0 &&
+                        parse_boolean(p) > 0;
+        }
+
+        return use_apparmor_cached;
+}
diff --git a/src/shared/apparmor-util.h b/src/shared/apparmor-util.h
new file mode 100644 (file)
index 0000000..4b056a1
--- /dev/null
@@ -0,0 +1,26 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+
+bool use_apparmor(void);
diff --git a/src/shared/ima-util.c b/src/shared/ima-util.c
new file mode 100644 (file)
index 0000000..f0d3c92
--- /dev/null
@@ -0,0 +1,35 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <unistd.h>
+
+#include "apparmor-util.h"
+
+
+static int use_ima_cached = -1;
+
+bool use_ima(void) {
+
+        if (use_ima_cached < 0)
+                use_ima_cached = access("/sys/kernel/security/ima/", F_OK) >= 0;
+
+        return use_ima_cached;
+}
diff --git a/src/shared/ima-util.h b/src/shared/ima-util.h
new file mode 100644 (file)
index 0000000..d382161
--- /dev/null
@@ -0,0 +1,26 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+
+bool use_ima(void);
index ff3375607fe5b93608e604cb156fec972bd1f896..026ae5a9e85b2f8e793f80e3a05fbf939d8134ec 100644 (file)
@@ -39,4 +39,13 @@ void retest_selinux(void) {
         use_selinux_cached = -1;
 }
 
         use_selinux_cached = -1;
 }
 
+#else
+
+bool use_selinux(void) {
+        return false;
+}
+
+void retest_selinux(void) {
+}
+
 #endif
 #endif
index a73eaac6de26559cce83a56020e4104be8d1f497..4e8cf796d38ffcc0d6ecc456bc69e31b259d41f9 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include "smack-util.h"
-
 #include <unistd.h>
 
 #include <unistd.h>
 
-static int use_smack_cached = -1;
+#include "smack-util.h"
 
 bool use_smack(void) {
 
 
 bool use_smack(void) {
 
+#ifdef HAVE_SMACK
+        static int use_smack_cached = -1;
+
         if (use_smack_cached < 0)
         if (use_smack_cached < 0)
-                use_smack_cached = (access("/sys/fs/smackfs", F_OK) >= 0);
+                use_smack_cached = access("/sys/fs/smackfs/", F_OK) >= 0;
 
         return use_smack_cached;
 
         return use_smack_cached;
+#else
+        return false;
+#endif
+
 }
 }
index 187e24e5b6f988ab66aa6258a4c9d73559367c72..c5d629d1ce1793127e845e0c96fb3202a3a4fb15 100644 (file)
@@ -32,6 +32,7 @@
 #include <attr/xattr.h>
 #endif
 
 #include <attr/xattr.h>
 #endif
 
+#include "smack-util.h"
 #include "udev.h"
 
 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
 #include "udev.h"
 
 static int node_symlink(struct udev_device *dev, const char *node, const char *slink)
@@ -311,7 +312,7 @@ static int node_permissions_apply(struct udev_device *dev, bool apply,
                                         log_debug("SECLABEL: set SELinux label '%s'", label);
 
 #ifdef HAVE_SMACK
                                         log_debug("SECLABEL: set SELinux label '%s'", label);
 
 #ifdef HAVE_SMACK
-                        } else if (streq(name, "smack")) {
+                        } else if (streq(name, "smack") && use_smack()) {
                                 smack = true;
                                 if (lsetxattr(devnode, "security.SMACK64", label, strlen(label), 0) < 0)
                                         log_error("SECLABEL: failed to set SMACK label '%s'", label);
                                 smack = true;
                                 if (lsetxattr(devnode, "security.SMACK64", label, strlen(label), 0) < 0)
                                         log_error("SECLABEL: failed to set SMACK label '%s'", label);
@@ -327,7 +328,7 @@ static int node_permissions_apply(struct udev_device *dev, bool apply,
                 if (!selinux)
                         label_fix(devnode, true, false);
 #ifdef HAVE_SMACK
                 if (!selinux)
                         label_fix(devnode, true, false);
 #ifdef HAVE_SMACK
-                if (!smack)
+                if (!smack && use_smack())
                         lremovexattr(devnode, "security.SMACK64");
 #endif
         }
                         lremovexattr(devnode, "security.SMACK64");
 #endif
         }