]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/smack-setup.c
headers: remove unneeded includes from util.h
[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>
ca78ad1d 15#include <unistd.h>
ffbd2c4d 16
b5efdb8a 17#include "alloc-util.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
e92aaed3 76 policy = fdopen(fd, "r");
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 108 r = -errno;
5e1ee764 109 log_error_errno(errno, "Failed to write '%s' to '%s' in '%s': %m",
6656aefb 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
e92aaed3 158 policy = fdopen(fd, "r");
6656aefb
WC
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;
5e1ee764 184 log_error_errno(errno, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s': %m",
6656aefb 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
e92aaed3 231 policy = fdopen(fd, "r");
ae176752
CS
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;
5e1ee764 256 log_error_errno(errno, "Failed to write line to /sys/fs/smackfs/netlabel: %m");
ae176752
CS
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)
9fd0b029
LP
282 log_warning_errno(errno, "Failed to read '/etc/smack/onlycap': %m");
283
217f95db
WC
284 return errno == ENOENT ? ENOENT : -errno;
285 }
286
ea8b6526
LP
287 for (;;) {
288 _cleanup_free_ char *buf = NULL;
217f95db
WC
289 size_t l;
290
ea8b6526
LP
291 r = read_line(f, LONG_LINE_MAX, &buf);
292 if (r < 0)
293 return log_error_errno(r, "Failed to read line from /etc/smack/onlycap: %m");
294 if (r == 0)
295 break;
296
297 if (isempty(buf) || strchr(COMMENTS, *buf))
217f95db
WC
298 continue;
299
300 l = strlen(buf);
301 if (!GREEDY_REALLOC(list, allocated, len + l + 1))
302 return log_oom();
303
304 stpcpy(list + len, buf)[0] = ' ';
305 len += l + 1;
306 }
307
9fd0b029 308 if (len == 0)
217f95db
WC
309 return 0;
310
311 list[len - 1] = 0;
312
313 onlycap_fd = open("/sys/fs/smackfs/onlycap", O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
314 if (onlycap_fd < 0) {
315 if (errno != ENOENT)
9fd0b029 316 log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/onlycap': %m");
217f95db
WC
317 return -errno; /* negative error */
318 }
319
320 r = write(onlycap_fd, list, len);
321 if (r < 0)
9fd0b029 322 return log_error_errno(errno, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list);
217f95db
WC
323
324 return 0;
325}
326
2b3e18de 327#endif
ffbd2c4d 328
8a188de9 329int mac_smack_setup(bool *loaded_policy) {
2b3e18de 330
f9fa32f0 331#if ENABLE_SMACK
2b3e18de 332
a4783bd1
ZJS
333 int r;
334
e49d3c01
ŁS
335 assert(loaded_policy);
336
6656aefb 337 r = write_access2_rules("/etc/smack/accesses.d/");
a4783bd1
ZJS
338 switch(r) {
339 case -ENOENT:
340 log_debug("Smack is not enabled in the kernel.");
341 return 0;
342 case ENOENT:
6656aefb 343 log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
a4783bd1
ZJS
344 return 0;
345 case 0:
346 log_info("Successfully loaded Smack policies.");
abbacb1d
NC
347 break;
348 default:
e53fc357 349 log_warning_errno(r, "Failed to load Smack access rules, ignoring: %m");
abbacb1d
NC
350 return 0;
351 }
352
8b197c3a 353#ifdef SMACK_RUN_LABEL
57512c89 354 r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
355 if (r < 0)
356 log_warning_errno(r, "Failed to set SMACK label \"" SMACK_RUN_LABEL "\" on self: %m");
57512c89 357 r = write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
358 if (r < 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",
57512c89 361 "0.0.0.0/0 " SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
362 if (r < 0)
363 log_warning_errno(r, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL "\": %m");
57512c89 364 r = write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", WRITE_STRING_FILE_DISABLE_BUFFER);
ae176752
CS
365 if (r < 0)
366 log_warning_errno(r, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
8b197c3a
AK
367#endif
368
6656aefb 369 r = write_cipso2_rules("/etc/smack/cipso.d/");
abbacb1d
NC
370 switch(r) {
371 case -ENOENT:
372 log_debug("Smack/CIPSO is not enabled in the kernel.");
373 return 0;
374 case ENOENT:
6656aefb 375 log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
ae176752 376 break;
abbacb1d
NC
377 case 0:
378 log_info("Successfully loaded Smack/CIPSO policies.");
b9289d4c 379 break;
a4783bd1 380 default:
e53fc357 381 log_warning_errno(r, "Failed to load Smack/CIPSO access rules, ignoring: %m");
ae176752
CS
382 break;
383 }
384
385 r = write_netlabel_rules("/etc/smack/netlabel.d/");
386 switch(r) {
387 case -ENOENT:
388 log_debug("Smack/CIPSO is not enabled in the kernel.");
a4783bd1 389 return 0;
ae176752
CS
390 case ENOENT:
391 log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
392 break;
393 case 0:
394 log_info("Successfully loaded Smack network host rules.");
395 break;
396 default:
397 log_warning_errno(r, "Failed to load Smack network host rules: %m, ignoring.");
398 break;
a4783bd1 399 }
2b3e18de 400
217f95db
WC
401 r = write_onlycap_list();
402 switch(r) {
403 case -ENOENT:
404 log_debug("Smack is not enabled in the kernel.");
405 break;
406 case ENOENT:
407 log_debug("Smack onlycap list file '/etc/smack/onlycap' not found");
408 break;
409 case 0:
410 log_info("Successfully wrote Smack onlycap list.");
411 break;
412 default:
5e1ee764 413 log_emergency_errno(r, "Failed to write Smack onlycap list: %m");
217f95db
WC
414 return r;
415 }
416
e49d3c01
ŁS
417 *loaded_policy = true;
418
2b3e18de
KL
419#endif
420
421 return 0;
ffbd2c4d 422}