]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/smack-setup.c
Merge pull request #19312 from yuwata/udev-escape-slash-nvme
[thirdparty/systemd.git] / src / core / smack-setup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
ffbd2c4d 2/***
810adae9 3 Copyright © 2013 Intel Corporation
ffbd2c4d
NC
4 Authors:
5 Nathaniel Chen <nathaniel.chen@intel.com>
ffbd2c4d
NC
6***/
7
ffbd2c4d 8#include <errno.h>
ffbd2c4d 9#include <fcntl.h>
07630cea
LP
10#include <stdio.h>
11#include <stdlib.h>
ca78ad1d 12#include <unistd.h>
ffbd2c4d 13
b5efdb8a 14#include "alloc-util.h"
a0956174 15#include "dirent-util.h"
3ffd4af2 16#include "fd-util.h"
8b197c3a 17#include "fileio.h"
ffbd2c4d 18#include "log.h"
07630cea 19#include "macro.h"
3ffd4af2 20#include "smack-setup.h"
07630cea
LP
21#include "string-util.h"
22#include "util.h"
ffbd2c4d 23
f9fa32f0 24#if ENABLE_SMACK
2b3e18de 25
b636d78a
ZJS
26static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *status, FILE **ret_file) {
27 int fd, r;
28 FILE *f;
29
30 fd = openat(dfd, name, O_RDONLY|O_CLOEXEC);
31 if (fd < 0) {
32 if (*status == 0)
33 *status = -errno;
34
35 return log_warning_errno(errno, "Failed to open \"%s/%s\": %m", dir, name);
36 }
37
38 r = fdopen_unlocked(fd, "r", &f);
39 if (r < 0) {
40 if (*status == 0)
41 *status = r;
42
43 safe_close(fd);
44 return log_error_errno(r, "Failed to open \"%s/%s\": %m", dir, name);
45 }
46
47 *ret_file = f;
48 return 0;
49}
50
51static int write_access2_rules(const char *srcdir) {
6656aefb 52 _cleanup_close_ int load2_fd = -1, change_fd = -1;
ffbd2c4d
NC
53 _cleanup_closedir_ DIR *dir = NULL;
54 struct dirent *entry;
b636d78a 55 int dfd = -1, r = 0;
ffbd2c4d 56
6656aefb
WC
57 load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
58 if (load2_fd < 0) {
59 if (errno != ENOENT)
60 log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/load2': %m");
61 return -errno; /* negative error */
62 }
63
64 change_fd = open("/sys/fs/smackfs/change-rule", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
65 if (change_fd < 0) {
a4783bd1 66 if (errno != ENOENT)
6656aefb 67 log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/change-rule': %m");
a4783bd1 68 return -errno; /* negative error */
ffbd2c4d
NC
69 }
70
6656aefb 71 /* write rules to load2 or change-rule from every file in the directory */
a4783bd1 72 dir = opendir(srcdir);
ffbd2c4d 73 if (!dir) {
a4783bd1 74 if (errno != ENOENT)
6656aefb 75 log_warning_errno(errno, "Failed to opendir '%s': %m", srcdir);
a4783bd1 76 return errno; /* positive on purpose */
ffbd2c4d
NC
77 }
78
79 dfd = dirfd(dir);
fea7838e 80 assert(dfd >= 0);
ffbd2c4d
NC
81
82 FOREACH_DIRENT(entry, dir, return 0) {
83 _cleanup_fclose_ FILE *policy = NULL;
ffbd2c4d 84
6656aefb
WC
85 if (!dirent_is_file(entry))
86 continue;
87
b636d78a 88 if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
ffbd2c4d 89 continue;
ffbd2c4d 90
ffbd2c4d 91 /* load2 write rules in the kernel require a line buffered stream */
ea8b6526
LP
92 for (;;) {
93 _cleanup_free_ char *buf = NULL, *sbj = NULL, *obj = NULL, *acc1 = NULL, *acc2 = NULL;
94 int q;
6656aefb 95
ea8b6526
LP
96 q = read_line(policy, NAME_MAX, &buf);
97 if (q < 0)
98 return log_error_errno(q, "Failed to read line from '%s': %m", entry->d_name);
99 if (q == 0)
100 break;
6656aefb 101
ea8b6526 102 if (isempty(buf) || strchr(COMMENTS, buf[0]))
6656aefb
WC
103 continue;
104
105 /* if 3 args -> load rule : subject object access1 */
106 /* if 4 args -> change rule : subject object access1 access2 */
107 if (sscanf(buf, "%ms %ms %ms %ms", &sbj, &obj, &acc1, &acc2) < 3) {
108 log_error_errno(errno, "Failed to parse rule '%s' in '%s', ignoring.", buf, entry->d_name);
109 continue;
110 }
111
112 if (write(isempty(acc2) ? load2_fd : change_fd, buf, strlen(buf)) < 0) {
a4783bd1 113 if (r == 0)
6656aefb 114 r = -errno;
5e1ee764 115 log_error_errno(errno, "Failed to write '%s' to '%s' in '%s': %m",
6656aefb 116 buf, isempty(acc2) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry->d_name);
a4783bd1 117 }
6656aefb
WC
118 }
119 }
120
121 return r;
122}
123
b636d78a 124static int write_cipso2_rules(const char *srcdir) {
6656aefb
WC
125 _cleanup_close_ int cipso2_fd = -1;
126 _cleanup_closedir_ DIR *dir = NULL;
127 struct dirent *entry;
b636d78a 128 int dfd = -1, r = 0;
6656aefb
WC
129
130 cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
131 if (cipso2_fd < 0) {
132 if (errno != ENOENT)
133 log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/cipso2': %m");
134 return -errno; /* negative error */
135 }
136
137 /* write rules to cipso2 from every file in the directory */
138 dir = opendir(srcdir);
139 if (!dir) {
140 if (errno != ENOENT)
141 log_warning_errno(errno, "Failed to opendir '%s': %m", srcdir);
142 return errno; /* positive on purpose */
143 }
144
145 dfd = dirfd(dir);
146 assert(dfd >= 0);
147
148 FOREACH_DIRENT(entry, dir, return 0) {
6656aefb
WC
149 _cleanup_fclose_ FILE *policy = NULL;
150
151 if (!dirent_is_file(entry))
152 continue;
153
b636d78a 154 if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
6656aefb 155 continue;
6656aefb
WC
156
157 /* cipso2 write rules in the kernel require a line buffered stream */
ea8b6526
LP
158 for (;;) {
159 _cleanup_free_ char *buf = NULL;
160 int q;
161
162 q = read_line(policy, NAME_MAX, &buf);
163 if (q < 0)
164 return log_error_errno(q, "Failed to read line from '%s': %m", entry->d_name);
165 if (q == 0)
166 break;
6656aefb 167
ea8b6526 168 if (isempty(buf) || strchr(COMMENTS, buf[0]))
6656aefb
WC
169 continue;
170
171 if (write(cipso2_fd, buf, strlen(buf)) < 0) {
a4783bd1
ZJS
172 if (r == 0)
173 r = -errno;
5e1ee764 174 log_error_errno(errno, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s': %m",
6656aefb 175 buf, entry->d_name);
a4783bd1
ZJS
176 break;
177 }
ffbd2c4d
NC
178 }
179 }
180
6656aefb 181 return r;
a4783bd1
ZJS
182}
183
b636d78a 184static int write_netlabel_rules(const char *srcdir) {
ae176752
CS
185 _cleanup_fclose_ FILE *dst = NULL;
186 _cleanup_closedir_ DIR *dir = NULL;
187 struct dirent *entry;
b636d78a 188 int dfd = -1, r = 0;
ae176752
CS
189
190 dst = fopen("/sys/fs/smackfs/netlabel", "we");
191 if (!dst) {
192 if (errno != ENOENT)
193 log_warning_errno(errno, "Failed to open /sys/fs/smackfs/netlabel: %m");
194 return -errno; /* negative error */
195 }
196
197 /* write rules to dst from every file in the directory */
198 dir = opendir(srcdir);
199 if (!dir) {
200 if (errno != ENOENT)
201 log_warning_errno(errno, "Failed to opendir %s: %m", srcdir);
202 return errno; /* positive on purpose */
203 }
204
205 dfd = dirfd(dir);
206 assert(dfd >= 0);
207
208 FOREACH_DIRENT(entry, dir, return 0) {
ae176752
CS
209 _cleanup_fclose_ FILE *policy = NULL;
210
b636d78a 211 if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
ae176752 212 continue;
0d536673 213
ae176752 214 /* load2 write rules in the kernel require a line buffered stream */
ea8b6526
LP
215 for (;;) {
216 _cleanup_free_ char *buf = NULL;
0d536673
LP
217 int q;
218
ea8b6526
LP
219 q = read_line(policy, NAME_MAX, &buf);
220 if (q < 0)
221 return log_error_errno(q, "Failed to read line from %s: %m", entry->d_name);
222 if (q == 0)
223 break;
224
0d536673 225 if (!fputs(buf, dst)) {
ae176752
CS
226 if (r == 0)
227 r = -EINVAL;
5e1ee764 228 log_error_errno(errno, "Failed to write line to /sys/fs/smackfs/netlabel: %m");
ae176752
CS
229 break;
230 }
0d536673
LP
231 q = fflush_and_check(dst);
232 if (q < 0) {
ae176752 233 if (r == 0)
0d536673
LP
234 r = q;
235 log_error_errno(q, "Failed to flush writes to /sys/fs/smackfs/netlabel: %m");
ae176752
CS
236 break;
237 }
238 }
239 }
240
7f508f2c 241 return r;
ae176752
CS
242}
243
217f95db
WC
244static int write_onlycap_list(void) {
245 _cleanup_close_ int onlycap_fd = -1;
246 _cleanup_free_ char *list = NULL;
247 _cleanup_fclose_ FILE *f = NULL;
319a4f4b 248 size_t len = 0;
217f95db
WC
249 int r;
250
251 f = fopen("/etc/smack/onlycap", "re");
252 if (!f) {
253 if (errno != ENOENT)
9fd0b029
LP
254 log_warning_errno(errno, "Failed to read '/etc/smack/onlycap': %m");
255
217f95db
WC
256 return errno == ENOENT ? ENOENT : -errno;
257 }
258
ea8b6526
LP
259 for (;;) {
260 _cleanup_free_ char *buf = NULL;
217f95db
WC
261 size_t l;
262
ea8b6526
LP
263 r = read_line(f, LONG_LINE_MAX, &buf);
264 if (r < 0)
265 return log_error_errno(r, "Failed to read line from /etc/smack/onlycap: %m");
266 if (r == 0)
267 break;
268
269 if (isempty(buf) || strchr(COMMENTS, *buf))
217f95db
WC
270 continue;
271
272 l = strlen(buf);
319a4f4b 273 if (!GREEDY_REALLOC(list, len + l + 1))
217f95db
WC
274 return log_oom();
275
276 stpcpy(list + len, buf)[0] = ' ';
277 len += l + 1;
278 }
279
9fd0b029 280 if (len == 0)
217f95db
WC
281 return 0;
282
283 list[len - 1] = 0;
284
285 onlycap_fd = open("/sys/fs/smackfs/onlycap", O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
286 if (onlycap_fd < 0) {
287 if (errno != ENOENT)
9fd0b029 288 log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/onlycap': %m");
217f95db
WC
289 return -errno; /* negative error */
290 }
291
292 r = write(onlycap_fd, list, len);
293 if (r < 0)
9fd0b029 294 return log_error_errno(errno, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list);
217f95db
WC
295
296 return 0;
297}
298
2b3e18de 299#endif
ffbd2c4d 300
8a188de9 301int mac_smack_setup(bool *loaded_policy) {
2b3e18de 302
f9fa32f0 303#if ENABLE_SMACK
2b3e18de 304
a4783bd1
ZJS
305 int r;
306
e49d3c01
ŁS
307 assert(loaded_policy);
308
6656aefb 309 r = write_access2_rules("/etc/smack/accesses.d/");
a4783bd1
ZJS
310 switch(r) {
311 case -ENOENT:
312 log_debug("Smack is not enabled in the kernel.");
313 return 0;
314 case ENOENT:
6656aefb 315 log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
a4783bd1
ZJS
316 return 0;
317 case 0:
318 log_info("Successfully loaded Smack policies.");
abbacb1d
NC
319 break;
320 default:
e53fc357 321 log_warning_errno(r, "Failed to load Smack access rules, ignoring: %m");
abbacb1d
NC
322 return 0;
323 }
324
8b197c3a 325#ifdef SMACK_RUN_LABEL
57512c89 326 r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
327 if (r < 0)
328 log_warning_errno(r, "Failed to set SMACK label \"" SMACK_RUN_LABEL "\" on self: %m");
57512c89 329 r = write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
330 if (r < 0)
331 log_warning_errno(r, "Failed to set SMACK ambient label \"" SMACK_RUN_LABEL "\": %m");
332 r = write_string_file("/sys/fs/smackfs/netlabel",
57512c89 333 "0.0.0.0/0 " SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
334 if (r < 0)
335 log_warning_errno(r, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL "\": %m");
57512c89 336 r = write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
337 if (r < 0)
338 log_warning_errno(r, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
8b197c3a
AK
339#endif
340
6656aefb 341 r = write_cipso2_rules("/etc/smack/cipso.d/");
abbacb1d
NC
342 switch(r) {
343 case -ENOENT:
344 log_debug("Smack/CIPSO is not enabled in the kernel.");
345 return 0;
346 case ENOENT:
6656aefb 347 log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
ae176752 348 break;
abbacb1d
NC
349 case 0:
350 log_info("Successfully loaded Smack/CIPSO policies.");
b9289d4c 351 break;
a4783bd1 352 default:
e53fc357 353 log_warning_errno(r, "Failed to load Smack/CIPSO access rules, ignoring: %m");
ae176752
CS
354 break;
355 }
356
357 r = write_netlabel_rules("/etc/smack/netlabel.d/");
358 switch(r) {
359 case -ENOENT:
360 log_debug("Smack/CIPSO is not enabled in the kernel.");
a4783bd1 361 return 0;
ae176752
CS
362 case ENOENT:
363 log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
364 break;
365 case 0:
366 log_info("Successfully loaded Smack network host rules.");
367 break;
368 default:
369 log_warning_errno(r, "Failed to load Smack network host rules: %m, ignoring.");
370 break;
a4783bd1 371 }
2b3e18de 372
217f95db
WC
373 r = write_onlycap_list();
374 switch(r) {
375 case -ENOENT:
376 log_debug("Smack is not enabled in the kernel.");
377 break;
378 case ENOENT:
379 log_debug("Smack onlycap list file '/etc/smack/onlycap' not found");
380 break;
381 case 0:
382 log_info("Successfully wrote Smack onlycap list.");
383 break;
384 default:
d85ff944 385 return log_emergency_errno(r, "Failed to write Smack onlycap list: %m");
217f95db
WC
386 }
387
e49d3c01
ŁS
388 *loaded_policy = true;
389
2b3e18de
KL
390#endif
391
392 return 0;
ffbd2c4d 393}