]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/smack-setup.c
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 "alloc-util.h"
32 #include "dirent-util.h"
37 #include "smack-setup.h"
38 #include "string-util.h"
43 static int write_access2_rules(const char* srcdir
) {
44 _cleanup_close_
int load2_fd
= -1, change_fd
= -1;
45 _cleanup_closedir_
DIR *dir
= NULL
;
51 load2_fd
= open("/sys/fs/smackfs/load2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
54 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/load2': %m");
55 return -errno
; /* negative error */
58 change_fd
= open("/sys/fs/smackfs/change-rule", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
61 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/change-rule': %m");
62 return -errno
; /* negative error */
65 /* write rules to load2 or change-rule from every file in the directory */
66 dir
= opendir(srcdir
);
69 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
70 return errno
; /* positive on purpose */
76 FOREACH_DIRENT(entry
, dir
, return 0) {
78 _cleanup_fclose_
FILE *policy
= NULL
;
80 if (!dirent_is_file(entry
))
83 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
87 log_warning_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
91 policy
= fdopen(fd
, "re");
96 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
100 /* load2 write rules in the kernel require a line buffered stream */
101 FOREACH_LINE(buf
, policy
,
102 log_error_errno(errno
, "Failed to read line from '%s': %m",
105 _cleanup_free_
char *sbj
= NULL
, *obj
= NULL
, *acc1
= NULL
, *acc2
= NULL
;
107 if (isempty(truncate_nl(buf
)))
110 /* if 3 args -> load rule : subject object access1 */
111 /* if 4 args -> change rule : subject object access1 access2 */
112 if (sscanf(buf
, "%ms %ms %ms %ms", &sbj
, &obj
, &acc1
, &acc2
) < 3) {
113 log_error_errno(errno
, "Failed to parse rule '%s' in '%s', ignoring.", buf
, entry
->d_name
);
117 if (write(isempty(acc2
) ? load2_fd
: change_fd
, buf
, strlen(buf
)) < 0) {
120 log_error_errno(errno
, "Failed to write '%s' to '%s' in '%s'",
121 buf
, isempty(acc2
) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry
->d_name
);
129 static int write_cipso2_rules(const char* srcdir
) {
130 _cleanup_close_
int cipso2_fd
= -1;
131 _cleanup_closedir_
DIR *dir
= NULL
;
132 struct dirent
*entry
;
137 cipso2_fd
= open("/sys/fs/smackfs/cipso2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
140 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/cipso2': %m");
141 return -errno
; /* negative error */
144 /* write rules to cipso2 from every file in the directory */
145 dir
= opendir(srcdir
);
148 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
149 return errno
; /* positive on purpose */
155 FOREACH_DIRENT(entry
, dir
, return 0) {
157 _cleanup_fclose_
FILE *policy
= NULL
;
159 if (!dirent_is_file(entry
))
162 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
166 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
170 policy
= fdopen(fd
, "re");
175 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
179 /* cipso2 write rules in the kernel require a line buffered stream */
180 FOREACH_LINE(buf
, policy
,
181 log_error_errno(errno
, "Failed to read line from '%s': %m",
184 if (isempty(truncate_nl(buf
)))
187 if (write(cipso2_fd
, buf
, strlen(buf
)) < 0) {
190 log_error_errno(errno
, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s'",
200 static int write_netlabel_rules(const char* srcdir
) {
201 _cleanup_fclose_
FILE *dst
= NULL
;
202 _cleanup_closedir_
DIR *dir
= NULL
;
203 struct dirent
*entry
;
208 dst
= fopen("/sys/fs/smackfs/netlabel", "we");
211 log_warning_errno(errno
, "Failed to open /sys/fs/smackfs/netlabel: %m");
212 return -errno
; /* negative error */
215 /* write rules to dst from every file in the directory */
216 dir
= opendir(srcdir
);
219 log_warning_errno(errno
, "Failed to opendir %s: %m", srcdir
);
220 return errno
; /* positive on purpose */
226 FOREACH_DIRENT(entry
, dir
, return 0) {
228 _cleanup_fclose_
FILE *policy
= NULL
;
230 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
234 log_warning_errno(errno
, "Failed to open %s: %m", entry
->d_name
);
238 policy
= fdopen(fd
, "re");
243 log_error_errno(errno
, "Failed to open %s: %m", entry
->d_name
);
247 /* load2 write rules in the kernel require a line buffered stream */
248 FOREACH_LINE(buf
, policy
,
249 log_error_errno(errno
, "Failed to read line from %s: %m",
251 if (!fputs(buf
, dst
)) {
254 log_error_errno(errno
, "Failed to write line to /sys/fs/smackfs/netlabel");
260 log_error_errno(errno
, "Failed to flush writes to /sys/fs/smackfs/netlabel: %m");
271 int mac_smack_setup(bool *loaded_policy
) {
277 assert(loaded_policy
);
279 r
= write_access2_rules("/etc/smack/accesses.d/");
282 log_debug("Smack is not enabled in the kernel.");
285 log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
288 log_info("Successfully loaded Smack policies.");
291 log_warning_errno(r
, "Failed to load Smack access rules, ignoring: %m");
295 #ifdef SMACK_RUN_LABEL
296 r
= write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL
, 0);
298 log_warning_errno(r
, "Failed to set SMACK label \"" SMACK_RUN_LABEL
"\" on self: %m");
299 r
= write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL
, 0);
301 log_warning_errno(r
, "Failed to set SMACK ambient label \"" SMACK_RUN_LABEL
"\": %m");
302 r
= write_string_file("/sys/fs/smackfs/netlabel",
303 "0.0.0.0/0 " SMACK_RUN_LABEL
, 0);
305 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL
"\": %m");
306 r
= write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", 0);
308 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
311 r
= write_cipso2_rules("/etc/smack/cipso.d/");
314 log_debug("Smack/CIPSO is not enabled in the kernel.");
317 log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
320 log_info("Successfully loaded Smack/CIPSO policies.");
323 log_warning_errno(r
, "Failed to load Smack/CIPSO access rules, ignoring: %m");
327 r
= write_netlabel_rules("/etc/smack/netlabel.d/");
330 log_debug("Smack/CIPSO is not enabled in the kernel.");
333 log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
336 log_info("Successfully loaded Smack network host rules.");
339 log_warning_errno(r
, "Failed to load Smack network host rules: %m, ignoring.");
343 *loaded_policy
= true;