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