1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <sys/utsname.h>
6 #include "analyze-security.h"
8 #include "bus-unit-util.h"
11 #include "format-table.h"
12 #include "in-addr-util.h"
13 #include "locale-util.h"
16 #include "nulstr-util.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "pretty-print.h"
21 # include "seccomp-util.h"
24 #include "stdio-util.h"
26 #include "terminal-util.h"
28 #include "unit-name.h"
30 struct security_info
{
35 bool default_dependencies
;
37 uint64_t ambient_capabilities
;
38 uint64_t capability_bounding_set
;
41 char **supplementary_groups
;
44 bool ip_address_deny_all
;
45 bool ip_address_allow_localhost
;
46 bool ip_address_allow_other
;
49 bool lock_personality
;
50 bool memory_deny_write_execute
;
51 bool no_new_privileges
;
53 bool protect_hostname
;
61 bool protect_control_groups
;
62 bool protect_kernel_modules
;
63 bool protect_kernel_tunables
;
70 bool restrict_address_family_inet
;
71 bool restrict_address_family_unix
;
72 bool restrict_address_family_netlink
;
73 bool restrict_address_family_packet
;
74 bool restrict_address_family_other
;
76 uint64_t restrict_namespaces
;
77 bool restrict_realtime
;
84 bool device_allow_non_empty
;
86 char **system_call_architectures
;
88 bool system_call_filter_whitelist
;
89 Set
*system_call_filter
;
94 struct security_assessor
{
96 const char *description_good
;
97 const char *description_bad
;
98 const char *description_na
;
102 int (*assess
)(const struct security_assessor
*a
, const struct security_info
*info
, const void *data
, uint64_t *ret_badness
, char **ret_description
);
105 bool default_dependencies_only
;
108 static void security_info_free(struct security_info
*i
) {
115 free(i
->fragment_path
);
119 free(i
->protect_home
);
120 free(i
->protect_system
);
122 free(i
->root_directory
);
125 free(i
->keyring_mode
);
126 free(i
->notify_access
);
128 free(i
->device_policy
);
130 strv_free(i
->supplementary_groups
);
131 strv_free(i
->system_call_architectures
);
133 set_free_free(i
->system_call_filter
);
136 static bool security_info_runs_privileged(const struct security_info
*i
) {
139 if (STRPTR_IN_SET(i
->user
, "0", "root"))
145 return isempty(i
->user
);
148 static int assess_bool(
149 const struct security_assessor
*a
,
150 const struct security_info
*info
,
152 uint64_t *ret_badness
,
153 char **ret_description
) {
155 const bool *b
= data
;
159 assert(ret_description
);
161 *ret_badness
= a
->parameter
? *b
: !*b
;
162 *ret_description
= NULL
;
167 static int assess_user(
168 const struct security_assessor
*a
,
169 const struct security_info
*info
,
171 uint64_t *ret_badness
,
172 char **ret_description
) {
174 _cleanup_free_
char *d
= NULL
;
178 assert(ret_description
);
180 if (streq_ptr(info
->user
, NOBODY_USER_NAME
)) {
181 d
= strdup("Service runs under as '" NOBODY_USER_NAME
"' user, which should not be used for services");
183 } else if (info
->dynamic_user
&& !STR_IN_SET(info
->user
, "0", "root")) {
184 d
= strdup("Service runs under a transient non-root user identity");
186 } else if (info
->user
&& !STR_IN_SET(info
->user
, "0", "root", "")) {
187 d
= strdup("Service runs under a static non-root user identity");
191 *ret_description
= NULL
;
199 *ret_description
= TAKE_PTR(d
);
204 static int assess_protect_home(
205 const struct security_assessor
*a
,
206 const struct security_info
*info
,
208 uint64_t *ret_badness
,
209 char **ret_description
) {
211 const char *description
;
217 assert(ret_description
);
220 description
= "Service has full access to home directories";
222 r
= parse_boolean(info
->protect_home
);
224 if (streq_ptr(info
->protect_home
, "read-only")) {
226 description
= "Service has read-only access to home directories";
227 } else if (streq_ptr(info
->protect_home
, "tmpfs")) {
229 description
= "Service has access to fake empty home directories";
233 description
= "Service has no access to home directories";
236 copy
= strdup(description
);
240 *ret_badness
= badness
;
241 *ret_description
= copy
;
246 static int assess_protect_system(
247 const struct security_assessor
*a
,
248 const struct security_info
*info
,
250 uint64_t *ret_badness
,
251 char **ret_description
) {
253 const char *description
;
259 assert(ret_description
);
262 description
= "Service has full access to the OS file hierarchy";
264 r
= parse_boolean(info
->protect_system
);
266 if (streq_ptr(info
->protect_system
, "full")) {
268 description
= "Service has very limited write access to the OS file hierarchy";
269 } else if (streq_ptr(info
->protect_system
, "strict")) {
271 description
= "Service has strict read-only access to the OS file hierarchy";
275 description
= "Service has limited write access to the OS file hierarchy";
278 copy
= strdup(description
);
282 *ret_badness
= badness
;
283 *ret_description
= copy
;
288 static int assess_root_directory(
289 const struct security_assessor
*a
,
290 const struct security_info
*info
,
292 uint64_t *ret_badness
,
293 char **ret_description
) {
296 assert(ret_description
);
299 (isempty(info
->root_directory
) ||
300 path_equal(info
->root_directory
, "/")) &&
301 (isempty(info
->root_image
) ||
302 path_equal(info
->root_image
, "/"));
303 *ret_description
= NULL
;
308 static int assess_capability_bounding_set(
309 const struct security_assessor
*a
,
310 const struct security_info
*info
,
312 uint64_t *ret_badness
,
313 char **ret_description
) {
316 assert(ret_description
);
318 *ret_badness
= !!(info
->capability_bounding_set
& a
->parameter
);
319 *ret_description
= NULL
;
324 static int assess_umask(
325 const struct security_assessor
*a
,
326 const struct security_info
*info
,
328 uint64_t *ret_badness
,
329 char **ret_description
) {
336 assert(ret_description
);
338 if (!FLAGS_SET(info
->_umask
, 0002)) {
339 d
= "Files created by service are world-writable by default";
341 } else if (!FLAGS_SET(info
->_umask
, 0004)) {
342 d
= "Files created by service are world-readable by default";
344 } else if (!FLAGS_SET(info
->_umask
, 0020)) {
345 d
= "Files created by service are group-writable by default";
347 } else if (!FLAGS_SET(info
->_umask
, 0040)) {
348 d
= "Files created by service are group-readable by default";
351 d
= "Files created by service are accessible only by service's own user by default";
360 *ret_description
= copy
;
365 static int assess_keyring_mode(
366 const struct security_assessor
*a
,
367 const struct security_info
*info
,
369 uint64_t *ret_badness
,
370 char **ret_description
) {
373 assert(ret_description
);
375 *ret_badness
= !streq_ptr(info
->keyring_mode
, "private");
376 *ret_description
= NULL
;
381 static int assess_notify_access(
382 const struct security_assessor
*a
,
383 const struct security_info
*info
,
385 uint64_t *ret_badness
,
386 char **ret_description
) {
389 assert(ret_description
);
391 *ret_badness
= streq_ptr(info
->notify_access
, "all");
392 *ret_description
= NULL
;
397 static int assess_remove_ipc(
398 const struct security_assessor
*a
,
399 const struct security_info
*info
,
401 uint64_t *ret_badness
,
402 char **ret_description
) {
405 assert(ret_description
);
407 if (security_info_runs_privileged(info
))
408 *ret_badness
= UINT64_MAX
;
410 *ret_badness
= !info
->remove_ipc
;
412 *ret_description
= NULL
;
416 static int assess_supplementary_groups(
417 const struct security_assessor
*a
,
418 const struct security_info
*info
,
420 uint64_t *ret_badness
,
421 char **ret_description
) {
424 assert(ret_description
);
426 if (security_info_runs_privileged(info
))
427 *ret_badness
= UINT64_MAX
;
429 *ret_badness
= !strv_isempty(info
->supplementary_groups
);
431 *ret_description
= NULL
;
435 static int assess_restrict_namespaces(
436 const struct security_assessor
*a
,
437 const struct security_info
*info
,
439 uint64_t *ret_badness
,
440 char **ret_description
) {
443 assert(ret_description
);
445 *ret_badness
= !!(info
->restrict_namespaces
& a
->parameter
);
446 *ret_description
= NULL
;
451 static int assess_system_call_architectures(
452 const struct security_assessor
*a
,
453 const struct security_info
*info
,
455 uint64_t *ret_badness
,
456 char **ret_description
) {
462 assert(ret_description
);
464 if (strv_isempty(info
->system_call_architectures
)) {
466 d
= strdup("Service may execute system calls with all ABIs");
467 } else if (strv_equal(info
->system_call_architectures
, STRV_MAKE("native"))) {
469 d
= strdup("Service may execute system calls only with native ABI");
472 d
= strdup("Service may execute system calls with multiple ABIs");
479 *ret_description
= d
;
486 static bool syscall_names_in_filter(Set
*s
, bool whitelist
, const SyscallFilterSet
*f
) {
489 NULSTR_FOREACH(syscall
, f
->value
) {
492 if (syscall
[0] == '@') {
493 const SyscallFilterSet
*g
;
495 assert_se(g
= syscall_filter_set_find(syscall
));
496 if (syscall_names_in_filter(s
, whitelist
, g
))
497 return true; /* bad! */
502 /* Let's see if the system call actually exists on this platform, before complaining */
503 id
= seccomp_syscall_resolve_name(syscall
);
507 if (set_contains(s
, syscall
) == whitelist
) {
508 log_debug("Offending syscall filter item: %s", syscall
);
509 return true; /* bad! */
516 static int assess_system_call_filter(
517 const struct security_assessor
*a
,
518 const struct security_info
*info
,
520 uint64_t *ret_badness
,
521 char **ret_description
) {
523 const SyscallFilterSet
*f
;
530 assert(ret_description
);
532 assert(a
->parameter
< _SYSCALL_FILTER_SET_MAX
);
533 f
= syscall_filter_sets
+ a
->parameter
;
535 if (!info
->system_call_filter_whitelist
&& set_isempty(info
->system_call_filter
)) {
536 d
= strdup("Service does not filter system calls");
541 log_debug("Analyzing system call filter, checking against: %s", f
->name
);
542 bad
= syscall_names_in_filter(info
->system_call_filter
, info
->system_call_filter_whitelist
, f
);
543 log_debug("Result: %s", bad
? "bad" : "good");
545 if (info
->system_call_filter_whitelist
) {
547 (void) asprintf(&d
, "System call whitelist defined for service, and %s is included", f
->name
);
550 (void) asprintf(&d
, "System call whitelist defined for service, and %s is not included", f
->name
);
555 (void) asprintf(&d
, "System call blacklist defined for service, and %s is not included", f
->name
);
558 (void) asprintf(&d
, "System call blacklist defined for service, and %s is included", f
->name
);
568 *ret_description
= d
;
575 static int assess_ip_address_allow(
576 const struct security_assessor
*a
,
577 const struct security_info
*info
,
579 uint64_t *ret_badness
,
580 char **ret_description
) {
587 assert(ret_description
);
589 if (!info
->ip_address_deny_all
) {
590 d
= strdup("Service does not define an IP address whitelist");
592 } else if (info
->ip_address_allow_other
) {
593 d
= strdup("Service defines IP address whitelist with non-localhost entries");
595 } else if (info
->ip_address_allow_localhost
) {
596 d
= strdup("Service defines IP address whitelits with only localhost entries");
599 d
= strdup("Service blocks all IP address ranges");
607 *ret_description
= d
;
612 static int assess_device_allow(
613 const struct security_assessor
*a
,
614 const struct security_info
*info
,
616 uint64_t *ret_badness
,
617 char **ret_description
) {
624 assert(ret_description
);
626 if (STRPTR_IN_SET(info
->device_policy
, "strict", "closed")) {
628 if (info
->device_allow_non_empty
) {
629 d
= strdup("Service has a device ACL with some special devices");
632 d
= strdup("Service has a minimal device ACL");
636 d
= strdup("Service has no device ACL");
644 *ret_description
= d
;
649 static int assess_ambient_capabilities(
650 const struct security_assessor
*a
,
651 const struct security_info
*info
,
653 uint64_t *ret_badness
,
654 char **ret_description
) {
657 assert(ret_description
);
659 *ret_badness
= info
->ambient_capabilities
!= 0;
660 *ret_description
= NULL
;
665 static const struct security_assessor security_assessor_table
[] = {
667 .id
= "User=/DynamicUser=",
668 .description_bad
= "Service runs as root user",
669 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#User=",
672 .assess
= assess_user
,
675 .id
= "SupplementaryGroups=",
676 .description_good
= "Service has no supplementary groups",
677 .description_bad
= "Service runs with supplementary groups",
678 .description_na
= "Service runs as root, option does not matter",
679 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SupplementaryGroups=",
682 .assess
= assess_supplementary_groups
,
685 .id
= "PrivateDevices=",
686 .description_good
= "Service has no access to hardware devices",
687 .description_bad
= "Service potentially has access to hardware devices",
688 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateDevices=",
691 .assess
= assess_bool
,
692 .offset
= offsetof(struct security_info
, private_devices
),
695 .id
= "PrivateMounts=",
696 .description_good
= "Service cannot install system mounts",
697 .description_bad
= "Service may install system mounts",
698 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateMounts=",
701 .assess
= assess_bool
,
702 .offset
= offsetof(struct security_info
, private_mounts
),
705 .id
= "PrivateNetwork=",
706 .description_good
= "Service has no access to the host's network",
707 .description_bad
= "Service has access to the host's network",
708 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateNetwork=",
711 .assess
= assess_bool
,
712 .offset
= offsetof(struct security_info
, private_network
),
716 .description_good
= "Service has no access to other software's temporary files",
717 .description_bad
= "Service has access to other software's temporary files",
718 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateTmp=",
721 .assess
= assess_bool
,
722 .offset
= offsetof(struct security_info
, private_tmp
),
723 .default_dependencies_only
= true,
726 .id
= "PrivateUsers=",
727 .description_good
= "Service does not have access to other users",
728 .description_bad
= "Service has access to other users",
729 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateUsers=",
732 .assess
= assess_bool
,
733 .offset
= offsetof(struct security_info
, private_users
),
736 .id
= "ProtectControlGroups=",
737 .description_good
= "Service cannot modify the control group file system",
738 .description_bad
= "Service may modify to the control group file system",
739 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectControlGroups=",
742 .assess
= assess_bool
,
743 .offset
= offsetof(struct security_info
, protect_control_groups
),
746 .id
= "ProtectKernelModules=",
747 .description_good
= "Service cannot load or read kernel modules",
748 .description_bad
= "Service may load or read kernel modules",
749 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelModules=",
752 .assess
= assess_bool
,
753 .offset
= offsetof(struct security_info
, protect_kernel_modules
),
756 .id
= "ProtectKernelTunables=",
757 .description_good
= "Service cannot alter kernel tunables (/proc/sys, …)",
758 .description_bad
= "Service may alter kernel tunables",
759 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelTunables=",
762 .assess
= assess_bool
,
763 .offset
= offsetof(struct security_info
, protect_kernel_tunables
),
766 .id
= "ProtectHome=",
767 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHome=",
770 .assess
= assess_protect_home
,
771 .default_dependencies_only
= true,
774 .id
= "ProtectHostname=",
775 .description_good
= "Service cannot change system host/domainname",
776 .description_bad
= "Service may change system host/domainname",
777 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHostname=",
780 .assess
= assess_bool
,
781 .offset
= offsetof(struct security_info
, protect_hostname
),
784 .id
= "ProtectSystem=",
785 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=",
788 .assess
= assess_protect_system
,
789 .default_dependencies_only
= true,
792 .id
= "RootDirectory=/RootImage=",
793 .description_good
= "Service has its own root directory/image",
794 .description_bad
= "Service runs within the host's root directory",
795 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RootDirectory=",
798 .assess
= assess_root_directory
,
799 .default_dependencies_only
= true,
802 .id
= "LockPersonality=",
803 .description_good
= "Service cannot change ABI personality",
804 .description_bad
= "Service may change ABI personality",
805 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#LockPersonality=",
808 .assess
= assess_bool
,
809 .offset
= offsetof(struct security_info
, lock_personality
),
812 .id
= "MemoryDenyWriteExecute=",
813 .description_good
= "Service cannot create writable executable memory mappings",
814 .description_bad
= "Service may create writable executable memory mappings",
815 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute=",
818 .assess
= assess_bool
,
819 .offset
= offsetof(struct security_info
, memory_deny_write_execute
),
822 .id
= "NoNewPrivileges=",
823 .description_good
= "Service processes cannot acquire new privileges",
824 .description_bad
= "Service processes may acquire new privileges",
825 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NoNewPrivileges=",
828 .assess
= assess_bool
,
829 .offset
= offsetof(struct security_info
, no_new_privileges
),
832 .id
= "CapabilityBoundingSet=~CAP_SYS_ADMIN",
833 .description_good
= "Service has no administrator privileges",
834 .description_bad
= "Service has administrator privileges",
835 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
838 .assess
= assess_capability_bounding_set
,
839 .parameter
= UINT64_C(1) << CAP_SYS_ADMIN
,
842 .id
= "CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)",
843 .description_good
= "Service cannot change UID/GID identities/capabilities",
844 .description_bad
= "Service may change UID/GID identities/capabilities",
845 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
848 .assess
= assess_capability_bounding_set
,
849 .parameter
= (UINT64_C(1) << CAP_SETUID
)|
850 (UINT64_C(1) << CAP_SETGID
)|
851 (UINT64_C(1) << CAP_SETPCAP
),
854 .id
= "CapabilityBoundingSet=~CAP_SYS_PTRACE",
855 .description_good
= "Service has no ptrace() debugging abilities",
856 .description_bad
= "Service has ptrace() debugging abilities",
857 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
860 .assess
= assess_capability_bounding_set
,
861 .parameter
= (UINT64_C(1) << CAP_SYS_PTRACE
),
864 .id
= "CapabilityBoundingSet=~CAP_SYS_TIME",
865 .description_good
= "Service processes cannot change the system clock",
866 .description_bad
= "Service processes may change the system clock",
867 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
870 .assess
= assess_capability_bounding_set
,
871 .parameter
= UINT64_C(1) << CAP_SYS_TIME
,
874 .id
= "CapabilityBoundingSet=~CAP_NET_ADMIN",
875 .description_good
= "Service has no network configuration privileges",
876 .description_bad
= "Service has network configuration privileges",
877 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
880 .assess
= assess_capability_bounding_set
,
881 .parameter
= (UINT64_C(1) << CAP_NET_ADMIN
),
884 .id
= "CapabilityBoundingSet=~CAP_RAWIO",
885 .description_good
= "Service has no raw I/O access",
886 .description_bad
= "Service has raw I/O access",
887 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
890 .assess
= assess_capability_bounding_set
,
891 .parameter
= (UINT64_C(1) << CAP_SYS_RAWIO
),
894 .id
= "CapabilityBoundingSet=~CAP_SYS_MODULE",
895 .description_good
= "Service cannot load kernel modules",
896 .description_bad
= "Service may load kernel modules",
897 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
900 .assess
= assess_capability_bounding_set
,
901 .parameter
= (UINT64_C(1) << CAP_SYS_MODULE
),
904 .id
= "CapabilityBoundingSet=~CAP_AUDIT_*",
905 .description_good
= "Service has no audit subsystem access",
906 .description_bad
= "Service has audit subsystem access",
907 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
910 .assess
= assess_capability_bounding_set
,
911 .parameter
= (UINT64_C(1) << CAP_AUDIT_CONTROL
) |
912 (UINT64_C(1) << CAP_AUDIT_READ
) |
913 (UINT64_C(1) << CAP_AUDIT_WRITE
),
916 .id
= "CapabilityBoundingSet=~CAP_SYSLOG",
917 .description_good
= "Service has no access to kernel logging",
918 .description_bad
= "Service has access to kernel logging",
919 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
922 .assess
= assess_capability_bounding_set
,
923 .parameter
= (UINT64_C(1) << CAP_SYSLOG
),
926 .id
= "CapabilityBoundingSet=~CAP_SYS_(NICE|RESOURCE)",
927 .description_good
= "Service has no privileges to change resource use parameters",
928 .description_bad
= "Service has privileges to change resource use parameters",
929 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
932 .assess
= assess_capability_bounding_set
,
933 .parameter
= (UINT64_C(1) << CAP_SYS_NICE
) |
934 (UINT64_C(1) << CAP_SYS_RESOURCE
),
937 .id
= "CapabilityBoundingSet=~CAP_MKNOD",
938 .description_good
= "Service cannot create device nodes",
939 .description_bad
= "Service may create device nodes",
940 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
943 .assess
= assess_capability_bounding_set
,
944 .parameter
= (UINT64_C(1) << CAP_MKNOD
),
947 .id
= "CapabilityBoundingSet=~CAP_(CHOWN|FSETID|SETFCAP)",
948 .description_good
= "Service cannot change file ownership/access mode/capabilities",
949 .description_bad
= "Service may change file ownership/access mode/capabilities unrestricted",
950 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
953 .assess
= assess_capability_bounding_set
,
954 .parameter
= (UINT64_C(1) << CAP_CHOWN
) |
955 (UINT64_C(1) << CAP_FSETID
) |
956 (UINT64_C(1) << CAP_SETFCAP
),
959 .id
= "CapabilityBoundingSet=~CAP_(DAC_*|FOWNER|IPC_OWNER)",
960 .description_good
= "Service cannot override UNIX file/IPC permission checks",
961 .description_bad
= "Service may override UNIX file/IPC permission checks",
962 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
965 .assess
= assess_capability_bounding_set
,
966 .parameter
= (UINT64_C(1) << CAP_DAC_OVERRIDE
) |
967 (UINT64_C(1) << CAP_DAC_READ_SEARCH
) |
968 (UINT64_C(1) << CAP_FOWNER
) |
969 (UINT64_C(1) << CAP_IPC_OWNER
),
972 .id
= "CapabilityBoundingSet=~CAP_KILL",
973 .description_good
= "Service cannot send UNIX signals to arbitrary processes",
974 .description_bad
= "Service may send UNIX signals to arbitrary processes",
975 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
978 .assess
= assess_capability_bounding_set
,
979 .parameter
= (UINT64_C(1) << CAP_KILL
),
982 .id
= "CapabilityBoundingSet=~CAP_NET_(BIND_SERVICE|BROADCAST|RAW)",
983 .description_good
= "Service has no elevated networking privileges",
984 .description_bad
= "Service has elevated networking privileges",
985 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
988 .assess
= assess_capability_bounding_set
,
989 .parameter
= (UINT64_C(1) << CAP_NET_BIND_SERVICE
) |
990 (UINT64_C(1) << CAP_NET_BROADCAST
) |
991 (UINT64_C(1) << CAP_NET_RAW
),
994 .id
= "CapabilityBoundingSet=~CAP_SYS_BOOT",
995 .description_good
= "Service cannot issue reboot()",
996 .description_bad
= "Service may issue reboot()",
997 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1000 .assess
= assess_capability_bounding_set
,
1001 .parameter
= (UINT64_C(1) << CAP_SYS_BOOT
),
1004 .id
= "CapabilityBoundingSet=~CAP_MAC_*",
1005 .description_good
= "Service cannot adjust SMACK MAC",
1006 .description_bad
= "Service may adjust SMACK MAC",
1007 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1010 .assess
= assess_capability_bounding_set
,
1011 .parameter
= (UINT64_C(1) << CAP_MAC_ADMIN
)|
1012 (UINT64_C(1) << CAP_MAC_OVERRIDE
),
1015 .id
= "CapabilityBoundingSet=~CAP_LINUX_IMMUTABLE",
1016 .description_good
= "Service cannot mark files immutable",
1017 .description_bad
= "Service may mark files immutable",
1018 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1021 .assess
= assess_capability_bounding_set
,
1022 .parameter
= (UINT64_C(1) << CAP_LINUX_IMMUTABLE
),
1025 .id
= "CapabilityBoundingSet=~CAP_IPC_LOCK",
1026 .description_good
= "Service cannot lock memory into RAM",
1027 .description_bad
= "Service may lock memory into RAM",
1028 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1031 .assess
= assess_capability_bounding_set
,
1032 .parameter
= (UINT64_C(1) << CAP_IPC_LOCK
),
1035 .id
= "CapabilityBoundingSet=~CAP_SYS_CHROOT",
1036 .description_good
= "Service cannot issue chroot()",
1037 .description_bad
= "Service may issue chroot()",
1038 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1041 .assess
= assess_capability_bounding_set
,
1042 .parameter
= (UINT64_C(1) << CAP_SYS_CHROOT
),
1045 .id
= "CapabilityBoundingSet=~CAP_BLOCK_SUSPEND",
1046 .description_good
= "Service cannot establish wake locks",
1047 .description_bad
= "Service may establish wake locks",
1048 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1051 .assess
= assess_capability_bounding_set
,
1052 .parameter
= (UINT64_C(1) << CAP_BLOCK_SUSPEND
),
1055 .id
= "CapabilityBoundingSet=~CAP_WAKE_ALARM",
1056 .description_good
= "Service cannot program timers that wake up the system",
1057 .description_bad
= "Service may program timers that wake up the system",
1058 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1061 .assess
= assess_capability_bounding_set
,
1062 .parameter
= (UINT64_C(1) << CAP_WAKE_ALARM
),
1065 .id
= "CapabilityBoundingSet=~CAP_LEASE",
1066 .description_good
= "Service cannot create file leases",
1067 .description_bad
= "Service may create file leases",
1068 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1071 .assess
= assess_capability_bounding_set
,
1072 .parameter
= (UINT64_C(1) << CAP_LEASE
),
1075 .id
= "CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG",
1076 .description_good
= "Service cannot issue vhangup()",
1077 .description_bad
= "Service may issue vhangup()",
1078 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1081 .assess
= assess_capability_bounding_set
,
1082 .parameter
= (UINT64_C(1) << CAP_SYS_TTY_CONFIG
),
1085 .id
= "CapabilityBoundingSet=~CAP_SYS_PACCT",
1086 .description_good
= "Service cannot use acct()",
1087 .description_bad
= "Service may use acct()",
1088 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
1091 .assess
= assess_capability_bounding_set
,
1092 .parameter
= (UINT64_C(1) << CAP_SYS_PACCT
),
1096 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#UMask=",
1099 .assess
= assess_umask
,
1102 .id
= "KeyringMode=",
1103 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#KeyringMode=",
1104 .description_good
= "Service doesn't share key material with other services",
1105 .description_bad
= "Service shares key material with other service",
1108 .assess
= assess_keyring_mode
,
1111 .id
= "NotifyAccess=",
1112 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NotifyAccess=",
1113 .description_good
= "Service child processes cannot alter service state",
1114 .description_bad
= "Service child processes may alter service state",
1117 .assess
= assess_notify_access
,
1121 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RemoveIPC=",
1122 .description_good
= "Service user cannot leave SysV IPC objects around",
1123 .description_bad
= "Service user may leave SysV IPC objects around",
1124 .description_na
= "Service runs as root, option does not apply",
1127 .assess
= assess_remove_ipc
,
1128 .offset
= offsetof(struct security_info
, remove_ipc
),
1132 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Delegate=",
1133 .description_good
= "Service does not maintain its own delegated control group subtree",
1134 .description_bad
= "Service maintains its own delegated control group subtree",
1137 .assess
= assess_bool
,
1138 .offset
= offsetof(struct security_info
, delegate
),
1139 .parameter
= true, /* invert! */
1142 .id
= "RestrictRealtime=",
1143 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictRealtime=",
1144 .description_good
= "Service realtime scheduling access is restricted",
1145 .description_bad
= "Service may acquire realtime scheduling",
1148 .assess
= assess_bool
,
1149 .offset
= offsetof(struct security_info
, restrict_realtime
),
1152 .id
= "RestrictNamespaces=~CLONE_NEWUSER",
1153 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1154 .description_good
= "Service cannot create user namespaces",
1155 .description_bad
= "Service may create user namespaces",
1158 .assess
= assess_restrict_namespaces
,
1159 .parameter
= CLONE_NEWUSER
,
1162 .id
= "RestrictNamespaces=~CLONE_NEWNS",
1163 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1164 .description_good
= "Service cannot create file system namespaces",
1165 .description_bad
= "Service may create file system namespaces",
1168 .assess
= assess_restrict_namespaces
,
1169 .parameter
= CLONE_NEWNS
,
1172 .id
= "RestrictNamespaces=~CLONE_NEWIPC",
1173 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1174 .description_good
= "Service cannot create IPC namespaces",
1175 .description_bad
= "Service may create IPC namespaces",
1178 .assess
= assess_restrict_namespaces
,
1179 .parameter
= CLONE_NEWIPC
,
1182 .id
= "RestrictNamespaces=~CLONE_NEWPID",
1183 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1184 .description_good
= "Service cannot create process namespaces",
1185 .description_bad
= "Service may create process namespaces",
1188 .assess
= assess_restrict_namespaces
,
1189 .parameter
= CLONE_NEWPID
,
1192 .id
= "RestrictNamespaces=~CLONE_NEWCGROUP",
1193 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1194 .description_good
= "Service cannot create cgroup namespaces",
1195 .description_bad
= "Service may create cgroup namespaces",
1198 .assess
= assess_restrict_namespaces
,
1199 .parameter
= CLONE_NEWCGROUP
,
1202 .id
= "RestrictNamespaces=~CLONE_NEWNET",
1203 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1204 .description_good
= "Service cannot create network namespaces",
1205 .description_bad
= "Service may create network namespaces",
1208 .assess
= assess_restrict_namespaces
,
1209 .parameter
= CLONE_NEWNET
,
1212 .id
= "RestrictNamespaces=~CLONE_NEWUTS",
1213 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
1214 .description_good
= "Service cannot create hostname namespaces",
1215 .description_bad
= "Service may create hostname namespaces",
1218 .assess
= assess_restrict_namespaces
,
1219 .parameter
= CLONE_NEWUTS
,
1222 .id
= "RestrictAddressFamilies=~AF_(INET|INET6)",
1223 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1224 .description_good
= "Service cannot allocate Internet sockets",
1225 .description_bad
= "Service may allocate Internet sockets",
1228 .assess
= assess_bool
,
1229 .offset
= offsetof(struct security_info
, restrict_address_family_inet
),
1232 .id
= "RestrictAddressFamilies=~AF_UNIX",
1233 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1234 .description_good
= "Service cannot allocate local sockets",
1235 .description_bad
= "Service may allocate local sockets",
1238 .assess
= assess_bool
,
1239 .offset
= offsetof(struct security_info
, restrict_address_family_unix
),
1242 .id
= "RestrictAddressFamilies=~AF_NETLINK",
1243 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1244 .description_good
= "Service cannot allocate netlink sockets",
1245 .description_bad
= "Service may allocate netlink sockets",
1248 .assess
= assess_bool
,
1249 .offset
= offsetof(struct security_info
, restrict_address_family_netlink
),
1252 .id
= "RestrictAddressFamilies=~AF_PACKET",
1253 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1254 .description_good
= "Service cannot allocate packet sockets",
1255 .description_bad
= "Service may allocate packet sockets",
1258 .assess
= assess_bool
,
1259 .offset
= offsetof(struct security_info
, restrict_address_family_packet
),
1262 .id
= "RestrictAddressFamilies=~…",
1263 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
1264 .description_good
= "Service cannot allocate exotic sockets",
1265 .description_bad
= "Service may allocate exotic sockets",
1268 .assess
= assess_bool
,
1269 .offset
= offsetof(struct security_info
, restrict_address_family_other
),
1272 .id
= "SystemCallArchitectures=",
1273 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallArchitectures=",
1276 .assess
= assess_system_call_architectures
,
1280 .id
= "SystemCallFilter=~@swap",
1281 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1284 .assess
= assess_system_call_filter
,
1285 .parameter
= SYSCALL_FILTER_SET_SWAP
,
1288 .id
= "SystemCallFilter=~@obsolete",
1289 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1292 .assess
= assess_system_call_filter
,
1293 .parameter
= SYSCALL_FILTER_SET_OBSOLETE
,
1296 .id
= "SystemCallFilter=~@clock",
1297 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1300 .assess
= assess_system_call_filter
,
1301 .parameter
= SYSCALL_FILTER_SET_CLOCK
,
1304 .id
= "SystemCallFilter=~@cpu-emulation",
1305 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1308 .assess
= assess_system_call_filter
,
1309 .parameter
= SYSCALL_FILTER_SET_CPU_EMULATION
,
1312 .id
= "SystemCallFilter=~@debug",
1313 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1316 .assess
= assess_system_call_filter
,
1317 .parameter
= SYSCALL_FILTER_SET_DEBUG
,
1320 .id
= "SystemCallFilter=~@mount",
1321 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1324 .assess
= assess_system_call_filter
,
1325 .parameter
= SYSCALL_FILTER_SET_MOUNT
,
1328 .id
= "SystemCallFilter=~@module",
1329 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1332 .assess
= assess_system_call_filter
,
1333 .parameter
= SYSCALL_FILTER_SET_MODULE
,
1336 .id
= "SystemCallFilter=~@raw-io",
1337 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1340 .assess
= assess_system_call_filter
,
1341 .parameter
= SYSCALL_FILTER_SET_RAW_IO
,
1344 .id
= "SystemCallFilter=~@reboot",
1345 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1348 .assess
= assess_system_call_filter
,
1349 .parameter
= SYSCALL_FILTER_SET_REBOOT
,
1352 .id
= "SystemCallFilter=~@privileged",
1353 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1356 .assess
= assess_system_call_filter
,
1357 .parameter
= SYSCALL_FILTER_SET_PRIVILEGED
,
1360 .id
= "SystemCallFilter=~@resources",
1361 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
1364 .assess
= assess_system_call_filter
,
1365 .parameter
= SYSCALL_FILTER_SET_RESOURCES
,
1369 .id
= "IPAddressDeny=",
1370 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#IPAddressDeny=",
1373 .assess
= assess_ip_address_allow
,
1376 .id
= "DeviceAllow=",
1377 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#DeviceAllow=",
1380 .assess
= assess_device_allow
,
1383 .id
= "AmbientCapabilities=",
1384 .url
= "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#AmbientCapabilities=",
1385 .description_good
= "Service process does not receive ambient capabilities",
1386 .description_bad
= "Service process receives ambient capabilities",
1389 .assess
= assess_ambient_capabilities
,
1393 static int assess(const struct security_info
*info
, Table
*overview_table
, AnalyzeSecurityFlags flags
) {
1394 static const struct {
1398 SpecialGlyph smiley
;
1399 } badness_table
[] = {
1400 { 100, "DANGEROUS", ANSI_HIGHLIGHT_RED
, SPECIAL_GLYPH_DEPRESSED_SMILEY
},
1401 { 90, "UNSAFE", ANSI_HIGHLIGHT_RED
, SPECIAL_GLYPH_UNHAPPY_SMILEY
},
1402 { 75, "EXPOSED", ANSI_HIGHLIGHT_YELLOW
, SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY
},
1403 { 50, "MEDIUM", NULL
, SPECIAL_GLYPH_NEUTRAL_SMILEY
},
1404 { 10, "OK", ANSI_HIGHLIGHT_GREEN
, SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY
},
1405 { 1, "SAFE", ANSI_HIGHLIGHT_GREEN
, SPECIAL_GLYPH_HAPPY_SMILEY
},
1406 { 0, "PERFECT", ANSI_HIGHLIGHT_GREEN
, SPECIAL_GLYPH_ECSTATIC_SMILEY
},
1409 uint64_t badness_sum
= 0, weight_sum
= 0, exposure
;
1410 _cleanup_(table_unrefp
) Table
*details_table
= NULL
;
1414 if (!FLAGS_SET(flags
, ANALYZE_SECURITY_SHORT
)) {
1415 details_table
= table_new(" ", "name", "description", "weight", "badness", "range", "exposure");
1419 (void) table_set_sort(details_table
, 3, 1, (size_t) -1);
1420 (void) table_set_reverse(details_table
, 3, true);
1422 if (getenv_bool("SYSTEMD_ANALYZE_DEBUG") <= 0)
1423 (void) table_set_display(details_table
, 0, 1, 2, 6, (size_t) -1);
1426 for (i
= 0; i
< ELEMENTSOF(security_assessor_table
); i
++) {
1427 const struct security_assessor
*a
= security_assessor_table
+ i
;
1428 _cleanup_free_
char *d
= NULL
;
1432 data
= (uint8_t*) info
+ a
->offset
;
1434 if (a
->default_dependencies_only
&& !info
->default_dependencies
) {
1435 badness
= UINT64_MAX
;
1436 d
= strdup("Service runs in special boot phase, option does not apply");
1440 r
= a
->assess(a
, info
, data
, &badness
, &d
);
1445 assert(a
->range
> 0);
1447 if (badness
!= UINT64_MAX
) {
1448 assert(badness
<= a
->range
);
1450 badness_sum
+= DIV_ROUND_UP(badness
* a
->weight
, a
->range
);
1451 weight_sum
+= a
->weight
;
1454 if (details_table
) {
1455 const char *checkmark
, *description
, *color
= NULL
;
1458 if (badness
== UINT64_MAX
) {
1460 description
= a
->description_na
;
1462 } else if (badness
== a
->range
) {
1463 checkmark
= special_glyph(SPECIAL_GLYPH_CROSS_MARK
);
1464 description
= a
->description_bad
;
1465 color
= ansi_highlight_red();
1466 } else if (badness
== 0) {
1467 checkmark
= special_glyph(SPECIAL_GLYPH_CHECK_MARK
);
1468 description
= a
->description_good
;
1469 color
= ansi_highlight_green();
1471 checkmark
= special_glyph(SPECIAL_GLYPH_CROSS_MARK
);
1473 color
= ansi_highlight_red();
1479 r
= table_add_cell_full(details_table
, &cell
, TABLE_STRING
, checkmark
, 1, 1, 0, 0, 0);
1481 return log_error_errno(r
, "Failed to add cell to table: %m");
1483 (void) table_set_color(details_table
, cell
, color
);
1485 r
= table_add_cell(details_table
, &cell
, TABLE_STRING
, a
->id
);
1487 return log_error_errno(r
, "Failed to add cell to table: %m");
1489 (void) table_set_url(details_table
, cell
, a
->url
);
1491 r
= table_add_cell(details_table
, NULL
, TABLE_STRING
, description
);
1493 return log_error_errno(r
, "Failed to add cell to table: %m");
1495 r
= table_add_cell(details_table
, &cell
, TABLE_UINT64
, &a
->weight
);
1497 return log_error_errno(r
, "Failed to add cell to table: %m");
1498 (void) table_set_align_percent(details_table
, cell
, 100);
1500 r
= table_add_cell(details_table
, &cell
, TABLE_UINT64
, &badness
);
1502 return log_error_errno(r
, "Failed to add cell to table: %m");
1503 (void) table_set_align_percent(details_table
, cell
, 100);
1505 r
= table_add_cell(details_table
, &cell
, TABLE_UINT64
, &a
->range
);
1507 return log_error_errno(r
, "Failed to add cell to table: %m");
1508 (void) table_set_align_percent(details_table
, cell
, 100);
1510 r
= table_add_cell(details_table
, &cell
, TABLE_EMPTY
, NULL
);
1512 return log_error_errno(r
, "Failed to add cell to table: %m");
1513 (void) table_set_align_percent(details_table
, cell
, 100);
1517 if (details_table
) {
1520 for (row
= 1; row
< table_get_rows(details_table
); row
++) {
1521 char buf
[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
1522 const uint64_t *weight
, *badness
, *range
;
1526 assert_se(weight
= table_get_at(details_table
, row
, 3));
1527 assert_se(badness
= table_get_at(details_table
, row
, 4));
1528 assert_se(range
= table_get_at(details_table
, row
, 5));
1530 if (*badness
== UINT64_MAX
|| *badness
== 0)
1533 assert_se(cell
= table_get_cell(details_table
, row
, 6));
1535 x
= DIV_ROUND_UP(DIV_ROUND_UP(*badness
* *weight
* 100U, *range
), weight_sum
);
1536 xsprintf(buf
, "%" PRIu64
".%" PRIu64
, x
/ 10, x
% 10);
1538 r
= table_update(details_table
, cell
, TABLE_STRING
, buf
);
1540 return log_error_errno(r
, "Failed to update cell in table: %m");
1543 r
= table_print(details_table
, stdout
);
1545 return log_error_errno(r
, "Failed to output table: %m");
1548 assert(weight_sum
> 0);
1549 exposure
= DIV_ROUND_UP(badness_sum
* 100U, weight_sum
);
1551 for (i
= 0; i
< ELEMENTSOF(badness_table
); i
++)
1552 if (exposure
>= badness_table
[i
].exposure
)
1555 assert(i
< ELEMENTSOF(badness_table
));
1557 if (details_table
) {
1558 _cleanup_free_
char *clickable
= NULL
;
1561 /* If we shall output the details table, also print the brief summary underneath */
1563 if (info
->fragment_path
) {
1564 r
= terminal_urlify_path(info
->fragment_path
, info
->id
, &clickable
);
1572 printf("\n%s %sOverall exposure level for %s%s: %s%" PRIu64
".%" PRIu64
" %s%s %s\n",
1573 special_glyph(SPECIAL_GLYPH_ARROW
),
1577 colors_enabled() ? strempty(badness_table
[i
].color
) : "",
1578 exposure
/ 10, exposure
% 10,
1579 badness_table
[i
].name
,
1581 special_glyph(badness_table
[i
].smiley
));
1586 if (overview_table
) {
1587 char buf
[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
1590 r
= table_add_cell(overview_table
, &cell
, TABLE_STRING
, info
->id
);
1592 return log_error_errno(r
, "Failed to add cell to table: %m");
1593 if (info
->fragment_path
) {
1594 _cleanup_free_
char *url
= NULL
;
1596 r
= file_url_from_path(info
->fragment_path
, &url
);
1598 return log_error_errno(r
, "Failed to generate URL from path: %m");
1600 (void) table_set_url(overview_table
, cell
, url
);
1603 xsprintf(buf
, "%" PRIu64
".%" PRIu64
, exposure
/ 10, exposure
% 10);
1604 r
= table_add_cell(overview_table
, &cell
, TABLE_STRING
, buf
);
1606 return log_error_errno(r
, "Failed to add cell to table: %m");
1607 (void) table_set_align_percent(overview_table
, cell
, 100);
1609 r
= table_add_cell(overview_table
, &cell
, TABLE_STRING
, badness_table
[i
].name
);
1611 return log_error_errno(r
, "Failed to add cell to table: %m");
1612 (void) table_set_color(overview_table
, cell
, strempty(badness_table
[i
].color
));
1614 r
= table_add_cell(overview_table
, NULL
, TABLE_STRING
, special_glyph(badness_table
[i
].smiley
));
1616 return log_error_errno(r
, "Failed to add cell to table: %m");
1622 static int property_read_restrict_address_families(
1626 sd_bus_error
*error
,
1629 struct security_info
*info
= userdata
;
1636 r
= sd_bus_message_enter_container(m
, 'r', "bas");
1640 r
= sd_bus_message_read(m
, "b", &whitelist
);
1644 info
->restrict_address_family_inet
=
1645 info
->restrict_address_family_unix
=
1646 info
->restrict_address_family_netlink
=
1647 info
->restrict_address_family_packet
=
1648 info
->restrict_address_family_other
= whitelist
;
1650 r
= sd_bus_message_enter_container(m
, 'a', "s");
1657 r
= sd_bus_message_read(m
, "s", &name
);
1663 if (STR_IN_SET(name
, "AF_INET", "AF_INET6"))
1664 info
->restrict_address_family_inet
= !whitelist
;
1665 else if (streq(name
, "AF_UNIX"))
1666 info
->restrict_address_family_unix
= !whitelist
;
1667 else if (streq(name
, "AF_NETLINK"))
1668 info
->restrict_address_family_netlink
= !whitelist
;
1669 else if (streq(name
, "AF_PACKET"))
1670 info
->restrict_address_family_packet
= !whitelist
;
1672 info
->restrict_address_family_other
= !whitelist
;
1675 r
= sd_bus_message_exit_container(m
);
1679 return sd_bus_message_exit_container(m
);
1682 static int property_read_system_call_filter(
1686 sd_bus_error
*error
,
1689 struct security_info
*info
= userdata
;
1696 r
= sd_bus_message_enter_container(m
, 'r', "bas");
1700 r
= sd_bus_message_read(m
, "b", &whitelist
);
1704 info
->system_call_filter_whitelist
= whitelist
;
1706 r
= sd_bus_message_enter_container(m
, 'a', "s");
1713 r
= sd_bus_message_read(m
, "s", &name
);
1719 r
= set_ensure_allocated(&info
->system_call_filter
, &string_hash_ops
);
1723 r
= set_put_strdup(info
->system_call_filter
, name
);
1728 r
= sd_bus_message_exit_container(m
);
1732 return sd_bus_message_exit_container(m
);
1735 static int property_read_ip_address_allow(
1739 sd_bus_error
*error
,
1742 struct security_info
*info
= userdata
;
1743 bool deny_ipv4
= false, deny_ipv6
= false;
1750 r
= sd_bus_message_enter_container(m
, 'a', "(iayu)");
1760 r
= sd_bus_message_enter_container(m
, 'r', "iayu");
1766 r
= sd_bus_message_read(m
, "i", &family
);
1770 r
= sd_bus_message_read_array(m
, 'y', &data
, &size
);
1774 r
= sd_bus_message_read(m
, "u", &prefixlen
);
1778 r
= sd_bus_message_exit_container(m
);
1782 if (streq(member
, "IPAddressAllow")) {
1783 union in_addr_union u
;
1785 if (family
== AF_INET
&& size
== 4 && prefixlen
== 8)
1786 memcpy(&u
.in
, data
, size
);
1787 else if (family
== AF_INET6
&& size
== 16 && prefixlen
== 128)
1788 memcpy(&u
.in6
, data
, size
);
1790 info
->ip_address_allow_other
= true;
1794 if (in_addr_is_localhost(family
, &u
))
1795 info
->ip_address_allow_localhost
= true;
1797 info
->ip_address_allow_other
= true;
1799 assert(streq(member
, "IPAddressDeny"));
1801 if (family
== AF_INET
&& size
== 4 && prefixlen
== 0)
1803 else if (family
== AF_INET6
&& size
== 16 && prefixlen
== 0)
1808 info
->ip_address_deny_all
= deny_ipv4
&& deny_ipv6
;
1810 return sd_bus_message_exit_container(m
);
1813 static int property_read_device_allow(
1817 sd_bus_error
*error
,
1820 struct security_info
*info
= userdata
;
1828 r
= sd_bus_message_enter_container(m
, 'a', "(ss)");
1833 const char *name
, *policy
;
1835 r
= sd_bus_message_read(m
, "(ss)", &name
, &policy
);
1844 info
->device_allow_non_empty
= n
> 0;
1846 return sd_bus_message_exit_container(m
);
1849 static int acquire_security_info(sd_bus
*bus
, const char *name
, struct security_info
*info
, AnalyzeSecurityFlags flags
) {
1851 static const struct bus_properties_map security_map
[] = {
1852 { "AmbientCapabilities", "t", NULL
, offsetof(struct security_info
, ambient_capabilities
) },
1853 { "CapabilityBoundingSet", "t", NULL
, offsetof(struct security_info
, capability_bounding_set
) },
1854 { "DefaultDependencies", "b", NULL
, offsetof(struct security_info
, default_dependencies
) },
1855 { "Delegate", "b", NULL
, offsetof(struct security_info
, delegate
) },
1856 { "DeviceAllow", "a(ss)", property_read_device_allow
, 0 },
1857 { "DevicePolicy", "s", NULL
, offsetof(struct security_info
, device_policy
) },
1858 { "DynamicUser", "b", NULL
, offsetof(struct security_info
, dynamic_user
) },
1859 { "FragmentPath", "s", NULL
, offsetof(struct security_info
, fragment_path
) },
1860 { "IPAddressAllow", "a(iayu)", property_read_ip_address_allow
, 0 },
1861 { "IPAddressDeny", "a(iayu)", property_read_ip_address_allow
, 0 },
1862 { "Id", "s", NULL
, offsetof(struct security_info
, id
) },
1863 { "KeyringMode", "s", NULL
, offsetof(struct security_info
, keyring_mode
) },
1864 { "LoadState", "s", NULL
, offsetof(struct security_info
, load_state
) },
1865 { "LockPersonality", "b", NULL
, offsetof(struct security_info
, lock_personality
) },
1866 { "MemoryDenyWriteExecute", "b", NULL
, offsetof(struct security_info
, memory_deny_write_execute
) },
1867 { "NoNewPrivileges", "b", NULL
, offsetof(struct security_info
, no_new_privileges
) },
1868 { "NotifyAccess", "s", NULL
, offsetof(struct security_info
, notify_access
) },
1869 { "PrivateDevices", "b", NULL
, offsetof(struct security_info
, private_devices
) },
1870 { "PrivateMounts", "b", NULL
, offsetof(struct security_info
, private_mounts
) },
1871 { "PrivateNetwork", "b", NULL
, offsetof(struct security_info
, private_network
) },
1872 { "PrivateTmp", "b", NULL
, offsetof(struct security_info
, private_tmp
) },
1873 { "PrivateUsers", "b", NULL
, offsetof(struct security_info
, private_users
) },
1874 { "ProtectControlGroups", "b", NULL
, offsetof(struct security_info
, protect_control_groups
) },
1875 { "ProtectHome", "s", NULL
, offsetof(struct security_info
, protect_home
) },
1876 { "ProtectHostname", "b", NULL
, offsetof(struct security_info
, protect_hostname
) },
1877 { "ProtectKernelModules", "b", NULL
, offsetof(struct security_info
, protect_kernel_modules
) },
1878 { "ProtectKernelTunables", "b", NULL
, offsetof(struct security_info
, protect_kernel_tunables
) },
1879 { "ProtectSystem", "s", NULL
, offsetof(struct security_info
, protect_system
) },
1880 { "RemoveIPC", "b", NULL
, offsetof(struct security_info
, remove_ipc
) },
1881 { "RestrictAddressFamilies", "(bas)", property_read_restrict_address_families
, 0 },
1882 { "RestrictNamespaces", "t", NULL
, offsetof(struct security_info
, restrict_namespaces
) },
1883 { "RestrictRealtime", "b", NULL
, offsetof(struct security_info
, restrict_realtime
) },
1884 { "RootDirectory", "s", NULL
, offsetof(struct security_info
, root_directory
) },
1885 { "RootImage", "s", NULL
, offsetof(struct security_info
, root_image
) },
1886 { "SupplementaryGroups", "as", NULL
, offsetof(struct security_info
, supplementary_groups
) },
1887 { "SystemCallArchitectures", "as", NULL
, offsetof(struct security_info
, system_call_architectures
) },
1888 { "SystemCallFilter", "(as)", property_read_system_call_filter
, 0 },
1889 { "Type", "s", NULL
, offsetof(struct security_info
, type
) },
1890 { "UMask", "u", NULL
, offsetof(struct security_info
, _umask
) },
1891 { "User", "s", NULL
, offsetof(struct security_info
, user
) },
1895 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1896 _cleanup_free_
char *path
= NULL
;
1899 /* Note: this mangles *info on failure! */
1905 path
= unit_dbus_path_from_name(name
);
1909 r
= bus_map_all_properties(bus
,
1910 "org.freedesktop.systemd1",
1913 BUS_MAP_STRDUP
|BUS_MAP_BOOLEAN_AS_BOOL
,
1918 return log_error_errno(r
, "Failed to get unit properties: %s", bus_error_message(&error
, r
));
1920 if (!streq_ptr(info
->load_state
, "loaded")) {
1922 if (FLAGS_SET(flags
, ANALYZE_SECURITY_ONLY_LOADED
))
1923 return -EMEDIUMTYPE
;
1925 if (streq_ptr(info
->load_state
, "not-found"))
1926 log_error("Unit %s not found, cannot analyze.", name
);
1927 else if (streq_ptr(info
->load_state
, "masked"))
1928 log_error("Unit %s is masked, cannot analyze.", name
);
1930 log_error("Unit %s not loaded properly, cannot analyze.", name
);
1935 if (FLAGS_SET(flags
, ANALYZE_SECURITY_ONLY_LONG_RUNNING
) && streq_ptr(info
->type
, "oneshot"))
1936 return -EMEDIUMTYPE
;
1938 if (info
->private_devices
||
1939 info
->private_tmp
||
1940 info
->protect_control_groups
||
1941 info
->protect_kernel_tunables
||
1942 info
->protect_kernel_modules
||
1943 !streq_ptr(info
->protect_home
, "no") ||
1944 !streq_ptr(info
->protect_system
, "no") ||
1946 info
->private_mounts
= true;
1948 if (info
->protect_kernel_modules
)
1949 info
->capability_bounding_set
&= ~(UINT64_C(1) << CAP_SYS_MODULE
);
1951 if (info
->private_devices
)
1952 info
->capability_bounding_set
&= ~((UINT64_C(1) << CAP_MKNOD
) |
1953 (UINT64_C(1) << CAP_SYS_RAWIO
));
1958 static int analyze_security_one(sd_bus
*bus
, const char *name
, Table
* overview_table
, AnalyzeSecurityFlags flags
) {
1959 _cleanup_(security_info_free
) struct security_info info
= {
1960 .default_dependencies
= true,
1961 .capability_bounding_set
= UINT64_MAX
,
1962 .restrict_namespaces
= UINT64_MAX
,
1970 r
= acquire_security_info(bus
, name
, &info
, flags
);
1971 if (r
== -EMEDIUMTYPE
) /* Ignore this one because not loaded or Type is oneshot */
1976 r
= assess(&info
, overview_table
, flags
);
1983 int analyze_security(sd_bus
*bus
, char **units
, AnalyzeSecurityFlags flags
) {
1984 _cleanup_(table_unrefp
) Table
*overview_table
= NULL
;
1989 if (strv_length(units
) != 1) {
1990 overview_table
= table_new("unit", "exposure", "predicate", "happy");
1991 if (!overview_table
)
1995 if (strv_isempty(units
)) {
1996 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1997 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1998 _cleanup_strv_free_
char **list
= NULL
;
1999 size_t allocated
= 0, n
= 0;
2002 r
= sd_bus_call_method(
2004 "org.freedesktop.systemd1",
2005 "/org/freedesktop/systemd1",
2006 "org.freedesktop.systemd1.Manager",
2011 return log_error_errno(r
, "Failed to list units: %s", bus_error_message(&error
, r
));
2013 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
2015 return bus_log_parse_error(r
);
2021 r
= bus_parse_unit_info(reply
, &info
);
2023 return bus_log_parse_error(r
);
2027 if (!endswith(info
.id
, ".service"))
2030 if (!GREEDY_REALLOC(list
, allocated
, n
+2))
2033 copy
= strdup(info
.id
);
2043 flags
|= ANALYZE_SECURITY_SHORT
|ANALYZE_SECURITY_ONLY_LOADED
|ANALYZE_SECURITY_ONLY_LONG_RUNNING
;
2045 STRV_FOREACH(i
, list
) {
2046 r
= analyze_security_one(bus
, *i
, overview_table
, flags
);
2047 if (r
< 0 && ret
>= 0)
2054 STRV_FOREACH(i
, units
) {
2055 _cleanup_free_
char *mangled
= NULL
, *instance
= NULL
;
2058 if (!FLAGS_SET(flags
, ANALYZE_SECURITY_SHORT
) && i
!= units
) {
2063 r
= unit_name_mangle_with_suffix(*i
, 0, ".service", &mangled
);
2065 return log_error_errno(r
, "Failed to mangle unit name '%s': %m", *i
);
2067 if (!endswith(mangled
, ".service")) {
2068 log_error("Unit %s is not a service unit, refusing.", *i
);
2072 if (unit_name_is_valid(mangled
, UNIT_NAME_TEMPLATE
)) {
2073 r
= unit_name_replace_instance(mangled
, "test-instance", &instance
);
2081 r
= analyze_security_one(bus
, name
, overview_table
, flags
);
2082 if (r
< 0 && ret
>= 0)
2087 if (overview_table
) {
2088 if (!FLAGS_SET(flags
, ANALYZE_SECURITY_SHORT
)) {
2093 r
= table_print(overview_table
, stdout
);
2095 return log_error_errno(r
, "Failed to output table: %m");