]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
ec16f3b6 | 2 | |
ec16f3b6 LP |
3 | #include <sys/utsname.h> |
4 | ||
1624114d | 5 | #include "af-list.h" |
ec16f3b6 | 6 | #include "analyze-security.h" |
bb43d853 | 7 | #include "analyze-verify.h" |
ec16f3b6 | 8 | #include "bus-error.h" |
807542be | 9 | #include "bus-map-properties.h" |
ec16f3b6 LP |
10 | #include "bus-unit-util.h" |
11 | #include "bus-util.h" | |
12 | #include "env-util.h" | |
13 | #include "format-table.h" | |
84ebe6f0 | 14 | #include "in-addr-prefix-util.h" |
ec16f3b6 LP |
15 | #include "locale-util.h" |
16 | #include "macro.h" | |
bb43d853 | 17 | #include "manager.h" |
f5947a5e YW |
18 | #include "missing_capability.h" |
19 | #include "missing_sched.h" | |
d8b4d14d | 20 | #include "nulstr-util.h" |
ec16f3b6 LP |
21 | #include "parse-util.h" |
22 | #include "path-util.h" | |
23 | #include "pretty-print.h" | |
3a5d89fa WKI |
24 | #if HAVE_SECCOMP |
25 | # include "seccomp-util.h" | |
26 | #endif | |
1624114d | 27 | #include "service.h" |
ec16f3b6 LP |
28 | #include "set.h" |
29 | #include "stdio-util.h" | |
30 | #include "strv.h" | |
31 | #include "terminal-util.h" | |
32 | #include "unit-def.h" | |
33 | #include "unit-name.h" | |
1624114d | 34 | #include "unit-serialize.h" |
ec16f3b6 | 35 | |
1624114d | 36 | typedef struct SecurityInfo { |
ec16f3b6 LP |
37 | char *id; |
38 | char *type; | |
39 | char *load_state; | |
40 | char *fragment_path; | |
41 | bool default_dependencies; | |
42 | ||
43 | uint64_t ambient_capabilities; | |
44 | uint64_t capability_bounding_set; | |
45 | ||
46 | char *user; | |
47 | char **supplementary_groups; | |
48 | bool dynamic_user; | |
49 | ||
50 | bool ip_address_deny_all; | |
51 | bool ip_address_allow_localhost; | |
52 | bool ip_address_allow_other; | |
53 | ||
fab34748 KL |
54 | bool ip_filters_custom_ingress; |
55 | bool ip_filters_custom_egress; | |
56 | ||
ec16f3b6 | 57 | char *keyring_mode; |
ed125c93 LP |
58 | char *protect_proc; |
59 | char *proc_subset; | |
ec16f3b6 LP |
60 | bool lock_personality; |
61 | bool memory_deny_write_execute; | |
62 | bool no_new_privileges; | |
63 | char *notify_access; | |
527bd7f1 | 64 | bool protect_hostname; |
ec16f3b6 LP |
65 | |
66 | bool private_devices; | |
67 | bool private_mounts; | |
68 | bool private_network; | |
69 | bool private_tmp; | |
70 | bool private_users; | |
71 | ||
72 | bool protect_control_groups; | |
73 | bool protect_kernel_modules; | |
74 | bool protect_kernel_tunables; | |
82dce83b | 75 | bool protect_kernel_logs; |
9f37272a | 76 | bool protect_clock; |
ec16f3b6 LP |
77 | |
78 | char *protect_home; | |
79 | char *protect_system; | |
80 | ||
81 | bool remove_ipc; | |
82 | ||
83 | bool restrict_address_family_inet; | |
84 | bool restrict_address_family_unix; | |
85 | bool restrict_address_family_netlink; | |
86 | bool restrict_address_family_packet; | |
87 | bool restrict_address_family_other; | |
88 | ||
1624114d | 89 | unsigned long long restrict_namespaces; |
ec16f3b6 | 90 | bool restrict_realtime; |
9d880b70 | 91 | bool restrict_suid_sgid; |
ec16f3b6 LP |
92 | |
93 | char *root_directory; | |
94 | char *root_image; | |
95 | ||
96 | bool delegate; | |
97 | char *device_policy; | |
98 | bool device_allow_non_empty; | |
99 | ||
1624114d | 100 | Set *system_call_architectures; |
ec16f3b6 | 101 | |
6b000af4 | 102 | bool system_call_filter_allow_list; |
1624114d | 103 | Hashmap *system_call_filter; |
ec16f3b6 | 104 | |
1624114d MG |
105 | mode_t _umask; |
106 | } SecurityInfo; | |
ec16f3b6 LP |
107 | |
108 | struct security_assessor { | |
109 | const char *id; | |
3838d22c | 110 | const char *json_field; |
ec16f3b6 LP |
111 | const char *description_good; |
112 | const char *description_bad; | |
113 | const char *description_na; | |
114 | const char *url; | |
115 | uint64_t weight; | |
116 | uint64_t range; | |
1ace223c SJ |
117 | int (*assess)( |
118 | const struct security_assessor *a, | |
1624114d | 119 | const SecurityInfo *info, |
1ace223c SJ |
120 | const void *data, |
121 | uint64_t *ret_badness, | |
122 | char **ret_description); | |
ec16f3b6 LP |
123 | size_t offset; |
124 | uint64_t parameter; | |
125 | bool default_dependencies_only; | |
126 | }; | |
127 | ||
1624114d MG |
128 | static SecurityInfo *security_info_new(void) { |
129 | SecurityInfo *info = new(SecurityInfo, 1); | |
130 | if (!info) | |
131 | return NULL; | |
132 | ||
133 | *info = (SecurityInfo) { | |
134 | .default_dependencies = true, | |
135 | .capability_bounding_set = UINT64_MAX, | |
136 | .restrict_namespaces = UINT64_MAX, | |
137 | ._umask = 0002, | |
138 | }; | |
139 | ||
140 | return info; | |
141 | } | |
142 | ||
143 | static SecurityInfo *security_info_free(SecurityInfo *i) { | |
ec16f3b6 | 144 | if (!i) |
1624114d | 145 | return NULL; |
ec16f3b6 LP |
146 | |
147 | free(i->id); | |
148 | free(i->type); | |
149 | free(i->load_state); | |
150 | free(i->fragment_path); | |
151 | ||
152 | free(i->user); | |
153 | ||
154 | free(i->protect_home); | |
155 | free(i->protect_system); | |
156 | ||
157 | free(i->root_directory); | |
158 | free(i->root_image); | |
159 | ||
160 | free(i->keyring_mode); | |
ed125c93 LP |
161 | free(i->protect_proc); |
162 | free(i->proc_subset); | |
ec16f3b6 LP |
163 | free(i->notify_access); |
164 | ||
165 | free(i->device_policy); | |
166 | ||
167 | strv_free(i->supplementary_groups); | |
1624114d | 168 | set_free(i->system_call_architectures); |
ec16f3b6 | 169 | |
1624114d MG |
170 | hashmap_free(i->system_call_filter); |
171 | ||
172 | return mfree(i); | |
ec16f3b6 LP |
173 | } |
174 | ||
1624114d MG |
175 | DEFINE_TRIVIAL_CLEANUP_FUNC(SecurityInfo*, security_info_free); |
176 | ||
177 | static bool security_info_runs_privileged(const SecurityInfo *i) { | |
ec16f3b6 LP |
178 | assert(i); |
179 | ||
180 | if (STRPTR_IN_SET(i->user, "0", "root")) | |
181 | return true; | |
182 | ||
183 | if (i->dynamic_user) | |
184 | return false; | |
185 | ||
186 | return isempty(i->user); | |
187 | } | |
188 | ||
189 | static int assess_bool( | |
190 | const struct security_assessor *a, | |
1624114d | 191 | const SecurityInfo *info, |
ec16f3b6 LP |
192 | const void *data, |
193 | uint64_t *ret_badness, | |
194 | char **ret_description) { | |
195 | ||
196 | const bool *b = data; | |
197 | ||
198 | assert(b); | |
199 | assert(ret_badness); | |
200 | assert(ret_description); | |
201 | ||
202 | *ret_badness = a->parameter ? *b : !*b; | |
203 | *ret_description = NULL; | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | static int assess_user( | |
209 | const struct security_assessor *a, | |
1624114d | 210 | const SecurityInfo *info, |
ec16f3b6 LP |
211 | const void *data, |
212 | uint64_t *ret_badness, | |
213 | char **ret_description) { | |
214 | ||
215 | _cleanup_free_ char *d = NULL; | |
216 | uint64_t b; | |
217 | ||
218 | assert(ret_badness); | |
219 | assert(ret_description); | |
220 | ||
221 | if (streq_ptr(info->user, NOBODY_USER_NAME)) { | |
222 | d = strdup("Service runs under as '" NOBODY_USER_NAME "' user, which should not be used for services"); | |
223 | b = 9; | |
224 | } else if (info->dynamic_user && !STR_IN_SET(info->user, "0", "root")) { | |
225 | d = strdup("Service runs under a transient non-root user identity"); | |
226 | b = 0; | |
227 | } else if (info->user && !STR_IN_SET(info->user, "0", "root", "")) { | |
228 | d = strdup("Service runs under a static non-root user identity"); | |
229 | b = 0; | |
230 | } else { | |
231 | *ret_badness = 10; | |
232 | *ret_description = NULL; | |
233 | return 0; | |
234 | } | |
235 | ||
236 | if (!d) | |
237 | return log_oom(); | |
238 | ||
239 | *ret_badness = b; | |
240 | *ret_description = TAKE_PTR(d); | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
245 | static int assess_protect_home( | |
246 | const struct security_assessor *a, | |
1624114d | 247 | const SecurityInfo *info, |
ec16f3b6 LP |
248 | const void *data, |
249 | uint64_t *ret_badness, | |
250 | char **ret_description) { | |
251 | ||
252 | const char *description; | |
253 | uint64_t badness; | |
254 | char *copy; | |
255 | int r; | |
256 | ||
257 | assert(ret_badness); | |
258 | assert(ret_description); | |
259 | ||
260 | badness = 10; | |
261 | description = "Service has full access to home directories"; | |
262 | ||
263 | r = parse_boolean(info->protect_home); | |
264 | if (r < 0) { | |
265 | if (streq_ptr(info->protect_home, "read-only")) { | |
266 | badness = 5; | |
267 | description = "Service has read-only access to home directories"; | |
268 | } else if (streq_ptr(info->protect_home, "tmpfs")) { | |
269 | badness = 1; | |
270 | description = "Service has access to fake empty home directories"; | |
271 | } | |
272 | } else if (r > 0) { | |
273 | badness = 0; | |
274 | description = "Service has no access to home directories"; | |
275 | } | |
276 | ||
277 | copy = strdup(description); | |
278 | if (!copy) | |
279 | return log_oom(); | |
280 | ||
281 | *ret_badness = badness; | |
282 | *ret_description = copy; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static int assess_protect_system( | |
288 | const struct security_assessor *a, | |
1624114d | 289 | const SecurityInfo *info, |
ec16f3b6 LP |
290 | const void *data, |
291 | uint64_t *ret_badness, | |
292 | char **ret_description) { | |
293 | ||
294 | const char *description; | |
295 | uint64_t badness; | |
296 | char *copy; | |
297 | int r; | |
298 | ||
299 | assert(ret_badness); | |
300 | assert(ret_description); | |
301 | ||
302 | badness = 10; | |
1e8817b3 | 303 | description = "Service has full access to the OS file hierarchy"; |
ec16f3b6 LP |
304 | |
305 | r = parse_boolean(info->protect_system); | |
306 | if (r < 0) { | |
307 | if (streq_ptr(info->protect_system, "full")) { | |
308 | badness = 3; | |
1e8817b3 | 309 | description = "Service has very limited write access to the OS file hierarchy"; |
ec16f3b6 LP |
310 | } else if (streq_ptr(info->protect_system, "strict")) { |
311 | badness = 0; | |
312 | description = "Service has strict read-only access to the OS file hierarchy"; | |
313 | } | |
314 | } else if (r > 0) { | |
315 | badness = 5; | |
316 | description = "Service has limited write access to the OS file hierarchy"; | |
317 | } | |
318 | ||
319 | copy = strdup(description); | |
320 | if (!copy) | |
321 | return log_oom(); | |
322 | ||
323 | *ret_badness = badness; | |
324 | *ret_description = copy; | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
329 | static int assess_root_directory( | |
330 | const struct security_assessor *a, | |
1624114d | 331 | const SecurityInfo *info, |
ec16f3b6 LP |
332 | const void *data, |
333 | uint64_t *ret_badness, | |
334 | char **ret_description) { | |
335 | ||
336 | assert(ret_badness); | |
337 | assert(ret_description); | |
338 | ||
339 | *ret_badness = | |
d909b40f | 340 | empty_or_root(info->root_directory) && |
d737b451 | 341 | empty_or_root(info->root_image); |
ec16f3b6 LP |
342 | *ret_description = NULL; |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
347 | static int assess_capability_bounding_set( | |
348 | const struct security_assessor *a, | |
1624114d | 349 | const SecurityInfo *info, |
ec16f3b6 LP |
350 | const void *data, |
351 | uint64_t *ret_badness, | |
352 | char **ret_description) { | |
353 | ||
354 | assert(ret_badness); | |
355 | assert(ret_description); | |
356 | ||
357 | *ret_badness = !!(info->capability_bounding_set & a->parameter); | |
358 | *ret_description = NULL; | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | static int assess_umask( | |
364 | const struct security_assessor *a, | |
1624114d | 365 | const SecurityInfo *info, |
ec16f3b6 LP |
366 | const void *data, |
367 | uint64_t *ret_badness, | |
368 | char **ret_description) { | |
369 | ||
370 | char *copy = NULL; | |
371 | const char *d; | |
372 | uint64_t b; | |
373 | ||
374 | assert(ret_badness); | |
375 | assert(ret_description); | |
376 | ||
377 | if (!FLAGS_SET(info->_umask, 0002)) { | |
378 | d = "Files created by service are world-writable by default"; | |
379 | b = 10; | |
380 | } else if (!FLAGS_SET(info->_umask, 0004)) { | |
381 | d = "Files created by service are world-readable by default"; | |
382 | b = 5; | |
383 | } else if (!FLAGS_SET(info->_umask, 0020)) { | |
384 | d = "Files created by service are group-writable by default"; | |
385 | b = 2; | |
386 | } else if (!FLAGS_SET(info->_umask, 0040)) { | |
387 | d = "Files created by service are group-readable by default"; | |
388 | b = 1; | |
389 | } else { | |
390 | d = "Files created by service are accessible only by service's own user by default"; | |
391 | b = 0; | |
392 | } | |
393 | ||
394 | copy = strdup(d); | |
395 | if (!copy) | |
396 | return log_oom(); | |
397 | ||
398 | *ret_badness = b; | |
399 | *ret_description = copy; | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | static int assess_keyring_mode( | |
405 | const struct security_assessor *a, | |
1624114d | 406 | const SecurityInfo *info, |
ec16f3b6 LP |
407 | const void *data, |
408 | uint64_t *ret_badness, | |
409 | char **ret_description) { | |
410 | ||
411 | assert(ret_badness); | |
412 | assert(ret_description); | |
413 | ||
414 | *ret_badness = !streq_ptr(info->keyring_mode, "private"); | |
415 | *ret_description = NULL; | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
ed125c93 LP |
420 | static int assess_protect_proc( |
421 | const struct security_assessor *a, | |
1624114d | 422 | const SecurityInfo *info, |
ed125c93 LP |
423 | const void *data, |
424 | uint64_t *ret_badness, | |
425 | char **ret_description) { | |
426 | ||
427 | assert(ret_badness); | |
428 | assert(ret_description); | |
429 | ||
430 | if (streq_ptr(info->protect_proc, "noaccess")) | |
431 | *ret_badness = 1; | |
432 | else if (STRPTR_IN_SET(info->protect_proc, "invisible", "ptraceable")) | |
433 | *ret_badness = 0; | |
434 | else | |
435 | *ret_badness = 3; | |
436 | ||
437 | *ret_description = NULL; | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
442 | static int assess_proc_subset( | |
443 | const struct security_assessor *a, | |
1624114d | 444 | const SecurityInfo *info, |
ed125c93 LP |
445 | const void *data, |
446 | uint64_t *ret_badness, | |
447 | char **ret_description) { | |
448 | ||
449 | assert(ret_badness); | |
450 | assert(ret_description); | |
451 | ||
452 | *ret_badness = !streq_ptr(info->proc_subset, "pid"); | |
453 | *ret_description = NULL; | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
ec16f3b6 LP |
458 | static int assess_notify_access( |
459 | const struct security_assessor *a, | |
1624114d | 460 | const SecurityInfo *info, |
ec16f3b6 LP |
461 | const void *data, |
462 | uint64_t *ret_badness, | |
463 | char **ret_description) { | |
464 | ||
465 | assert(ret_badness); | |
466 | assert(ret_description); | |
467 | ||
468 | *ret_badness = streq_ptr(info->notify_access, "all"); | |
469 | *ret_description = NULL; | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | static int assess_remove_ipc( | |
475 | const struct security_assessor *a, | |
1624114d | 476 | const SecurityInfo *info, |
ec16f3b6 LP |
477 | const void *data, |
478 | uint64_t *ret_badness, | |
479 | char **ret_description) { | |
480 | ||
481 | assert(ret_badness); | |
482 | assert(ret_description); | |
483 | ||
484 | if (security_info_runs_privileged(info)) | |
485 | *ret_badness = UINT64_MAX; | |
486 | else | |
487 | *ret_badness = !info->remove_ipc; | |
488 | ||
489 | *ret_description = NULL; | |
490 | return 0; | |
491 | } | |
492 | ||
493 | static int assess_supplementary_groups( | |
494 | const struct security_assessor *a, | |
1624114d | 495 | const SecurityInfo *info, |
ec16f3b6 LP |
496 | const void *data, |
497 | uint64_t *ret_badness, | |
498 | char **ret_description) { | |
499 | ||
500 | assert(ret_badness); | |
501 | assert(ret_description); | |
502 | ||
503 | if (security_info_runs_privileged(info)) | |
504 | *ret_badness = UINT64_MAX; | |
505 | else | |
506 | *ret_badness = !strv_isempty(info->supplementary_groups); | |
507 | ||
508 | *ret_description = NULL; | |
509 | return 0; | |
510 | } | |
511 | ||
512 | static int assess_restrict_namespaces( | |
513 | const struct security_assessor *a, | |
1624114d | 514 | const SecurityInfo *info, |
ec16f3b6 LP |
515 | const void *data, |
516 | uint64_t *ret_badness, | |
517 | char **ret_description) { | |
518 | ||
519 | assert(ret_badness); | |
520 | assert(ret_description); | |
521 | ||
522 | *ret_badness = !!(info->restrict_namespaces & a->parameter); | |
523 | *ret_description = NULL; | |
524 | ||
525 | return 0; | |
526 | } | |
527 | ||
528 | static int assess_system_call_architectures( | |
529 | const struct security_assessor *a, | |
1624114d | 530 | const SecurityInfo *info, |
ec16f3b6 LP |
531 | const void *data, |
532 | uint64_t *ret_badness, | |
533 | char **ret_description) { | |
534 | ||
535 | char *d; | |
536 | uint64_t b; | |
537 | ||
538 | assert(ret_badness); | |
539 | assert(ret_description); | |
540 | ||
1624114d | 541 | if (set_isempty(info->system_call_architectures)) { |
ec16f3b6 LP |
542 | b = 10; |
543 | d = strdup("Service may execute system calls with all ABIs"); | |
1624114d MG |
544 | } else if (set_contains(info->system_call_architectures, "native") && |
545 | set_size(info->system_call_architectures) == 1) { | |
ec16f3b6 LP |
546 | b = 0; |
547 | d = strdup("Service may execute system calls only with native ABI"); | |
548 | } else { | |
549 | b = 8; | |
550 | d = strdup("Service may execute system calls with multiple ABIs"); | |
551 | } | |
552 | ||
553 | if (!d) | |
554 | return log_oom(); | |
555 | ||
556 | *ret_badness = b; | |
557 | *ret_description = d; | |
558 | ||
559 | return 0; | |
560 | } | |
561 | ||
3a5d89fa WKI |
562 | #if HAVE_SECCOMP |
563 | ||
1624114d | 564 | static bool syscall_names_in_filter(Hashmap *s, bool allow_list, const SyscallFilterSet *f, const char **ret_offending_syscall) { |
ec16f3b6 LP |
565 | const char *syscall; |
566 | ||
567 | NULSTR_FOREACH(syscall, f->value) { | |
95832a0f | 568 | int id; |
ec16f3b6 LP |
569 | |
570 | if (syscall[0] == '@') { | |
571 | const SyscallFilterSet *g; | |
ec16f3b6 | 572 | |
95832a0f | 573 | assert_se(g = syscall_filter_set_find(syscall)); |
a9134af2 | 574 | if (syscall_names_in_filter(s, allow_list, g, ret_offending_syscall)) |
95832a0f | 575 | return true; /* bad! */ |
ec16f3b6 | 576 | |
95832a0f | 577 | continue; |
ec16f3b6 LP |
578 | } |
579 | ||
95832a0f YW |
580 | /* Let's see if the system call actually exists on this platform, before complaining */ |
581 | id = seccomp_syscall_resolve_name(syscall); | |
582 | if (id < 0) | |
583 | continue; | |
584 | ||
1624114d | 585 | if (hashmap_contains(s, syscall) == allow_list) { |
ec16f3b6 | 586 | log_debug("Offending syscall filter item: %s", syscall); |
a9134af2 ZJS |
587 | if (ret_offending_syscall) |
588 | *ret_offending_syscall = syscall; | |
ec16f3b6 LP |
589 | return true; /* bad! */ |
590 | } | |
591 | } | |
592 | ||
a9134af2 | 593 | *ret_offending_syscall = NULL; |
ec16f3b6 LP |
594 | return false; |
595 | } | |
596 | ||
597 | static int assess_system_call_filter( | |
598 | const struct security_assessor *a, | |
1624114d | 599 | const SecurityInfo *info, |
ec16f3b6 LP |
600 | const void *data, |
601 | uint64_t *ret_badness, | |
602 | char **ret_description) { | |
603 | ||
ec16f3b6 LP |
604 | assert(a); |
605 | assert(info); | |
606 | assert(ret_badness); | |
607 | assert(ret_description); | |
608 | ||
609 | assert(a->parameter < _SYSCALL_FILTER_SET_MAX); | |
a9134af2 ZJS |
610 | const SyscallFilterSet *f = syscall_filter_sets + a->parameter; |
611 | ||
12619d0a | 612 | _cleanup_free_ char *d = NULL; |
a9134af2 | 613 | uint64_t b; |
12619d0a | 614 | int r; |
ec16f3b6 | 615 | |
1624114d | 616 | if (!info->system_call_filter_allow_list && hashmap_isempty(info->system_call_filter)) { |
12619d0a | 617 | r = free_and_strdup(&d, "Service does not filter system calls"); |
ec16f3b6 LP |
618 | b = 10; |
619 | } else { | |
620 | bool bad; | |
a9134af2 | 621 | const char *offender = NULL; |
ec16f3b6 LP |
622 | |
623 | log_debug("Analyzing system call filter, checking against: %s", f->name); | |
a9134af2 | 624 | bad = syscall_names_in_filter(info->system_call_filter, info->system_call_filter_allow_list, f, &offender); |
ec16f3b6 LP |
625 | log_debug("Result: %s", bad ? "bad" : "good"); |
626 | ||
6b000af4 | 627 | if (info->system_call_filter_allow_list) { |
ec16f3b6 | 628 | if (bad) { |
12619d0a ZJS |
629 | r = asprintf(&d, "System call allow list defined for service, and %s is included " |
630 | "(e.g. %s is allowed)", | |
631 | f->name, offender); | |
ec16f3b6 LP |
632 | b = 9; |
633 | } else { | |
12619d0a ZJS |
634 | r = asprintf(&d, "System call allow list defined for service, and %s is not included", |
635 | f->name); | |
ec16f3b6 LP |
636 | b = 0; |
637 | } | |
638 | } else { | |
639 | if (bad) { | |
12619d0a ZJS |
640 | r = asprintf(&d, "System call deny list defined for service, and %s is not included " |
641 | "(e.g. %s is allowed)", | |
642 | f->name, offender); | |
ec16f3b6 LP |
643 | b = 10; |
644 | } else { | |
12619d0a ZJS |
645 | r = asprintf(&d, "System call deny list defined for service, and %s is included", |
646 | f->name); | |
01ecb367 | 647 | b = 0; |
ec16f3b6 LP |
648 | } |
649 | } | |
650 | } | |
12619d0a | 651 | if (r < 0) |
ec16f3b6 LP |
652 | return log_oom(); |
653 | ||
654 | *ret_badness = b; | |
12619d0a | 655 | *ret_description = TAKE_PTR(d); |
ec16f3b6 LP |
656 | |
657 | return 0; | |
658 | } | |
659 | ||
3a5d89fa WKI |
660 | #endif |
661 | ||
ec16f3b6 LP |
662 | static int assess_ip_address_allow( |
663 | const struct security_assessor *a, | |
1624114d | 664 | const SecurityInfo *info, |
ec16f3b6 LP |
665 | const void *data, |
666 | uint64_t *ret_badness, | |
667 | char **ret_description) { | |
668 | ||
669 | char *d = NULL; | |
670 | uint64_t b; | |
671 | ||
672 | assert(info); | |
673 | assert(ret_badness); | |
674 | assert(ret_description); | |
675 | ||
fab34748 KL |
676 | if (info->ip_filters_custom_ingress || info->ip_filters_custom_egress) { |
677 | d = strdup("Service defines custom ingress/egress IP filters with BPF programs"); | |
678 | b = 0; | |
679 | } else if (!info->ip_address_deny_all) { | |
6b000af4 | 680 | d = strdup("Service does not define an IP address allow list"); |
ec16f3b6 LP |
681 | b = 10; |
682 | } else if (info->ip_address_allow_other) { | |
6b000af4 | 683 | d = strdup("Service defines IP address allow list with non-localhost entries"); |
ec16f3b6 LP |
684 | b = 5; |
685 | } else if (info->ip_address_allow_localhost) { | |
6b000af4 | 686 | d = strdup("Service defines IP address allow list with only localhost entries"); |
ec16f3b6 LP |
687 | b = 2; |
688 | } else { | |
689 | d = strdup("Service blocks all IP address ranges"); | |
690 | b = 0; | |
691 | } | |
692 | ||
693 | if (!d) | |
694 | return log_oom(); | |
695 | ||
696 | *ret_badness = b; | |
697 | *ret_description = d; | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | static int assess_device_allow( | |
703 | const struct security_assessor *a, | |
1624114d | 704 | const SecurityInfo *info, |
ec16f3b6 LP |
705 | const void *data, |
706 | uint64_t *ret_badness, | |
707 | char **ret_description) { | |
708 | ||
709 | char *d = NULL; | |
710 | uint64_t b; | |
711 | ||
712 | assert(info); | |
713 | assert(ret_badness); | |
714 | assert(ret_description); | |
715 | ||
716 | if (STRPTR_IN_SET(info->device_policy, "strict", "closed")) { | |
717 | ||
718 | if (info->device_allow_non_empty) { | |
719 | d = strdup("Service has a device ACL with some special devices"); | |
720 | b = 5; | |
721 | } else { | |
722 | d = strdup("Service has a minimal device ACL"); | |
723 | b = 0; | |
724 | } | |
725 | } else { | |
726 | d = strdup("Service has no device ACL"); | |
727 | b = 10; | |
728 | } | |
729 | ||
730 | if (!d) | |
731 | return log_oom(); | |
732 | ||
733 | *ret_badness = b; | |
734 | *ret_description = d; | |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
739 | static int assess_ambient_capabilities( | |
740 | const struct security_assessor *a, | |
1624114d | 741 | const SecurityInfo *info, |
ec16f3b6 LP |
742 | const void *data, |
743 | uint64_t *ret_badness, | |
744 | char **ret_description) { | |
745 | ||
746 | assert(ret_badness); | |
747 | assert(ret_description); | |
748 | ||
749 | *ret_badness = info->ambient_capabilities != 0; | |
750 | *ret_description = NULL; | |
751 | ||
752 | return 0; | |
753 | } | |
754 | ||
755 | static const struct security_assessor security_assessor_table[] = { | |
756 | { | |
757 | .id = "User=/DynamicUser=", | |
3838d22c | 758 | .json_field = "UserOrDynamicUser", |
ec16f3b6 LP |
759 | .description_bad = "Service runs as root user", |
760 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#User=", | |
761 | .weight = 2000, | |
762 | .range = 10, | |
763 | .assess = assess_user, | |
764 | }, | |
765 | { | |
766 | .id = "SupplementaryGroups=", | |
3838d22c | 767 | .json_field = "SupplementaryGroups", |
ec16f3b6 LP |
768 | .description_good = "Service has no supplementary groups", |
769 | .description_bad = "Service runs with supplementary groups", | |
770 | .description_na = "Service runs as root, option does not matter", | |
771 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SupplementaryGroups=", | |
772 | .weight = 200, | |
773 | .range = 1, | |
774 | .assess = assess_supplementary_groups, | |
775 | }, | |
776 | { | |
777 | .id = "PrivateDevices=", | |
3838d22c | 778 | .json_field = "PrivateDevices", |
ec16f3b6 LP |
779 | .description_good = "Service has no access to hardware devices", |
780 | .description_bad = "Service potentially has access to hardware devices", | |
781 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateDevices=", | |
782 | .weight = 1000, | |
783 | .range = 1, | |
784 | .assess = assess_bool, | |
1624114d | 785 | .offset = offsetof(SecurityInfo, private_devices), |
ec16f3b6 LP |
786 | }, |
787 | { | |
788 | .id = "PrivateMounts=", | |
3838d22c | 789 | .json_field = "PrivateMounts", |
ec16f3b6 LP |
790 | .description_good = "Service cannot install system mounts", |
791 | .description_bad = "Service may install system mounts", | |
792 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateMounts=", | |
793 | .weight = 1000, | |
794 | .range = 1, | |
795 | .assess = assess_bool, | |
1624114d | 796 | .offset = offsetof(SecurityInfo, private_mounts), |
ec16f3b6 LP |
797 | }, |
798 | { | |
799 | .id = "PrivateNetwork=", | |
3838d22c | 800 | .json_field = "PrivateNetwork", |
ec16f3b6 LP |
801 | .description_good = "Service has no access to the host's network", |
802 | .description_bad = "Service has access to the host's network", | |
803 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateNetwork=", | |
804 | .weight = 2500, | |
805 | .range = 1, | |
806 | .assess = assess_bool, | |
1624114d | 807 | .offset = offsetof(SecurityInfo, private_network), |
ec16f3b6 LP |
808 | }, |
809 | { | |
810 | .id = "PrivateTmp=", | |
3838d22c | 811 | .json_field = "PrivateTmp", |
ec16f3b6 LP |
812 | .description_good = "Service has no access to other software's temporary files", |
813 | .description_bad = "Service has access to other software's temporary files", | |
814 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateTmp=", | |
815 | .weight = 1000, | |
816 | .range = 1, | |
817 | .assess = assess_bool, | |
1624114d | 818 | .offset = offsetof(SecurityInfo, private_tmp), |
ec16f3b6 LP |
819 | .default_dependencies_only = true, |
820 | }, | |
821 | { | |
822 | .id = "PrivateUsers=", | |
3838d22c | 823 | .json_field = "PrivateUsers", |
ec16f3b6 LP |
824 | .description_good = "Service does not have access to other users", |
825 | .description_bad = "Service has access to other users", | |
826 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateUsers=", | |
827 | .weight = 1000, | |
828 | .range = 1, | |
829 | .assess = assess_bool, | |
1624114d | 830 | .offset = offsetof(SecurityInfo, private_users), |
ec16f3b6 LP |
831 | }, |
832 | { | |
833 | .id = "ProtectControlGroups=", | |
3838d22c | 834 | .json_field = "ProtectControlGroups", |
ec16f3b6 | 835 | .description_good = "Service cannot modify the control group file system", |
287cf2d8 | 836 | .description_bad = "Service may modify the control group file system", |
ec16f3b6 LP |
837 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectControlGroups=", |
838 | .weight = 1000, | |
839 | .range = 1, | |
840 | .assess = assess_bool, | |
1624114d | 841 | .offset = offsetof(SecurityInfo, protect_control_groups), |
ec16f3b6 LP |
842 | }, |
843 | { | |
844 | .id = "ProtectKernelModules=", | |
3838d22c | 845 | .json_field = "ProtectKernelModules", |
ec16f3b6 LP |
846 | .description_good = "Service cannot load or read kernel modules", |
847 | .description_bad = "Service may load or read kernel modules", | |
848 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelModules=", | |
849 | .weight = 1000, | |
850 | .range = 1, | |
851 | .assess = assess_bool, | |
1624114d | 852 | .offset = offsetof(SecurityInfo, protect_kernel_modules), |
ec16f3b6 LP |
853 | }, |
854 | { | |
855 | .id = "ProtectKernelTunables=", | |
3838d22c | 856 | .json_field = "ProtectKernelTunables", |
ec16f3b6 LP |
857 | .description_good = "Service cannot alter kernel tunables (/proc/sys, …)", |
858 | .description_bad = "Service may alter kernel tunables", | |
859 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelTunables=", | |
860 | .weight = 1000, | |
861 | .range = 1, | |
862 | .assess = assess_bool, | |
1624114d | 863 | .offset = offsetof(SecurityInfo, protect_kernel_tunables), |
ec16f3b6 | 864 | }, |
82dce83b KK |
865 | { |
866 | .id = "ProtectKernelLogs=", | |
3838d22c | 867 | .json_field = "ProtectKernelLogs", |
82dce83b KK |
868 | .description_good = "Service cannot read from or write to the kernel log ring buffer", |
869 | .description_bad = "Service may read from or write to the kernel log ring buffer", | |
870 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelLogs=", | |
871 | .weight = 1000, | |
872 | .range = 1, | |
873 | .assess = assess_bool, | |
1624114d | 874 | .offset = offsetof(SecurityInfo, protect_kernel_logs), |
82dce83b | 875 | }, |
9f37272a KK |
876 | { |
877 | .id = "ProtectClock=", | |
3838d22c | 878 | .json_field = "ProtectClock", |
9f37272a KK |
879 | .description_good = "Service cannot write to the hardware clock or system clock", |
880 | .description_bad = "Service may write to the hardware clock or system clock", | |
881 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectClock=", | |
882 | .weight = 1000, | |
883 | .range = 1, | |
884 | .assess = assess_bool, | |
1624114d | 885 | .offset = offsetof(SecurityInfo, protect_clock), |
9f37272a | 886 | }, |
ec16f3b6 LP |
887 | { |
888 | .id = "ProtectHome=", | |
3838d22c | 889 | .json_field = "ProtectHome", |
ec16f3b6 LP |
890 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHome=", |
891 | .weight = 1000, | |
892 | .range = 10, | |
893 | .assess = assess_protect_home, | |
894 | .default_dependencies_only = true, | |
895 | }, | |
527bd7f1 TM |
896 | { |
897 | .id = "ProtectHostname=", | |
3838d22c | 898 | .json_field = "ProtectHostname", |
527bd7f1 TM |
899 | .description_good = "Service cannot change system host/domainname", |
900 | .description_bad = "Service may change system host/domainname", | |
901 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHostname=", | |
902 | .weight = 50, | |
903 | .range = 1, | |
904 | .assess = assess_bool, | |
1624114d | 905 | .offset = offsetof(SecurityInfo, protect_hostname), |
527bd7f1 | 906 | }, |
ec16f3b6 LP |
907 | { |
908 | .id = "ProtectSystem=", | |
3838d22c | 909 | .json_field = "ProtectSystem", |
ec16f3b6 LP |
910 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=", |
911 | .weight = 1000, | |
912 | .range = 10, | |
913 | .assess = assess_protect_system, | |
914 | .default_dependencies_only = true, | |
915 | }, | |
916 | { | |
917 | .id = "RootDirectory=/RootImage=", | |
3838d22c | 918 | .json_field = "RootDirectoryOrRootImage", |
ec16f3b6 LP |
919 | .description_good = "Service has its own root directory/image", |
920 | .description_bad = "Service runs within the host's root directory", | |
921 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RootDirectory=", | |
922 | .weight = 200, | |
923 | .range = 1, | |
924 | .assess = assess_root_directory, | |
925 | .default_dependencies_only = true, | |
926 | }, | |
927 | { | |
928 | .id = "LockPersonality=", | |
3838d22c | 929 | .json_field = "LockPersonality", |
ec16f3b6 LP |
930 | .description_good = "Service cannot change ABI personality", |
931 | .description_bad = "Service may change ABI personality", | |
932 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#LockPersonality=", | |
933 | .weight = 100, | |
934 | .range = 1, | |
935 | .assess = assess_bool, | |
1624114d | 936 | .offset = offsetof(SecurityInfo, lock_personality), |
ec16f3b6 LP |
937 | }, |
938 | { | |
939 | .id = "MemoryDenyWriteExecute=", | |
3838d22c | 940 | .json_field = "MemoryDenyWriteExecute", |
ec16f3b6 LP |
941 | .description_good = "Service cannot create writable executable memory mappings", |
942 | .description_bad = "Service may create writable executable memory mappings", | |
943 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute=", | |
944 | .weight = 100, | |
945 | .range = 1, | |
946 | .assess = assess_bool, | |
1624114d | 947 | .offset = offsetof(SecurityInfo, memory_deny_write_execute), |
ec16f3b6 LP |
948 | }, |
949 | { | |
950 | .id = "NoNewPrivileges=", | |
3838d22c | 951 | .json_field = "NoNewPrivileges", |
ec16f3b6 LP |
952 | .description_good = "Service processes cannot acquire new privileges", |
953 | .description_bad = "Service processes may acquire new privileges", | |
954 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NoNewPrivileges=", | |
955 | .weight = 1000, | |
956 | .range = 1, | |
957 | .assess = assess_bool, | |
1624114d | 958 | .offset = offsetof(SecurityInfo, no_new_privileges), |
ec16f3b6 LP |
959 | }, |
960 | { | |
961 | .id = "CapabilityBoundingSet=~CAP_SYS_ADMIN", | |
3838d22c | 962 | .json_field = "CapabilityBoundingSet_CAP_SYS_ADMIN", |
ec16f3b6 LP |
963 | .description_good = "Service has no administrator privileges", |
964 | .description_bad = "Service has administrator privileges", | |
965 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
966 | .weight = 1500, | |
967 | .range = 1, | |
968 | .assess = assess_capability_bounding_set, | |
969 | .parameter = UINT64_C(1) << CAP_SYS_ADMIN, | |
970 | }, | |
971 | { | |
972 | .id = "CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)", | |
3838d22c | 973 | .json_field = "CapabilityBoundingSet_CAP_SET_UID_GID_PCAP", |
ec16f3b6 LP |
974 | .description_good = "Service cannot change UID/GID identities/capabilities", |
975 | .description_bad = "Service may change UID/GID identities/capabilities", | |
976 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
977 | .weight = 1500, | |
978 | .range = 1, | |
979 | .assess = assess_capability_bounding_set, | |
980 | .parameter = (UINT64_C(1) << CAP_SETUID)| | |
981 | (UINT64_C(1) << CAP_SETGID)| | |
982 | (UINT64_C(1) << CAP_SETPCAP), | |
983 | }, | |
984 | { | |
985 | .id = "CapabilityBoundingSet=~CAP_SYS_PTRACE", | |
3838d22c | 986 | .json_field = "CapabilityBoundingSet_CAP_SYS_PTRACE", |
ec16f3b6 LP |
987 | .description_good = "Service has no ptrace() debugging abilities", |
988 | .description_bad = "Service has ptrace() debugging abilities", | |
989 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
990 | .weight = 1500, | |
991 | .range = 1, | |
992 | .assess = assess_capability_bounding_set, | |
993 | .parameter = (UINT64_C(1) << CAP_SYS_PTRACE), | |
994 | }, | |
995 | { | |
996 | .id = "CapabilityBoundingSet=~CAP_SYS_TIME", | |
3838d22c | 997 | .json_field = "CapabilityBoundingSet_CAP_SYS_TIME", |
ec16f3b6 LP |
998 | .description_good = "Service processes cannot change the system clock", |
999 | .description_bad = "Service processes may change the system clock", | |
1000 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1001 | .weight = 1000, | |
1002 | .range = 1, | |
1003 | .assess = assess_capability_bounding_set, | |
1004 | .parameter = UINT64_C(1) << CAP_SYS_TIME, | |
1005 | }, | |
1006 | { | |
1007 | .id = "CapabilityBoundingSet=~CAP_NET_ADMIN", | |
3838d22c | 1008 | .json_field = "CapabilityBoundingSet_CAP_NET_ADMIN", |
ec16f3b6 LP |
1009 | .description_good = "Service has no network configuration privileges", |
1010 | .description_bad = "Service has network configuration privileges", | |
1011 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1012 | .weight = 1000, | |
1013 | .range = 1, | |
1014 | .assess = assess_capability_bounding_set, | |
1015 | .parameter = (UINT64_C(1) << CAP_NET_ADMIN), | |
1016 | }, | |
1017 | { | |
b5ef6610 | 1018 | .id = "CapabilityBoundingSet=~CAP_SYS_RAWIO", |
3838d22c | 1019 | .json_field = "CapabilityBoundingSet_CAP_SYS_RAWIO", |
ec16f3b6 LP |
1020 | .description_good = "Service has no raw I/O access", |
1021 | .description_bad = "Service has raw I/O access", | |
1022 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1023 | .weight = 1000, | |
1024 | .range = 1, | |
1025 | .assess = assess_capability_bounding_set, | |
1026 | .parameter = (UINT64_C(1) << CAP_SYS_RAWIO), | |
1027 | }, | |
1028 | { | |
1029 | .id = "CapabilityBoundingSet=~CAP_SYS_MODULE", | |
3838d22c | 1030 | .json_field = "CapabilityBoundingSet_CAP_SYS_MODULE", |
ec16f3b6 LP |
1031 | .description_good = "Service cannot load kernel modules", |
1032 | .description_bad = "Service may load kernel modules", | |
1033 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1034 | .weight = 1000, | |
1035 | .range = 1, | |
1036 | .assess = assess_capability_bounding_set, | |
1037 | .parameter = (UINT64_C(1) << CAP_SYS_MODULE), | |
1038 | }, | |
1039 | { | |
1040 | .id = "CapabilityBoundingSet=~CAP_AUDIT_*", | |
3838d22c | 1041 | .json_field = "CapabilityBoundingSet_CAP_AUDIT", |
ec16f3b6 LP |
1042 | .description_good = "Service has no audit subsystem access", |
1043 | .description_bad = "Service has audit subsystem access", | |
1044 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1045 | .weight = 500, | |
1046 | .range = 1, | |
1047 | .assess = assess_capability_bounding_set, | |
1048 | .parameter = (UINT64_C(1) << CAP_AUDIT_CONTROL) | | |
1049 | (UINT64_C(1) << CAP_AUDIT_READ) | | |
1050 | (UINT64_C(1) << CAP_AUDIT_WRITE), | |
1051 | }, | |
1052 | { | |
1053 | .id = "CapabilityBoundingSet=~CAP_SYSLOG", | |
3838d22c | 1054 | .json_field = "CapabilityBoundingSet_CAP_SYSLOG", |
ec16f3b6 LP |
1055 | .description_good = "Service has no access to kernel logging", |
1056 | .description_bad = "Service has access to kernel logging", | |
1057 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1058 | .weight = 500, | |
1059 | .range = 1, | |
1060 | .assess = assess_capability_bounding_set, | |
1061 | .parameter = (UINT64_C(1) << CAP_SYSLOG), | |
1062 | }, | |
1063 | { | |
1064 | .id = "CapabilityBoundingSet=~CAP_SYS_(NICE|RESOURCE)", | |
3838d22c | 1065 | .json_field = "CapabilityBoundingSet_CAP_SYS_NICE_RESOURCE", |
ec16f3b6 LP |
1066 | .description_good = "Service has no privileges to change resource use parameters", |
1067 | .description_bad = "Service has privileges to change resource use parameters", | |
1068 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1069 | .weight = 500, | |
1070 | .range = 1, | |
1071 | .assess = assess_capability_bounding_set, | |
1072 | .parameter = (UINT64_C(1) << CAP_SYS_NICE) | | |
1073 | (UINT64_C(1) << CAP_SYS_RESOURCE), | |
1074 | }, | |
1075 | { | |
1076 | .id = "CapabilityBoundingSet=~CAP_MKNOD", | |
3838d22c | 1077 | .json_field = "CapabilityBoundingSet_CAP_MKNOD", |
ec16f3b6 LP |
1078 | .description_good = "Service cannot create device nodes", |
1079 | .description_bad = "Service may create device nodes", | |
1080 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1081 | .weight = 500, | |
1082 | .range = 1, | |
1083 | .assess = assess_capability_bounding_set, | |
1084 | .parameter = (UINT64_C(1) << CAP_MKNOD), | |
1085 | }, | |
1086 | { | |
1087 | .id = "CapabilityBoundingSet=~CAP_(CHOWN|FSETID|SETFCAP)", | |
3838d22c | 1088 | .json_field = "CapabilityBoundingSet_CAP_CHOWN_FSETID_SETFCAP", |
ec16f3b6 LP |
1089 | .description_good = "Service cannot change file ownership/access mode/capabilities", |
1090 | .description_bad = "Service may change file ownership/access mode/capabilities unrestricted", | |
1091 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1092 | .weight = 1000, | |
1093 | .range = 1, | |
1094 | .assess = assess_capability_bounding_set, | |
1095 | .parameter = (UINT64_C(1) << CAP_CHOWN) | | |
1096 | (UINT64_C(1) << CAP_FSETID) | | |
1097 | (UINT64_C(1) << CAP_SETFCAP), | |
1098 | }, | |
1099 | { | |
1100 | .id = "CapabilityBoundingSet=~CAP_(DAC_*|FOWNER|IPC_OWNER)", | |
3838d22c | 1101 | .json_field = "CapabilityBoundingSet_CAP_DAC_FOWNER_IPC_OWNER", |
ec16f3b6 LP |
1102 | .description_good = "Service cannot override UNIX file/IPC permission checks", |
1103 | .description_bad = "Service may override UNIX file/IPC permission checks", | |
1104 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1105 | .weight = 1000, | |
1106 | .range = 1, | |
1107 | .assess = assess_capability_bounding_set, | |
1108 | .parameter = (UINT64_C(1) << CAP_DAC_OVERRIDE) | | |
1109 | (UINT64_C(1) << CAP_DAC_READ_SEARCH) | | |
1110 | (UINT64_C(1) << CAP_FOWNER) | | |
1111 | (UINT64_C(1) << CAP_IPC_OWNER), | |
1112 | }, | |
1113 | { | |
1114 | .id = "CapabilityBoundingSet=~CAP_KILL", | |
3838d22c | 1115 | .json_field = "CapabilityBoundingSet_CAP_KILL", |
ec16f3b6 LP |
1116 | .description_good = "Service cannot send UNIX signals to arbitrary processes", |
1117 | .description_bad = "Service may send UNIX signals to arbitrary processes", | |
1118 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1119 | .weight = 500, | |
1120 | .range = 1, | |
1121 | .assess = assess_capability_bounding_set, | |
1122 | .parameter = (UINT64_C(1) << CAP_KILL), | |
1123 | }, | |
1124 | { | |
1125 | .id = "CapabilityBoundingSet=~CAP_NET_(BIND_SERVICE|BROADCAST|RAW)", | |
3838d22c | 1126 | .json_field = "CapabilityBoundingSet_CAP_NET_BIND_SERVICE_BROADCAST_RAW)", |
ec16f3b6 LP |
1127 | .description_good = "Service has no elevated networking privileges", |
1128 | .description_bad = "Service has elevated networking privileges", | |
1129 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1130 | .weight = 500, | |
1131 | .range = 1, | |
1132 | .assess = assess_capability_bounding_set, | |
1133 | .parameter = (UINT64_C(1) << CAP_NET_BIND_SERVICE) | | |
1134 | (UINT64_C(1) << CAP_NET_BROADCAST) | | |
1135 | (UINT64_C(1) << CAP_NET_RAW), | |
1136 | }, | |
1137 | { | |
1138 | .id = "CapabilityBoundingSet=~CAP_SYS_BOOT", | |
3838d22c | 1139 | .json_field = "CapabilityBoundingSet_CAP_SYS_BOOT", |
ec16f3b6 LP |
1140 | .description_good = "Service cannot issue reboot()", |
1141 | .description_bad = "Service may issue reboot()", | |
1142 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1143 | .weight = 100, | |
1144 | .range = 1, | |
1145 | .assess = assess_capability_bounding_set, | |
1146 | .parameter = (UINT64_C(1) << CAP_SYS_BOOT), | |
1147 | }, | |
1148 | { | |
1149 | .id = "CapabilityBoundingSet=~CAP_MAC_*", | |
3838d22c | 1150 | .json_field = "CapabilityBoundingSet_CAP_MAC", |
ec16f3b6 LP |
1151 | .description_good = "Service cannot adjust SMACK MAC", |
1152 | .description_bad = "Service may adjust SMACK MAC", | |
1153 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1154 | .weight = 100, | |
1155 | .range = 1, | |
1156 | .assess = assess_capability_bounding_set, | |
1157 | .parameter = (UINT64_C(1) << CAP_MAC_ADMIN)| | |
1158 | (UINT64_C(1) << CAP_MAC_OVERRIDE), | |
1159 | }, | |
1160 | { | |
1161 | .id = "CapabilityBoundingSet=~CAP_LINUX_IMMUTABLE", | |
3838d22c | 1162 | .json_field = "CapabilityBoundingSet_CAP_LINUX_IMMUTABLE", |
ec16f3b6 LP |
1163 | .description_good = "Service cannot mark files immutable", |
1164 | .description_bad = "Service may mark files immutable", | |
1165 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1166 | .weight = 75, | |
1167 | .range = 1, | |
1168 | .assess = assess_capability_bounding_set, | |
1169 | .parameter = (UINT64_C(1) << CAP_LINUX_IMMUTABLE), | |
1170 | }, | |
1171 | { | |
1172 | .id = "CapabilityBoundingSet=~CAP_IPC_LOCK", | |
3838d22c | 1173 | .json_field = "CapabilityBoundingSet_CAP_IPC_LOCK", |
ec16f3b6 LP |
1174 | .description_good = "Service cannot lock memory into RAM", |
1175 | .description_bad = "Service may lock memory into RAM", | |
1176 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1177 | .weight = 50, | |
1178 | .range = 1, | |
1179 | .assess = assess_capability_bounding_set, | |
1180 | .parameter = (UINT64_C(1) << CAP_IPC_LOCK), | |
1181 | }, | |
1182 | { | |
1183 | .id = "CapabilityBoundingSet=~CAP_SYS_CHROOT", | |
3838d22c | 1184 | .json_field = "CapabilityBoundingSet_CAP_SYS_CHROOT", |
ec16f3b6 LP |
1185 | .description_good = "Service cannot issue chroot()", |
1186 | .description_bad = "Service may issue chroot()", | |
1187 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1188 | .weight = 50, | |
1189 | .range = 1, | |
1190 | .assess = assess_capability_bounding_set, | |
1191 | .parameter = (UINT64_C(1) << CAP_SYS_CHROOT), | |
1192 | }, | |
1193 | { | |
1194 | .id = "CapabilityBoundingSet=~CAP_BLOCK_SUSPEND", | |
3838d22c | 1195 | .json_field = "CapabilityBoundingSet_CAP_BLOCK_SUSPEND", |
ec16f3b6 LP |
1196 | .description_good = "Service cannot establish wake locks", |
1197 | .description_bad = "Service may establish wake locks", | |
1198 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1199 | .weight = 25, | |
1200 | .range = 1, | |
1201 | .assess = assess_capability_bounding_set, | |
1202 | .parameter = (UINT64_C(1) << CAP_BLOCK_SUSPEND), | |
1203 | }, | |
1204 | { | |
1205 | .id = "CapabilityBoundingSet=~CAP_WAKE_ALARM", | |
3838d22c | 1206 | .json_field = "CapabilityBoundingSet_CAP_WAKE_ALARM", |
ec16f3b6 LP |
1207 | .description_good = "Service cannot program timers that wake up the system", |
1208 | .description_bad = "Service may program timers that wake up the system", | |
1209 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1210 | .weight = 25, | |
1211 | .range = 1, | |
1212 | .assess = assess_capability_bounding_set, | |
1213 | .parameter = (UINT64_C(1) << CAP_WAKE_ALARM), | |
1214 | }, | |
1215 | { | |
1216 | .id = "CapabilityBoundingSet=~CAP_LEASE", | |
3838d22c | 1217 | .json_field = "CapabilityBoundingSet_CAP_LEASE", |
ec16f3b6 LP |
1218 | .description_good = "Service cannot create file leases", |
1219 | .description_bad = "Service may create file leases", | |
1220 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1221 | .weight = 25, | |
1222 | .range = 1, | |
1223 | .assess = assess_capability_bounding_set, | |
1224 | .parameter = (UINT64_C(1) << CAP_LEASE), | |
1225 | }, | |
1226 | { | |
1227 | .id = "CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG", | |
3838d22c | 1228 | .json_field = "CapabilityBoundingSet_CAP_SYS_TTY_CONFIG", |
ec16f3b6 LP |
1229 | .description_good = "Service cannot issue vhangup()", |
1230 | .description_bad = "Service may issue vhangup()", | |
1231 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1232 | .weight = 25, | |
1233 | .range = 1, | |
1234 | .assess = assess_capability_bounding_set, | |
1235 | .parameter = (UINT64_C(1) << CAP_SYS_TTY_CONFIG), | |
1236 | }, | |
1237 | { | |
1238 | .id = "CapabilityBoundingSet=~CAP_SYS_PACCT", | |
3838d22c | 1239 | .json_field = "CapabilityBoundingSet_CAP_SYS_PACCT", |
ec16f3b6 LP |
1240 | .description_good = "Service cannot use acct()", |
1241 | .description_bad = "Service may use acct()", | |
1242 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=", | |
1243 | .weight = 25, | |
1244 | .range = 1, | |
1245 | .assess = assess_capability_bounding_set, | |
1246 | .parameter = (UINT64_C(1) << CAP_SYS_PACCT), | |
1247 | }, | |
1248 | { | |
1249 | .id = "UMask=", | |
3838d22c | 1250 | .json_field = "UMask", |
ec16f3b6 LP |
1251 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#UMask=", |
1252 | .weight = 100, | |
1253 | .range = 10, | |
1254 | .assess = assess_umask, | |
1255 | }, | |
1256 | { | |
1257 | .id = "KeyringMode=", | |
3838d22c | 1258 | .json_field = "KeyringMode", |
ec16f3b6 LP |
1259 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#KeyringMode=", |
1260 | .description_good = "Service doesn't share key material with other services", | |
1261 | .description_bad = "Service shares key material with other service", | |
1262 | .weight = 1000, | |
1263 | .range = 1, | |
1264 | .assess = assess_keyring_mode, | |
1265 | }, | |
ed125c93 LP |
1266 | { |
1267 | .id = "ProtectProc=", | |
3838d22c | 1268 | .json_field = "ProtectProc", |
ed125c93 LP |
1269 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectProc=", |
1270 | .description_good = "Service has restricted access to process tree (/proc hidepid=)", | |
1271 | .description_bad = "Service has full access to process tree (/proc hidepid=)", | |
1272 | .weight = 1000, | |
1273 | .range = 3, | |
1274 | .assess = assess_protect_proc, | |
1275 | }, | |
1276 | { | |
1277 | .id = "ProcSubset=", | |
3838d22c | 1278 | .json_field = "ProcSubset", |
ed125c93 LP |
1279 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProcSubset=", |
1280 | .description_good = "Service has no access to non-process /proc files (/proc subset=)", | |
1281 | .description_bad = "Service has full access to non-process /proc files (/proc subset=)", | |
1282 | .weight = 10, | |
1283 | .range = 1, | |
1284 | .assess = assess_proc_subset, | |
1285 | }, | |
ec16f3b6 LP |
1286 | { |
1287 | .id = "NotifyAccess=", | |
3838d22c | 1288 | .json_field = "NotifyAccess", |
ec16f3b6 LP |
1289 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NotifyAccess=", |
1290 | .description_good = "Service child processes cannot alter service state", | |
1291 | .description_bad = "Service child processes may alter service state", | |
1292 | .weight = 1000, | |
1293 | .range = 1, | |
1294 | .assess = assess_notify_access, | |
1295 | }, | |
1296 | { | |
1297 | .id = "RemoveIPC=", | |
3838d22c | 1298 | .json_field = "RemoveIPC", |
ec16f3b6 LP |
1299 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RemoveIPC=", |
1300 | .description_good = "Service user cannot leave SysV IPC objects around", | |
1301 | .description_bad = "Service user may leave SysV IPC objects around", | |
1302 | .description_na = "Service runs as root, option does not apply", | |
1303 | .weight = 100, | |
1304 | .range = 1, | |
1305 | .assess = assess_remove_ipc, | |
1624114d | 1306 | .offset = offsetof(SecurityInfo, remove_ipc), |
ec16f3b6 LP |
1307 | }, |
1308 | { | |
1309 | .id = "Delegate=", | |
3838d22c | 1310 | .json_field = "Delegate", |
ec16f3b6 LP |
1311 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Delegate=", |
1312 | .description_good = "Service does not maintain its own delegated control group subtree", | |
1313 | .description_bad = "Service maintains its own delegated control group subtree", | |
1314 | .weight = 100, | |
1315 | .range = 1, | |
1316 | .assess = assess_bool, | |
1624114d | 1317 | .offset = offsetof(SecurityInfo, delegate), |
ec16f3b6 LP |
1318 | .parameter = true, /* invert! */ |
1319 | }, | |
1320 | { | |
1321 | .id = "RestrictRealtime=", | |
3838d22c | 1322 | .json_field = "RestrictRealtime", |
ec16f3b6 LP |
1323 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictRealtime=", |
1324 | .description_good = "Service realtime scheduling access is restricted", | |
1325 | .description_bad = "Service may acquire realtime scheduling", | |
1326 | .weight = 500, | |
1327 | .range = 1, | |
1328 | .assess = assess_bool, | |
1624114d | 1329 | .offset = offsetof(SecurityInfo, restrict_realtime), |
ec16f3b6 | 1330 | }, |
9d880b70 LP |
1331 | { |
1332 | .id = "RestrictSUIDSGID=", | |
3838d22c | 1333 | .json_field = "RestrictSUIDSGID", |
9d880b70 LP |
1334 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictSUIDSGID=", |
1335 | .description_good = "SUID/SGID file creation by service is restricted", | |
1336 | .description_bad = "Service may create SUID/SGID files", | |
1337 | .weight = 1000, | |
1338 | .range = 1, | |
1339 | .assess = assess_bool, | |
1624114d | 1340 | .offset = offsetof(SecurityInfo, restrict_suid_sgid), |
9d880b70 | 1341 | }, |
ec16f3b6 | 1342 | { |
c1e6f215 KL |
1343 | .id = "RestrictNamespaces=~user", |
1344 | .json_field = "RestrictNamespaces_user", | |
ec16f3b6 LP |
1345 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1346 | .description_good = "Service cannot create user namespaces", | |
1347 | .description_bad = "Service may create user namespaces", | |
1348 | .weight = 1500, | |
1349 | .range = 1, | |
1350 | .assess = assess_restrict_namespaces, | |
1351 | .parameter = CLONE_NEWUSER, | |
1352 | }, | |
1353 | { | |
c1e6f215 KL |
1354 | .id = "RestrictNamespaces=~mnt", |
1355 | .json_field = "RestrictNamespaces_mnt", | |
ec16f3b6 LP |
1356 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1357 | .description_good = "Service cannot create file system namespaces", | |
1358 | .description_bad = "Service may create file system namespaces", | |
1359 | .weight = 500, | |
1360 | .range = 1, | |
1361 | .assess = assess_restrict_namespaces, | |
1362 | .parameter = CLONE_NEWNS, | |
1363 | }, | |
1364 | { | |
c1e6f215 KL |
1365 | .id = "RestrictNamespaces=~ipc", |
1366 | .json_field = "RestrictNamespaces_ipc", | |
ec16f3b6 LP |
1367 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1368 | .description_good = "Service cannot create IPC namespaces", | |
1369 | .description_bad = "Service may create IPC namespaces", | |
1370 | .weight = 500, | |
1371 | .range = 1, | |
1372 | .assess = assess_restrict_namespaces, | |
1373 | .parameter = CLONE_NEWIPC, | |
1374 | }, | |
1375 | { | |
c1e6f215 KL |
1376 | .id = "RestrictNamespaces=~pid", |
1377 | .json_field = "RestrictNamespaces_pid", | |
ec16f3b6 LP |
1378 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1379 | .description_good = "Service cannot create process namespaces", | |
1380 | .description_bad = "Service may create process namespaces", | |
1381 | .weight = 500, | |
1382 | .range = 1, | |
1383 | .assess = assess_restrict_namespaces, | |
1384 | .parameter = CLONE_NEWPID, | |
1385 | }, | |
1386 | { | |
c1e6f215 KL |
1387 | .id = "RestrictNamespaces=~cgroup", |
1388 | .json_field = "RestrictNamespaces_cgroup", | |
ec16f3b6 LP |
1389 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1390 | .description_good = "Service cannot create cgroup namespaces", | |
1391 | .description_bad = "Service may create cgroup namespaces", | |
1392 | .weight = 500, | |
1393 | .range = 1, | |
1394 | .assess = assess_restrict_namespaces, | |
1395 | .parameter = CLONE_NEWCGROUP, | |
1396 | }, | |
1397 | { | |
c1e6f215 KL |
1398 | .id = "RestrictNamespaces=~net", |
1399 | .json_field = "RestrictNamespaces_net", | |
ec16f3b6 LP |
1400 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1401 | .description_good = "Service cannot create network namespaces", | |
1402 | .description_bad = "Service may create network namespaces", | |
1403 | .weight = 500, | |
1404 | .range = 1, | |
1405 | .assess = assess_restrict_namespaces, | |
1406 | .parameter = CLONE_NEWNET, | |
1407 | }, | |
1408 | { | |
c1e6f215 KL |
1409 | .id = "RestrictNamespaces=~uts", |
1410 | .json_field = "RestrictNamespaces_uts", | |
ec16f3b6 LP |
1411 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=", |
1412 | .description_good = "Service cannot create hostname namespaces", | |
1413 | .description_bad = "Service may create hostname namespaces", | |
1414 | .weight = 100, | |
1415 | .range = 1, | |
1416 | .assess = assess_restrict_namespaces, | |
1417 | .parameter = CLONE_NEWUTS, | |
1418 | }, | |
1419 | { | |
1420 | .id = "RestrictAddressFamilies=~AF_(INET|INET6)", | |
3838d22c | 1421 | .json_field = "RestrictAddressFamilies_AF_INET_INET6", |
ec16f3b6 LP |
1422 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=", |
1423 | .description_good = "Service cannot allocate Internet sockets", | |
1424 | .description_bad = "Service may allocate Internet sockets", | |
1425 | .weight = 1500, | |
1426 | .range = 1, | |
1427 | .assess = assess_bool, | |
1624114d | 1428 | .offset = offsetof(SecurityInfo, restrict_address_family_inet), |
ec16f3b6 LP |
1429 | }, |
1430 | { | |
1431 | .id = "RestrictAddressFamilies=~AF_UNIX", | |
3838d22c | 1432 | .json_field = "RestrictAddressFamilies_AF_UNIX", |
ec16f3b6 LP |
1433 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=", |
1434 | .description_good = "Service cannot allocate local sockets", | |
1435 | .description_bad = "Service may allocate local sockets", | |
1436 | .weight = 25, | |
1437 | .range = 1, | |
1438 | .assess = assess_bool, | |
1624114d | 1439 | .offset = offsetof(SecurityInfo, restrict_address_family_unix), |
ec16f3b6 LP |
1440 | }, |
1441 | { | |
1442 | .id = "RestrictAddressFamilies=~AF_NETLINK", | |
3838d22c | 1443 | .json_field = "RestrictAddressFamilies_AF_NETLINK", |
ec16f3b6 LP |
1444 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=", |
1445 | .description_good = "Service cannot allocate netlink sockets", | |
1446 | .description_bad = "Service may allocate netlink sockets", | |
1447 | .weight = 200, | |
1448 | .range = 1, | |
1449 | .assess = assess_bool, | |
1624114d | 1450 | .offset = offsetof(SecurityInfo, restrict_address_family_netlink), |
ec16f3b6 LP |
1451 | }, |
1452 | { | |
1453 | .id = "RestrictAddressFamilies=~AF_PACKET", | |
3838d22c | 1454 | .json_field = "RestrictAddressFamilies_AF_PACKET", |
ec16f3b6 LP |
1455 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=", |
1456 | .description_good = "Service cannot allocate packet sockets", | |
1457 | .description_bad = "Service may allocate packet sockets", | |
1458 | .weight = 1000, | |
1459 | .range = 1, | |
1460 | .assess = assess_bool, | |
1624114d | 1461 | .offset = offsetof(SecurityInfo, restrict_address_family_packet), |
ec16f3b6 LP |
1462 | }, |
1463 | { | |
1464 | .id = "RestrictAddressFamilies=~…", | |
3838d22c | 1465 | .json_field = "RestrictAddressFamilies_OTHER", |
ec16f3b6 LP |
1466 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=", |
1467 | .description_good = "Service cannot allocate exotic sockets", | |
1468 | .description_bad = "Service may allocate exotic sockets", | |
1469 | .weight = 1250, | |
1470 | .range = 1, | |
1471 | .assess = assess_bool, | |
1624114d | 1472 | .offset = offsetof(SecurityInfo, restrict_address_family_other), |
ec16f3b6 LP |
1473 | }, |
1474 | { | |
1475 | .id = "SystemCallArchitectures=", | |
3838d22c | 1476 | .json_field = "SystemCallArchitectures", |
ec16f3b6 LP |
1477 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallArchitectures=", |
1478 | .weight = 1000, | |
1479 | .range = 10, | |
1480 | .assess = assess_system_call_architectures, | |
1481 | }, | |
3a5d89fa | 1482 | #if HAVE_SECCOMP |
ec16f3b6 LP |
1483 | { |
1484 | .id = "SystemCallFilter=~@swap", | |
3838d22c | 1485 | .json_field = "SystemCallFilter_swap", |
ec16f3b6 LP |
1486 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1487 | .weight = 1000, | |
1488 | .range = 10, | |
1489 | .assess = assess_system_call_filter, | |
1490 | .parameter = SYSCALL_FILTER_SET_SWAP, | |
1491 | }, | |
1492 | { | |
1493 | .id = "SystemCallFilter=~@obsolete", | |
3838d22c | 1494 | .json_field = "SystemCallFilter_obsolete", |
ec16f3b6 LP |
1495 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1496 | .weight = 250, | |
1497 | .range = 10, | |
1498 | .assess = assess_system_call_filter, | |
1499 | .parameter = SYSCALL_FILTER_SET_OBSOLETE, | |
1500 | }, | |
1501 | { | |
1502 | .id = "SystemCallFilter=~@clock", | |
3838d22c | 1503 | .json_field = "SystemCallFilter_clock", |
ec16f3b6 LP |
1504 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1505 | .weight = 1000, | |
1506 | .range = 10, | |
1507 | .assess = assess_system_call_filter, | |
1508 | .parameter = SYSCALL_FILTER_SET_CLOCK, | |
1509 | }, | |
1510 | { | |
1511 | .id = "SystemCallFilter=~@cpu-emulation", | |
3838d22c | 1512 | .json_field = "SystemCallFilter_cpu_emulation", |
ec16f3b6 LP |
1513 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1514 | .weight = 250, | |
1515 | .range = 10, | |
1516 | .assess = assess_system_call_filter, | |
1517 | .parameter = SYSCALL_FILTER_SET_CPU_EMULATION, | |
1518 | }, | |
1519 | { | |
1520 | .id = "SystemCallFilter=~@debug", | |
3838d22c | 1521 | .json_field = "SystemCallFilter_debug", |
ec16f3b6 LP |
1522 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1523 | .weight = 1000, | |
1524 | .range = 10, | |
1525 | .assess = assess_system_call_filter, | |
1526 | .parameter = SYSCALL_FILTER_SET_DEBUG, | |
1527 | }, | |
1528 | { | |
1529 | .id = "SystemCallFilter=~@mount", | |
3838d22c | 1530 | .json_field = "SystemCallFilter_mount", |
ec16f3b6 LP |
1531 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1532 | .weight = 1000, | |
1533 | .range = 10, | |
1534 | .assess = assess_system_call_filter, | |
1535 | .parameter = SYSCALL_FILTER_SET_MOUNT, | |
1536 | }, | |
1537 | { | |
1538 | .id = "SystemCallFilter=~@module", | |
3838d22c | 1539 | .json_field = "SystemCallFilter_module", |
ec16f3b6 LP |
1540 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1541 | .weight = 1000, | |
1542 | .range = 10, | |
1543 | .assess = assess_system_call_filter, | |
1544 | .parameter = SYSCALL_FILTER_SET_MODULE, | |
1545 | }, | |
1546 | { | |
1547 | .id = "SystemCallFilter=~@raw-io", | |
3838d22c | 1548 | .json_field = "SystemCallFilter_raw_io", |
ec16f3b6 LP |
1549 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1550 | .weight = 1000, | |
1551 | .range = 10, | |
1552 | .assess = assess_system_call_filter, | |
1553 | .parameter = SYSCALL_FILTER_SET_RAW_IO, | |
1554 | }, | |
1555 | { | |
1556 | .id = "SystemCallFilter=~@reboot", | |
3838d22c | 1557 | .json_field = "SystemCallFilter_reboot", |
ec16f3b6 LP |
1558 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1559 | .weight = 1000, | |
1560 | .range = 10, | |
1561 | .assess = assess_system_call_filter, | |
1562 | .parameter = SYSCALL_FILTER_SET_REBOOT, | |
1563 | }, | |
1564 | { | |
1565 | .id = "SystemCallFilter=~@privileged", | |
3838d22c | 1566 | .json_field = "SystemCallFilter_privileged", |
ec16f3b6 LP |
1567 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1568 | .weight = 700, | |
1569 | .range = 10, | |
1570 | .assess = assess_system_call_filter, | |
1571 | .parameter = SYSCALL_FILTER_SET_PRIVILEGED, | |
1572 | }, | |
1573 | { | |
1574 | .id = "SystemCallFilter=~@resources", | |
3838d22c | 1575 | .json_field = "SystemCallFilter_resources", |
ec16f3b6 LP |
1576 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=", |
1577 | .weight = 700, | |
1578 | .range = 10, | |
1579 | .assess = assess_system_call_filter, | |
1580 | .parameter = SYSCALL_FILTER_SET_RESOURCES, | |
1581 | }, | |
3a5d89fa | 1582 | #endif |
ec16f3b6 LP |
1583 | { |
1584 | .id = "IPAddressDeny=", | |
3838d22c | 1585 | .json_field = "IPAddressDeny", |
ec16f3b6 LP |
1586 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#IPAddressDeny=", |
1587 | .weight = 1000, | |
1588 | .range = 10, | |
1589 | .assess = assess_ip_address_allow, | |
1590 | }, | |
1591 | { | |
1592 | .id = "DeviceAllow=", | |
3838d22c | 1593 | .json_field = "DeviceAllow", |
ec16f3b6 LP |
1594 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#DeviceAllow=", |
1595 | .weight = 1000, | |
1596 | .range = 10, | |
1597 | .assess = assess_device_allow, | |
1598 | }, | |
1599 | { | |
1600 | .id = "AmbientCapabilities=", | |
3838d22c | 1601 | .json_field = "AmbientCapabilities", |
ec16f3b6 LP |
1602 | .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#AmbientCapabilities=", |
1603 | .description_good = "Service process does not receive ambient capabilities", | |
1604 | .description_bad = "Service process receives ambient capabilities", | |
1605 | .weight = 500, | |
1606 | .range = 1, | |
1607 | .assess = assess_ambient_capabilities, | |
1608 | }, | |
1609 | }; | |
1610 | ||
3838d22c MG |
1611 | static JsonVariant* security_assessor_find_in_policy(const struct security_assessor *a, JsonVariant *policy, const char *name) { |
1612 | JsonVariant *item; | |
1613 | assert(a); | |
1614 | ||
1615 | if (!policy) | |
1616 | return NULL; | |
1617 | if (!json_variant_is_object(policy)) { | |
05403363 | 1618 | log_debug("Specified policy is not a JSON object, ignoring."); |
3838d22c MG |
1619 | return NULL; |
1620 | } | |
1621 | ||
1622 | item = json_variant_by_key(policy, a->json_field); | |
1623 | if (!item) | |
1624 | return NULL; | |
1625 | if (!json_variant_is_object(item)) { | |
1626 | log_debug("Item for '%s' in policy JSON object is not an object, ignoring.", a->id); | |
1627 | return NULL; | |
1628 | } | |
1629 | ||
1630 | return name ? json_variant_by_key(item, name) : item; | |
1631 | } | |
1632 | ||
1633 | static uint64_t access_weight(const struct security_assessor *a, JsonVariant *policy) { | |
1634 | JsonVariant *val; | |
1635 | ||
1636 | assert(a); | |
1637 | ||
1638 | val = security_assessor_find_in_policy(a, policy, "weight"); | |
1639 | if (val) { | |
1640 | if (json_variant_is_unsigned(val)) | |
1641 | return json_variant_unsigned(val); | |
1642 | log_debug("JSON field 'weight' of policy for %s is not an unsigned integer, ignoring.", a->id); | |
1643 | } | |
1644 | ||
1645 | return a->weight; | |
1646 | } | |
1647 | ||
1648 | static uint64_t access_range(const struct security_assessor *a, JsonVariant *policy) { | |
1649 | JsonVariant *val; | |
1650 | ||
1651 | assert(a); | |
1652 | ||
1653 | val = security_assessor_find_in_policy(a, policy, "range"); | |
1654 | if (val) { | |
1655 | if (json_variant_is_unsigned(val)) | |
1656 | return json_variant_unsigned(val); | |
1657 | log_debug("JSON field 'range' of policy for %s is not an unsigned integer, ignoring.", a->id); | |
1658 | } | |
1659 | ||
1660 | return a->range; | |
1661 | } | |
1662 | ||
1663 | static const char *access_description_na(const struct security_assessor *a, JsonVariant *policy) { | |
1664 | JsonVariant *val; | |
1665 | ||
1666 | assert(a); | |
1667 | ||
1668 | val = security_assessor_find_in_policy(a, policy, "description_na"); | |
1669 | if (val) { | |
1670 | if (json_variant_is_string(val)) | |
1671 | return json_variant_string(val); | |
1672 | log_debug("JSON field 'description_na' of policy for %s is not a string, ignoring.", a->id); | |
1673 | } | |
1674 | ||
1675 | return a->description_na; | |
1676 | } | |
1677 | ||
1678 | static const char *access_description_good(const struct security_assessor *a, JsonVariant *policy) { | |
1679 | JsonVariant *val; | |
1680 | ||
1681 | assert(a); | |
1682 | ||
1683 | val = security_assessor_find_in_policy(a, policy, "description_good"); | |
1684 | if (val) { | |
1685 | if (json_variant_is_string(val)) | |
1686 | return json_variant_string(val); | |
1687 | log_debug("JSON field 'description_good' of policy for %s is not a string, ignoring.", a->id); | |
1688 | } | |
1689 | ||
1690 | return a->description_good; | |
1691 | } | |
1692 | ||
1693 | static const char *access_description_bad(const struct security_assessor *a, JsonVariant *policy) { | |
1694 | JsonVariant *val; | |
1695 | ||
1696 | assert(a); | |
1697 | ||
1698 | val = security_assessor_find_in_policy(a, policy, "description_bad"); | |
1699 | if (val) { | |
1700 | if (json_variant_is_string(val)) | |
1701 | return json_variant_string(val); | |
1702 | log_debug("JSON field 'description_bad' of policy for %s is not a string, ignoring.", a->id); | |
1703 | } | |
1704 | ||
1705 | return a->description_bad; | |
1706 | } | |
1707 | ||
1708 | static int assess(const SecurityInfo *info, | |
1709 | Table *overview_table, | |
1710 | AnalyzeSecurityFlags flags, | |
1711 | unsigned threshold, | |
4b4a8ef7 MG |
1712 | JsonVariant *policy, |
1713 | PagerFlags pager_flags, | |
1714 | JsonFormatFlags json_format_flags) { | |
3838d22c | 1715 | |
ec16f3b6 LP |
1716 | static const struct { |
1717 | uint64_t exposure; | |
1718 | const char *name; | |
1719 | const char *color; | |
1720 | SpecialGlyph smiley; | |
1721 | } badness_table[] = { | |
9a6f746f LP |
1722 | { 100, "DANGEROUS", ANSI_HIGHLIGHT_RED, SPECIAL_GLYPH_DEPRESSED_SMILEY }, |
1723 | { 90, "UNSAFE", ANSI_HIGHLIGHT_RED, SPECIAL_GLYPH_UNHAPPY_SMILEY }, | |
1724 | { 75, "EXPOSED", ANSI_HIGHLIGHT_YELLOW, SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY }, | |
1725 | { 50, "MEDIUM", NULL, SPECIAL_GLYPH_NEUTRAL_SMILEY }, | |
1726 | { 10, "OK", ANSI_HIGHLIGHT_GREEN, SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY }, | |
1727 | { 1, "SAFE", ANSI_HIGHLIGHT_GREEN, SPECIAL_GLYPH_HAPPY_SMILEY }, | |
1728 | { 0, "PERFECT", ANSI_HIGHLIGHT_GREEN, SPECIAL_GLYPH_ECSTATIC_SMILEY }, | |
ec16f3b6 LP |
1729 | }; |
1730 | ||
1731 | uint64_t badness_sum = 0, weight_sum = 0, exposure; | |
1732 | _cleanup_(table_unrefp) Table *details_table = NULL; | |
1733 | size_t i; | |
1734 | int r; | |
1735 | ||
1736 | if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT)) { | |
4b4a8ef7 | 1737 | details_table = table_new(" ", "name", "json_field", "description", "weight", "badness", "range", "exposure"); |
ec16f3b6 LP |
1738 | if (!details_table) |
1739 | return log_oom(); | |
1740 | ||
4b4a8ef7 MG |
1741 | r = table_set_json_field_name(details_table, 0, "set"); |
1742 | if (r < 0) | |
1743 | return log_error_errno(r, "Failed to set JSON field name of column 0: %m"); | |
1744 | ||
ef1e0b9a | 1745 | (void) table_set_sort(details_table, (size_t) 3, (size_t) 1); |
ec16f3b6 LP |
1746 | (void) table_set_reverse(details_table, 3, true); |
1747 | ||
1748 | if (getenv_bool("SYSTEMD_ANALYZE_DEBUG") <= 0) | |
4b4a8ef7 | 1749 | (void) table_set_display(details_table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 7); |
ec16f3b6 LP |
1750 | } |
1751 | ||
1752 | for (i = 0; i < ELEMENTSOF(security_assessor_table); i++) { | |
1753 | const struct security_assessor *a = security_assessor_table + i; | |
1754 | _cleanup_free_ char *d = NULL; | |
1755 | uint64_t badness; | |
1756 | void *data; | |
3838d22c MG |
1757 | uint64_t weight = access_weight(a, policy); |
1758 | uint64_t range = access_range(a, policy); | |
ec16f3b6 | 1759 | |
1ace223c | 1760 | data = (uint8_t *) info + a->offset; |
ec16f3b6 LP |
1761 | |
1762 | if (a->default_dependencies_only && !info->default_dependencies) { | |
1763 | badness = UINT64_MAX; | |
77552b95 | 1764 | d = strdup("Service runs in special boot phase, option is not appropriate"); |
ec16f3b6 | 1765 | if (!d) |
82100ef4 LB |
1766 | return log_oom(); |
1767 | } else if (weight == 0) { | |
1768 | badness = UINT64_MAX; | |
1769 | d = strdup("Option excluded by policy, skipping"); | |
1770 | if (!d) | |
ec16f3b6 LP |
1771 | return log_oom(); |
1772 | } else { | |
1773 | r = a->assess(a, info, data, &badness, &d); | |
1774 | if (r < 0) | |
1775 | return r; | |
1776 | } | |
1777 | ||
3838d22c | 1778 | assert(range > 0); |
ec16f3b6 LP |
1779 | |
1780 | if (badness != UINT64_MAX) { | |
3838d22c | 1781 | assert(badness <= range); |
ec16f3b6 | 1782 | |
3838d22c MG |
1783 | badness_sum += DIV_ROUND_UP(badness * weight, range); |
1784 | weight_sum += weight; | |
ec16f3b6 LP |
1785 | } |
1786 | ||
1787 | if (details_table) { | |
4b4a8ef7 MG |
1788 | const char *description, *color = NULL; |
1789 | int checkmark; | |
ec16f3b6 LP |
1790 | |
1791 | if (badness == UINT64_MAX) { | |
4b4a8ef7 | 1792 | checkmark = -1; |
3838d22c | 1793 | description = access_description_na(a, policy); |
ec16f3b6 LP |
1794 | color = NULL; |
1795 | } else if (badness == a->range) { | |
4b4a8ef7 | 1796 | checkmark = 0; |
3838d22c | 1797 | description = access_description_bad(a, policy); |
ec16f3b6 LP |
1798 | color = ansi_highlight_red(); |
1799 | } else if (badness == 0) { | |
4b4a8ef7 | 1800 | checkmark = 1; |
3838d22c | 1801 | description = access_description_good(a, policy); |
ec16f3b6 LP |
1802 | color = ansi_highlight_green(); |
1803 | } else { | |
4b4a8ef7 | 1804 | checkmark = 0; |
ec16f3b6 LP |
1805 | description = NULL; |
1806 | color = ansi_highlight_red(); | |
1807 | } | |
1808 | ||
1809 | if (d) | |
1810 | description = d; | |
1811 | ||
4b4a8ef7 MG |
1812 | if (checkmark < 0) { |
1813 | r = table_add_many(details_table, TABLE_EMPTY); | |
1814 | if (r < 0) | |
1815 | return table_log_add_error(r); | |
1816 | } else { | |
1817 | r = table_add_many(details_table, | |
1818 | TABLE_BOOLEAN_CHECKMARK, checkmark > 0, | |
1819 | TABLE_SET_MINIMUM_WIDTH, 1, | |
1820 | TABLE_SET_MAXIMUM_WIDTH, 1, | |
1821 | TABLE_SET_ELLIPSIZE_PERCENT, 0, | |
1822 | TABLE_SET_COLOR, color); | |
1823 | if (r < 0) | |
1824 | return table_log_add_error(r); | |
1825 | } | |
3838d22c | 1826 | |
2cb434cf | 1827 | r = table_add_many(details_table, |
4b4a8ef7 MG |
1828 | TABLE_STRING, a->id, TABLE_SET_URL, a->url, |
1829 | TABLE_STRING, a->json_field, | |
2cb434cf | 1830 | TABLE_STRING, description, |
3838d22c | 1831 | TABLE_UINT64, weight, TABLE_SET_ALIGN_PERCENT, 100, |
2cb434cf | 1832 | TABLE_UINT64, badness, TABLE_SET_ALIGN_PERCENT, 100, |
3838d22c | 1833 | TABLE_UINT64, range, TABLE_SET_ALIGN_PERCENT, 100, |
2cb434cf | 1834 | TABLE_EMPTY, TABLE_SET_ALIGN_PERCENT, 100); |
ec16f3b6 | 1835 | if (r < 0) |
9c46b437 | 1836 | return table_log_add_error(r); |
ec16f3b6 LP |
1837 | } |
1838 | } | |
1839 | ||
28a06f5a JS |
1840 | assert(weight_sum > 0); |
1841 | ||
ec16f3b6 LP |
1842 | if (details_table) { |
1843 | size_t row; | |
1844 | ||
1845 | for (row = 1; row < table_get_rows(details_table); row++) { | |
1846 | char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1]; | |
1847 | const uint64_t *weight, *badness, *range; | |
1848 | TableCell *cell; | |
1849 | uint64_t x; | |
1850 | ||
4b4a8ef7 MG |
1851 | assert_se(weight = table_get_at(details_table, row, 4)); |
1852 | assert_se(badness = table_get_at(details_table, row, 5)); | |
1853 | assert_se(range = table_get_at(details_table, row, 6)); | |
ec16f3b6 LP |
1854 | |
1855 | if (*badness == UINT64_MAX || *badness == 0) | |
1856 | continue; | |
1857 | ||
4b4a8ef7 | 1858 | assert_se(cell = table_get_cell(details_table, row, 7)); |
ec16f3b6 LP |
1859 | |
1860 | x = DIV_ROUND_UP(DIV_ROUND_UP(*badness * *weight * 100U, *range), weight_sum); | |
1861 | xsprintf(buf, "%" PRIu64 ".%" PRIu64, x / 10, x % 10); | |
1862 | ||
1863 | r = table_update(details_table, cell, TABLE_STRING, buf); | |
1864 | if (r < 0) | |
1865 | return log_error_errno(r, "Failed to update cell in table: %m"); | |
1866 | } | |
1867 | ||
4b4a8ef7 MG |
1868 | if (json_format_flags & JSON_FORMAT_OFF) { |
1869 | r = table_hide_column_from_display(details_table, (size_t) 2); | |
1870 | if (r < 0) | |
1871 | return log_error_errno(r, "Failed to set columns to display: %m"); | |
1872 | } | |
1873 | ||
1874 | r = table_print_with_pager(details_table, json_format_flags, pager_flags, /* show_header= */true); | |
ec16f3b6 LP |
1875 | if (r < 0) |
1876 | return log_error_errno(r, "Failed to output table: %m"); | |
1877 | } | |
1878 | ||
1879 | exposure = DIV_ROUND_UP(badness_sum * 100U, weight_sum); | |
1880 | ||
1881 | for (i = 0; i < ELEMENTSOF(badness_table); i++) | |
1882 | if (exposure >= badness_table[i].exposure) | |
1883 | break; | |
1884 | ||
1885 | assert(i < ELEMENTSOF(badness_table)); | |
1886 | ||
4b4a8ef7 | 1887 | if (details_table && (json_format_flags & JSON_FORMAT_OFF)) { |
ec16f3b6 LP |
1888 | _cleanup_free_ char *clickable = NULL; |
1889 | const char *name; | |
1890 | ||
1891 | /* If we shall output the details table, also print the brief summary underneath */ | |
1892 | ||
1893 | if (info->fragment_path) { | |
1894 | r = terminal_urlify_path(info->fragment_path, info->id, &clickable); | |
1895 | if (r < 0) | |
1896 | return log_oom(); | |
1897 | ||
1898 | name = clickable; | |
1899 | } else | |
1900 | name = info->id; | |
1901 | ||
1902 | printf("\n%s %sOverall exposure level for %s%s: %s%" PRIu64 ".%" PRIu64 " %s%s %s\n", | |
9a6f746f | 1903 | special_glyph(SPECIAL_GLYPH_ARROW), |
ec16f3b6 LP |
1904 | ansi_highlight(), |
1905 | name, | |
1906 | ansi_normal(), | |
1907 | colors_enabled() ? strempty(badness_table[i].color) : "", | |
1908 | exposure / 10, exposure % 10, | |
1909 | badness_table[i].name, | |
1910 | ansi_normal(), | |
1911 | special_glyph(badness_table[i].smiley)); | |
1912 | } | |
1913 | ||
1914 | fflush(stdout); | |
1915 | ||
1916 | if (overview_table) { | |
1917 | char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1]; | |
9c46b437 | 1918 | _cleanup_free_ char *url = NULL; |
ec16f3b6 | 1919 | |
ec16f3b6 | 1920 | if (info->fragment_path) { |
ec16f3b6 LP |
1921 | r = file_url_from_path(info->fragment_path, &url); |
1922 | if (r < 0) | |
1923 | return log_error_errno(r, "Failed to generate URL from path: %m"); | |
ec16f3b6 LP |
1924 | } |
1925 | ||
1926 | xsprintf(buf, "%" PRIu64 ".%" PRIu64, exposure / 10, exposure % 10); | |
ec16f3b6 | 1927 | |
9c46b437 YW |
1928 | r = table_add_many(overview_table, |
1929 | TABLE_STRING, info->id, | |
1930 | TABLE_SET_URL, url, | |
1931 | TABLE_STRING, buf, | |
1932 | TABLE_SET_ALIGN_PERCENT, 100, | |
1933 | TABLE_STRING, badness_table[i].name, | |
1934 | TABLE_SET_COLOR, strempty(badness_table[i].color), | |
1935 | TABLE_STRING, special_glyph(badness_table[i].smiley)); | |
ec16f3b6 | 1936 | if (r < 0) |
9c46b437 | 1937 | return table_log_add_error(r); |
ec16f3b6 LP |
1938 | } |
1939 | ||
dfbda879 MG |
1940 | /* Return error when overall exposure level is over threshold */ |
1941 | if (exposure > threshold) | |
1942 | return -EINVAL; | |
1943 | ||
ec16f3b6 LP |
1944 | return 0; |
1945 | } | |
1946 | ||
1624114d MG |
1947 | static int property_read_restrict_namespaces( |
1948 | sd_bus *bus, | |
1949 | const char *member, | |
1950 | sd_bus_message *m, | |
1951 | sd_bus_error *error, | |
1952 | void *userdata) { | |
1953 | ||
1954 | SecurityInfo *info = userdata; | |
1955 | int r; | |
1956 | uint64_t namespaces; | |
1957 | ||
1958 | assert(bus); | |
1959 | assert(member); | |
1960 | assert(m); | |
1961 | assert(info); | |
1962 | ||
1963 | r = sd_bus_message_read(m, "t", &namespaces); | |
1964 | if (r < 0) | |
1965 | return r; | |
1966 | ||
1967 | info->restrict_namespaces = (unsigned long long) namespaces; | |
1968 | ||
1969 | return 0; | |
1970 | } | |
1971 | ||
1972 | static int property_read_umask( | |
1973 | sd_bus *bus, | |
1974 | const char *member, | |
1975 | sd_bus_message *m, | |
1976 | sd_bus_error *error, | |
1977 | void *userdata) { | |
1978 | ||
1979 | SecurityInfo *info = userdata; | |
1980 | int r; | |
1981 | uint32_t umask; | |
1982 | ||
1983 | assert(bus); | |
1984 | assert(member); | |
1985 | assert(m); | |
1986 | assert(info); | |
1987 | ||
1988 | r = sd_bus_message_read(m, "u", &umask); | |
1989 | if (r < 0) | |
1990 | return r; | |
1991 | ||
1992 | info->_umask = (mode_t) umask; | |
1993 | ||
1994 | return 0; | |
1995 | } | |
1996 | ||
ec16f3b6 LP |
1997 | static int property_read_restrict_address_families( |
1998 | sd_bus *bus, | |
1999 | const char *member, | |
2000 | sd_bus_message *m, | |
2001 | sd_bus_error *error, | |
2002 | void *userdata) { | |
2003 | ||
1624114d | 2004 | SecurityInfo *info = userdata; |
6b000af4 | 2005 | int allow_list, r; |
ec16f3b6 LP |
2006 | |
2007 | assert(bus); | |
2008 | assert(member); | |
2009 | assert(m); | |
2010 | ||
2011 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
2012 | if (r < 0) | |
2013 | return r; | |
2014 | ||
6b000af4 | 2015 | r = sd_bus_message_read(m, "b", &allow_list); |
ec16f3b6 LP |
2016 | if (r < 0) |
2017 | return r; | |
2018 | ||
2019 | info->restrict_address_family_inet = | |
2020 | info->restrict_address_family_unix = | |
2021 | info->restrict_address_family_netlink = | |
2022 | info->restrict_address_family_packet = | |
6b000af4 | 2023 | info->restrict_address_family_other = allow_list; |
ec16f3b6 LP |
2024 | |
2025 | r = sd_bus_message_enter_container(m, 'a', "s"); | |
2026 | if (r < 0) | |
2027 | return r; | |
2028 | ||
2029 | for (;;) { | |
2030 | const char *name; | |
2031 | ||
2032 | r = sd_bus_message_read(m, "s", &name); | |
2033 | if (r < 0) | |
2034 | return r; | |
2035 | if (r == 0) | |
2036 | break; | |
2037 | ||
2038 | if (STR_IN_SET(name, "AF_INET", "AF_INET6")) | |
6b000af4 | 2039 | info->restrict_address_family_inet = !allow_list; |
ec16f3b6 | 2040 | else if (streq(name, "AF_UNIX")) |
6b000af4 | 2041 | info->restrict_address_family_unix = !allow_list; |
ec16f3b6 | 2042 | else if (streq(name, "AF_NETLINK")) |
6b000af4 | 2043 | info->restrict_address_family_netlink = !allow_list; |
ec16f3b6 | 2044 | else if (streq(name, "AF_PACKET")) |
6b000af4 | 2045 | info->restrict_address_family_packet = !allow_list; |
ec16f3b6 | 2046 | else |
6b000af4 | 2047 | info->restrict_address_family_other = !allow_list; |
ec16f3b6 LP |
2048 | } |
2049 | ||
2050 | r = sd_bus_message_exit_container(m); | |
2051 | if (r < 0) | |
2052 | return r; | |
2053 | ||
2054 | return sd_bus_message_exit_container(m); | |
2055 | } | |
2056 | ||
1624114d MG |
2057 | static int property_read_syscall_archs( |
2058 | sd_bus *bus, | |
2059 | const char *member, | |
2060 | sd_bus_message *m, | |
2061 | sd_bus_error *error, | |
2062 | void *userdata) { | |
2063 | ||
2064 | SecurityInfo *info = userdata; | |
2065 | int r; | |
2066 | ||
2067 | assert(bus); | |
2068 | assert(member); | |
2069 | assert(m); | |
2070 | assert(info); | |
2071 | ||
2072 | r = sd_bus_message_enter_container(m, 'a', "s"); | |
2073 | if (r < 0) | |
2074 | return r; | |
2075 | ||
2076 | for (;;) { | |
2077 | const char *name; | |
2078 | ||
2079 | r = sd_bus_message_read(m, "s", &name); | |
2080 | if (r < 0) | |
2081 | return r; | |
2082 | if (r == 0) | |
2083 | break; | |
2084 | ||
2085 | r = set_put_strdup(&info->system_call_architectures, name); | |
2086 | if (r < 0) | |
2087 | return r; | |
2088 | } | |
2089 | ||
2090 | return sd_bus_message_exit_container(m); | |
2091 | } | |
2092 | ||
ec16f3b6 LP |
2093 | static int property_read_system_call_filter( |
2094 | sd_bus *bus, | |
2095 | const char *member, | |
2096 | sd_bus_message *m, | |
2097 | sd_bus_error *error, | |
2098 | void *userdata) { | |
2099 | ||
1624114d | 2100 | SecurityInfo *info = userdata; |
6b000af4 | 2101 | int allow_list, r; |
ec16f3b6 LP |
2102 | |
2103 | assert(bus); | |
2104 | assert(member); | |
2105 | assert(m); | |
2106 | ||
2107 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
2108 | if (r < 0) | |
2109 | return r; | |
2110 | ||
6b000af4 | 2111 | r = sd_bus_message_read(m, "b", &allow_list); |
ec16f3b6 LP |
2112 | if (r < 0) |
2113 | return r; | |
2114 | ||
6b000af4 | 2115 | info->system_call_filter_allow_list = allow_list; |
ec16f3b6 LP |
2116 | |
2117 | r = sd_bus_message_enter_container(m, 'a', "s"); | |
2118 | if (r < 0) | |
2119 | return r; | |
2120 | ||
2121 | for (;;) { | |
2122 | const char *name; | |
2123 | ||
2124 | r = sd_bus_message_read(m, "s", &name); | |
2125 | if (r < 0) | |
2126 | return r; | |
2127 | if (r == 0) | |
2128 | break; | |
2129 | ||
1624114d MG |
2130 | /* The actual ExecContext stores the system call id as the map value, which we don't |
2131 | * need. So we assign NULL to all values here. */ | |
2132 | r = hashmap_put_strdup(&info->system_call_filter, name, NULL); | |
ec16f3b6 LP |
2133 | if (r < 0) |
2134 | return r; | |
2135 | } | |
2136 | ||
2137 | r = sd_bus_message_exit_container(m); | |
2138 | if (r < 0) | |
2139 | return r; | |
2140 | ||
2141 | return sd_bus_message_exit_container(m); | |
2142 | } | |
2143 | ||
2144 | static int property_read_ip_address_allow( | |
2145 | sd_bus *bus, | |
2146 | const char *member, | |
2147 | sd_bus_message *m, | |
2148 | sd_bus_error *error, | |
2149 | void *userdata) { | |
2150 | ||
1624114d | 2151 | SecurityInfo *info = userdata; |
ec16f3b6 LP |
2152 | bool deny_ipv4 = false, deny_ipv6 = false; |
2153 | int r; | |
2154 | ||
2155 | assert(bus); | |
2156 | assert(member); | |
2157 | assert(m); | |
2158 | ||
2159 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
2160 | if (r < 0) | |
2161 | return r; | |
2162 | ||
2163 | for (;;) { | |
2164 | const void *data; | |
2165 | size_t size; | |
2166 | int32_t family; | |
2167 | uint32_t prefixlen; | |
2168 | ||
2169 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
2170 | if (r < 0) | |
2171 | return r; | |
2172 | if (r == 0) | |
2173 | break; | |
2174 | ||
2175 | r = sd_bus_message_read(m, "i", &family); | |
2176 | if (r < 0) | |
2177 | return r; | |
2178 | ||
2179 | r = sd_bus_message_read_array(m, 'y', &data, &size); | |
2180 | if (r < 0) | |
2181 | return r; | |
2182 | ||
2183 | r = sd_bus_message_read(m, "u", &prefixlen); | |
2184 | if (r < 0) | |
2185 | return r; | |
2186 | ||
2187 | r = sd_bus_message_exit_container(m); | |
2188 | if (r < 0) | |
2189 | return r; | |
2190 | ||
2191 | if (streq(member, "IPAddressAllow")) { | |
2192 | union in_addr_union u; | |
2193 | ||
2194 | if (family == AF_INET && size == 4 && prefixlen == 8) | |
2195 | memcpy(&u.in, data, size); | |
2196 | else if (family == AF_INET6 && size == 16 && prefixlen == 128) | |
2197 | memcpy(&u.in6, data, size); | |
2198 | else { | |
2199 | info->ip_address_allow_other = true; | |
2200 | continue; | |
2201 | } | |
2202 | ||
2203 | if (in_addr_is_localhost(family, &u)) | |
2204 | info->ip_address_allow_localhost = true; | |
2205 | else | |
2206 | info->ip_address_allow_other = true; | |
2207 | } else { | |
2208 | assert(streq(member, "IPAddressDeny")); | |
2209 | ||
2210 | if (family == AF_INET && size == 4 && prefixlen == 0) | |
2211 | deny_ipv4 = true; | |
2212 | else if (family == AF_INET6 && size == 16 && prefixlen == 0) | |
2213 | deny_ipv6 = true; | |
2214 | } | |
2215 | } | |
2216 | ||
2217 | info->ip_address_deny_all = deny_ipv4 && deny_ipv6; | |
2218 | ||
2219 | return sd_bus_message_exit_container(m); | |
2220 | } | |
2221 | ||
fab34748 KL |
2222 | static int property_read_ip_filters( |
2223 | sd_bus *bus, | |
2224 | const char *member, | |
2225 | sd_bus_message *m, | |
2226 | sd_bus_error *error, | |
2227 | void *userdata) { | |
2228 | ||
1624114d | 2229 | SecurityInfo *info = userdata; |
fab34748 KL |
2230 | _cleanup_(strv_freep) char **l = NULL; |
2231 | int r; | |
2232 | ||
2233 | assert(bus); | |
2234 | assert(member); | |
2235 | assert(m); | |
2236 | ||
2237 | r = sd_bus_message_read_strv(m, &l); | |
2238 | if (r < 0) | |
2239 | return r; | |
2240 | ||
2241 | if (streq(member, "IPIngressFilterPath")) | |
2242 | info->ip_filters_custom_ingress = !strv_isempty(l); | |
2243 | else if (streq(member, "IPEgressFilterPath")) | |
3da57008 | 2244 | info->ip_filters_custom_egress = !strv_isempty(l); |
fab34748 KL |
2245 | |
2246 | return 0; | |
2247 | } | |
2248 | ||
ec16f3b6 LP |
2249 | static int property_read_device_allow( |
2250 | sd_bus *bus, | |
2251 | const char *member, | |
2252 | sd_bus_message *m, | |
2253 | sd_bus_error *error, | |
2254 | void *userdata) { | |
2255 | ||
1624114d | 2256 | SecurityInfo *info = userdata; |
ec16f3b6 LP |
2257 | size_t n = 0; |
2258 | int r; | |
2259 | ||
2260 | assert(bus); | |
2261 | assert(member); | |
2262 | assert(m); | |
2263 | ||
2264 | r = sd_bus_message_enter_container(m, 'a', "(ss)"); | |
2265 | if (r < 0) | |
2266 | return r; | |
2267 | ||
2268 | for (;;) { | |
2269 | const char *name, *policy; | |
2270 | ||
2271 | r = sd_bus_message_read(m, "(ss)", &name, &policy); | |
2272 | if (r < 0) | |
2273 | return r; | |
2274 | if (r == 0) | |
2275 | break; | |
2276 | ||
2277 | n++; | |
2278 | } | |
2279 | ||
2280 | info->device_allow_non_empty = n > 0; | |
2281 | ||
2282 | return sd_bus_message_exit_container(m); | |
2283 | } | |
2284 | ||
1624114d | 2285 | static int acquire_security_info(sd_bus *bus, const char *name, SecurityInfo *info, AnalyzeSecurityFlags flags) { |
ec16f3b6 LP |
2286 | |
2287 | static const struct bus_properties_map security_map[] = { | |
1624114d MG |
2288 | { "AmbientCapabilities", "t", NULL, offsetof(SecurityInfo, ambient_capabilities) }, |
2289 | { "CapabilityBoundingSet", "t", NULL, offsetof(SecurityInfo, capability_bounding_set) }, | |
2290 | { "DefaultDependencies", "b", NULL, offsetof(SecurityInfo, default_dependencies) }, | |
2291 | { "Delegate", "b", NULL, offsetof(SecurityInfo, delegate) }, | |
2292 | { "DeviceAllow", "a(ss)", property_read_device_allow, 0 }, | |
2293 | { "DevicePolicy", "s", NULL, offsetof(SecurityInfo, device_policy) }, | |
2294 | { "DynamicUser", "b", NULL, offsetof(SecurityInfo, dynamic_user) }, | |
2295 | { "FragmentPath", "s", NULL, offsetof(SecurityInfo, fragment_path) }, | |
2296 | { "IPAddressAllow", "a(iayu)", property_read_ip_address_allow, 0 }, | |
2297 | { "IPAddressDeny", "a(iayu)", property_read_ip_address_allow, 0 }, | |
2298 | { "IPIngressFilterPath", "as", property_read_ip_filters, 0 }, | |
2299 | { "IPEgressFilterPath", "as", property_read_ip_filters, 0 }, | |
2300 | { "Id", "s", NULL, offsetof(SecurityInfo, id) }, | |
2301 | { "KeyringMode", "s", NULL, offsetof(SecurityInfo, keyring_mode) }, | |
2302 | { "ProtectProc", "s", NULL, offsetof(SecurityInfo, protect_proc) }, | |
2303 | { "ProcSubset", "s", NULL, offsetof(SecurityInfo, proc_subset) }, | |
2304 | { "LoadState", "s", NULL, offsetof(SecurityInfo, load_state) }, | |
2305 | { "LockPersonality", "b", NULL, offsetof(SecurityInfo, lock_personality) }, | |
2306 | { "MemoryDenyWriteExecute", "b", NULL, offsetof(SecurityInfo, memory_deny_write_execute) }, | |
2307 | { "NoNewPrivileges", "b", NULL, offsetof(SecurityInfo, no_new_privileges) }, | |
2308 | { "NotifyAccess", "s", NULL, offsetof(SecurityInfo, notify_access) }, | |
2309 | { "PrivateDevices", "b", NULL, offsetof(SecurityInfo, private_devices) }, | |
2310 | { "PrivateMounts", "b", NULL, offsetof(SecurityInfo, private_mounts) }, | |
2311 | { "PrivateNetwork", "b", NULL, offsetof(SecurityInfo, private_network) }, | |
2312 | { "PrivateTmp", "b", NULL, offsetof(SecurityInfo, private_tmp) }, | |
2313 | { "PrivateUsers", "b", NULL, offsetof(SecurityInfo, private_users) }, | |
2314 | { "ProtectControlGroups", "b", NULL, offsetof(SecurityInfo, protect_control_groups) }, | |
2315 | { "ProtectHome", "s", NULL, offsetof(SecurityInfo, protect_home) }, | |
2316 | { "ProtectHostname", "b", NULL, offsetof(SecurityInfo, protect_hostname) }, | |
2317 | { "ProtectKernelModules", "b", NULL, offsetof(SecurityInfo, protect_kernel_modules) }, | |
2318 | { "ProtectKernelTunables", "b", NULL, offsetof(SecurityInfo, protect_kernel_tunables) }, | |
2319 | { "ProtectKernelLogs", "b", NULL, offsetof(SecurityInfo, protect_kernel_logs) }, | |
2320 | { "ProtectClock", "b", NULL, offsetof(SecurityInfo, protect_clock) }, | |
2321 | { "ProtectSystem", "s", NULL, offsetof(SecurityInfo, protect_system) }, | |
2322 | { "RemoveIPC", "b", NULL, offsetof(SecurityInfo, remove_ipc) }, | |
2323 | { "RestrictAddressFamilies", "(bas)", property_read_restrict_address_families, 0 }, | |
2324 | { "RestrictNamespaces", "t", property_read_restrict_namespaces, 0 }, | |
2325 | { "RestrictRealtime", "b", NULL, offsetof(SecurityInfo, restrict_realtime) }, | |
2326 | { "RestrictSUIDSGID", "b", NULL, offsetof(SecurityInfo, restrict_suid_sgid) }, | |
2327 | { "RootDirectory", "s", NULL, offsetof(SecurityInfo, root_directory) }, | |
2328 | { "RootImage", "s", NULL, offsetof(SecurityInfo, root_image) }, | |
2329 | { "SupplementaryGroups", "as", NULL, offsetof(SecurityInfo, supplementary_groups) }, | |
2330 | { "SystemCallArchitectures", "as", property_read_syscall_archs, 0 }, | |
2331 | { "SystemCallFilter", "(as)", property_read_system_call_filter, 0 }, | |
2332 | { "Type", "s", NULL, offsetof(SecurityInfo, type) }, | |
2333 | { "UMask", "u", property_read_umask, 0 }, | |
2334 | { "User", "s", NULL, offsetof(SecurityInfo, user) }, | |
ec16f3b6 LP |
2335 | {} |
2336 | }; | |
2337 | ||
2338 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2339 | _cleanup_free_ char *path = NULL; | |
2340 | int r; | |
2341 | ||
2342 | /* Note: this mangles *info on failure! */ | |
2343 | ||
2344 | assert(bus); | |
2345 | assert(name); | |
2346 | assert(info); | |
2347 | ||
2348 | path = unit_dbus_path_from_name(name); | |
2349 | if (!path) | |
2350 | return log_oom(); | |
2351 | ||
1ace223c SJ |
2352 | r = bus_map_all_properties( |
2353 | bus, | |
2354 | "org.freedesktop.systemd1", | |
2355 | path, | |
2356 | security_map, | |
2357 | BUS_MAP_STRDUP | BUS_MAP_BOOLEAN_AS_BOOL, | |
2358 | &error, | |
2359 | NULL, | |
2360 | info); | |
ec16f3b6 LP |
2361 | if (r < 0) |
2362 | return log_error_errno(r, "Failed to get unit properties: %s", bus_error_message(&error, r)); | |
2363 | ||
2364 | if (!streq_ptr(info->load_state, "loaded")) { | |
2365 | ||
2366 | if (FLAGS_SET(flags, ANALYZE_SECURITY_ONLY_LOADED)) | |
2367 | return -EMEDIUMTYPE; | |
2368 | ||
2369 | if (streq_ptr(info->load_state, "not-found")) | |
2370 | log_error("Unit %s not found, cannot analyze.", name); | |
2371 | else if (streq_ptr(info->load_state, "masked")) | |
2372 | log_error("Unit %s is masked, cannot analyze.", name); | |
2373 | else | |
2374 | log_error("Unit %s not loaded properly, cannot analyze.", name); | |
2375 | ||
2376 | return -EINVAL; | |
2377 | } | |
2378 | ||
2379 | if (FLAGS_SET(flags, ANALYZE_SECURITY_ONLY_LONG_RUNNING) && streq_ptr(info->type, "oneshot")) | |
2380 | return -EMEDIUMTYPE; | |
2381 | ||
2382 | if (info->private_devices || | |
2383 | info->private_tmp || | |
2384 | info->protect_control_groups || | |
2385 | info->protect_kernel_tunables || | |
2386 | info->protect_kernel_modules || | |
2387 | !streq_ptr(info->protect_home, "no") || | |
2388 | !streq_ptr(info->protect_system, "no") || | |
2389 | info->root_image) | |
2390 | info->private_mounts = true; | |
2391 | ||
2392 | if (info->protect_kernel_modules) | |
2393 | info->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE); | |
2394 | ||
82dce83b KK |
2395 | if (info->protect_kernel_logs) |
2396 | info->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYSLOG); | |
2397 | ||
9f37272a KK |
2398 | if (info->protect_clock) |
2399 | info->capability_bounding_set &= ~((UINT64_C(1) << CAP_SYS_TIME) | | |
2400 | (UINT64_C(1) << CAP_WAKE_ALARM)); | |
2401 | ||
ec16f3b6 LP |
2402 | if (info->private_devices) |
2403 | info->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) | | |
2404 | (UINT64_C(1) << CAP_SYS_RAWIO)); | |
2405 | ||
2406 | return 0; | |
2407 | } | |
2408 | ||
3838d22c MG |
2409 | static int analyze_security_one(sd_bus *bus, |
2410 | const char *name, | |
2411 | Table *overview_table, | |
2412 | AnalyzeSecurityFlags flags, | |
2413 | unsigned threshold, | |
4b4a8ef7 MG |
2414 | JsonVariant *policy, |
2415 | PagerFlags pager_flags, | |
2416 | JsonFormatFlags json_format_flags) { | |
dfbda879 | 2417 | |
1624114d MG |
2418 | _cleanup_(security_info_freep) SecurityInfo *info = security_info_new(); |
2419 | if (!info) | |
2420 | return log_oom(); | |
2421 | ||
ec16f3b6 LP |
2422 | int r; |
2423 | ||
2424 | assert(bus); | |
2425 | assert(name); | |
2426 | ||
1624114d | 2427 | r = acquire_security_info(bus, name, info, flags); |
ec16f3b6 LP |
2428 | if (r == -EMEDIUMTYPE) /* Ignore this one because not loaded or Type is oneshot */ |
2429 | return 0; | |
2430 | if (r < 0) | |
2431 | return r; | |
2432 | ||
4b4a8ef7 | 2433 | r = assess(info, overview_table, flags, threshold, policy, pager_flags, json_format_flags); |
ec16f3b6 LP |
2434 | if (r < 0) |
2435 | return r; | |
2436 | ||
2437 | return 0; | |
2438 | } | |
2439 | ||
1624114d MG |
2440 | /* Refactoring SecurityInfo so that it can make use of existing struct variables instead of reading from dbus */ |
2441 | static int get_security_info(Unit *u, ExecContext *c, CGroupContext *g, SecurityInfo **ret_info) { | |
2442 | assert(ret_info); | |
2443 | ||
2444 | _cleanup_(security_info_freep) SecurityInfo *info = security_info_new(); | |
2445 | if (!info) | |
2446 | return log_oom(); | |
2447 | ||
2448 | if (u) { | |
2449 | if (u->id) { | |
2450 | info->id = strdup(u->id); | |
2451 | if (!info->id) | |
2452 | return log_oom(); | |
2453 | } | |
2454 | if (unit_type_to_string(u->type)) { | |
2455 | info->type = strdup(unit_type_to_string(u->type)); | |
2456 | if (!info->type) | |
2457 | return log_oom(); | |
2458 | } | |
2459 | if (unit_load_state_to_string(u->load_state)) { | |
2460 | info->load_state = strdup(unit_load_state_to_string(u->load_state)); | |
2461 | if (!info->load_state) | |
2462 | return log_oom(); | |
2463 | } | |
2464 | if (u->fragment_path) { | |
2465 | info->fragment_path = strdup(u->fragment_path); | |
2466 | if (!info->fragment_path) | |
2467 | return log_oom(); | |
2468 | } | |
2469 | info->default_dependencies = u->default_dependencies; | |
2470 | if (u->type == UNIT_SERVICE && notify_access_to_string(SERVICE(u)->notify_access)) { | |
2471 | info->notify_access = strdup(notify_access_to_string(SERVICE(u)->notify_access)); | |
2472 | if (!info->notify_access) | |
2473 | return log_oom(); | |
2474 | } | |
2475 | } | |
2476 | ||
2477 | if (c) { | |
2478 | info->ambient_capabilities = c->capability_ambient_set; | |
2479 | info->capability_bounding_set = c->capability_bounding_set; | |
2480 | if (c->user) { | |
2481 | info->user = strdup(c->user); | |
2482 | if (!info->user) | |
2483 | return log_oom(); | |
2484 | } | |
2485 | if (c->supplementary_groups) { | |
2486 | info->supplementary_groups = strv_copy(c->supplementary_groups); | |
2487 | if (!info->supplementary_groups) | |
2488 | return log_oom(); | |
2489 | } | |
2490 | info->dynamic_user = c->dynamic_user; | |
2491 | if (exec_keyring_mode_to_string(c->keyring_mode)) { | |
2492 | info->keyring_mode = strdup(exec_keyring_mode_to_string(c->keyring_mode)); | |
2493 | if (!info->keyring_mode) | |
2494 | return log_oom(); | |
2495 | } | |
2496 | if (protect_proc_to_string(c->protect_proc)) { | |
2497 | info->protect_proc = strdup(protect_proc_to_string(c->protect_proc)); | |
2498 | if (!info->protect_proc) | |
2499 | return log_oom(); | |
2500 | } | |
2501 | if (proc_subset_to_string(c->proc_subset)) { | |
2502 | info->proc_subset = strdup(proc_subset_to_string(c->proc_subset)); | |
2503 | if (!info->proc_subset) | |
2504 | return log_oom(); | |
2505 | } | |
2506 | info->lock_personality = c->lock_personality; | |
2507 | info->memory_deny_write_execute = c->memory_deny_write_execute; | |
2508 | info->no_new_privileges = c->no_new_privileges; | |
2509 | info->protect_hostname = c->protect_hostname; | |
2510 | info->private_devices = c->private_devices; | |
2511 | info->private_mounts = c->private_mounts; | |
2512 | info->private_network = c->private_network; | |
2513 | info->private_tmp = c->private_tmp; | |
2514 | info->private_users = c->private_users; | |
2515 | info->protect_control_groups = c->protect_control_groups; | |
2516 | info->protect_kernel_modules = c->protect_kernel_modules; | |
2517 | info->protect_kernel_tunables = c->protect_kernel_tunables; | |
2518 | info->protect_kernel_logs = c->protect_kernel_logs; | |
2519 | info->protect_clock = c->protect_clock; | |
2520 | if (protect_home_to_string(c->protect_home)) { | |
2521 | info->protect_home = strdup(protect_home_to_string(c->protect_home)); | |
2522 | if (!info->protect_home) | |
2523 | return log_oom(); | |
2524 | } | |
2525 | if (protect_system_to_string(c->protect_system)) { | |
2526 | info->protect_system = strdup(protect_system_to_string(c->protect_system)); | |
2527 | if (!info->protect_system) | |
2528 | return log_oom(); | |
2529 | } | |
2530 | info->remove_ipc = c->remove_ipc; | |
2531 | info->restrict_address_family_inet = | |
2532 | info->restrict_address_family_unix = | |
2533 | info->restrict_address_family_netlink = | |
2534 | info->restrict_address_family_packet = | |
2535 | info->restrict_address_family_other = | |
2536 | c->address_families_allow_list; | |
2537 | ||
2538 | void *key; | |
2539 | SET_FOREACH(key, c->address_families) { | |
2540 | int family = PTR_TO_INT(key); | |
2541 | if (family == 0) | |
2542 | continue; | |
2543 | if (IN_SET(family, AF_INET, AF_INET6)) | |
2544 | info->restrict_address_family_inet = !c->address_families_allow_list; | |
2545 | else if (family == AF_UNIX) | |
2546 | info->restrict_address_family_unix = !c->address_families_allow_list; | |
2547 | else if (family == AF_NETLINK) | |
2548 | info->restrict_address_family_netlink = !c->address_families_allow_list; | |
2549 | else if (family == AF_PACKET) | |
2550 | info->restrict_address_family_packet = !c->address_families_allow_list; | |
2551 | else | |
2552 | info->restrict_address_family_other = !c->address_families_allow_list; | |
2553 | } | |
2554 | ||
2555 | info->restrict_namespaces = c->restrict_namespaces; | |
2556 | info->restrict_realtime = c->restrict_realtime; | |
2557 | info->restrict_suid_sgid = c->restrict_suid_sgid; | |
2558 | if (c->root_directory) { | |
2559 | info->root_directory = strdup(c->root_directory); | |
2560 | if (!info->root_directory) | |
2561 | return log_oom(); | |
2562 | } | |
2563 | if (c->root_image) { | |
2564 | info->root_image = strdup(c->root_image); | |
2565 | if (!info->root_image) | |
2566 | return log_oom(); | |
2567 | } | |
2568 | info->_umask = c->umask; | |
2569 | if (c->syscall_archs) { | |
2570 | info->system_call_architectures = set_copy(c->syscall_archs); | |
2571 | if (!info->system_call_architectures) | |
2572 | return log_oom(); | |
2573 | } | |
2574 | info->system_call_filter_allow_list = c->syscall_allow_list; | |
2575 | if (c->syscall_filter) { | |
2576 | info->system_call_filter = hashmap_copy(c->syscall_filter); | |
2577 | if (!info->system_call_filter) | |
2578 | return log_oom(); | |
2579 | } | |
2580 | } | |
2581 | ||
2582 | if (g) { | |
2583 | info->delegate = g->delegate; | |
2584 | if (cgroup_device_policy_to_string(g->device_policy)) { | |
2585 | info->device_policy = strdup(cgroup_device_policy_to_string(g->device_policy)); | |
2586 | if (!info->device_policy) | |
2587 | return log_oom(); | |
2588 | } | |
2589 | ||
84ebe6f0 | 2590 | struct in_addr_prefix *i; |
1624114d MG |
2591 | bool deny_ipv4 = false, deny_ipv6 = false; |
2592 | ||
84ebe6f0 | 2593 | SET_FOREACH(i, g->ip_address_deny) { |
1624114d MG |
2594 | if (i->family == AF_INET && i->prefixlen == 0) |
2595 | deny_ipv4 = true; | |
2596 | else if (i->family == AF_INET6 && i->prefixlen == 0) | |
2597 | deny_ipv6 = true; | |
2598 | } | |
2599 | info->ip_address_deny_all = deny_ipv4 && deny_ipv6; | |
2600 | ||
2601 | info->ip_address_allow_localhost = info->ip_address_allow_other = false; | |
84ebe6f0 | 2602 | SET_FOREACH(i, g->ip_address_allow) { |
1624114d MG |
2603 | if (in_addr_is_localhost(i->family, &i->address)) |
2604 | info->ip_address_allow_localhost = true; | |
2605 | else | |
2606 | info->ip_address_allow_other = true; | |
2607 | } | |
2608 | ||
2609 | info->ip_filters_custom_ingress = !strv_isempty(g->ip_filters_ingress); | |
2610 | info->ip_filters_custom_egress = !strv_isempty(g->ip_filters_egress); | |
2611 | info->device_allow_non_empty = !LIST_IS_EMPTY(g->device_allow); | |
2612 | } | |
2613 | ||
2614 | *ret_info = TAKE_PTR(info); | |
2615 | ||
2616 | return 0; | |
2617 | } | |
2618 | ||
4b4a8ef7 MG |
2619 | static int offline_security_check(Unit *u, |
2620 | unsigned threshold, | |
2621 | JsonVariant *policy, | |
2622 | PagerFlags pager_flags, | |
2623 | JsonFormatFlags json_format_flags) { | |
2624 | ||
bb43d853 MG |
2625 | _cleanup_(table_unrefp) Table *overview_table = NULL; |
2626 | AnalyzeSecurityFlags flags = 0; | |
2627 | _cleanup_(security_info_freep) SecurityInfo *info = NULL; | |
2628 | int r; | |
2629 | ||
2630 | assert(u); | |
2631 | ||
2632 | if (DEBUG_LOGGING) | |
2633 | unit_dump(u, stdout, "\t"); | |
2634 | ||
2635 | r = get_security_info(u, unit_get_exec_context(u), unit_get_cgroup_context(u), &info); | |
2636 | if (r < 0) | |
2637 | return r; | |
2638 | ||
4b4a8ef7 | 2639 | return assess(info, overview_table, flags, threshold, policy, pager_flags, json_format_flags); |
bb43d853 MG |
2640 | } |
2641 | ||
3838d22c MG |
2642 | static int offline_security_checks(char **filenames, |
2643 | JsonVariant *policy, | |
2644 | UnitFileScope scope, | |
2645 | bool check_man, | |
2646 | bool run_generators, | |
2647 | unsigned threshold, | |
4b4a8ef7 MG |
2648 | const char *root, |
2649 | PagerFlags pager_flags, | |
2650 | JsonFormatFlags json_format_flags) { | |
3838d22c | 2651 | |
bb43d853 MG |
2652 | const ManagerTestRunFlags flags = |
2653 | MANAGER_TEST_RUN_MINIMAL | | |
2654 | MANAGER_TEST_RUN_ENV_GENERATORS | | |
3f1487f5 | 2655 | MANAGER_TEST_RUN_IGNORE_DEPENDENCIES | |
bb43d853 MG |
2656 | run_generators * MANAGER_TEST_RUN_GENERATORS; |
2657 | ||
2658 | _cleanup_(manager_freep) Manager *m = NULL; | |
2659 | Unit *units[strv_length(filenames)]; | |
2660 | _cleanup_free_ char *var = NULL; | |
2661 | int r, k; | |
2662 | size_t count = 0; | |
2663 | char **filename; | |
2664 | ||
2665 | if (strv_isempty(filenames)) | |
2666 | return 0; | |
2667 | ||
2668 | /* set the path */ | |
2669 | r = verify_generate_path(&var, filenames); | |
2670 | if (r < 0) | |
2671 | return log_error_errno(r, "Failed to generate unit load path: %m"); | |
2672 | ||
2673 | assert_se(set_unit_path(var) >= 0); | |
2674 | ||
2675 | r = manager_new(scope, flags, &m); | |
2676 | if (r < 0) | |
2677 | return log_error_errno(r, "Failed to initialize manager: %m"); | |
2678 | ||
2679 | log_debug("Starting manager..."); | |
2680 | ||
2681 | r = manager_startup(m, /* serialization= */ NULL, /* fds= */ NULL, root); | |
2682 | if (r < 0) | |
2683 | return r; | |
2684 | ||
2685 | log_debug("Loading remaining units from the command line..."); | |
2686 | ||
2687 | STRV_FOREACH(filename, filenames) { | |
2688 | _cleanup_free_ char *prepared = NULL; | |
2689 | ||
2690 | log_debug("Handling %s...", *filename); | |
2691 | ||
2692 | k = verify_prepare_filename(*filename, &prepared); | |
2693 | if (k < 0) { | |
2694 | log_warning_errno(k, "Failed to prepare filename %s: %m", *filename); | |
2695 | if (r == 0) | |
2696 | r = k; | |
2697 | continue; | |
2698 | } | |
2699 | ||
2700 | k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]); | |
2701 | if (k < 0) { | |
2702 | if (r == 0) | |
2703 | r = k; | |
2704 | continue; | |
2705 | } | |
2706 | ||
2707 | count++; | |
2708 | } | |
2709 | ||
2710 | for (size_t i = 0; i < count; i++) { | |
4b4a8ef7 | 2711 | k = offline_security_check(units[i], threshold, policy, pager_flags, json_format_flags); |
bb43d853 MG |
2712 | if (k < 0 && r == 0) |
2713 | r = k; | |
2714 | } | |
2715 | ||
2716 | return r; | |
2717 | } | |
2718 | ||
ecfd082b MG |
2719 | int analyze_security(sd_bus *bus, |
2720 | char **units, | |
2721 | JsonVariant *policy, | |
2722 | UnitFileScope scope, | |
2723 | bool check_man, | |
2724 | bool run_generators, | |
2725 | bool offline, | |
2726 | unsigned threshold, | |
2727 | const char *root, | |
4b4a8ef7 MG |
2728 | JsonFormatFlags json_format_flags, |
2729 | PagerFlags pager_flags, | |
ecfd082b | 2730 | AnalyzeSecurityFlags flags) { |
dfbda879 | 2731 | |
ec16f3b6 LP |
2732 | _cleanup_(table_unrefp) Table *overview_table = NULL; |
2733 | int ret = 0, r; | |
2734 | ||
2735 | assert(bus); | |
2736 | ||
bb43d853 | 2737 | if (offline) |
4b4a8ef7 | 2738 | return offline_security_checks(units, policy, scope, check_man, run_generators, threshold, root, pager_flags, json_format_flags); |
bb43d853 | 2739 | |
ec16f3b6 | 2740 | if (strv_length(units) != 1) { |
9969b542 | 2741 | overview_table = table_new("unit", "exposure", "predicate", "happy"); |
ec16f3b6 LP |
2742 | if (!overview_table) |
2743 | return log_oom(); | |
2744 | } | |
2745 | ||
2746 | if (strv_isempty(units)) { | |
2747 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2748 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
2749 | _cleanup_strv_free_ char **list = NULL; | |
319a4f4b | 2750 | size_t n = 0; |
ec16f3b6 LP |
2751 | char **i; |
2752 | ||
2753 | r = sd_bus_call_method( | |
2754 | bus, | |
2755 | "org.freedesktop.systemd1", | |
2756 | "/org/freedesktop/systemd1", | |
2757 | "org.freedesktop.systemd1.Manager", | |
2758 | "ListUnits", | |
1ace223c SJ |
2759 | &error, |
2760 | &reply, | |
ec16f3b6 LP |
2761 | NULL); |
2762 | if (r < 0) | |
2763 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
2764 | ||
2765 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); | |
2766 | if (r < 0) | |
2767 | return bus_log_parse_error(r); | |
2768 | ||
2769 | for (;;) { | |
2770 | UnitInfo info; | |
2771 | char *copy = NULL; | |
2772 | ||
2773 | r = bus_parse_unit_info(reply, &info); | |
2774 | if (r < 0) | |
2775 | return bus_log_parse_error(r); | |
2776 | if (r == 0) | |
2777 | break; | |
2778 | ||
2779 | if (!endswith(info.id, ".service")) | |
2780 | continue; | |
2781 | ||
319a4f4b | 2782 | if (!GREEDY_REALLOC(list, n + 2)) |
ec16f3b6 LP |
2783 | return log_oom(); |
2784 | ||
2785 | copy = strdup(info.id); | |
2786 | if (!copy) | |
2787 | return log_oom(); | |
2788 | ||
2789 | list[n++] = copy; | |
2790 | list[n] = NULL; | |
2791 | } | |
2792 | ||
2793 | strv_sort(list); | |
2794 | ||
2795 | flags |= ANALYZE_SECURITY_SHORT|ANALYZE_SECURITY_ONLY_LOADED|ANALYZE_SECURITY_ONLY_LONG_RUNNING; | |
2796 | ||
2797 | STRV_FOREACH(i, list) { | |
4b4a8ef7 | 2798 | r = analyze_security_one(bus, *i, overview_table, flags, threshold, policy, pager_flags, json_format_flags); |
ec16f3b6 LP |
2799 | if (r < 0 && ret >= 0) |
2800 | ret = r; | |
2801 | } | |
2802 | ||
2803 | } else { | |
2804 | char **i; | |
2805 | ||
2806 | STRV_FOREACH(i, units) { | |
2807 | _cleanup_free_ char *mangled = NULL, *instance = NULL; | |
2808 | const char *name; | |
2809 | ||
2810 | if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT) && i != units) { | |
2811 | putc('\n', stdout); | |
2812 | fflush(stdout); | |
2813 | } | |
2814 | ||
df7c4eb6 | 2815 | r = unit_name_mangle(*i, 0, &mangled); |
ec16f3b6 LP |
2816 | if (r < 0) |
2817 | return log_error_errno(r, "Failed to mangle unit name '%s': %m", *i); | |
2818 | ||
d7a0f1f4 FS |
2819 | if (!endswith(mangled, ".service")) |
2820 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2821 | "Unit %s is not a service unit, refusing.", | |
2822 | *i); | |
ec16f3b6 LP |
2823 | |
2824 | if (unit_name_is_valid(mangled, UNIT_NAME_TEMPLATE)) { | |
2825 | r = unit_name_replace_instance(mangled, "test-instance", &instance); | |
2826 | if (r < 0) | |
2827 | return log_oom(); | |
2828 | ||
2829 | name = instance; | |
2830 | } else | |
2831 | name = mangled; | |
2832 | ||
4b4a8ef7 | 2833 | r = analyze_security_one(bus, name, overview_table, flags, threshold, policy, pager_flags, json_format_flags); |
ec16f3b6 LP |
2834 | if (r < 0 && ret >= 0) |
2835 | ret = r; | |
2836 | } | |
2837 | } | |
2838 | ||
2839 | if (overview_table) { | |
2840 | if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT)) { | |
2841 | putc('\n', stdout); | |
2842 | fflush(stdout); | |
2843 | } | |
2844 | ||
4b4a8ef7 | 2845 | r = table_print_with_pager(overview_table, json_format_flags, pager_flags, /* show_header= */true); |
ec16f3b6 LP |
2846 | if (r < 0) |
2847 | return log_error_errno(r, "Failed to output table: %m"); | |
2848 | } | |
ec16f3b6 LP |
2849 | return ret; |
2850 | } |