Debian Codegraph @

src/analyze/analyze-security.c

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