]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/smack-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Intel Corporation
5 Nathaniel Chen <nathaniel.chen@intel.com>
12 #include <stdio_ext.h>
16 #include "alloc-util.h"
18 #include "dirent-util.h"
23 #include "smack-setup.h"
24 #include "string-util.h"
29 static int write_access2_rules(const char* srcdir
) {
30 _cleanup_close_
int load2_fd
= -1, change_fd
= -1;
31 _cleanup_closedir_
DIR *dir
= NULL
;
36 load2_fd
= open("/sys/fs/smackfs/load2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
39 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/load2': %m");
40 return -errno
; /* negative error */
43 change_fd
= open("/sys/fs/smackfs/change-rule", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
46 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/change-rule': %m");
47 return -errno
; /* negative error */
50 /* write rules to load2 or change-rule from every file in the directory */
51 dir
= opendir(srcdir
);
54 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
55 return errno
; /* positive on purpose */
61 FOREACH_DIRENT(entry
, dir
, return 0) {
63 _cleanup_fclose_
FILE *policy
= NULL
;
65 if (!dirent_is_file(entry
))
68 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
72 log_warning_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
76 policy
= fdopen(fd
, "re");
81 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
85 /* load2 write rules in the kernel require a line buffered stream */
87 _cleanup_free_
char *buf
= NULL
, *sbj
= NULL
, *obj
= NULL
, *acc1
= NULL
, *acc2
= NULL
;
90 q
= read_line(policy
, NAME_MAX
, &buf
);
92 return log_error_errno(q
, "Failed to read line from '%s': %m", entry
->d_name
);
96 if (isempty(buf
) || strchr(COMMENTS
, buf
[0]))
99 /* if 3 args -> load rule : subject object access1 */
100 /* if 4 args -> change rule : subject object access1 access2 */
101 if (sscanf(buf
, "%ms %ms %ms %ms", &sbj
, &obj
, &acc1
, &acc2
) < 3) {
102 log_error_errno(errno
, "Failed to parse rule '%s' in '%s', ignoring.", buf
, entry
->d_name
);
106 if (write(isempty(acc2
) ? load2_fd
: change_fd
, buf
, strlen(buf
)) < 0) {
109 log_error_errno(errno
, "Failed to write '%s' to '%s' in '%s'",
110 buf
, isempty(acc2
) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry
->d_name
);
118 static int write_cipso2_rules(const char* srcdir
) {
119 _cleanup_close_
int cipso2_fd
= -1;
120 _cleanup_closedir_
DIR *dir
= NULL
;
121 struct dirent
*entry
;
125 cipso2_fd
= open("/sys/fs/smackfs/cipso2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
128 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/cipso2': %m");
129 return -errno
; /* negative error */
132 /* write rules to cipso2 from every file in the directory */
133 dir
= opendir(srcdir
);
136 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
137 return errno
; /* positive on purpose */
143 FOREACH_DIRENT(entry
, dir
, return 0) {
145 _cleanup_fclose_
FILE *policy
= NULL
;
147 if (!dirent_is_file(entry
))
150 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
154 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
158 policy
= fdopen(fd
, "re");
163 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
167 /* cipso2 write rules in the kernel require a line buffered stream */
169 _cleanup_free_
char *buf
= NULL
;
172 q
= read_line(policy
, NAME_MAX
, &buf
);
174 return log_error_errno(q
, "Failed to read line from '%s': %m", entry
->d_name
);
178 if (isempty(buf
) || strchr(COMMENTS
, buf
[0]))
181 if (write(cipso2_fd
, buf
, strlen(buf
)) < 0) {
184 log_error_errno(errno
, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s'",
194 static int write_netlabel_rules(const char* srcdir
) {
195 _cleanup_fclose_
FILE *dst
= NULL
;
196 _cleanup_closedir_
DIR *dir
= NULL
;
197 struct dirent
*entry
;
201 dst
= fopen("/sys/fs/smackfs/netlabel", "we");
204 log_warning_errno(errno
, "Failed to open /sys/fs/smackfs/netlabel: %m");
205 return -errno
; /* negative error */
208 /* write rules to dst from every file in the directory */
209 dir
= opendir(srcdir
);
212 log_warning_errno(errno
, "Failed to opendir %s: %m", srcdir
);
213 return errno
; /* positive on purpose */
219 FOREACH_DIRENT(entry
, dir
, return 0) {
221 _cleanup_fclose_
FILE *policy
= NULL
;
223 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
227 log_warning_errno(errno
, "Failed to open %s: %m", entry
->d_name
);
231 policy
= fdopen(fd
, "re");
236 log_error_errno(errno
, "Failed to open %s: %m", entry
->d_name
);
240 (void) __fsetlocking(policy
, FSETLOCKING_BYCALLER
);
242 /* load2 write rules in the kernel require a line buffered stream */
244 _cleanup_free_
char *buf
= NULL
;
247 q
= read_line(policy
, NAME_MAX
, &buf
);
249 return log_error_errno(q
, "Failed to read line from %s: %m", entry
->d_name
);
253 if (!fputs(buf
, dst
)) {
256 log_error_errno(errno
, "Failed to write line to /sys/fs/smackfs/netlabel");
259 q
= fflush_and_check(dst
);
263 log_error_errno(q
, "Failed to flush writes to /sys/fs/smackfs/netlabel: %m");
272 static int write_onlycap_list(void) {
273 _cleanup_close_
int onlycap_fd
= -1;
274 _cleanup_free_
char *list
= NULL
;
275 _cleanup_fclose_
FILE *f
= NULL
;
276 size_t len
= 0, allocated
= 0;
279 f
= fopen("/etc/smack/onlycap", "re");
282 log_warning_errno(errno
, "Failed to read '/etc/smack/onlycap': %m");
284 return errno
== ENOENT
? ENOENT
: -errno
;
288 _cleanup_free_
char *buf
= NULL
;
291 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
293 return log_error_errno(r
, "Failed to read line from /etc/smack/onlycap: %m");
297 if (isempty(buf
) || strchr(COMMENTS
, *buf
))
301 if (!GREEDY_REALLOC(list
, allocated
, len
+ l
+ 1))
304 stpcpy(list
+ len
, buf
)[0] = ' ';
313 onlycap_fd
= open("/sys/fs/smackfs/onlycap", O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
314 if (onlycap_fd
< 0) {
316 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/onlycap': %m");
317 return -errno
; /* negative error */
320 r
= write(onlycap_fd
, list
, len
);
322 return log_error_errno(errno
, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list
);
329 int mac_smack_setup(bool *loaded_policy
) {
335 assert(loaded_policy
);
337 r
= write_access2_rules("/etc/smack/accesses.d/");
340 log_debug("Smack is not enabled in the kernel.");
343 log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
346 log_info("Successfully loaded Smack policies.");
349 log_warning_errno(r
, "Failed to load Smack access rules, ignoring: %m");
353 #ifdef SMACK_RUN_LABEL
354 r
= write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL
, 0);
356 log_warning_errno(r
, "Failed to set SMACK label \"" SMACK_RUN_LABEL
"\" on self: %m");
357 r
= write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL
, 0);
359 log_warning_errno(r
, "Failed to set SMACK ambient label \"" SMACK_RUN_LABEL
"\": %m");
360 r
= write_string_file("/sys/fs/smackfs/netlabel",
361 "0.0.0.0/0 " SMACK_RUN_LABEL
, 0);
363 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL
"\": %m");
364 r
= write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", 0);
366 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
369 r
= write_cipso2_rules("/etc/smack/cipso.d/");
372 log_debug("Smack/CIPSO is not enabled in the kernel.");
375 log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
378 log_info("Successfully loaded Smack/CIPSO policies.");
381 log_warning_errno(r
, "Failed to load Smack/CIPSO access rules, ignoring: %m");
385 r
= write_netlabel_rules("/etc/smack/netlabel.d/");
388 log_debug("Smack/CIPSO is not enabled in the kernel.");
391 log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
394 log_info("Successfully loaded Smack network host rules.");
397 log_warning_errno(r
, "Failed to load Smack network host rules: %m, ignoring.");
401 r
= write_onlycap_list();
404 log_debug("Smack is not enabled in the kernel.");
407 log_debug("Smack onlycap list file '/etc/smack/onlycap' not found");
410 log_info("Successfully wrote Smack onlycap list.");
413 log_emergency_errno(r
, "Failed to write Smack onlycap list.");
417 *loaded_policy
= true;