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