1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2013 Intel Corporation
8 Nathaniel Chen <nathaniel.chen@intel.com>
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2.1 of the License,
13 or (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/types.h>
33 #include <sys/mount.h>
37 #include "smack-setup.h"
43 #define SMACK_CONFIG "/etc/smack/accesses.d/"
44 #define CIPSO_CONFIG "/etc/smack/cipso.d/"
48 static int write_rules(const char* dstpath, const char* srcdir) {
49 _cleanup_fclose_ FILE *dst = NULL;
50 _cleanup_closedir_ DIR *dir = NULL;
56 dst = fopen(dstpath, "we");
59 log_warning("Failed to open %s: %m", dstpath);
60 return -errno; /* negative error */
63 /* write rules to dst from every file in the directory */
64 dir = opendir(srcdir);
67 log_warning("Failed to opendir %s: %m", srcdir);
68 return errno; /* positive on purpose */
74 FOREACH_DIRENT(entry, dir, return 0) {
76 _cleanup_fclose_ FILE *policy = NULL;
78 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
82 log_warning("Failed to open %s: %m", entry->d_name);
86 policy = fdopen(fd, "re");
90 close_nointr_nofail(fd);
91 log_error("Failed to open %s: %m", entry->d_name);
95 /* load2 write rules in the kernel require a line buffered stream */
96 FOREACH_LINE(buf, policy,
97 log_error("Failed to read line from %s: %m",
99 if (!fputs(buf, dst)) {
102 log_error("Failed to write line to %s", dstpath);
108 log_error("Failed to flush writes to %s: %m", dstpath);
119 int smack_setup(void) {
125 r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
128 log_debug("Smack is not enabled in the kernel.");
131 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
134 log_info("Successfully loaded Smack policies.");
137 log_warning("Failed to load Smack access rules: %s, ignoring.",
142 #ifdef SMACK_RUN_LABEL
143 r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
145 log_warning("Failed to set SMACK label \"%s\" on self: %s",
146 SMACK_RUN_LABEL, strerror(-r));
149 r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
152 log_debug("Smack/CIPSO is not enabled in the kernel.");
155 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
158 log_info("Successfully loaded Smack/CIPSO policies.");
161 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",