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