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"
17 #include "dirent-util.h"
22 #include "smack-setup.h"
23 #include "string-util.h"
28 static int write_access2_rules(const char* srcdir
) {
29 _cleanup_close_
int load2_fd
= -1, change_fd
= -1;
30 _cleanup_closedir_
DIR *dir
= NULL
;
35 load2_fd
= open("/sys/fs/smackfs/load2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
38 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/load2': %m");
39 return -errno
; /* negative error */
42 change_fd
= open("/sys/fs/smackfs/change-rule", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
45 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/change-rule': %m");
46 return -errno
; /* negative error */
49 /* write rules to load2 or change-rule from every file in the directory */
50 dir
= opendir(srcdir
);
53 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
54 return errno
; /* positive on purpose */
60 FOREACH_DIRENT(entry
, dir
, return 0) {
62 _cleanup_fclose_
FILE *policy
= NULL
;
64 if (!dirent_is_file(entry
))
67 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
71 log_warning_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
75 policy
= fdopen(fd
, "r");
80 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
84 /* load2 write rules in the kernel require a line buffered stream */
86 _cleanup_free_
char *buf
= NULL
, *sbj
= NULL
, *obj
= NULL
, *acc1
= NULL
, *acc2
= NULL
;
89 q
= read_line(policy
, NAME_MAX
, &buf
);
91 return log_error_errno(q
, "Failed to read line from '%s': %m", entry
->d_name
);
95 if (isempty(buf
) || strchr(COMMENTS
, buf
[0]))
98 /* if 3 args -> load rule : subject object access1 */
99 /* if 4 args -> change rule : subject object access1 access2 */
100 if (sscanf(buf
, "%ms %ms %ms %ms", &sbj
, &obj
, &acc1
, &acc2
) < 3) {
101 log_error_errno(errno
, "Failed to parse rule '%s' in '%s', ignoring.", buf
, entry
->d_name
);
105 if (write(isempty(acc2
) ? load2_fd
: change_fd
, buf
, strlen(buf
)) < 0) {
108 log_error_errno(errno
, "Failed to write '%s' to '%s' in '%s': %m",
109 buf
, isempty(acc2
) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry
->d_name
);
117 static int write_cipso2_rules(const char* srcdir
) {
118 _cleanup_close_
int cipso2_fd
= -1;
119 _cleanup_closedir_
DIR *dir
= NULL
;
120 struct dirent
*entry
;
124 cipso2_fd
= open("/sys/fs/smackfs/cipso2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
127 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/cipso2': %m");
128 return -errno
; /* negative error */
131 /* write rules to cipso2 from every file in the directory */
132 dir
= opendir(srcdir
);
135 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
136 return errno
; /* positive on purpose */
142 FOREACH_DIRENT(entry
, dir
, return 0) {
144 _cleanup_fclose_
FILE *policy
= NULL
;
146 if (!dirent_is_file(entry
))
149 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
153 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
157 policy
= fdopen(fd
, "r");
162 log_error_errno(errno
, "Failed to open '%s': %m", entry
->d_name
);
166 /* cipso2 write rules in the kernel require a line buffered stream */
168 _cleanup_free_
char *buf
= NULL
;
171 q
= read_line(policy
, NAME_MAX
, &buf
);
173 return log_error_errno(q
, "Failed to read line from '%s': %m", entry
->d_name
);
177 if (isempty(buf
) || strchr(COMMENTS
, buf
[0]))
180 if (write(cipso2_fd
, buf
, strlen(buf
)) < 0) {
183 log_error_errno(errno
, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s': %m",
193 static int write_netlabel_rules(const char* srcdir
) {
194 _cleanup_fclose_
FILE *dst
= NULL
;
195 _cleanup_closedir_
DIR *dir
= NULL
;
196 struct dirent
*entry
;
200 dst
= fopen("/sys/fs/smackfs/netlabel", "we");
203 log_warning_errno(errno
, "Failed to open /sys/fs/smackfs/netlabel: %m");
204 return -errno
; /* negative error */
207 /* write rules to dst from every file in the directory */
208 dir
= opendir(srcdir
);
211 log_warning_errno(errno
, "Failed to opendir %s: %m", srcdir
);
212 return errno
; /* positive on purpose */
218 FOREACH_DIRENT(entry
, dir
, return 0) {
220 _cleanup_fclose_
FILE *policy
= NULL
;
222 fd
= openat(dfd
, entry
->d_name
, O_RDONLY
|O_CLOEXEC
);
226 log_warning_errno(errno
, "Failed to open %s: %m", entry
->d_name
);
230 policy
= fdopen(fd
, "r");
235 log_error_errno(errno
, "Failed to open %s: %m", entry
->d_name
);
239 (void) __fsetlocking(policy
, FSETLOCKING_BYCALLER
);
241 /* load2 write rules in the kernel require a line buffered stream */
243 _cleanup_free_
char *buf
= NULL
;
246 q
= read_line(policy
, NAME_MAX
, &buf
);
248 return log_error_errno(q
, "Failed to read line from %s: %m", entry
->d_name
);
252 if (!fputs(buf
, dst
)) {
255 log_error_errno(errno
, "Failed to write line to /sys/fs/smackfs/netlabel: %m");
258 q
= fflush_and_check(dst
);
262 log_error_errno(q
, "Failed to flush writes to /sys/fs/smackfs/netlabel: %m");
271 static int write_onlycap_list(void) {
272 _cleanup_close_
int onlycap_fd
= -1;
273 _cleanup_free_
char *list
= NULL
;
274 _cleanup_fclose_
FILE *f
= NULL
;
275 size_t len
= 0, allocated
= 0;
278 f
= fopen("/etc/smack/onlycap", "re");
281 log_warning_errno(errno
, "Failed to read '/etc/smack/onlycap': %m");
283 return errno
== ENOENT
? ENOENT
: -errno
;
287 _cleanup_free_
char *buf
= NULL
;
290 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
292 return log_error_errno(r
, "Failed to read line from /etc/smack/onlycap: %m");
296 if (isempty(buf
) || strchr(COMMENTS
, *buf
))
300 if (!GREEDY_REALLOC(list
, allocated
, len
+ l
+ 1))
303 stpcpy(list
+ len
, buf
)[0] = ' ';
312 onlycap_fd
= open("/sys/fs/smackfs/onlycap", O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
313 if (onlycap_fd
< 0) {
315 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/onlycap': %m");
316 return -errno
; /* negative error */
319 r
= write(onlycap_fd
, list
, len
);
321 return log_error_errno(errno
, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list
);
328 int mac_smack_setup(bool *loaded_policy
) {
334 assert(loaded_policy
);
336 r
= write_access2_rules("/etc/smack/accesses.d/");
339 log_debug("Smack is not enabled in the kernel.");
342 log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
345 log_info("Successfully loaded Smack policies.");
348 log_warning_errno(r
, "Failed to load Smack access rules, ignoring: %m");
352 #ifdef SMACK_RUN_LABEL
353 r
= write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL
, WRITE_STRING_FILE_DISABLE_BUFFER
);
355 log_warning_errno(r
, "Failed to set SMACK label \"" SMACK_RUN_LABEL
"\" on self: %m");
356 r
= write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL
, WRITE_STRING_FILE_DISABLE_BUFFER
);
358 log_warning_errno(r
, "Failed to set SMACK ambient label \"" SMACK_RUN_LABEL
"\": %m");
359 r
= write_string_file("/sys/fs/smackfs/netlabel",
360 "0.0.0.0/0 " SMACK_RUN_LABEL
, WRITE_STRING_FILE_DISABLE_BUFFER
);
362 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL
"\": %m");
363 r
= write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", WRITE_STRING_FILE_DISABLE_BUFFER
);
365 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
368 r
= write_cipso2_rules("/etc/smack/cipso.d/");
371 log_debug("Smack/CIPSO is not enabled in the kernel.");
374 log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
377 log_info("Successfully loaded Smack/CIPSO policies.");
380 log_warning_errno(r
, "Failed to load Smack/CIPSO access rules, ignoring: %m");
384 r
= write_netlabel_rules("/etc/smack/netlabel.d/");
387 log_debug("Smack/CIPSO is not enabled in the kernel.");
390 log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
393 log_info("Successfully loaded Smack network host rules.");
396 log_warning_errno(r
, "Failed to load Smack network host rules: %m, ignoring.");
400 r
= write_onlycap_list();
403 log_debug("Smack is not enabled in the kernel.");
406 log_debug("Smack onlycap list file '/etc/smack/onlycap' not found");
409 log_info("Successfully wrote Smack onlycap list.");
412 log_emergency_errno(r
, "Failed to write Smack onlycap list: %m");
416 *loaded_policy
= true;