[systemd-devel] [PATCH] mount and initialize Smack

Nathaniel Chen nathaniel.chen at intel.com
Thu Mar 7 11:06:58 PST 2013


SMACK is the Simple Mandatory Access Control Kernel, a minimal
approach to Access Control implemented as a kernel LSM.

The kernel exposes the smackfs filesystem API through which access
rules can be loaded. At boot time, we want to load the access rules
as early as possible to ensure all early boot steps are checked by Smack.

This patch mounts smackfs at the new location at /sys/fs/smackfs for
kernels 3.8 and above. The /smack mountpoint is not supported.
After mounting smackfs, rules are loaded from the usual location.

For more information about Smack see:
  http://www.kernel.org/doc/Documentation/security/Smack.txt
---
 Makefile.am            |  2 ++
 README                 |  2 ++
 src/core/main.c        |  3 ++
 src/core/mount-setup.c |  4 ++-
 src/core/smack-setup.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/core/smack-setup.h | 26 ++++++++++++++
 6 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 src/core/smack-setup.c
 create mode 100644 src/core/smack-setup.h

diff --git a/Makefile.am b/Makefile.am
index 7e40879..3f64937 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -842,6 +842,8 @@ libsystemd_core_la_SOURCES = \
 	src/core/selinux-access.h \
 	src/core/selinux-setup.c \
 	src/core/selinux-setup.h \
+	src/core/smack-setup.c \
+	src/core/smack-setup.h \
 	src/core/ima-setup.c \
 	src/core/ima-setup.h \
 	src/core/locale-setup.h \
diff --git a/README b/README
index 5649a51..6a70b47 100644
--- a/README
+++ b/README
@@ -44,6 +44,8 @@ REQUIREMENTS:
           CONFIG_NET
           CONFIG_SYSFS
 
+	Linux kernel >= 3.8 for Smack support
+
         Udev will fail to work with the legacy layout:
           CONFIG_SYSFS_DEPRECATED=n
 
diff --git a/src/core/main.c b/src/core/main.c
index c4a3075..b7f7faa 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -67,6 +67,7 @@
 #include "selinux-setup.h"
 #include "ima-setup.h"
 #include "fileio.h"
+#include "smack-setup.h"
 
 static enum {
         ACTION_RUN,
@@ -1351,6 +1352,8 @@ int main(int argc, char *argv[]) {
                                 goto finish;
                         if (ima_setup() < 0)
                                 goto finish;
+                        if (smack_setup() < 0)
+                                goto finish;
                 }
 
                 if (label_init(NULL) < 0)
diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c
index dab3601..42cdc6d 100644
--- a/src/core/mount-setup.c
+++ b/src/core/mount-setup.c
@@ -66,7 +66,7 @@ typedef struct MountPoint {
 /* The first three entries we might need before SELinux is up. The
  * fourth (securityfs) is needed by IMA to load a custom policy. The
  * other ones we can delay until SELinux and IMA are loaded. */
-#define N_EARLY_MOUNT 4
+#define N_EARLY_MOUNT 5
 
 static const MountPoint mount_table[] = {
         { "proc",       "/proc",                     "proc",       NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
@@ -77,6 +77,8 @@ static const MountPoint mount_table[] = {
           NULL,       MNT_FATAL|MNT_IN_CONTAINER },
         { "securityfs", "/sys/kernel/security",      "securityfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
           NULL,       MNT_NONE },
+        { "smackfs",    "/sys/fs/smackfs",           "smackfs",    "smackfsdef=*", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
+          NULL,       MNT_NONE },
         { "tmpfs",      "/dev/shm",                  "tmpfs",      "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
           NULL,       MNT_FATAL|MNT_IN_CONTAINER },
         { "devpts",     "/dev/pts",                  "devpts",     "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC,
diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c
new file mode 100644
index 0000000..7577ecd
--- /dev/null
+++ b/src/core/smack-setup.c
@@ -0,0 +1,93 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2013 Intel Corporation
+  Authors:
+        Nathaniel Chen <nathaniel.chen at intel.com>
+
+  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 <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/vfs.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/mount.h>
+#include <stdint.h>
+
+#include "macro.h"
+#include "smack-setup.h"
+#include "util.h"
+#include "log.h"
+#include "label.h"
+
+#define ACCESSES_D_PATH "/etc/smack/accesses.d/"
+
+int smack_setup(void) {
+
+        FILE _cleanup_fclose_ *smack = NULL;
+        FILE _cleanup_fclose_ *policy = NULL;
+        int _cleanup_close_ dfd = -1;
+        int _cleanup_close_ pol = -1;
+        DIR *dir = NULL;
+        struct dirent *entry;
+        char buf[NAME_MAX];
+
+        smack = fopen("/sys/fs/smackfs/load2", "we");
+        if (smack == NULL)  {
+                log_info("Smack is not enabled in the kernel, not loading access rules.");
+                return 0;
+        }
+
+        /* write rules to load2 from every file in the directory */
+        dir = opendir(ACCESSES_D_PATH);
+        if (dir == NULL) {
+                log_info("Smack access rules directory not found: %s", ACCESSES_D_PATH);
+                return 0;
+        }
+        dfd = dirfd(dir);
+        if (dfd == -1) {
+                log_error("Smack access rules directory %s not opened: %m.", ACCESSES_D_PATH);
+                return 0;
+        }
+        FOREACH_DIRENT(entry, dir, return 0;) {
+                if (entry->d_name[0] == '.')
+                        continue;
+                pol = openat(dfd, entry->d_name, O_RDONLY);
+                if (pol == -1) {
+                        log_error("Smack access rule file %s not opened: %m", entry->d_name);
+                        continue;
+                }
+                policy = fdopen(pol, "re");
+                if (policy == NULL) {
+                        log_error("Smack access rule file %s not opened: %m", entry->d_name);
+                        continue;
+                }
+                /* load2 write rules in the kernel require a line buffered stream */
+                FOREACH_LINE(buf, policy, log_error("Failed to read from Smack access rule file %s: %m", policy); continue;) {
+                        fputs(buf, smack);
+                        fflush(smack);
+                }
+        }
+        log_info("Successfully loaded Smack policies.");
+
+return 0;
+}
diff --git a/src/core/smack-setup.h b/src/core/smack-setup.h
new file mode 100644
index 0000000..ffe9184
--- /dev/null
+++ b/src/core/smack-setup.h
@@ -0,0 +1,26 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2013 Intel Corporation
+  Authors:
+        Nathaniel Chen <nathaniel.chen at intel.com>
+
+  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/>.
+***/
+
+int smack_setup(void);
-- 
1.8.1.4



More information about the systemd-devel mailing list