1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2013 Intel Corporation
5 Nathaniel Chen <nathaniel.chen@intel.com>
14 #include "alloc-util.h"
15 #include "dirent-util.h"
20 #include "smack-setup.h"
21 #include "string-util.h"
26 static int fdopen_unlocked_at(int dfd
, const char *dir
, const char *name
, int *status
, FILE **ret_file
) {
30 fd
= openat(dfd
, name
, O_RDONLY
|O_CLOEXEC
);
35 return log_warning_errno(errno
, "Failed to open \"%s/%s\": %m", dir
, name
);
38 r
= fdopen_unlocked(fd
, "r", &f
);
44 return log_error_errno(r
, "Failed to open \"%s/%s\": %m", dir
, name
);
51 static int write_access2_rules(const char *srcdir
) {
52 _cleanup_close_
int load2_fd
= -1, change_fd
= -1;
53 _cleanup_closedir_
DIR *dir
= NULL
;
56 load2_fd
= open("/sys/fs/smackfs/load2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
59 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/load2': %m");
60 return -errno
; /* negative error */
63 change_fd
= open("/sys/fs/smackfs/change-rule", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
66 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/change-rule': %m");
67 return -errno
; /* negative error */
70 /* write rules to load2 or change-rule from every file in the directory */
71 dir
= opendir(srcdir
);
74 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
75 return errno
; /* positive on purpose */
81 FOREACH_DIRENT(entry
, dir
, return 0) {
82 _cleanup_fclose_
FILE *policy
= NULL
;
84 if (!dirent_is_file(entry
))
87 if (fdopen_unlocked_at(dfd
, srcdir
, entry
->d_name
, &r
, &policy
) < 0)
90 /* load2 write rules in the kernel require a line buffered stream */
92 _cleanup_free_
char *buf
= NULL
, *sbj
= NULL
, *obj
= NULL
, *acc1
= NULL
, *acc2
= NULL
;
95 q
= read_line(policy
, NAME_MAX
, &buf
);
97 return log_error_errno(q
, "Failed to read line from '%s': %m", entry
->d_name
);
101 if (isempty(buf
) || strchr(COMMENTS
, buf
[0]))
104 /* if 3 args -> load rule : subject object access1 */
105 /* if 4 args -> change rule : subject object access1 access2 */
106 if (sscanf(buf
, "%ms %ms %ms %ms", &sbj
, &obj
, &acc1
, &acc2
) < 3) {
107 log_error_errno(errno
, "Failed to parse rule '%s' in '%s', ignoring.", buf
, entry
->d_name
);
111 if (write(isempty(acc2
) ? load2_fd
: change_fd
, buf
, strlen(buf
)) < 0) {
114 log_error_errno(errno
, "Failed to write '%s' to '%s' in '%s': %m",
115 buf
, isempty(acc2
) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry
->d_name
);
123 static int write_cipso2_rules(const char *srcdir
) {
124 _cleanup_close_
int cipso2_fd
= -1;
125 _cleanup_closedir_
DIR *dir
= NULL
;
128 cipso2_fd
= open("/sys/fs/smackfs/cipso2", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
131 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/cipso2': %m");
132 return -errno
; /* negative error */
135 /* write rules to cipso2 from every file in the directory */
136 dir
= opendir(srcdir
);
139 log_warning_errno(errno
, "Failed to opendir '%s': %m", srcdir
);
140 return errno
; /* positive on purpose */
146 FOREACH_DIRENT(entry
, dir
, return 0) {
147 _cleanup_fclose_
FILE *policy
= NULL
;
149 if (!dirent_is_file(entry
))
152 if (fdopen_unlocked_at(dfd
, srcdir
, entry
->d_name
, &r
, &policy
) < 0)
155 /* cipso2 write rules in the kernel require a line buffered stream */
157 _cleanup_free_
char *buf
= NULL
;
160 q
= read_line(policy
, NAME_MAX
, &buf
);
162 return log_error_errno(q
, "Failed to read line from '%s': %m", entry
->d_name
);
166 if (isempty(buf
) || strchr(COMMENTS
, buf
[0]))
169 if (write(cipso2_fd
, buf
, strlen(buf
)) < 0) {
172 log_error_errno(errno
, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s': %m",
182 static int write_netlabel_rules(const char *srcdir
) {
183 _cleanup_fclose_
FILE *dst
= NULL
;
184 _cleanup_closedir_
DIR *dir
= NULL
;
187 dst
= fopen("/sys/fs/smackfs/netlabel", "we");
190 log_warning_errno(errno
, "Failed to open /sys/fs/smackfs/netlabel: %m");
191 return -errno
; /* negative error */
194 /* write rules to dst from every file in the directory */
195 dir
= opendir(srcdir
);
198 log_warning_errno(errno
, "Failed to opendir %s: %m", srcdir
);
199 return errno
; /* positive on purpose */
205 FOREACH_DIRENT(entry
, dir
, return 0) {
206 _cleanup_fclose_
FILE *policy
= NULL
;
208 if (fdopen_unlocked_at(dfd
, srcdir
, entry
->d_name
, &r
, &policy
) < 0)
211 /* load2 write rules in the kernel require a line buffered stream */
213 _cleanup_free_
char *buf
= NULL
;
216 q
= read_line(policy
, NAME_MAX
, &buf
);
218 return log_error_errno(q
, "Failed to read line from %s: %m", entry
->d_name
);
222 if (!fputs(buf
, dst
)) {
225 log_error_errno(errno
, "Failed to write line to /sys/fs/smackfs/netlabel: %m");
228 q
= fflush_and_check(dst
);
232 log_error_errno(q
, "Failed to flush writes to /sys/fs/smackfs/netlabel: %m");
241 static int write_onlycap_list(void) {
242 _cleanup_close_
int onlycap_fd
= -1;
243 _cleanup_free_
char *list
= NULL
;
244 _cleanup_fclose_
FILE *f
= NULL
;
248 f
= fopen("/etc/smack/onlycap", "re");
251 log_warning_errno(errno
, "Failed to read '/etc/smack/onlycap': %m");
253 return errno
== ENOENT
? ENOENT
: -errno
;
257 _cleanup_free_
char *buf
= NULL
;
260 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
262 return log_error_errno(r
, "Failed to read line from /etc/smack/onlycap: %m");
266 if (isempty(buf
) || strchr(COMMENTS
, *buf
))
270 if (!GREEDY_REALLOC(list
, len
+ l
+ 1))
273 stpcpy(list
+ len
, buf
)[0] = ' ';
282 onlycap_fd
= open("/sys/fs/smackfs/onlycap", O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
283 if (onlycap_fd
< 0) {
285 log_warning_errno(errno
, "Failed to open '/sys/fs/smackfs/onlycap': %m");
286 return -errno
; /* negative error */
289 r
= write(onlycap_fd
, list
, len
);
291 return log_error_errno(errno
, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list
);
298 int mac_smack_setup(bool *loaded_policy
) {
304 assert(loaded_policy
);
306 r
= write_access2_rules("/etc/smack/accesses.d/");
309 log_debug("Smack is not enabled in the kernel.");
312 log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
315 log_info("Successfully loaded Smack policies.");
318 log_warning_errno(r
, "Failed to load Smack access rules, ignoring: %m");
322 #if HAVE_SMACK_RUN_LABEL
323 r
= write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL
, WRITE_STRING_FILE_DISABLE_BUFFER
);
325 log_warning_errno(r
, "Failed to set SMACK label \"" SMACK_RUN_LABEL
"\" on self: %m");
326 r
= write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL
, WRITE_STRING_FILE_DISABLE_BUFFER
);
328 log_warning_errno(r
, "Failed to set SMACK ambient label \"" SMACK_RUN_LABEL
"\": %m");
329 r
= write_string_file("/sys/fs/smackfs/netlabel",
330 "0.0.0.0/0 " SMACK_RUN_LABEL
, WRITE_STRING_FILE_DISABLE_BUFFER
);
332 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL
"\": %m");
333 r
= write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", WRITE_STRING_FILE_DISABLE_BUFFER
);
335 log_warning_errno(r
, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
338 r
= write_cipso2_rules("/etc/smack/cipso.d/");
341 log_debug("Smack/CIPSO is not enabled in the kernel.");
344 log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
347 log_info("Successfully loaded Smack/CIPSO policies.");
350 log_warning_errno(r
, "Failed to load Smack/CIPSO access rules, ignoring: %m");
354 r
= write_netlabel_rules("/etc/smack/netlabel.d/");
357 log_debug("Smack/CIPSO is not enabled in the kernel.");
360 log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
363 log_info("Successfully loaded Smack network host rules.");
366 log_warning_errno(r
, "Failed to load Smack network host rules: %m, ignoring.");
370 r
= write_onlycap_list();
373 log_debug("Smack is not enabled in the kernel.");
376 log_debug("Smack onlycap list file '/etc/smack/onlycap' not found");
379 log_info("Successfully wrote Smack onlycap list.");
382 return log_emergency_errno(r
, "Failed to write Smack onlycap list: %m");
385 *loaded_policy
= true;