1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <sys/utsname.h>
6 #include "analyze-security.h"
7 #include "analyze-verify.h"
9 #include "bus-map-properties.h"
10 #include "bus-unit-util.h"
14 #include "format-table.h"
15 #include "in-addr-prefix-util.h"
16 #include "locale-util.h"
19 #include "missing_capability.h"
20 #include "missing_sched.h"
22 #include "nulstr-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "pretty-print.h"
27 # include "seccomp-util.h"
31 #include "stdio-util.h"
33 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "unit-serialize.h"
38 typedef struct SecurityInfo
{
43 bool default_dependencies
;
45 uint64_t ambient_capabilities
;
46 uint64_t capability_bounding_set
;
49 char **supplementary_groups
;
52 bool ip_address_deny_all
;
53 bool ip_address_allow_localhost
;
54 bool ip_address_allow_other
;
56 bool ip_filters_custom_ingress
;
57 bool ip_filters_custom_egress
;
62 bool lock_personality
;
63 bool memory_deny_write_execute
;
64 bool no_new_privileges
;
66 bool protect_hostname
;
74 bool protect_control_groups
;
75 bool protect_kernel_modules
;
76 bool protect_kernel_tunables
;
77 bool protect_kernel_logs
;
85 bool restrict_address_family_inet
;
86 bool restrict_address_family_unix
;
87 bool restrict_address_family_netlink
;
88 bool restrict_address_family_packet
;
89 bool restrict_address_family_other
;
91 unsigned long long restrict_namespaces
;
92 bool restrict_realtime
;
93 bool restrict_suid_sgid
;
100 bool device_allow_non_empty
;
102 Set
*system_call_architectures
;
104 bool system_call_filter_allow_list
;
105 Hashmap
*system_call_filter
;
110 struct security_assessor
{
112 const char *json_field
;
113 const char *description_good
;
114 const char *description_bad
;
115 const char *description_na
;
120 const struct security_assessor
*a
,
121 const SecurityInfo
*info
,
123 uint64_t *ret_badness
,
124 char **ret_description
);
127 bool default_dependencies_only
;
130 static SecurityInfo
*security_info_new(void) {
131 SecurityInfo
*info
= new(SecurityInfo
, 1);
135 *info
= (SecurityInfo
) {
136 .default_dependencies
= true,
137 .capability_bounding_set
= UINT64_MAX
,
138 .restrict_namespaces
= UINT64_MAX
,
145 static SecurityInfo
*security_info_free(SecurityInfo
*i
) {
152 free(i
->fragment_path
);
156 free(i
->protect_home
);
157 free(i
->protect_system
);
159 free(i
->root_directory
);
162 free(i
->keyring_mode
);
163 free(i
->protect_proc
);
164 free(i
->proc_subset
);
165 free(i
->notify_access
);
167 free(i
->device_policy
);
169 strv_free(i
->supplementary_groups
);
170 set_free(i
->system_call_architectures
);
172 hashmap_free(i
->system_call_filter
);
177 DEFINE_TRIVIAL_CLEANUP_FUNC(SecurityInfo
*, security_info_free
);
179 static bool security_info_runs_privileged(const SecurityInfo
*i
) {
182 if (STRPTR_IN_SET(i
->user
, "0", "root"))
188 return isempty(i
->user
);
191 static int assess_bool(
192 const struct security_assessor
*a
,
193 const SecurityInfo
*info
,
195 uint64_t *ret_badness
,
196 char **ret_description
) {
198 const bool *b
= data
;
202 assert(ret_description
);
204 *ret_badness
= a
->parameter
? *b
: !*b
;
205 *ret_description
= NULL
;
210 static int assess_user(
211 const struct security_assessor
*a
,
212 const SecurityInfo
*info
,
214 uint64_t *ret_badness
,
215 char **ret_description
) {
217 _cleanup_free_
char *d
= NULL
;
221 assert(ret_description
);
223 if (streq_ptr(info
->user
, NOBODY_USER_NAME
)) {
224 d
= strdup("Service runs under as '" NOBODY_USER_NAME
"' user, which should not be used for services");
226 } else if (info
->dynamic_user
&& !STR_IN_SET(info
->user
, "0", "root")) {
227 d
= strdup("Service runs under a transient non-root user identity");
229 } else if (info
->user
&& !STR_IN_SET(info
->user
, "0", "root", "")) {
230 d
= strdup("Service runs under a static non-root user identity");
234 *ret_description
= NULL
;
242 *ret_description
= TAKE_PTR(d
);
247 static int assess_protect_home(
248 const struct security_assessor
*a
,
249 const SecurityInfo
*info
,
251 uint64_t *ret_badness
,
252 char **ret_description
) {
254 const char *description
;
260 assert(ret_description
);
263 description
= "Service has full access to home directories";
265 r
= parse_boolean(info
->protect_home
);
267 if (streq_ptr(info
->protect_home
, "read-only")) {
269 description
= "Service has read-only access to home directories";
270 } else if (streq_ptr(info
->protect_home
, "tmpfs")) {
272 description
= "Service has access to fake empty home directories";
276 description
= "Service has no access to home directories";
279 copy
= strdup(description
);
283 *ret_badness
= badness
;
284 *ret_description
= copy
;
289 static int assess_protect_system(
290 const struct security_assessor
*a
,
291 const SecurityInfo
*info
,
293 uint64_t *ret_badness
,
294 char **ret_description
) {
296 const char *description
;
302 assert(ret_description
);
305 description
= "Service has full access to the OS file hierarchy";
307 r
= parse_boolean(info
->protect_system
);
309 if (streq_ptr(info
->protect_system
, "full")) {
311 description
= "Service has very limited write access to the OS file hierarchy";
312 } else if (streq_ptr(info
->protect_system
, "strict")) {
314 description
= "Service has strict read-only access to the OS file hierarchy";
318 description
= "Service has limited write access to the OS file hierarchy";
321 copy
= strdup(description
);
325 *ret_badness
= badness
;
326 *ret_description
= copy
;
331 static int assess_root_directory(
332 const struct security_assessor
*a
,
333 const SecurityInfo
*info
,
335 uint64_t *ret_badness
,
336 char **ret_description
) {
339 assert(ret_description
);
342 empty_or_root(info
->root_directory
) &&
343 empty_or_root(info
->root_image
);
344 *ret_description
= NULL
;
349 static int assess_capability_bounding_set(
350 const struct security_assessor
*a
,
351 const SecurityInfo
*info
,
353 uint64_t *ret_badness
,
354 char **ret_description
) {
357 assert(ret_description
);
359 *ret_badness
= !!(info
->capability_bounding_set
& a
->parameter
);
360 *ret_description
= NULL
;
365 static int assess_umask(
366 const struct security_assessor
*a
,
367 const SecurityInfo
*info
,
369 uint64_t *ret_badness
,
370 char **ret_description
) {
377 assert(ret_description
);
379 if (!FLAGS_SET(info
->_umask
, 0002)) {
380 d
= "Files created by service are world-writable by default";
382 } else if (!FLAGS_SET(info
->_umask
, 0004)) {
383 d
= "Files created by service are world-readable by default";
385 } else if (!FLAGS_SET(info
->_umask
, 0020)) {
386 d
= "Files created by service are group-writable by default";
388 } else if (!FLAGS_SET(info
->_umask
, 0040)) {
389 d
= "Files created by service are group-readable by default";
392 d
= "Files created by service are accessible only by service's own user by default";
401 *ret_description
= copy
;
406 static int assess_keyring_mode(
407 const struct security_assessor
*a
,
408 const SecurityInfo
*info
,
410 uint64_t *ret_badness
,
411 char **ret_description
) {
414 assert(ret_description
);
416 *ret_badness
= !streq_ptr(info
->keyring_mode
, "private");
417 *ret_description
= NULL
;
422 static int assess_protect_proc(
423 const struct security_assessor
*a
,
424 const SecurityInfo
*info
,
426 uint64_t *ret_badness
,
427 char **ret_description
) {
430 assert(ret_description
);
432 if (streq_ptr(info
->protect_proc
, "noaccess"))
434 else if (STRPTR_IN_SET(info
->protect_proc
, "invisible", "ptraceable"))
439 *ret_description
= NULL
;
444 static int assess_proc_subset(
445 const struct security_assessor
*a
,
446 const SecurityInfo
*info
,
448 uint64_t *ret_badness
,
449 char **ret_description
) {
452 assert(ret_description
);
454 *ret_badness
= !streq_ptr(info
->proc_subset
, "pid");
455 *ret_description
= NULL
;
460 static int assess_notify_access(
461 const struct security_assessor
*a
,
462 const SecurityInfo
*info
,
464 uint64_t *ret_badness
,
465 char **ret_description
) {
468 assert(ret_description
);
470 *ret_badness
= streq_ptr(info
->notify_access
, "all");
471 *ret_description
= NULL
;
476 static int assess_remove_ipc(
477 const struct security_assessor
*a
,
478 const SecurityInfo
*info
,
480 uint64_t *ret_badness
,
481 char **ret_description
) {
484 assert(ret_description
);
486 if (security_info_runs_privileged(info
))
487 *ret_badness
= UINT64_MAX
;
489 *ret_badness
= !info
->remove_ipc
;
491 *ret_description
= NULL
;
495 static int assess_supplementary_groups(
496 const struct security_assessor
*a
,
497 const SecurityInfo
*info
,
499 uint64_t *ret_badness
,
500 char **ret_description
) {
503 assert(ret_description
);
505 if (security_info_runs_privileged(info
))
506 *ret_badness
= UINT64_MAX
;
508 *ret_badness
= !strv_isempty(info
->supplementary_groups
);
510 *ret_description
= NULL
;
514 static int assess_restrict_namespaces(
515 const struct security_assessor
*a
,
516 const SecurityInfo
*info
,
518 uint64_t *ret_badness
,
519 char **ret_description
) {
522 assert(ret_description
);
524 *ret_badness
= !!(info
->restrict_namespaces
& a
->parameter
);
525 *ret_description
= NULL
;
530 static int assess_system_call_architectures(
531 const struct security_assessor
*a
,
532 const SecurityInfo
*info
,
534 uint64_t *ret_badness
,
535 char **ret_description
) {
541 assert(ret_description
);
543 if (set_isempty(info
->system_call_architectures
)) {
545 d
= strdup("Service may execute system calls with all ABIs");
546 } else if (set_contains(info
->system_call_architectures
, "native") &&
547 set_size(info
->system_call_architectures
) == 1) {
549 d
= strdup("Service may execute system calls only with native ABI");
552 d
= strdup("Service may execute system calls with multiple ABIs");
559 *ret_description
= d
;
566 static bool syscall_names_in_filter(Hashmap
*s
, bool allow_list
, const SyscallFilterSet
*f
, const char **ret_offending_syscall
) {
569 NULSTR_FOREACH(syscall
, f
->value
) {
572 if (syscall
[0] == '@') {
573 const SyscallFilterSet
*g
;
575 assert_se(g
= syscall_filter_set_find(syscall
));
576 if (syscall_names_in_filter(s
, allow_list
, g
, ret_offending_syscall
))
577 return true; /* bad! */
582 /* Let's see if the system call actually exists on this platform, before complaining */
583 id
= seccomp_syscall_resolve_name(syscall
);
587 if (hashmap_contains(s
, syscall
) == allow_list
) {
588 log_debug("Offending syscall filter item: %s", syscall
);
589 if (ret_offending_syscall
)
590 *ret_offending_syscall
= syscall
;
591 return true; /* bad! */
595 *ret_offending_syscall
= NULL
;
599 static int assess_system_call_filter(
600 const struct security_assessor
*a
,
601 const SecurityInfo
*info
,
603 uint64_t *ret_badness
,
604 char **ret_description
) {
609 assert(ret_description
);
611 assert(a
->parameter
< _SYSCALL_FILTER_SET_MAX
);
612 const SyscallFilterSet
*f
= syscall_filter_sets
+ a
->parameter
;
614 _cleanup_free_
char *d
= NULL
;
618 if (!info
->system_call_filter_allow_list
&& hashmap_isempty(info
->system_call_filter
)) {
619 r
= free_and_strdup(&d
, "Service does not filter system calls");
623 const char *offender
= NULL
;
625 log_debug("Analyzing system call filter, checking against: %s", f
->name
);
626 bad
= syscall_names_in_filter(info
->system_call_filter
, info
->system_call_filter_allow_list
, f
, &offender
);
627 log_debug("Result: %s", bad
? "bad" : "good");
629 if (info
->system_call_filter_allow_list
) {
631 r
= asprintf(&d
, "System call allow list defined for service, and %s is included "
632 "(e.g. %s is allowed)",
636 r
= asprintf(&d
, "System call allow list defined for service, and %s is not included",
642 r
= asprintf(&d
, "System call deny list defined for service, and %s is not included "
643 "(e.g. %s is allowed)",
647 r
= asprintf(&d
, "System call deny list defined for service, and %s is included",
657 *ret_description
= TAKE_PTR(d
);
664 static int assess_ip_address_allow(
665 const struct security_assessor
*a
,
666 const SecurityInfo
*info
,
668 uint64_t *ret_badness
,
669 char **ret_description
) {
676 assert(ret_description
);
678 if (info
->ip_filters_custom_ingress
|| info
->ip_filters_custom_egress
) {
679 d
= strdup("Service defines custom ingress/egress IP filters with BPF programs");
681 } else if (!info
->ip_address_deny_all
) {
682 d
= strdup("Service does not define an IP address allow list");
684 } else if (info
->ip_address_allow_other
) {
685 d
= strdup("Service defines IP address allow list with non-localhost entries");
687 } else if (info
->ip_address_allow_localhost
) {
688 d
= strdup("Service defines IP address allow list with only localhost entries");
691 d
= strdup("Service blocks all IP address ranges");
699 *ret_description
= d
;
704 static int assess_device_allow(
705 const struct security_assessor
*a
,
706 const SecurityInfo
*info
,
708 uint64_t *ret_badness
,
709 char **ret_description
) {
716 assert(ret_description
);
718 if (STRPTR_IN_SET(info
->device_policy
, "strict", "closed")) {
720 if (info
->device_allow_non_empty
) {
721 d
= strdup("Service has a device ACL with some special devices");
724 d
= strdup("Service has a minimal device ACL");
728 d
= strdup("Service has no device ACL");
736 *ret_description
= d
;
741 static int assess_ambient_capabilities(
742 const struct security_assessor
*a
,
743 const SecurityInfo
*info
,
745 uint64_t *ret_badness
,
746 char **ret_description
) {
749 assert(ret_description
);
751 *ret_badness
= info
->ambient_capabilities
!= 0;
752 *ret_description
= NULL
;
757 static const struct security_assessor security_assessor_table
[] = {
759 .id
= "User=/DynamicUser=",
760 .json_field
= "UserOrDynamicUser",
761 .description_bad
= "Service runs as root user",
762 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#User=",
765 .assess
= assess_user
,
768 .id
= "SupplementaryGroups=",
769 .json_field
= "SupplementaryGroups",
770 .description_good
= "Service has no supplementary groups",
771 .description_bad
= "Service runs with supplementary groups",
772 .description_na
= "Service runs as root, option does not matter",
773 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SupplementaryGroups=",
776 .assess
= assess_supplementary_groups
,
779 .id
= "PrivateDevices=",
780 .json_field
= "PrivateDevices",
781 .description_good
= "Service has no access to hardware devices",
782 .description_bad
= "Service potentially has access to hardware devices",
783 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateDevices=",
786 .assess
= assess_bool
,
787 .offset
= offsetof(SecurityInfo
, private_devices
),
790 .id
= "PrivateMounts=",
791 .json_field
= "PrivateMounts",
792 .description_good
= "Service cannot install system mounts",
793 .description_bad
= "Service may install system mounts",
794 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateMounts=",
797 .assess
= assess_bool
,
798 .offset
= offsetof(SecurityInfo
, private_mounts
),
801 .id
= "PrivateNetwork=",
802 .json_field
= "PrivateNetwork",
803 .description_good
= "Service has no access to the host's network",
804 .description_bad
= "Service has access to the host's network",
805 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateNetwork=",
808 .assess
= assess_bool
,
809 .offset
= offsetof(SecurityInfo
, private_network
),
813 .json_field
= "PrivateTmp",
814 .description_good
= "Service has no access to other software's temporary files",
815 .description_bad
= "Service has access to other software's temporary files",
816 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateTmp=",
819 .assess
= assess_bool
,
820 .offset
= offsetof(SecurityInfo
, private_tmp
),
821 .default_dependencies_only
= true,
824 .id
= "PrivateUsers=",
825 .json_field
= "PrivateUsers",
826 .description_good
= "Service does not have access to other users",
827 .description_bad
= "Service has access to other users",
828 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateUsers=",
831 .assess
= assess_bool
,
832 .offset
= offsetof(SecurityInfo
, private_users
),
835 .id
= "ProtectControlGroups=",
836 .json_field
= "ProtectControlGroups",
837 .description_good
= "Service cannot modify the control group file system",
838 .description_bad
= "Service may modify the control group file system",
839 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectControlGroups=",
842 .assess
= assess_bool
,
843 .offset
= offsetof(SecurityInfo
, protect_control_groups
),
846 .id
= "ProtectKernelModules=",
847 .json_field
= "ProtectKernelModules",
848 .description_good
= "Service cannot load or read kernel modules",
849 .description_bad
= "Service may load or read kernel modules",
850 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelModules=",
853 .assess
= assess_bool
,
854 .offset
= offsetof(SecurityInfo
, protect_kernel_modules
),
857 .id
= "ProtectKernelTunables=",
858 .json_field
= "ProtectKernelTunables",
859 .description_good
= "Service cannot alter kernel tunables (/proc/sys, …)",
860 .description_bad
= "Service may alter kernel tunables",
861 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelTunables=",
864 .assess
= assess_bool
,
865 .offset
= offsetof(SecurityInfo
, protect_kernel_tunables
),
868 .id
= "ProtectKernelLogs=",
869 .json_field
= "ProtectKernelLogs",
870 .description_good
= "Service cannot read from or write to the kernel log ring buffer",
871 .description_bad
= "Service may read from or write to the kernel log ring buffer",
872 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelLogs=",
875 .assess
= assess_bool
,
876 .offset
= offsetof(SecurityInfo
, protect_kernel_logs
),
879 .id
= "ProtectClock=",
880 .json_field
= "ProtectClock",
881 .description_good
= "Service cannot write to the hardware clock or system clock",
882 .description_bad
= "Service may write to the hardware clock or system clock",
883 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectClock=",
886 .assess
= assess_bool
,
887 .offset
= offsetof(SecurityInfo
, protect_clock
),
890 .id
= "ProtectHome=",
891 .json_field
= "ProtectHome",
892 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHome=",
895 .assess
= assess_protect_home
,
896 .default_dependencies_only
= true,
899 .id
= "ProtectHostname=",
900 .json_field
= "ProtectHostname",
901 .description_good
= "Service cannot change system host/domainname",
902 .description_bad
= "Service may change system host/domainname",
903 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHostname=",
906 .assess
= assess_bool
,
907 .offset
= offsetof(SecurityInfo
, protect_hostname
),
910 .id
= "ProtectSystem=",
911 .json_field
= "ProtectSystem",
912 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=",
915 .assess
= assess_protect_system
,
916 .default_dependencies_only
= true,
919 .id
= "RootDirectory=/RootImage=",
920 .json_field
= "RootDirectoryOrRootImage",
921 .description_good
= "Service has its own root directory/image",
922 .description_bad
= "Service runs within the host's root directory",
923 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RootDirectory=",
926 .assess
= assess_root_directory
,
927 .default_dependencies_only
= true,
930 .id
= "LockPersonality=",
931 .json_field
= "LockPersonality",
932 .description_good
= "Service cannot change ABI personality",
933 .description_bad
= "Service may change ABI personality",
934 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#LockPersonality=",
937 .assess
= assess_bool
,
938 .offset
= offsetof(SecurityInfo
, lock_personality
),
941 .id
= "MemoryDenyWriteExecute=",
942 .json_field
= "MemoryDenyWriteExecute",
943 .description_good
= "Service cannot create writable executable memory mappings",
944 .description_bad
= "Service may create writable executable memory mappings",
945 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute=",
948 .assess
= assess_bool
,
949 .offset
= offsetof(SecurityInfo
, memory_deny_write_execute
),
952 .id
= "NoNewPrivileges=",
953 .json_field
= "NoNewPrivileges",
954 .description_good
= "Service processes cannot acquire new privileges",
955 .description_bad
= "Service processes may acquire new privileges",
956 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NoNewPrivileges=",
959 .assess
= assess_bool
,
960 .offset
= offsetof(SecurityInfo
, no_new_privileges
),
963 .id
= "CapabilityBoundingSet=~CAP_SYS_ADMIN",
964 .json_field
= "CapabilityBoundingSet_CAP_SYS_ADMIN",
965 .description_good
= "Service has no administrator privileges",
966 .description_bad
= "Service has administrator privileges",
967 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
970 .assess
= assess_capability_bounding_set
,
971 .parameter
= UINT64_C(1) << CAP_SYS_ADMIN
,
974 .id
= "CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)",
975 .json_field
= "CapabilityBoundingSet_CAP_SET_UID_GID_PCAP",
976 .description_good
= "Service cannot change UID/GID identities/capabilities",
977 .description_bad
= "Service may change UID/GID identities/capabilities",
978 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
981 .assess
= assess_capability_bounding_set
,
982 .parameter
= (UINT64_C(1) << CAP_SETUID
)|
983 (UINT64_C(1) << CAP_SETGID
)|
984 (UINT64_C(1) << CAP_SETPCAP
),
987 .id
= "CapabilityBoundingSet=~CAP_SYS_PTRACE",
988 .json_field
= "CapabilityBoundingSet_CAP_SYS_PTRACE",
989 .description_good
= "Service has no ptrace() debugging abilities",
990 .description_bad
= "Service has ptrace() debugging abilities",
991 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
994 .assess
= assess_capability_bounding_set
,
995 .parameter
= (UINT64_C(1) << CAP_SYS_PTRACE
),
998 .id
= "CapabilityBoundingSet=~CAP_SYS_TIME",
999 .json_field
= "CapabilityBoundingSet_CAP_SYS_TIME",
1000 .description_good
= "Service processes cannot change the system clock",
1001 .description_bad
= "Service processes may change the system clock",
1002 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1005 .assess
= assess_capability_bounding_set
,
1006 .parameter
= UINT64_C(1) << CAP_SYS_TIME
,
1009 .id
= "CapabilityBoundingSet=~CAP_NET_ADMIN",
1010 .json_field
= "CapabilityBoundingSet_CAP_NET_ADMIN",
1011 .description_good
= "Service has no network configuration privileges",
1012 .description_bad
= "Service has network configuration privileges",
1013 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1016 .assess
= assess_capability_bounding_set
,
1017 .parameter
= (UINT64_C(1) << CAP_NET_ADMIN
),
1020 .id
= "CapabilityBoundingSet=~CAP_SYS_RAWIO",
1021 .json_field
= "CapabilityBoundingSet_CAP_SYS_RAWIO",
1022 .description_good
= "Service has no raw I/O access",
1023 .description_bad
= "Service has raw I/O access",
1024 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1027 .assess
= assess_capability_bounding_set
,
1028 .parameter
= (UINT64_C(1) << CAP_SYS_RAWIO
),
1031 .id
= "CapabilityBoundingSet=~CAP_SYS_MODULE",
1032 .json_field
= "CapabilityBoundingSet_CAP_SYS_MODULE",
1033 .description_good
= "Service cannot load kernel modules",
1034 .description_bad
= "Service may load kernel modules",
1035 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1038 .assess
= assess_capability_bounding_set
,
1039 .parameter
= (UINT64_C(1) << CAP_SYS_MODULE
),
1042 .id
= "CapabilityBoundingSet=~CAP_AUDIT_*",
1043 .json_field
= "CapabilityBoundingSet_CAP_AUDIT",
1044 .description_good
= "Service has no audit subsystem access",
1045 .description_bad
= "Service has audit subsystem access",
1046 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1049 .assess
= assess_capability_bounding_set
,
1050 .parameter
= (UINT64_C(1) << CAP_AUDIT_CONTROL
) |
1051 (UINT64_C(1) << CAP_AUDIT_READ
) |
1052 (UINT64_C(1) << CAP_AUDIT_WRITE
),
1055 .id
= "CapabilityBoundingSet=~CAP_SYSLOG",
1056 .json_field
= "CapabilityBoundingSet_CAP_SYSLOG",
1057 .description_good
= "Service has no access to kernel logging",
1058 .description_bad
= "Service has access to kernel logging",
1059 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1062 .assess
= assess_capability_bounding_set
,
1063 .parameter
= (UINT64_C(1) << CAP_SYSLOG
),
1066 .id
= "CapabilityBoundingSet=~CAP_SYS_(NICE|RESOURCE)",
1067 .json_field
= "CapabilityBoundingSet_CAP_SYS_NICE_RESOURCE",
1068 .description_good
= "Service has no privileges to change resource use parameters",
1069 .description_bad
= "Service has privileges to change resource use parameters",
1070 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1073 .assess
= assess_capability_bounding_set
,
1074 .parameter
= (UINT64_C(1) << CAP_SYS_NICE
) |
1075 (UINT64_C(1) << CAP_SYS_RESOURCE
),
1078 .id
= "CapabilityBoundingSet=~CAP_MKNOD",
1079 .json_field
= "CapabilityBoundingSet_CAP_MKNOD",
1080 .description_good
= "Service cannot create device nodes",
1081 .description_bad
= "Service may create device nodes",
1082 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1085 .assess
= assess_capability_bounding_set
,
1086 .parameter
= (UINT64_C(1) << CAP_MKNOD
),
1089 .id
= "CapabilityBoundingSet=~CAP_(CHOWN|FSETID|SETFCAP)",
1090 .json_field
= "CapabilityBoundingSet_CAP_CHOWN_FSETID_SETFCAP",
1091 .description_good
= "Service cannot change file ownership/access mode/capabilities",
1092 .description_bad
= "Service may change file ownership/access mode/capabilities unrestricted",
1093 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1096 .assess
= assess_capability_bounding_set
,
1097 .parameter
= (UINT64_C(1) << CAP_CHOWN
) |
1098 (UINT64_C(1) << CAP_FSETID
) |
1099 (UINT64_C(1) << CAP_SETFCAP
),
1102 .id
= "CapabilityBoundingSet=~CAP_(DAC_*|FOWNER|IPC_OWNER)",
1103 .json_field
= "CapabilityBoundingSet_CAP_DAC_FOWNER_IPC_OWNER",
1104 .description_good
= "Service cannot override UNIX file/IPC permission checks",
1105 .description_bad
= "Service may override UNIX file/IPC permission checks",
1106 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1109 .assess
= assess_capability_bounding_set
,
1110 .parameter
= (UINT64_C(1) << CAP_DAC_OVERRIDE
) |
1111 (UINT64_C(1) << CAP_DAC_READ_SEARCH
) |
1112 (UINT64_C(1) << CAP_FOWNER
) |
1113 (UINT64_C(1) << CAP_IPC_OWNER
),
1116 .id
= "CapabilityBoundingSet=~CAP_KILL",
1117 .json_field
= "CapabilityBoundingSet_CAP_KILL",
1118 .description_good
= "Service cannot send UNIX signals to arbitrary processes",
1119 .description_bad
= "Service may send UNIX signals to arbitrary processes",
1120 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1123 .assess
= assess_capability_bounding_set
,
1124 .parameter
= (UINT64_C(1) << CAP_KILL
),
1127 .id
= "CapabilityBoundingSet=~CAP_NET_(BIND_SERVICE|BROADCAST|RAW)",
1128 .json_field
= "CapabilityBoundingSet_CAP_NET_BIND_SERVICE_BROADCAST_RAW)",
1129 .description_good
= "Service has no elevated networking privileges",
1130 .description_bad
= "Service has elevated networking privileges",
1131 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1134 .assess
= assess_capability_bounding_set
,
1135 .parameter
= (UINT64_C(1) << CAP_NET_BIND_SERVICE
) |
1136 (UINT64_C(1) << CAP_NET_BROADCAST
) |
1137 (UINT64_C(1) << CAP_NET_RAW
),
1140 .id
= "CapabilityBoundingSet=~CAP_SYS_BOOT",
1141 .json_field
= "CapabilityBoundingSet_CAP_SYS_BOOT",
1142 .description_good
= "Service cannot issue reboot()",
1143 .description_bad
= "Service may issue reboot()",
1144 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1147 .assess
= assess_capability_bounding_set
,
1148 .parameter
= (UINT64_C(1) << CAP_SYS_BOOT
),
1151 .id
= "CapabilityBoundingSet=~CAP_MAC_*",
1152 .json_field
= "CapabilityBoundingSet_CAP_MAC",
1153 .description_good
= "Service cannot adjust SMACK MAC",
1154 .description_bad
= "Service may adjust SMACK MAC",
1155 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1158 .assess
= assess_capability_bounding_set
,
1159 .parameter
= (UINT64_C(1) << CAP_MAC_ADMIN
)|
1160 (UINT64_C(1) << CAP_MAC_OVERRIDE
),
1163 .id
= "CapabilityBoundingSet=~CAP_LINUX_IMMUTABLE",
1164 .json_field
= "CapabilityBoundingSet_CAP_LINUX_IMMUTABLE",
1165 .description_good
= "Service cannot mark files immutable",
1166 .description_bad
= "Service may mark files immutable",
1167 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1170 .assess
= assess_capability_bounding_set
,
1171 .parameter
= (UINT64_C(1) << CAP_LINUX_IMMUTABLE
),
1174 .id
= "CapabilityBoundingSet=~CAP_IPC_LOCK",
1175 .json_field
= "CapabilityBoundingSet_CAP_IPC_LOCK",
1176 .description_good
= "Service cannot lock memory into RAM",
1177 .description_bad
= "Service may lock memory into RAM",
1178 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1181 .assess
= assess_capability_bounding_set
,
1182 .parameter
= (UINT64_C(1) << CAP_IPC_LOCK
),
1185 .id
= "CapabilityBoundingSet=~CAP_SYS_CHROOT",
1186 .json_field
= "CapabilityBoundingSet_CAP_SYS_CHROOT",
1187 .description_good
= "Service cannot issue chroot()",
1188 .description_bad
= "Service may issue chroot()",
1189 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1192 .assess
= assess_capability_bounding_set
,
1193 .parameter
= (UINT64_C(1) << CAP_SYS_CHROOT
),
1196 .id
= "CapabilityBoundingSet=~CAP_BLOCK_SUSPEND",
1197 .json_field
= "CapabilityBoundingSet_CAP_BLOCK_SUSPEND",
1198 .description_good
= "Service cannot establish wake locks",
1199 .description_bad
= "Service may establish wake locks",
1200 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1203 .assess
= assess_capability_bounding_set
,
1204 .parameter
= (UINT64_C(1) << CAP_BLOCK_SUSPEND
),
1207 .id
= "CapabilityBoundingSet=~CAP_WAKE_ALARM",
1208 .json_field
= "CapabilityBoundingSet_CAP_WAKE_ALARM",
1209 .description_good
= "Service cannot program timers that wake up the system",
1210 .description_bad
= "Service may program timers that wake up the system",
1211 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1214 .assess
= assess_capability_bounding_set
,
1215 .parameter
= (UINT64_C(1) << CAP_WAKE_ALARM
),
1218 .id
= "CapabilityBoundingSet=~CAP_LEASE",
1219 .json_field
= "CapabilityBoundingSet_CAP_LEASE",
1220 .description_good
= "Service cannot create file leases",
1221 .description_bad
= "Service may create file leases",
1222 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1225 .assess
= assess_capability_bounding_set
,
1226 .parameter
= (UINT64_C(1) << CAP_LEASE
),
1229 .id
= "CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG",
1230 .json_field
= "CapabilityBoundingSet_CAP_SYS_TTY_CONFIG",
1231 .description_good
= "Service cannot issue vhangup()",
1232 .description_bad
= "Service may issue vhangup()",
1233 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1236 .assess
= assess_capability_bounding_set
,
1237 .parameter
= (UINT64_C(1) << CAP_SYS_TTY_CONFIG
),
1240 .id
= "CapabilityBoundingSet=~CAP_SYS_PACCT",
1241 .json_field
= "CapabilityBoundingSet_CAP_SYS_PACCT",
1242 .description_good
= "Service cannot use acct()",
1243 .description_bad
= "Service may use acct()",
1244 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1247 .assess
= assess_capability_bounding_set
,
1248 .parameter
= (UINT64_C(1) << CAP_SYS_PACCT
),
1252 .json_field
= "UMask",
1253 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#UMask=",
1256 .assess
= assess_umask
,
1259 .id
= "KeyringMode=",
1260 .json_field
= "KeyringMode",
1261 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#KeyringMode=",
1262 .description_good
= "Service doesn't share key material with other services",
1263 .description_bad
= "Service shares key material with other service",
1266 .assess
= assess_keyring_mode
,
1269 .id
= "ProtectProc=",
1270 .json_field
= "ProtectProc",
1271 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectProc=",
1272 .description_good
= "Service has restricted access to process tree (/proc hidepid=)",
1273 .description_bad
= "Service has full access to process tree (/proc hidepid=)",
1276 .assess
= assess_protect_proc
,
1279 .id
= "ProcSubset=",
1280 .json_field
= "ProcSubset",
1281 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProcSubset=",
1282 .description_good
= "Service has no access to non-process /proc files (/proc subset=)",
1283 .description_bad
= "Service has full access to non-process /proc files (/proc subset=)",
1286 .assess
= assess_proc_subset
,
1289 .id
= "NotifyAccess=",
1290 .json_field
= "NotifyAccess",
1291 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NotifyAccess=",
1292 .description_good
= "Service child processes cannot alter service state",
1293 .description_bad
= "Service child processes may alter service state",
1296 .assess
= assess_notify_access
,
1300 .json_field
= "RemoveIPC",
1301 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RemoveIPC=",
1302 .description_good
= "Service user cannot leave SysV IPC objects around",
1303 .description_bad
= "Service user may leave SysV IPC objects around",
1304 .description_na
= "Service runs as root, option does not apply",
1307 .assess
= assess_remove_ipc
,
1308 .offset
= offsetof(SecurityInfo
, remove_ipc
),
1312 .json_field
= "Delegate",
1313 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Delegate=",
1314 .description_good
= "Service does not maintain its own delegated control group subtree",
1315 .description_bad
= "Service maintains its own delegated control group subtree",
1318 .assess
= assess_bool
,
1319 .offset
= offsetof(SecurityInfo
, delegate
),
1320 .parameter
= true, /* invert! */
1323 .id
= "RestrictRealtime=",
1324 .json_field
= "RestrictRealtime",
1325 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictRealtime=",
1326 .description_good
= "Service realtime scheduling access is restricted",
1327 .description_bad
= "Service may acquire realtime scheduling",
1330 .assess
= assess_bool
,
1331 .offset
= offsetof(SecurityInfo
, restrict_realtime
),
1334 .id
= "RestrictSUIDSGID=",
1335 .json_field
= "RestrictSUIDSGID",
1336 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictSUIDSGID=",
1337 .description_good
= "SUID/SGID file creation by service is restricted",
1338 .description_bad
= "Service may create SUID/SGID files",
1341 .assess
= assess_bool
,
1342 .offset
= offsetof(SecurityInfo
, restrict_suid_sgid
),
1345 .id
= "RestrictNamespaces=~user",
1346 .json_field
= "RestrictNamespaces_user",
1347 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1348 .description_good
= "Service cannot create user namespaces",
1349 .description_bad
= "Service may create user namespaces",
1352 .assess
= assess_restrict_namespaces
,
1353 .parameter
= CLONE_NEWUSER
,
1356 .id
= "RestrictNamespaces=~mnt",
1357 .json_field
= "RestrictNamespaces_mnt",
1358 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1359 .description_good
= "Service cannot create file system namespaces",
1360 .description_bad
= "Service may create file system namespaces",
1363 .assess
= assess_restrict_namespaces
,
1364 .parameter
= CLONE_NEWNS
,
1367 .id
= "RestrictNamespaces=~ipc",
1368 .json_field
= "RestrictNamespaces_ipc",
1369 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1370 .description_good
= "Service cannot create IPC namespaces",
1371 .description_bad
= "Service may create IPC namespaces",
1374 .assess
= assess_restrict_namespaces
,
1375 .parameter
= CLONE_NEWIPC
,
1378 .id
= "RestrictNamespaces=~pid",
1379 .json_field
= "RestrictNamespaces_pid",
1380 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1381 .description_good
= "Service cannot create process namespaces",
1382 .description_bad
= "Service may create process namespaces",
1385 .assess
= assess_restrict_namespaces
,
1386 .parameter
= CLONE_NEWPID
,
1389 .id
= "RestrictNamespaces=~cgroup",
1390 .json_field
= "RestrictNamespaces_cgroup",
1391 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1392 .description_good
= "Service cannot create cgroup namespaces",
1393 .description_bad
= "Service may create cgroup namespaces",
1396 .assess
= assess_restrict_namespaces
,
1397 .parameter
= CLONE_NEWCGROUP
,
1400 .id
= "RestrictNamespaces=~net",
1401 .json_field
= "RestrictNamespaces_net",
1402 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1403 .description_good
= "Service cannot create network namespaces",
1404 .description_bad
= "Service may create network namespaces",
1407 .assess
= assess_restrict_namespaces
,
1408 .parameter
= CLONE_NEWNET
,
1411 .id
= "RestrictNamespaces=~uts",
1412 .json_field
= "RestrictNamespaces_uts",
1413 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1414 .description_good
= "Service cannot create hostname namespaces",
1415 .description_bad
= "Service may create hostname namespaces",
1418 .assess
= assess_restrict_namespaces
,
1419 .parameter
= CLONE_NEWUTS
,
1422 .id
= "RestrictAddressFamilies=~AF_(INET|INET6)",
1423 .json_field
= "RestrictAddressFamilies_AF_INET_INET6",
1424 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1425 .description_good
= "Service cannot allocate Internet sockets",
1426 .description_bad
= "Service may allocate Internet sockets",
1429 .assess
= assess_bool
,
1430 .offset
= offsetof(SecurityInfo
, restrict_address_family_inet
),
1433 .id
= "RestrictAddressFamilies=~AF_UNIX",
1434 .json_field
= "RestrictAddressFamilies_AF_UNIX",
1435 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1436 .description_good
= "Service cannot allocate local sockets",
1437 .description_bad
= "Service may allocate local sockets",
1440 .assess
= assess_bool
,
1441 .offset
= offsetof(SecurityInfo
, restrict_address_family_unix
),
1444 .id
= "RestrictAddressFamilies=~AF_NETLINK",
1445 .json_field
= "RestrictAddressFamilies_AF_NETLINK",
1446 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1447 .description_good
= "Service cannot allocate netlink sockets",
1448 .description_bad
= "Service may allocate netlink sockets",
1451 .assess
= assess_bool
,
1452 .offset
= offsetof(SecurityInfo
, restrict_address_family_netlink
),
1455 .id
= "RestrictAddressFamilies=~AF_PACKET",
1456 .json_field
= "RestrictAddressFamilies_AF_PACKET",
1457 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1458 .description_good
= "Service cannot allocate packet sockets",
1459 .description_bad
= "Service may allocate packet sockets",
1462 .assess
= assess_bool
,
1463 .offset
= offsetof(SecurityInfo
, restrict_address_family_packet
),
1466 .id
= "RestrictAddressFamilies=~…",
1467 .json_field
= "RestrictAddressFamilies_OTHER",
1468 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1469 .description_good
= "Service cannot allocate exotic sockets",
1470 .description_bad
= "Service may allocate exotic sockets",
1473 .assess
= assess_bool
,
1474 .offset
= offsetof(SecurityInfo
, restrict_address_family_other
),
1477 .id
= "SystemCallArchitectures=",
1478 .json_field
= "SystemCallArchitectures",
1479 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallArchitectures=",
1482 .assess
= assess_system_call_architectures
,
1486 .id
= "SystemCallFilter=~@swap",
1487 .json_field
= "SystemCallFilter_swap",
1488 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1491 .assess
= assess_system_call_filter
,
1492 .parameter
= SYSCALL_FILTER_SET_SWAP
,
1495 .id
= "SystemCallFilter=~@obsolete",
1496 .json_field
= "SystemCallFilter_obsolete",
1497 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1500 .assess
= assess_system_call_filter
,
1501 .parameter
= SYSCALL_FILTER_SET_OBSOLETE
,
1504 .id
= "SystemCallFilter=~@clock",
1505 .json_field
= "SystemCallFilter_clock",
1506 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1509 .assess
= assess_system_call_filter
,
1510 .parameter
= SYSCALL_FILTER_SET_CLOCK
,
1513 .id
= "SystemCallFilter=~@cpu-emulation",
1514 .json_field
= "SystemCallFilter_cpu_emulation",
1515 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1518 .assess
= assess_system_call_filter
,
1519 .parameter
= SYSCALL_FILTER_SET_CPU_EMULATION
,
1522 .id
= "SystemCallFilter=~@debug",
1523 .json_field
= "SystemCallFilter_debug",
1524 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1527 .assess
= assess_system_call_filter
,
1528 .parameter
= SYSCALL_FILTER_SET_DEBUG
,
1531 .id
= "SystemCallFilter=~@mount",
1532 .json_field
= "SystemCallFilter_mount",
1533 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1536 .assess
= assess_system_call_filter
,
1537 .parameter
= SYSCALL_FILTER_SET_MOUNT
,
1540 .id
= "SystemCallFilter=~@module",
1541 .json_field
= "SystemCallFilter_module",
1542 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1545 .assess
= assess_system_call_filter
,
1546 .parameter
= SYSCALL_FILTER_SET_MODULE
,
1549 .id
= "SystemCallFilter=~@raw-io",
1550 .json_field
= "SystemCallFilter_raw_io",
1551 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1554 .assess
= assess_system_call_filter
,
1555 .parameter
= SYSCALL_FILTER_SET_RAW_IO
,
1558 .id
= "SystemCallFilter=~@reboot",
1559 .json_field
= "SystemCallFilter_reboot",
1560 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1563 .assess
= assess_system_call_filter
,
1564 .parameter
= SYSCALL_FILTER_SET_REBOOT
,
1567 .id
= "SystemCallFilter=~@privileged",
1568 .json_field
= "SystemCallFilter_privileged",
1569 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1572 .assess
= assess_system_call_filter
,
1573 .parameter
= SYSCALL_FILTER_SET_PRIVILEGED
,
1576 .id
= "SystemCallFilter=~@resources",
1577 .json_field
= "SystemCallFilter_resources",
1578 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1581 .assess
= assess_system_call_filter
,
1582 .parameter
= SYSCALL_FILTER_SET_RESOURCES
,
1586 .id
= "IPAddressDeny=",
1587 .json_field
= "IPAddressDeny",
1588 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#IPAddressDeny=",
1591 .assess
= assess_ip_address_allow
,
1594 .id
= "DeviceAllow=",
1595 .json_field
= "DeviceAllow",
1596 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#DeviceAllow=",
1599 .assess
= assess_device_allow
,
1602 .id
= "AmbientCapabilities=",
1603 .json_field
= "AmbientCapabilities",
1604 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#AmbientCapabilities=",
1605 .description_good
= "Service process does not receive ambient capabilities",
1606 .description_bad
= "Service process receives ambient capabilities",
1609 .assess
= assess_ambient_capabilities
,
1613 static JsonVariant
* security_assessor_find_in_policy(const struct security_assessor
*a
, JsonVariant
*policy
, const char *name
) {
1619 if (!json_variant_is_object(policy
)) {
1620 log_debug("Specified policy is not a JSON object, ignoring.");
1624 item
= json_variant_by_key(policy
, a
->json_field
);
1627 if (!json_variant_is_object(item
)) {
1628 log_debug("Item for '%s' in policy JSON object is not an object, ignoring.", a
->id
);
1632 return name
? json_variant_by_key(item
, name
) : item
;
1635 static uint64_t access_weight(const struct security_assessor
*a
, JsonVariant
*policy
) {
1640 val
= security_assessor_find_in_policy(a
, policy
, "weight");
1642 if (json_variant_is_unsigned(val
))
1643 return json_variant_unsigned(val
);
1644 log_debug("JSON field 'weight' of policy for %s is not an unsigned integer, ignoring.", a
->id
);
1650 static uint64_t access_range(const struct security_assessor
*a
, JsonVariant
*policy
) {
1655 val
= security_assessor_find_in_policy(a
, policy
, "range");
1657 if (json_variant_is_unsigned(val
))
1658 return json_variant_unsigned(val
);
1659 log_debug("JSON field 'range' of policy for %s is not an unsigned integer, ignoring.", a
->id
);
1665 static const char *access_description_na(const struct security_assessor
*a
, JsonVariant
*policy
) {
1670 val
= security_assessor_find_in_policy(a
, policy
, "description_na");
1672 if (json_variant_is_string(val
))
1673 return json_variant_string(val
);
1674 log_debug("JSON field 'description_na' of policy for %s is not a string, ignoring.", a
->id
);
1677 return a
->description_na
;
1680 static const char *access_description_good(const struct security_assessor
*a
, JsonVariant
*policy
) {
1685 val
= security_assessor_find_in_policy(a
, policy
, "description_good");
1687 if (json_variant_is_string(val
))
1688 return json_variant_string(val
);
1689 log_debug("JSON field 'description_good' of policy for %s is not a string, ignoring.", a
->id
);
1692 return a
->description_good
;
1695 static const char *access_description_bad(const struct security_assessor
*a
, JsonVariant
*policy
) {
1700 val
= security_assessor_find_in_policy(a
, policy
, "description_bad");
1702 if (json_variant_is_string(val
))
1703 return json_variant_string(val
);
1704 log_debug("JSON field 'description_bad' of policy for %s is not a string, ignoring.", a
->id
);
1707 return a
->description_bad
;
1710 static int assess(const SecurityInfo
*info
,
1711 Table
*overview_table
,
1712 AnalyzeSecurityFlags flags
,
1714 JsonVariant
*policy
,
1715 PagerFlags pager_flags
,
1716 JsonFormatFlags json_format_flags
) {
1718 static const struct {
1722 SpecialGlyph smiley
;
1723 } badness_table
[] = {
1724 { 100, "DANGEROUS", ANSI_HIGHLIGHT_RED
, SPECIAL_GLYPH_DEPRESSED_SMILEY
},
1725 { 90, "UNSAFE", ANSI_HIGHLIGHT_RED
, SPECIAL_GLYPH_UNHAPPY_SMILEY
},
1726 { 75, "EXPOSED", ANSI_HIGHLIGHT_YELLOW
, SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY
},
1727 { 50, "MEDIUM", NULL
, SPECIAL_GLYPH_NEUTRAL_SMILEY
},
1728 { 10, "OK", ANSI_HIGHLIGHT_GREEN
, SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY
},
1729 { 1, "SAFE", ANSI_HIGHLIGHT_GREEN
, SPECIAL_GLYPH_HAPPY_SMILEY
},
1730 { 0, "PERFECT", ANSI_HIGHLIGHT_GREEN
, SPECIAL_GLYPH_ECSTATIC_SMILEY
},
1733 uint64_t badness_sum
= 0, weight_sum
= 0, exposure
;
1734 _cleanup_(table_unrefp
) Table
*details_table
= NULL
;
1738 if (!FLAGS_SET(flags
, ANALYZE_SECURITY_SHORT
)) {
1739 details_table
= table_new(" ", "name", "json_field", "description", "weight", "badness", "range", "exposure");
1743 r
= table_set_json_field_name(details_table
, 0, "set");
1745 return log_error_errno(r
, "Failed to set JSON field name of column 0: %m");
1747 (void) table_set_sort(details_table
, (size_t) 3, (size_t) 1);
1748 (void) table_set_reverse(details_table
, 3, true);
1750 if (getenv_bool("SYSTEMD_ANALYZE_DEBUG") <= 0)
1751 (void) table_set_display(details_table
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 7);
1754 for (i
= 0; i
< ELEMENTSOF(security_assessor_table
); i
++) {
1755 const struct security_assessor
*a
= security_assessor_table
+ i
;
1756 _cleanup_free_
char *d
= NULL
;
1759 uint64_t weight
= access_weight(a
, policy
);
1760 uint64_t range
= access_range(a
, policy
);
1762 data
= (uint8_t *) info
+ a
->offset
;
1764 if (a
->default_dependencies_only
&& !info
->default_dependencies
) {
1765 badness
= UINT64_MAX
;
1766 d
= strdup("Service runs in special boot phase, option is not appropriate");
1769 } else if (weight
== 0) {
1770 badness
= UINT64_MAX
;
1771 d
= strdup("Option excluded by policy, skipping");
1775 r
= a
->assess(a
, info
, data
, &badness
, &d
);
1782 if (badness
!= UINT64_MAX
) {
1783 assert(badness
<= range
);
1785 badness_sum
+= DIV_ROUND_UP(badness
* weight
, range
);
1786 weight_sum
+= weight
;
1789 if (details_table
) {
1790 const char *description
, *color
= NULL
;
1793 if (badness
== UINT64_MAX
) {
1795 description
= access_description_na(a
, policy
);
1797 } else if (badness
== a
->range
) {
1799 description
= access_description_bad(a
, policy
);
1800 color
= ansi_highlight_red();
1801 } else if (badness
== 0) {
1803 description
= access_description_good(a
, policy
);
1804 color
= ansi_highlight_green();
1808 color
= ansi_highlight_red();
1814 if (checkmark
< 0) {
1815 r
= table_add_many(details_table
, TABLE_EMPTY
);
1817 return table_log_add_error(r
);
1819 r
= table_add_many(details_table
,
1820 TABLE_BOOLEAN_CHECKMARK
, checkmark
> 0,
1821 TABLE_SET_MINIMUM_WIDTH
, 1,
1822 TABLE_SET_MAXIMUM_WIDTH
, 1,
1823 TABLE_SET_ELLIPSIZE_PERCENT
, 0,
1824 TABLE_SET_COLOR
, color
);
1826 return table_log_add_error(r
);
1829 r
= table_add_many(details_table
,
1830 TABLE_STRING
, a
->id
, TABLE_SET_URL
, a
->url
,
1831 TABLE_STRING
, a
->json_field
,
1832 TABLE_STRING
, description
,
1833 TABLE_UINT64
, weight
, TABLE_SET_ALIGN_PERCENT
, 100,
1834 TABLE_UINT64
, badness
, TABLE_SET_ALIGN_PERCENT
, 100,
1835 TABLE_UINT64
, range
, TABLE_SET_ALIGN_PERCENT
, 100,
1836 TABLE_EMPTY
, TABLE_SET_ALIGN_PERCENT
, 100);
1838 return table_log_add_error(r
);
1842 assert(weight_sum
> 0);
1844 if (details_table
) {
1847 for (row
= 1; row
< table_get_rows(details_table
); row
++) {
1848 char buf
[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
1849 const uint64_t *weight
, *badness
, *range
;
1853 assert_se(weight
= table_get_at(details_table
, row
, 4));
1854 assert_se(badness
= table_get_at(details_table
, row
, 5));
1855 assert_se(range
= table_get_at(details_table
, row
, 6));
1857 if (*badness
== UINT64_MAX
|| *badness
== 0)
1860 assert_se(cell
= table_get_cell(details_table
, row
, 7));
1862 x
= DIV_ROUND_UP(DIV_ROUND_UP(*badness
* *weight
* 100U, *range
), weight_sum
);
1863 xsprintf(buf
, "%" PRIu64
".%" PRIu64
, x
/ 10, x
% 10);
1865 r
= table_update(details_table
, cell
, TABLE_STRING
, buf
);
1867 return log_error_errno(r
, "Failed to update cell in table: %m");
1870 if (json_format_flags
& JSON_FORMAT_OFF
) {
1871 r
= table_hide_column_from_display(details_table
, (size_t) 2);
1873 return log_error_errno(r
, "Failed to set columns to display: %m");
1876 r
= table_print_with_pager(details_table
, json_format_flags
, pager_flags
, /* show_header= */true);
1878 return log_error_errno(r
, "Failed to output table: %m");
1881 exposure
= DIV_ROUND_UP(badness_sum
* 100U, weight_sum
);
1883 for (i
= 0; i
< ELEMENTSOF(badness_table
); i
++)
1884 if (exposure
>= badness_table
[i
].exposure
)
1887 assert(i
< ELEMENTSOF(badness_table
));
1889 if (details_table
&& (json_format_flags
& JSON_FORMAT_OFF
)) {
1890 _cleanup_free_
char *clickable
= NULL
;
1893 /* If we shall output the details table, also print the brief summary underneath */
1895 if (info
->fragment_path
) {
1896 r
= terminal_urlify_path(info
->fragment_path
, info
->id
, &clickable
);
1904 printf("\n%s %sOverall exposure level for %s%s: %s%" PRIu64
".%" PRIu64
" %s%s %s\n",
1905 special_glyph(SPECIAL_GLYPH_ARROW
),
1909 colors_enabled() ? strempty(badness_table
[i
].color
) : "",
1910 exposure
/ 10, exposure
% 10,
1911 badness_table
[i
].name
,
1913 special_glyph(badness_table
[i
].smiley
));
1918 if (overview_table
) {
1919 char buf
[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
1920 _cleanup_free_
char *url
= NULL
;
1922 if (info
->fragment_path
) {
1923 r
= file_url_from_path(info
->fragment_path
, &url
);
1925 return log_error_errno(r
, "Failed to generate URL from path: %m");
1928 xsprintf(buf
, "%" PRIu64
".%" PRIu64
, exposure
/ 10, exposure
% 10);
1930 r
= table_add_many(overview_table
,
1931 TABLE_STRING
, info
->id
,
1934 TABLE_SET_ALIGN_PERCENT
, 100,
1935 TABLE_STRING
, badness_table
[i
].name
,
1936 TABLE_SET_COLOR
, strempty(badness_table
[i
].color
),
1937 TABLE_STRING
, special_glyph(badness_table
[i
].smiley
));
1939 return table_log_add_error(r
);
1942 /* Return error when overall exposure level is over threshold */
1943 if (exposure
> threshold
)
1949 static int property_read_restrict_namespaces(
1953 sd_bus_error
*error
,
1956 SecurityInfo
*info
= userdata
;
1958 uint64_t namespaces
;
1965 r
= sd_bus_message_read(m
, "t", &namespaces
);
1969 info
->restrict_namespaces
= (unsigned long long) namespaces
;
1974 static int property_read_umask(
1978 sd_bus_error
*error
,
1981 SecurityInfo
*info
= userdata
;
1990 r
= sd_bus_message_read(m
, "u", &umask
);
1994 info
->_umask
= (mode_t
) umask
;
1999 static int property_read_restrict_address_families(
2003 sd_bus_error
*error
,
2006 SecurityInfo
*info
= userdata
;
2013 r
= sd_bus_message_enter_container(m
, 'r', "bas");
2017 r
= sd_bus_message_read(m
, "b", &allow_list
);
2021 info
->restrict_address_family_inet
=
2022 info
->restrict_address_family_unix
=
2023 info
->restrict_address_family_netlink
=
2024 info
->restrict_address_family_packet
=
2025 info
->restrict_address_family_other
= allow_list
;
2027 r
= sd_bus_message_enter_container(m
, 'a', "s");
2034 r
= sd_bus_message_read(m
, "s", &name
);
2040 if (STR_IN_SET(name
, "AF_INET", "AF_INET6"))
2041 info
->restrict_address_family_inet
= !allow_list
;
2042 else if (streq(name
, "AF_UNIX"))
2043 info
->restrict_address_family_unix
= !allow_list
;
2044 else if (streq(name
, "AF_NETLINK"))
2045 info
->restrict_address_family_netlink
= !allow_list
;
2046 else if (streq(name
, "AF_PACKET"))
2047 info
->restrict_address_family_packet
= !allow_list
;
2049 info
->restrict_address_family_other
= !allow_list
;
2052 r
= sd_bus_message_exit_container(m
);
2056 return sd_bus_message_exit_container(m
);
2059 static int property_read_syscall_archs(
2063 sd_bus_error
*error
,
2066 SecurityInfo
*info
= userdata
;
2074 r
= sd_bus_message_enter_container(m
, 'a', "s");
2081 r
= sd_bus_message_read(m
, "s", &name
);
2087 r
= set_put_strdup(&info
->system_call_architectures
, name
);
2092 return sd_bus_message_exit_container(m
);
2095 static int property_read_system_call_filter(
2099 sd_bus_error
*error
,
2102 SecurityInfo
*info
= userdata
;
2109 r
= sd_bus_message_enter_container(m
, 'r', "bas");
2113 r
= sd_bus_message_read(m
, "b", &allow_list
);
2117 info
->system_call_filter_allow_list
= allow_list
;
2119 r
= sd_bus_message_enter_container(m
, 'a', "s");
2126 r
= sd_bus_message_read(m
, "s", &name
);
2132 /* The actual ExecContext stores the system call id as the map value, which we don't
2133 * need. So we assign NULL to all values here. */
2134 r
= hashmap_put_strdup(&info
->system_call_filter
, name
, NULL
);
2139 r
= sd_bus_message_exit_container(m
);
2143 return sd_bus_message_exit_container(m
);
2146 static int property_read_ip_address_allow(
2150 sd_bus_error
*error
,
2153 SecurityInfo
*info
= userdata
;
2154 bool deny_ipv4
= false, deny_ipv6
= false;
2161 r
= sd_bus_message_enter_container(m
, 'a', "(iayu)");
2171 r
= sd_bus_message_enter_container(m
, 'r', "iayu");
2177 r
= sd_bus_message_read(m
, "i", &family
);
2181 r
= sd_bus_message_read_array(m
, 'y', &data
, &size
);
2185 r
= sd_bus_message_read(m
, "u", &prefixlen
);
2189 r
= sd_bus_message_exit_container(m
);
2193 if (streq(member
, "IPAddressAllow")) {
2194 union in_addr_union u
;
2196 if (family
== AF_INET
&& size
== 4 && prefixlen
== 8)
2197 memcpy(&u
.in
, data
, size
);
2198 else if (family
== AF_INET6
&& size
== 16 && prefixlen
== 128)
2199 memcpy(&u
.in6
, data
, size
);
2201 info
->ip_address_allow_other
= true;
2205 if (in_addr_is_localhost(family
, &u
))
2206 info
->ip_address_allow_localhost
= true;
2208 info
->ip_address_allow_other
= true;
2210 assert(streq(member
, "IPAddressDeny"));
2212 if (family
== AF_INET
&& size
== 4 && prefixlen
== 0)
2214 else if (family
== AF_INET6
&& size
== 16 && prefixlen
== 0)
2219 info
->ip_address_deny_all
= deny_ipv4
&& deny_ipv6
;
2221 return sd_bus_message_exit_container(m
);
2224 static int property_read_ip_filters(
2228 sd_bus_error
*error
,
2231 SecurityInfo
*info
= userdata
;
2232 _cleanup_(strv_freep
) char **l
= NULL
;
2239 r
= sd_bus_message_read_strv(m
, &l
);
2243 if (streq(member
, "IPIngressFilterPath"))
2244 info
->ip_filters_custom_ingress
= !strv_isempty(l
);
2245 else if (streq(member
, "IPEgressFilterPath"))
2246 info
->ip_filters_custom_egress
= !strv_isempty(l
);
2251 static int property_read_device_allow(
2255 sd_bus_error
*error
,
2258 SecurityInfo
*info
= userdata
;
2266 r
= sd_bus_message_enter_container(m
, 'a', "(ss)");
2271 const char *name
, *policy
;
2273 r
= sd_bus_message_read(m
, "(ss)", &name
, &policy
);
2282 info
->device_allow_non_empty
= n
> 0;
2284 return sd_bus_message_exit_container(m
);
2287 static int acquire_security_info(sd_bus
*bus
, const char *name
, SecurityInfo
*info
, AnalyzeSecurityFlags flags
) {
2289 static const struct bus_properties_map security_map
[] = {
2290 { "AmbientCapabilities", "t", NULL
, offsetof(SecurityInfo
, ambient_capabilities
) },
2291 { "CapabilityBoundingSet", "t", NULL
, offsetof(SecurityInfo
, capability_bounding_set
) },
2292 { "DefaultDependencies", "b", NULL
, offsetof(SecurityInfo
, default_dependencies
) },
2293 { "Delegate", "b", NULL
, offsetof(SecurityInfo
, delegate
) },
2294 { "DeviceAllow", "a(ss)", property_read_device_allow
, 0 },
2295 { "DevicePolicy", "s", NULL
, offsetof(SecurityInfo
, device_policy
) },
2296 { "DynamicUser", "b", NULL
, offsetof(SecurityInfo
, dynamic_user
) },
2297 { "FragmentPath", "s", NULL
, offsetof(SecurityInfo
, fragment_path
) },
2298 { "IPAddressAllow", "a(iayu)", property_read_ip_address_allow
, 0 },
2299 { "IPAddressDeny", "a(iayu)", property_read_ip_address_allow
, 0 },
2300 { "IPIngressFilterPath", "as", property_read_ip_filters
, 0 },
2301 { "IPEgressFilterPath", "as", property_read_ip_filters
, 0 },
2302 { "Id", "s", NULL
, offsetof(SecurityInfo
, id
) },
2303 { "KeyringMode", "s", NULL
, offsetof(SecurityInfo
, keyring_mode
) },
2304 { "ProtectProc", "s", NULL
, offsetof(SecurityInfo
, protect_proc
) },
2305 { "ProcSubset", "s", NULL
, offsetof(SecurityInfo
, proc_subset
) },
2306 { "LoadState", "s", NULL
, offsetof(SecurityInfo
, load_state
) },
2307 { "LockPersonality", "b", NULL
, offsetof(SecurityInfo
, lock_personality
) },
2308 { "MemoryDenyWriteExecute", "b", NULL
, offsetof(SecurityInfo
, memory_deny_write_execute
) },
2309 { "NoNewPrivileges", "b", NULL
, offsetof(SecurityInfo
, no_new_privileges
) },
2310 { "NotifyAccess", "s", NULL
, offsetof(SecurityInfo
, notify_access
) },
2311 { "PrivateDevices", "b", NULL
, offsetof(SecurityInfo
, private_devices
) },
2312 { "PrivateMounts", "b", NULL
, offsetof(SecurityInfo
, private_mounts
) },
2313 { "PrivateNetwork", "b", NULL
, offsetof(SecurityInfo
, private_network
) },
2314 { "PrivateTmp", "b", NULL
, offsetof(SecurityInfo
, private_tmp
) },
2315 { "PrivateUsers", "b", NULL
, offsetof(SecurityInfo
, private_users
) },
2316 { "ProtectControlGroups", "b", NULL
, offsetof(SecurityInfo
, protect_control_groups
) },
2317 { "ProtectHome", "s", NULL
, offsetof(SecurityInfo
, protect_home
) },
2318 { "ProtectHostname", "b", NULL
, offsetof(SecurityInfo
, protect_hostname
) },
2319 { "ProtectKernelModules", "b", NULL
, offsetof(SecurityInfo
, protect_kernel_modules
) },
2320 { "ProtectKernelTunables", "b", NULL
, offsetof(SecurityInfo
, protect_kernel_tunables
) },
2321 { "ProtectKernelLogs", "b", NULL
, offsetof(SecurityInfo
, protect_kernel_logs
) },
2322 { "ProtectClock", "b", NULL
, offsetof(SecurityInfo
, protect_clock
) },
2323 { "ProtectSystem", "s", NULL
, offsetof(SecurityInfo
, protect_system
) },
2324 { "RemoveIPC", "b", NULL
, offsetof(SecurityInfo
, remove_ipc
) },
2325 { "RestrictAddressFamilies", "(bas)", property_read_restrict_address_families
, 0 },
2326 { "RestrictNamespaces", "t", property_read_restrict_namespaces
, 0 },
2327 { "RestrictRealtime", "b", NULL
, offsetof(SecurityInfo
, restrict_realtime
) },
2328 { "RestrictSUIDSGID", "b", NULL
, offsetof(SecurityInfo
, restrict_suid_sgid
) },
2329 { "RootDirectory", "s", NULL
, offsetof(SecurityInfo
, root_directory
) },
2330 { "RootImage", "s", NULL
, offsetof(SecurityInfo
, root_image
) },
2331 { "SupplementaryGroups", "as", NULL
, offsetof(SecurityInfo
, supplementary_groups
) },
2332 { "SystemCallArchitectures", "as", property_read_syscall_archs
, 0 },
2333 { "SystemCallFilter", "(as)", property_read_system_call_filter
, 0 },
2334 { "Type", "s", NULL
, offsetof(SecurityInfo
, type
) },
2335 { "UMask", "u", property_read_umask
, 0 },
2336 { "User", "s", NULL
, offsetof(SecurityInfo
, user
) },
2340 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2341 _cleanup_free_
char *path
= NULL
;
2344 /* Note: this mangles *info on failure! */
2350 path
= unit_dbus_path_from_name(name
);
2354 r
= bus_map_all_properties(
2356 "org.freedesktop.systemd1",
2359 BUS_MAP_STRDUP
| BUS_MAP_BOOLEAN_AS_BOOL
,
2364 return log_error_errno(r
, "Failed to get unit properties: %s", bus_error_message(&error
, r
));
2366 if (!streq_ptr(info
->load_state
, "loaded")) {
2368 if (FLAGS_SET(flags
, ANALYZE_SECURITY_ONLY_LOADED
))
2369 return -EMEDIUMTYPE
;
2371 if (streq_ptr(info
->load_state
, "not-found"))
2372 log_error("Unit %s not found, cannot analyze.", name
);
2373 else if (streq_ptr(info
->load_state
, "masked"))
2374 log_error("Unit %s is masked, cannot analyze.", name
);
2376 log_error("Unit %s not loaded properly, cannot analyze.", name
);
2381 if (FLAGS_SET(flags
, ANALYZE_SECURITY_ONLY_LONG_RUNNING
) && streq_ptr(info
->type
, "oneshot"))
2382 return -EMEDIUMTYPE
;
2384 if (info
->private_devices
||
2385 info
->private_tmp
||
2386 info
->protect_control_groups
||
2387 info
->protect_kernel_tunables
||
2388 info
->protect_kernel_modules
||
2389 !streq_ptr(info
->protect_home
, "no") ||
2390 !streq_ptr(info
->protect_system
, "no") ||
2392 info
->private_mounts
= true;
2394 if (info
->protect_kernel_modules
)
2395 info
->capability_bounding_set
&= ~(UINT64_C(1) << CAP_SYS_MODULE
);
2397 if (info
->protect_kernel_logs
)
2398 info
->capability_bounding_set
&= ~(UINT64_C(1) << CAP_SYSLOG
);
2400 if (info
->protect_clock
)
2401 info
->capability_bounding_set
&= ~((UINT64_C(1) << CAP_SYS_TIME
) |
2402 (UINT64_C(1) << CAP_WAKE_ALARM
));
2404 if (info
->private_devices
)
2405 info
->capability_bounding_set
&= ~((UINT64_C(1) << CAP_MKNOD
) |
2406 (UINT64_C(1) << CAP_SYS_RAWIO
));
2411 static int analyze_security_one(sd_bus
*bus
,
2413 Table
*overview_table
,
2414 AnalyzeSecurityFlags flags
,
2416 JsonVariant
*policy
,
2417 PagerFlags pager_flags
,
2418 JsonFormatFlags json_format_flags
) {
2420 _cleanup_(security_info_freep
) SecurityInfo
*info
= security_info_new();
2429 r
= acquire_security_info(bus
, name
, info
, flags
);
2430 if (r
== -EMEDIUMTYPE
) /* Ignore this one because not loaded or Type is oneshot */
2435 r
= assess(info
, overview_table
, flags
, threshold
, policy
, pager_flags
, json_format_flags
);
2442 /* Refactoring SecurityInfo so that it can make use of existing struct variables instead of reading from dbus */
2443 static int get_security_info(Unit
*u
, ExecContext
*c
, CGroupContext
*g
, SecurityInfo
**ret_info
) {
2446 _cleanup_(security_info_freep
) SecurityInfo
*info
= security_info_new();
2452 info
->id
= strdup(u
->id
);
2456 if (unit_type_to_string(u
->type
)) {
2457 info
->type
= strdup(unit_type_to_string(u
->type
));
2461 if (unit_load_state_to_string(u
->load_state
)) {
2462 info
->load_state
= strdup(unit_load_state_to_string(u
->load_state
));
2463 if (!info
->load_state
)
2466 if (u
->fragment_path
) {
2467 info
->fragment_path
= strdup(u
->fragment_path
);
2468 if (!info
->fragment_path
)
2471 info
->default_dependencies
= u
->default_dependencies
;
2472 if (u
->type
== UNIT_SERVICE
&& notify_access_to_string(SERVICE(u
)->notify_access
)) {
2473 info
->notify_access
= strdup(notify_access_to_string(SERVICE(u
)->notify_access
));
2474 if (!info
->notify_access
)
2480 info
->ambient_capabilities
= c
->capability_ambient_set
;
2481 info
->capability_bounding_set
= c
->capability_bounding_set
;
2483 info
->user
= strdup(c
->user
);
2487 if (c
->supplementary_groups
) {
2488 info
->supplementary_groups
= strv_copy(c
->supplementary_groups
);
2489 if (!info
->supplementary_groups
)
2492 info
->dynamic_user
= c
->dynamic_user
;
2493 if (exec_keyring_mode_to_string(c
->keyring_mode
)) {
2494 info
->keyring_mode
= strdup(exec_keyring_mode_to_string(c
->keyring_mode
));
2495 if (!info
->keyring_mode
)
2498 if (protect_proc_to_string(c
->protect_proc
)) {
2499 info
->protect_proc
= strdup(protect_proc_to_string(c
->protect_proc
));
2500 if (!info
->protect_proc
)
2503 if (proc_subset_to_string(c
->proc_subset
)) {
2504 info
->proc_subset
= strdup(proc_subset_to_string(c
->proc_subset
));
2505 if (!info
->proc_subset
)
2508 info
->lock_personality
= c
->lock_personality
;
2509 info
->memory_deny_write_execute
= c
->memory_deny_write_execute
;
2510 info
->no_new_privileges
= c
->no_new_privileges
;
2511 info
->protect_hostname
= c
->protect_hostname
;
2512 info
->private_devices
= c
->private_devices
;
2513 info
->private_mounts
= c
->private_mounts
;
2514 info
->private_network
= c
->private_network
;
2515 info
->private_tmp
= c
->private_tmp
;
2516 info
->private_users
= c
->private_users
;
2517 info
->protect_control_groups
= c
->protect_control_groups
;
2518 info
->protect_kernel_modules
= c
->protect_kernel_modules
;
2519 info
->protect_kernel_tunables
= c
->protect_kernel_tunables
;
2520 info
->protect_kernel_logs
= c
->protect_kernel_logs
;
2521 info
->protect_clock
= c
->protect_clock
;
2522 if (protect_home_to_string(c
->protect_home
)) {
2523 info
->protect_home
= strdup(protect_home_to_string(c
->protect_home
));
2524 if (!info
->protect_home
)
2527 if (protect_system_to_string(c
->protect_system
)) {
2528 info
->protect_system
= strdup(protect_system_to_string(c
->protect_system
));
2529 if (!info
->protect_system
)
2532 info
->remove_ipc
= c
->remove_ipc
;
2533 info
->restrict_address_family_inet
=
2534 info
->restrict_address_family_unix
=
2535 info
->restrict_address_family_netlink
=
2536 info
->restrict_address_family_packet
=
2537 info
->restrict_address_family_other
=
2538 c
->address_families_allow_list
;
2541 SET_FOREACH(key
, c
->address_families
) {
2542 int family
= PTR_TO_INT(key
);
2545 if (IN_SET(family
, AF_INET
, AF_INET6
))
2546 info
->restrict_address_family_inet
= !c
->address_families_allow_list
;
2547 else if (family
== AF_UNIX
)
2548 info
->restrict_address_family_unix
= !c
->address_families_allow_list
;
2549 else if (family
== AF_NETLINK
)
2550 info
->restrict_address_family_netlink
= !c
->address_families_allow_list
;
2551 else if (family
== AF_PACKET
)
2552 info
->restrict_address_family_packet
= !c
->address_families_allow_list
;
2554 info
->restrict_address_family_other
= !c
->address_families_allow_list
;
2557 info
->restrict_namespaces
= c
->restrict_namespaces
;
2558 info
->restrict_realtime
= c
->restrict_realtime
;
2559 info
->restrict_suid_sgid
= c
->restrict_suid_sgid
;
2560 if (c
->root_directory
) {
2561 info
->root_directory
= strdup(c
->root_directory
);
2562 if (!info
->root_directory
)
2565 if (c
->root_image
) {
2566 info
->root_image
= strdup(c
->root_image
);
2567 if (!info
->root_image
)
2570 info
->_umask
= c
->umask
;
2571 if (c
->syscall_archs
) {
2572 info
->system_call_architectures
= set_copy(c
->syscall_archs
);
2573 if (!info
->system_call_architectures
)
2576 info
->system_call_filter_allow_list
= c
->syscall_allow_list
;
2577 if (c
->syscall_filter
) {
2578 info
->system_call_filter
= hashmap_copy(c
->syscall_filter
);
2579 if (!info
->system_call_filter
)
2585 info
->delegate
= g
->delegate
;
2586 if (cgroup_device_policy_to_string(g
->device_policy
)) {
2587 info
->device_policy
= strdup(cgroup_device_policy_to_string(g
->device_policy
));
2588 if (!info
->device_policy
)
2592 struct in_addr_prefix
*i
;
2593 bool deny_ipv4
= false, deny_ipv6
= false;
2595 SET_FOREACH(i
, g
->ip_address_deny
) {
2596 if (i
->family
== AF_INET
&& i
->prefixlen
== 0)
2598 else if (i
->family
== AF_INET6
&& i
->prefixlen
== 0)
2601 info
->ip_address_deny_all
= deny_ipv4
&& deny_ipv6
;
2603 info
->ip_address_allow_localhost
= info
->ip_address_allow_other
= false;
2604 SET_FOREACH(i
, g
->ip_address_allow
) {
2605 if (in_addr_is_localhost(i
->family
, &i
->address
))
2606 info
->ip_address_allow_localhost
= true;
2608 info
->ip_address_allow_other
= true;
2611 info
->ip_filters_custom_ingress
= !strv_isempty(g
->ip_filters_ingress
);
2612 info
->ip_filters_custom_egress
= !strv_isempty(g
->ip_filters_egress
);
2613 info
->device_allow_non_empty
= !LIST_IS_EMPTY(g
->device_allow
);
2616 *ret_info
= TAKE_PTR(info
);
2621 static int offline_security_check(Unit
*u
,
2623 JsonVariant
*policy
,
2624 PagerFlags pager_flags
,
2625 JsonFormatFlags json_format_flags
) {
2627 _cleanup_(table_unrefp
) Table
*overview_table
= NULL
;
2628 AnalyzeSecurityFlags flags
= 0;
2629 _cleanup_(security_info_freep
) SecurityInfo
*info
= NULL
;
2635 unit_dump(u
, stdout
, "\t");
2637 r
= get_security_info(u
, unit_get_exec_context(u
), unit_get_cgroup_context(u
), &info
);
2641 return assess(info
, overview_table
, flags
, threshold
, policy
, pager_flags
, json_format_flags
);
2644 static int offline_security_checks(char **filenames
,
2645 JsonVariant
*policy
,
2646 UnitFileScope scope
,
2648 bool run_generators
,
2651 const char *profile
,
2652 PagerFlags pager_flags
,
2653 JsonFormatFlags json_format_flags
) {
2655 const ManagerTestRunFlags flags
=
2656 MANAGER_TEST_RUN_MINIMAL
|
2657 MANAGER_TEST_RUN_ENV_GENERATORS
|
2658 MANAGER_TEST_RUN_IGNORE_DEPENDENCIES
|
2659 run_generators
* MANAGER_TEST_RUN_GENERATORS
;
2661 _cleanup_(manager_freep
) Manager
*m
= NULL
;
2662 Unit
*units
[strv_length(filenames
)];
2663 _cleanup_free_
char *var
= NULL
;
2668 if (strv_isempty(filenames
))
2672 r
= verify_generate_path(&var
, filenames
);
2674 return log_error_errno(r
, "Failed to generate unit load path: %m");
2676 assert_se(set_unit_path(var
) >= 0);
2678 r
= manager_new(scope
, flags
, &m
);
2680 return log_error_errno(r
, "Failed to initialize manager: %m");
2682 log_debug("Starting manager...");
2684 r
= manager_startup(m
, /* serialization= */ NULL
, /* fds= */ NULL
, root
);
2689 /* Ensure the temporary directory is in the search path, so that we can add drop-ins. */
2690 r
= strv_extend(&m
->lookup_paths
.search_path
, m
->lookup_paths
.temporary_dir
);
2695 log_debug("Loading remaining units from the command line...");
2697 STRV_FOREACH(filename
, filenames
) {
2698 _cleanup_free_
char *prepared
= NULL
;
2700 log_debug("Handling %s...", *filename
);
2702 k
= verify_prepare_filename(*filename
, &prepared
);
2704 log_warning_errno(k
, "Failed to prepare filename %s: %m", *filename
);
2710 /* When a portable image is analyzed, the profile is what provides a good chunk of
2711 * the security-related settings, but they are obviously not shipped with the image.
2712 * This allows to take them in consideration. */
2714 _cleanup_free_
char *unit_name
= NULL
, *dropin
= NULL
, *profile_path
= NULL
;
2716 r
= path_extract_filename(prepared
, &unit_name
);
2720 dropin
= strjoin(m
->lookup_paths
.temporary_dir
, "/", unit_name
, ".d/profile.conf");
2723 (void) mkdir_parents(dropin
, 0755);
2725 if (!is_path(profile
)) {
2726 r
= find_portable_profile(profile
, unit_name
, &profile_path
);
2728 return log_error_errno(r
, "Failed to find portable profile %s: %m", profile
);
2729 profile
= profile_path
;
2732 r
= copy_file(profile
, dropin
, 0, 0644, 0, 0, 0);
2734 return log_error_errno(r
, "Failed to copy: %m");
2737 k
= manager_load_startable_unit_or_warn(m
, NULL
, prepared
, &units
[count
]);
2747 for (size_t i
= 0; i
< count
; i
++) {
2748 k
= offline_security_check(units
[i
], threshold
, policy
, pager_flags
, json_format_flags
);
2749 if (k
< 0 && r
== 0)
2756 int analyze_security(sd_bus
*bus
,
2758 JsonVariant
*policy
,
2759 UnitFileScope scope
,
2761 bool run_generators
,
2765 const char *profile
,
2766 JsonFormatFlags json_format_flags
,
2767 PagerFlags pager_flags
,
2768 AnalyzeSecurityFlags flags
) {
2770 _cleanup_(table_unrefp
) Table
*overview_table
= NULL
;
2776 return offline_security_checks(units
, policy
, scope
, check_man
, run_generators
, threshold
, root
, profile
, pager_flags
, json_format_flags
);
2778 if (strv_length(units
) != 1) {
2779 overview_table
= table_new("unit", "exposure", "predicate", "happy");
2780 if (!overview_table
)
2784 if (strv_isempty(units
)) {
2785 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2786 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2787 _cleanup_strv_free_
char **list
= NULL
;
2791 r
= sd_bus_call_method(
2793 "org.freedesktop.systemd1",
2794 "/org/freedesktop/systemd1",
2795 "org.freedesktop.systemd1.Manager",
2801 return log_error_errno(r
, "Failed to list units: %s", bus_error_message(&error
, r
));
2803 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
2805 return bus_log_parse_error(r
);
2811 r
= bus_parse_unit_info(reply
, &info
);
2813 return bus_log_parse_error(r
);
2817 if (!endswith(info
.id
, ".service"))
2820 if (!GREEDY_REALLOC(list
, n
+ 2))
2823 copy
= strdup(info
.id
);
2833 flags
|= ANALYZE_SECURITY_SHORT
|ANALYZE_SECURITY_ONLY_LOADED
|ANALYZE_SECURITY_ONLY_LONG_RUNNING
;
2835 STRV_FOREACH(i
, list
) {
2836 r
= analyze_security_one(bus
, *i
, overview_table
, flags
, threshold
, policy
, pager_flags
, json_format_flags
);
2837 if (r
< 0 && ret
>= 0)
2844 STRV_FOREACH(i
, units
) {
2845 _cleanup_free_
char *mangled
= NULL
, *instance
= NULL
;
2848 if (!FLAGS_SET(flags
, ANALYZE_SECURITY_SHORT
) && i
!= units
) {
2853 r
= unit_name_mangle(*i
, 0, &mangled
);
2855 return log_error_errno(r
, "Failed to mangle unit name '%s': %m", *i
);
2857 if (!endswith(mangled
, ".service"))
2858 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2859 "Unit %s is not a service unit, refusing.",
2862 if (unit_name_is_valid(mangled
, UNIT_NAME_TEMPLATE
)) {
2863 r
= unit_name_replace_instance(mangled
, "test-instance", &instance
);
2871 r
= analyze_security_one(bus
, name
, overview_table
, flags
, threshold
, policy
, pager_flags
, json_format_flags
);
2872 if (r
< 0 && ret
>= 0)
2877 if (overview_table
) {
2878 if (!FLAGS_SET(flags
, ANALYZE_SECURITY_SHORT
)) {
2883 r
= table_print_with_pager(overview_table
, json_format_flags
, pager_flags
, /* show_header= */true);
2885 return log_error_errno(r
, "Failed to output table: %m");