teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0139-analyze-add-new-security-verb.patch

ff6046
From f5bd75fb574b9be80879fda5a889ddfd2b706248 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Thu, 8 Nov 2018 09:32:17 +0100
ff6046
Subject: [PATCH] analyze: add new security verb
ff6046
ff6046
(cherry picked from commit ec16f3b6dd8b03e3ce6eff1fa9f21432208ef42b)
ff6046
ff6046
Conflicts:
ff6046
	src/analyze/analyze.c
ff6046
ff6046
Resolves: #1689832
ff6046
---
ff6046
 src/analyze/analyze-security.c | 2078 ++++++++++++++++++++++++++++++++
ff6046
 src/analyze/analyze-security.h |   12 +
ff6046
 src/analyze/analyze.c          |   16 +
ff6046
 src/analyze/meson.build        |    2 +
ff6046
 src/basic/format-table.c       |    1 -
ff6046
 src/basic/macro.h              |   13 +-
ff6046
 src/basic/terminal-util.c      |   53 +-
ff6046
 src/basic/terminal-util.h      |    1 +
ff6046
 8 files changed, 2150 insertions(+), 26 deletions(-)
ff6046
 create mode 100644 src/analyze/analyze-security.c
ff6046
 create mode 100644 src/analyze/analyze-security.h
ff6046
ff6046
diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c
ff6046
new file mode 100644
ff6046
index 0000000000..541fc0d97a
ff6046
--- /dev/null
ff6046
+++ b/src/analyze/analyze-security.c
ff6046
@@ -0,0 +1,2078 @@
ff6046
+/* SPDX-License-Identifier: LGPL-2.1+ */
ff6046
+
ff6046
+#include <sched.h>
ff6046
+#include <sys/utsname.h>
ff6046
+
ff6046
+#include "analyze-security.h"
ff6046
+#include "bus-error.h"
ff6046
+#include "bus-unit-util.h"
ff6046
+#include "bus-util.h"
ff6046
+#include "env-util.h"
ff6046
+#include "format-table.h"
ff6046
+#include "in-addr-util.h"
ff6046
+#include "locale-util.h"
ff6046
+#include "macro.h"
ff6046
+#include "parse-util.h"
ff6046
+#include "path-util.h"
ff6046
+#include "seccomp-util.h"
ff6046
+#include "set.h"
ff6046
+#include "stdio-util.h"
ff6046
+#include "strv.h"
ff6046
+#include "terminal-util.h"
ff6046
+#include "unit-def.h"
ff6046
+#include "unit-name.h"
ff6046
+
ff6046
+struct security_info {
ff6046
+        char *id;
ff6046
+        char *type;
ff6046
+        char *load_state;
ff6046
+        char *fragment_path;
ff6046
+        bool default_dependencies;
ff6046
+
ff6046
+        uint64_t ambient_capabilities;
ff6046
+        uint64_t capability_bounding_set;
ff6046
+
ff6046
+        char *user;
ff6046
+        char **supplementary_groups;
ff6046
+        bool dynamic_user;
ff6046
+
ff6046
+        bool ip_address_deny_all;
ff6046
+        bool ip_address_allow_localhost;
ff6046
+        bool ip_address_allow_other;
ff6046
+
ff6046
+        char *keyring_mode;
ff6046
+        bool lock_personality;
ff6046
+        bool memory_deny_write_execute;
ff6046
+        bool no_new_privileges;
ff6046
+        char *notify_access;
ff6046
+
ff6046
+        bool private_devices;
ff6046
+        bool private_mounts;
ff6046
+        bool private_network;
ff6046
+        bool private_tmp;
ff6046
+        bool private_users;
ff6046
+
ff6046
+        bool protect_control_groups;
ff6046
+        bool protect_kernel_modules;
ff6046
+        bool protect_kernel_tunables;
ff6046
+
ff6046
+        char *protect_home;
ff6046
+        char *protect_system;
ff6046
+
ff6046
+        bool remove_ipc;
ff6046
+
ff6046
+        bool restrict_address_family_inet;
ff6046
+        bool restrict_address_family_unix;
ff6046
+        bool restrict_address_family_netlink;
ff6046
+        bool restrict_address_family_packet;
ff6046
+        bool restrict_address_family_other;
ff6046
+
ff6046
+        uint64_t restrict_namespaces;
ff6046
+        bool restrict_realtime;
ff6046
+
ff6046
+        char *root_directory;
ff6046
+        char *root_image;
ff6046
+
ff6046
+        bool delegate;
ff6046
+        char *device_policy;
ff6046
+        bool device_allow_non_empty;
ff6046
+
ff6046
+        char **system_call_architectures;
ff6046
+
ff6046
+        bool system_call_filter_whitelist;
ff6046
+        Set *system_call_filter;
ff6046
+
ff6046
+        uint32_t _umask;
ff6046
+};
ff6046
+
ff6046
+struct security_assessor {
ff6046
+        const char *id;
ff6046
+        const char *description_good;
ff6046
+        const char *description_bad;
ff6046
+        const char *description_na;
ff6046
+        const char *url;
ff6046
+        uint64_t weight;
ff6046
+        uint64_t range;
ff6046
+        int (*assess)(const struct security_assessor *a, const struct security_info *info, const void *data, uint64_t *ret_badness, char **ret_description);
ff6046
+        size_t offset;
ff6046
+        uint64_t parameter;
ff6046
+        bool default_dependencies_only;
ff6046
+};
ff6046
+
ff6046
+static void security_info_free(struct security_info *i) {
ff6046
+        if (!i)
ff6046
+                return;
ff6046
+
ff6046
+        free(i->id);
ff6046
+        free(i->type);
ff6046
+        free(i->load_state);
ff6046
+        free(i->fragment_path);
ff6046
+
ff6046
+        free(i->user);
ff6046
+
ff6046
+        free(i->protect_home);
ff6046
+        free(i->protect_system);
ff6046
+
ff6046
+        free(i->root_directory);
ff6046
+        free(i->root_image);
ff6046
+
ff6046
+        free(i->keyring_mode);
ff6046
+        free(i->notify_access);
ff6046
+
ff6046
+        free(i->device_policy);
ff6046
+
ff6046
+        strv_free(i->supplementary_groups);
ff6046
+        strv_free(i->system_call_architectures);
ff6046
+
ff6046
+        set_free_free(i->system_call_filter);
ff6046
+}
ff6046
+
ff6046
+static bool security_info_runs_privileged(const struct security_info *i)  {
ff6046
+        assert(i);
ff6046
+
ff6046
+        if (STRPTR_IN_SET(i->user, "0", "root"))
ff6046
+                return true;
ff6046
+
ff6046
+        if (i->dynamic_user)
ff6046
+                return false;
ff6046
+
ff6046
+        return isempty(i->user);
ff6046
+}
ff6046
+
ff6046
+static int assess_bool(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        const bool *b = data;
ff6046
+
ff6046
+        assert(b);
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness = a->parameter ? *b : !*b;
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_user(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        _cleanup_free_ char *d = NULL;
ff6046
+        uint64_t b;
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (streq_ptr(info->user, NOBODY_USER_NAME)) {
ff6046
+                d = strdup("Service runs under as '" NOBODY_USER_NAME "' user, which should not be used for services");
ff6046
+                b = 9;
ff6046
+        } else if (info->dynamic_user && !STR_IN_SET(info->user, "0", "root")) {
ff6046
+                d = strdup("Service runs under a transient non-root user identity");
ff6046
+                b = 0;
ff6046
+        } else if (info->user && !STR_IN_SET(info->user, "0", "root", "")) {
ff6046
+                d = strdup("Service runs under a static non-root user identity");
ff6046
+                b = 0;
ff6046
+        } else {
ff6046
+                *ret_badness = 10;
ff6046
+                *ret_description = NULL;
ff6046
+                return 0;
ff6046
+        }
ff6046
+
ff6046
+        if (!d)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = b;
ff6046
+        *ret_description = TAKE_PTR(d);
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_protect_home(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        const char *description;
ff6046
+        uint64_t badness;
ff6046
+        char *copy;
ff6046
+        int r;
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        badness = 10;
ff6046
+        description = "Service has full access to home directories";
ff6046
+
ff6046
+        r = parse_boolean(info->protect_home);
ff6046
+        if (r < 0) {
ff6046
+                if (streq_ptr(info->protect_home, "read-only")) {
ff6046
+                        badness = 5;
ff6046
+                        description = "Service has read-only access to home directories";
ff6046
+                } else if (streq_ptr(info->protect_home, "tmpfs")) {
ff6046
+                        badness = 1;
ff6046
+                        description = "Service has access to fake empty home directories";
ff6046
+                }
ff6046
+        } else if (r > 0) {
ff6046
+                badness = 0;
ff6046
+                description = "Service has no access to home directories";
ff6046
+        }
ff6046
+
ff6046
+        copy = strdup(description);
ff6046
+        if (!copy)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = badness;
ff6046
+        *ret_description = copy;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_protect_system(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        const char *description;
ff6046
+        uint64_t badness;
ff6046
+        char *copy;
ff6046
+        int r;
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        badness = 10;
ff6046
+        description = "Service has full access the OS file hierarchy";
ff6046
+
ff6046
+        r = parse_boolean(info->protect_system);
ff6046
+        if (r < 0) {
ff6046
+                if (streq_ptr(info->protect_system, "full")) {
ff6046
+                        badness = 3;
ff6046
+                        description = "Service has very limited write access to OS file hierarchy";
ff6046
+                } else if (streq_ptr(info->protect_system, "strict")) {
ff6046
+                        badness = 0;
ff6046
+                        description = "Service has strict read-only access to the OS file hierarchy";
ff6046
+                }
ff6046
+        } else if (r > 0) {
ff6046
+                badness = 5;
ff6046
+                description = "Service has limited write access to the OS file hierarchy";
ff6046
+        }
ff6046
+
ff6046
+        copy = strdup(description);
ff6046
+        if (!copy)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = badness;
ff6046
+        *ret_description = copy;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_root_directory(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness =
ff6046
+                (isempty(info->root_directory) ||
ff6046
+                 path_equal(info->root_directory, "/")) &&
ff6046
+                (isempty(info->root_image) ||
ff6046
+                 path_equal(info->root_image, "/"));
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_capability_bounding_set(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness = !!(info->capability_bounding_set & a->parameter);
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_umask(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        char *copy = NULL;
ff6046
+        const char *d;
ff6046
+        uint64_t b;
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (!FLAGS_SET(info->_umask, 0002)) {
ff6046
+                d = "Files created by service are world-writable by default";
ff6046
+                b = 10;
ff6046
+        } else if (!FLAGS_SET(info->_umask, 0004)) {
ff6046
+                d = "Files created by service are world-readable by default";
ff6046
+                b = 5;
ff6046
+        } else if (!FLAGS_SET(info->_umask, 0020)) {
ff6046
+                d = "Files created by service are group-writable by default";
ff6046
+                b = 2;
ff6046
+        } else if (!FLAGS_SET(info->_umask, 0040)) {
ff6046
+                d = "Files created by service are group-readable by default";
ff6046
+                b = 1;
ff6046
+        } else {
ff6046
+                d = "Files created by service are accessible only by service's own user by default";
ff6046
+                b = 0;
ff6046
+        }
ff6046
+
ff6046
+        copy = strdup(d);
ff6046
+        if (!copy)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = b;
ff6046
+        *ret_description = copy;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_keyring_mode(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness = !streq_ptr(info->keyring_mode, "private");
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_notify_access(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness = streq_ptr(info->notify_access, "all");
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_remove_ipc(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (security_info_runs_privileged(info))
ff6046
+                *ret_badness = UINT64_MAX;
ff6046
+        else
ff6046
+                *ret_badness = !info->remove_ipc;
ff6046
+
ff6046
+        *ret_description = NULL;
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_supplementary_groups(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (security_info_runs_privileged(info))
ff6046
+                *ret_badness = UINT64_MAX;
ff6046
+        else
ff6046
+                *ret_badness = !strv_isempty(info->supplementary_groups);
ff6046
+
ff6046
+        *ret_description = NULL;
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_restrict_namespaces(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness = !!(info->restrict_namespaces & a->parameter);
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_system_call_architectures(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        char *d;
ff6046
+        uint64_t b;
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (strv_isempty(info->system_call_architectures)) {
ff6046
+                b = 10;
ff6046
+                d = strdup("Service may execute system calls with all ABIs");
ff6046
+        } else if (strv_equal(info->system_call_architectures, STRV_MAKE("native"))) {
ff6046
+                b = 0;
ff6046
+                d = strdup("Service may execute system calls only with native ABI");
ff6046
+        } else {
ff6046
+                b = 8;
ff6046
+                d = strdup("Service may execute system calls with multiple ABIs");
ff6046
+        }
ff6046
+
ff6046
+        if (!d)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = b;
ff6046
+        *ret_description = d;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static bool syscall_names_in_filter(Set *s, bool whitelist, const SyscallFilterSet *f) {
ff6046
+        const char *syscall;
ff6046
+
ff6046
+        NULSTR_FOREACH(syscall, f->value) {
ff6046
+                bool b;
ff6046
+
ff6046
+                if (syscall[0] == '@') {
ff6046
+                        const SyscallFilterSet *g;
ff6046
+                        assert_se(g = syscall_filter_set_find(syscall));
ff6046
+                        b = syscall_names_in_filter(s, whitelist, g);
ff6046
+                } else {
ff6046
+#if HAVE_SECCOMP
ff6046
+                        int id;
ff6046
+
ff6046
+                        /* Let's see if the system call actually exists on this platform, before complaining */
ff6046
+                        id = seccomp_syscall_resolve_name(syscall);
ff6046
+                        if (id < 0)
ff6046
+                                continue;
ff6046
+#endif
ff6046
+
ff6046
+                        b = set_contains(s, syscall);
ff6046
+                }
ff6046
+
ff6046
+                if (whitelist == b) {
ff6046
+                        log_debug("Offending syscall filter item: %s", syscall);
ff6046
+                        return true; /* bad! */
ff6046
+                }
ff6046
+        }
ff6046
+
ff6046
+        return false;
ff6046
+}
ff6046
+
ff6046
+static int assess_system_call_filter(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        const SyscallFilterSet *f;
ff6046
+        char *d = NULL;
ff6046
+        uint64_t b;
ff6046
+
ff6046
+        assert(a);
ff6046
+        assert(info);
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        assert(a->parameter < _SYSCALL_FILTER_SET_MAX);
ff6046
+        f = syscall_filter_sets + a->parameter;
ff6046
+
ff6046
+        if (!info->system_call_filter_whitelist && set_isempty(info->system_call_filter)) {
ff6046
+                d = strdup("Service does not filter system calls");
ff6046
+                b = 10;
ff6046
+        } else {
ff6046
+                bool bad;
ff6046
+
ff6046
+                log_debug("Analyzing system call filter, checking against: %s", f->name);
ff6046
+                bad = syscall_names_in_filter(info->system_call_filter, info->system_call_filter_whitelist, f);
ff6046
+                log_debug("Result: %s", bad ? "bad" : "good");
ff6046
+
ff6046
+                if (info->system_call_filter_whitelist) {
ff6046
+                        if (bad) {
ff6046
+                                (void) asprintf(&d, "System call whitelist defined for service, and %s is included", f->name);
ff6046
+                                b = 9;
ff6046
+                        } else {
ff6046
+                                (void) asprintf(&d, "System call whitelist defined for service, and %s is not included", f->name);
ff6046
+                                b = 0;
ff6046
+                        }
ff6046
+                } else {
ff6046
+                        if (bad) {
ff6046
+                                (void) asprintf(&d, "System call blacklist defined for service, and %s is not included", f->name);
ff6046
+                                b = 10;
ff6046
+                        } else {
ff6046
+                                (void) asprintf(&d, "System call blacklist defined for service, and %s is included", f->name);
ff6046
+                                b = 5;
ff6046
+                        }
ff6046
+                }
ff6046
+        }
ff6046
+
ff6046
+        if (!d)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = b;
ff6046
+        *ret_description = d;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_ip_address_allow(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        char *d = NULL;
ff6046
+        uint64_t b;
ff6046
+
ff6046
+        assert(info);
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (!info->ip_address_deny_all) {
ff6046
+                d = strdup("Service does not define an IP address whitelist");
ff6046
+                b = 10;
ff6046
+        } else if (info->ip_address_allow_other) {
ff6046
+                d = strdup("Service defines IP address whitelist with non-localhost entries");
ff6046
+                b = 5;
ff6046
+        } else if (info->ip_address_allow_localhost) {
ff6046
+                d = strdup("Service defines IP address whitelits with only localhost entries");
ff6046
+                b = 2;
ff6046
+        } else {
ff6046
+                d = strdup("Service blocks all IP address ranges");
ff6046
+                b = 0;
ff6046
+        }
ff6046
+
ff6046
+        if (!d)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = b;
ff6046
+        *ret_description = d;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_device_allow(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        char *d = NULL;
ff6046
+        uint64_t b;
ff6046
+
ff6046
+        assert(info);
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        if (STRPTR_IN_SET(info->device_policy, "strict", "closed")) {
ff6046
+
ff6046
+                if (info->device_allow_non_empty) {
ff6046
+                        d = strdup("Service has a device ACL with some special devices");
ff6046
+                        b = 5;
ff6046
+                } else {
ff6046
+                        d = strdup("Service has a minimal device ACL");
ff6046
+                        b = 0;
ff6046
+                }
ff6046
+        } else {
ff6046
+                d = strdup("Service has no device ACL");
ff6046
+                b = 10;
ff6046
+        }
ff6046
+
ff6046
+        if (!d)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        *ret_badness = b;
ff6046
+        *ret_description = d;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int assess_ambient_capabilities(
ff6046
+                const struct security_assessor *a,
ff6046
+                const struct security_info *info,
ff6046
+                const void *data,
ff6046
+                uint64_t *ret_badness,
ff6046
+                char **ret_description) {
ff6046
+
ff6046
+        assert(ret_badness);
ff6046
+        assert(ret_description);
ff6046
+
ff6046
+        *ret_badness = info->ambient_capabilities != 0;
ff6046
+        *ret_description = NULL;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static const struct security_assessor security_assessor_table[] = {
ff6046
+        {
ff6046
+                .id = "User=/DynamicUser=",
ff6046
+                .description_bad = "Service runs as root user",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#User=",
ff6046
+                .weight = 2000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_user,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SupplementaryGroups=",
ff6046
+                .description_good = "Service has no supplementary groups",
ff6046
+                .description_bad = "Service runs with supplementary groups",
ff6046
+                .description_na = "Service runs as root, option does not matter",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SupplementaryGroups=",
ff6046
+                .weight = 200,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_supplementary_groups,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "PrivateDevices=",
ff6046
+                .description_good = "Service has no access to hardware devices",
ff6046
+                .description_bad = "Service potentially has access to hardware devices",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateDevices=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, private_devices),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "PrivateMounts=",
ff6046
+                .description_good = "Service cannot install system mounts",
ff6046
+                .description_bad = "Service may install system mounts",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateMounts=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, private_mounts),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "PrivateNetwork=",
ff6046
+                .description_good = "Service has no access to the host's network",
ff6046
+                .description_bad = "Service has access to the host's network",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateNetwork=",
ff6046
+                .weight = 2500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, private_network),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "PrivateTmp=",
ff6046
+                .description_good = "Service has no access to other software's temporary files",
ff6046
+                .description_bad = "Service has access to other software's temporary files",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateTmp=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, private_tmp),
ff6046
+                .default_dependencies_only = true,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "PrivateUsers=",
ff6046
+                .description_good = "Service does not have access to other users",
ff6046
+                .description_bad = "Service has access to other users",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateUsers=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, private_users),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "ProtectControlGroups=",
ff6046
+                .description_good = "Service cannot modify the control group file system",
ff6046
+                .description_bad = "Service may modify to the control group file system",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectControlGroups=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, protect_control_groups),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "ProtectKernelModules=",
ff6046
+                .description_good = "Service cannot load or read kernel modules",
ff6046
+                .description_bad = "Service may load or read kernel modules",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelModules=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, protect_kernel_modules),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "ProtectKernelTunables=",
ff6046
+                .description_good = "Service cannot alter kernel tunables (/proc/sys, …)",
ff6046
+                .description_bad = "Service may alter kernel tunables",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelTunables=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, protect_kernel_tunables),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "ProtectHome=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHome=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_protect_home,
ff6046
+                .default_dependencies_only = true,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "ProtectSystem=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_protect_system,
ff6046
+                .default_dependencies_only = true,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RootDirectory=/RootImage=",
ff6046
+                .description_good = "Service has its own root directory/image",
ff6046
+                .description_bad = "Service runs within the host's root directory",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RootDirectory=",
ff6046
+                .weight = 200,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_root_directory,
ff6046
+                .default_dependencies_only = true,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "LockPersonality=",
ff6046
+                .description_good = "Service cannot change ABI personality",
ff6046
+                .description_bad = "Service may change ABI personality",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#LockPersonality=",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, lock_personality),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "MemoryDenyWriteExecute=",
ff6046
+                .description_good = "Service cannot create writable executable memory mappings",
ff6046
+                .description_bad = "Service may create writable executable memory mappings",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute=",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, memory_deny_write_execute),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "NoNewPrivileges=",
ff6046
+                .description_good = "Service processes cannot acquire new privileges",
ff6046
+                .description_bad = "Service processes may acquire new privileges",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NoNewPrivileges=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, no_new_privileges),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_ADMIN",
ff6046
+                .description_good = "Service has no administrator privileges",
ff6046
+                .description_bad = "Service has administrator privileges",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = UINT64_C(1) << CAP_SYS_ADMIN,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)",
ff6046
+                .description_good = "Service cannot change UID/GID identities/capabilities",
ff6046
+                .description_bad = "Service may change UID/GID identities/capabilities",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SETUID)|
ff6046
+                             (UINT64_C(1) << CAP_SETGID)|
ff6046
+                             (UINT64_C(1) << CAP_SETPCAP),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_PTRACE",
ff6046
+                .description_good = "Service has no ptrace() debugging abilities",
ff6046
+                .description_bad = "Service has ptrace() debugging abilities",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_PTRACE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_TIME",
ff6046
+                .description_good = "Service processes cannot change the system clock",
ff6046
+                .description_bad = "Service processes may change the system clock",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = UINT64_C(1) << CAP_SYS_TIME,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_NET_ADMIN",
ff6046
+                .description_good = "Service has no network configuration privileges",
ff6046
+                .description_bad = "Service has network configuration privileges",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_NET_ADMIN),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_RAWIO",
ff6046
+                .description_good = "Service has no raw I/O access",
ff6046
+                .description_bad = "Service has raw I/O access",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_RAWIO),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_MODULE",
ff6046
+                .description_good = "Service cannot load kernel modules",
ff6046
+                .description_bad = "Service may load kernel modules",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_MODULE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_AUDIT_*",
ff6046
+                .description_good = "Service has no audit subsystem access",
ff6046
+                .description_bad = "Service has audit subsystem access",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_AUDIT_CONTROL) |
ff6046
+                             (UINT64_C(1) << CAP_AUDIT_READ) |
ff6046
+                             (UINT64_C(1) << CAP_AUDIT_WRITE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYSLOG",
ff6046
+                .description_good = "Service has no access to kernel logging",
ff6046
+                .description_bad = "Service has access to kernel logging",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYSLOG),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_(NICE|RESOURCE)",
ff6046
+                .description_good = "Service has no privileges to change resource use parameters",
ff6046
+                .description_bad = "Service has privileges to change resource use parameters",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_NICE) |
ff6046
+                             (UINT64_C(1) << CAP_SYS_RESOURCE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_MKNOD",
ff6046
+                .description_good = "Service cannot create device nodes",
ff6046
+                .description_bad = "Service may create device nodes",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_MKNOD),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_(CHOWN|FSETID|SETFCAP)",
ff6046
+                .description_good = "Service cannot change file ownership/access mode/capabilities",
ff6046
+                .description_bad = "Service may change file ownership/access mode/capabilities unrestricted",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_CHOWN) |
ff6046
+                             (UINT64_C(1) << CAP_FSETID) |
ff6046
+                             (UINT64_C(1) << CAP_SETFCAP),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_(DAC_*|FOWNER|IPC_OWNER)",
ff6046
+                .description_good = "Service cannot override UNIX file/IPC permission checks",
ff6046
+                .description_bad = "Service may override UNIX file/IPC permission checks",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_DAC_OVERRIDE) |
ff6046
+                             (UINT64_C(1) << CAP_DAC_READ_SEARCH) |
ff6046
+                             (UINT64_C(1) << CAP_FOWNER) |
ff6046
+                             (UINT64_C(1) << CAP_IPC_OWNER),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_KILL",
ff6046
+                .description_good = "Service cannot send UNIX signals to arbitrary processes",
ff6046
+                .description_bad = "Service may send UNIX signals to arbitrary processes",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_KILL),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_NET_(BIND_SERVICE|BROADCAST|RAW)",
ff6046
+                .description_good = "Service has no elevated networking privileges",
ff6046
+                .description_bad = "Service has elevated networking privileges",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_NET_BIND_SERVICE) |
ff6046
+                             (UINT64_C(1) << CAP_NET_BROADCAST) |
ff6046
+                             (UINT64_C(1) << CAP_NET_RAW),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_BOOT",
ff6046
+                .description_good = "Service cannot issue reboot()",
ff6046
+                .description_bad = "Service may issue reboot()",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_BOOT),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_MAC_*",
ff6046
+                .description_good = "Service cannot adjust SMACK MAC",
ff6046
+                .description_bad = "Service may adjust SMACK MAC",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_MAC_ADMIN)|
ff6046
+                             (UINT64_C(1) << CAP_MAC_OVERRIDE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_LINUX_IMMUTABLE",
ff6046
+                .description_good = "Service cannot mark files immutable",
ff6046
+                .description_bad = "Service may mark files immutable",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 75,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_LINUX_IMMUTABLE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_IPC_LOCK",
ff6046
+                .description_good = "Service cannot lock memory into RAM",
ff6046
+                .description_bad = "Service may lock memory into RAM",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 50,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_IPC_LOCK),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_CHROOT",
ff6046
+                .description_good = "Service cannot issue chroot()",
ff6046
+                .description_bad = "Service may issue chroot()",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 50,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_CHROOT),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_BLOCK_SUSPEND",
ff6046
+                .description_good = "Service cannot establish wake locks",
ff6046
+                .description_bad = "Service may establish wake locks",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 25,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_BLOCK_SUSPEND),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_WAKE_ALARM",
ff6046
+                .description_good = "Service cannot program timers that wake up the system",
ff6046
+                .description_bad = "Service may program timers that wake up the system",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 25,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_WAKE_ALARM),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_LEASE",
ff6046
+                .description_good = "Service cannot create file leases",
ff6046
+                .description_bad = "Service may create file leases",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 25,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_LEASE),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG",
ff6046
+                .description_good = "Service cannot issue vhangup()",
ff6046
+                .description_bad = "Service may issue vhangup()",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 25,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_TTY_CONFIG),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "CapabilityBoundingSet=~CAP_SYS_PACCT",
ff6046
+                .description_good = "Service cannot use acct()",
ff6046
+                .description_bad = "Service may use acct()",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
ff6046
+                .weight = 25,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_capability_bounding_set,
ff6046
+                .parameter = (UINT64_C(1) << CAP_SYS_PACCT),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "UMask=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#UMask=",
ff6046
+                .weight = 100,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_umask,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "KeyringMode=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#KeyringMode=",
ff6046
+                .description_good = "Service doesn't share key material with other services",
ff6046
+                .description_bad = "Service shares key material with other service",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_keyring_mode,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "NotifyAccess=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NotifyAccess=",
ff6046
+                .description_good = "Service child processes cannot alter service state",
ff6046
+                .description_bad = "Service child processes may alter service state",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_notify_access,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RemoveIPC=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RemoveIPC=",
ff6046
+                .description_good = "Service user cannot leave SysV IPC objects around",
ff6046
+                .description_bad = "Service user may leave SysV IPC objects around",
ff6046
+                .description_na = "Service runs as root, option does not apply",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_remove_ipc,
ff6046
+                .offset = offsetof(struct security_info, remove_ipc),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "Delegate=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Delegate=",
ff6046
+                .description_good = "Service does not maintain its own delegated control group subtree",
ff6046
+                .description_bad = "Service maintains its own delegated control group subtree",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, delegate),
ff6046
+                .parameter = true, /* invert! */
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictRealtime=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictRealtime=",
ff6046
+                .description_good = "Service realtime scheduling access is restricted",
ff6046
+                .description_bad = "Service may acquire realtime scheduling",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, restrict_realtime),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWUSER",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create user namespaces",
ff6046
+                .description_bad = "Service may create user namespaces",
ff6046
+                .weight = 1500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWUSER,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWNS",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create file system namespaces",
ff6046
+                .description_bad = "Service may create file system namespaces",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWNS,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWIPC",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create IPC namespaces",
ff6046
+                .description_bad = "Service may create IPC namespaces",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWIPC,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWPID",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create process namespaces",
ff6046
+                .description_bad = "Service may create process namespaces",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWPID,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWCGROUP",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create cgroup namespaces",
ff6046
+                .description_bad = "Service may create cgroup namespaces",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWCGROUP,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWNET",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create network namespaces",
ff6046
+                .description_bad = "Service may create network namespaces",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWNET,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictNamespaces=~CLONE_NEWUTS",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
ff6046
+                .description_good = "Service cannot create hostname namespaces",
ff6046
+                .description_bad = "Service may create hostname namespaces",
ff6046
+                .weight = 100,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_restrict_namespaces,
ff6046
+                .parameter = CLONE_NEWUTS,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictAddressFamilies=~AF_(INET|INET6)",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
ff6046
+                .description_good = "Service cannot allocate Internet sockets",
ff6046
+                .description_bad = "Service may allocate Internet sockets",
ff6046
+                .weight = 1500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, restrict_address_family_inet),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictAddressFamilies=~AF_UNIX",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
ff6046
+                .description_good = "Service cannot allocate local sockets",
ff6046
+                .description_bad = "Service may allocate local sockets",
ff6046
+                .weight = 25,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, restrict_address_family_unix),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictAddressFamilies=~AF_NETLINK",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
ff6046
+                .description_good = "Service cannot allocate netlink sockets",
ff6046
+                .description_bad = "Service may allocate netlink sockets",
ff6046
+                .weight = 200,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, restrict_address_family_netlink),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictAddressFamilies=~AF_PACKET",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
ff6046
+                .description_good = "Service cannot allocate packet sockets",
ff6046
+                .description_bad = "Service may allocate packet sockets",
ff6046
+                .weight = 1000,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, restrict_address_family_packet),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "RestrictAddressFamilies=~…",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
ff6046
+                .description_good = "Service cannot allocate exotic sockets",
ff6046
+                .description_bad = "Service may allocate exotic sockets",
ff6046
+                .weight = 1250,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_bool,
ff6046
+                .offset = offsetof(struct security_info, restrict_address_family_other),
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallArchitectures=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallArchitectures=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_architectures,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@swap",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_SWAP,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@obsolete",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 250,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_OBSOLETE,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@clock",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_CLOCK,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@cpu-emulation",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 250,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_CPU_EMULATION,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@debug",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_DEBUG,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@mount",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_MOUNT,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@module",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_MODULE,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@raw-io",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_RAW_IO,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@reboot",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_REBOOT,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@privileged",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 700,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_PRIVILEGED,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "SystemCallFilter=~@resources",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
ff6046
+                .weight = 700,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_system_call_filter,
ff6046
+                .parameter = SYSCALL_FILTER_SET_RESOURCES,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "IPAddressDeny=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#IPAddressDeny=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_ip_address_allow,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "DeviceAllow=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#DeviceAllow=",
ff6046
+                .weight = 1000,
ff6046
+                .range = 10,
ff6046
+                .assess = assess_device_allow,
ff6046
+        },
ff6046
+        {
ff6046
+                .id = "AmbientCapabilities=",
ff6046
+                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#AmbientCapabilities=",
ff6046
+                .description_good = "Service process does not receive ambient capabilities",
ff6046
+                .description_bad = "Service process receives ambient capabilities",
ff6046
+                .weight = 500,
ff6046
+                .range = 1,
ff6046
+                .assess = assess_ambient_capabilities,
ff6046
+        },
ff6046
+};
ff6046
+
ff6046
+static int assess(const struct security_info *info, Table *overview_table, AnalyzeSecurityFlags flags) {
ff6046
+        static const struct {
ff6046
+                uint64_t exposure;
ff6046
+                const char *name;
ff6046
+                const char *color;
ff6046
+                SpecialGlyph smiley;
ff6046
+        } badness_table[] = {
ff6046
+                { 100, "DANGEROUS", ANSI_HIGHLIGHT_RED,    DEPRESSED_SMILEY        },
ff6046
+                { 90,  "UNSAFE",    ANSI_HIGHLIGHT_RED,    UNHAPPY_SMILEY          },
ff6046
+                { 75,  "EXPOSED",   ANSI_HIGHLIGHT_YELLOW, SLIGHTLY_UNHAPPY_SMILEY },
ff6046
+                { 50,  "MEDIUM",    NULL,                  NEUTRAL_SMILEY          },
ff6046
+                { 10,  "OK",        ANSI_HIGHLIGHT_GREEN,  SLIGHTLY_HAPPY_SMILEY   },
ff6046
+                { 1,   "SAFE",      ANSI_HIGHLIGHT_GREEN,  HAPPY_SMILEY            },
ff6046
+                { 0,   "PERFECT",   ANSI_HIGHLIGHT_GREEN,  ECSTATIC_SMILEY         },
ff6046
+        };
ff6046
+
ff6046
+        uint64_t badness_sum = 0, weight_sum = 0, exposure;
ff6046
+        _cleanup_(table_unrefp) Table *details_table = NULL;
ff6046
+        size_t i;
ff6046
+        int r;
ff6046
+
ff6046
+        if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT)) {
ff6046
+                details_table = table_new("", "NAME", "DESCRIPTION", "WEIGHT", "BADNESS", "RANGE", "EXPOSURE");
ff6046
+                if (!details_table)
ff6046
+                        return log_oom();
ff6046
+
ff6046
+                (void) table_set_sort(details_table, 3, 1, (size_t) -1);
ff6046
+                (void) table_set_reverse(details_table, 3, true);
ff6046
+
ff6046
+                if (getenv_bool("SYSTEMD_ANALYZE_DEBUG") <= 0)
ff6046
+                        (void) table_set_display(details_table, 0, 1, 2, 6, (size_t) -1);
ff6046
+        }
ff6046
+
ff6046
+        for (i = 0; i < ELEMENTSOF(security_assessor_table); i++) {
ff6046
+                const struct security_assessor *a = security_assessor_table + i;
ff6046
+                _cleanup_free_ char *d = NULL;
ff6046
+                uint64_t badness;
ff6046
+                void *data;
ff6046
+
ff6046
+                data = (uint8_t*) info + a->offset;
ff6046
+
ff6046
+                if (a->default_dependencies_only && !info->default_dependencies) {
ff6046
+                        badness = UINT64_MAX;
ff6046
+                        d = strdup("Service runs in special boot phase, option does not apply");
ff6046
+                        if (!d)
ff6046
+                                return log_oom();
ff6046
+                } else {
ff6046
+                        r = a->assess(a, info, data, &badness, &d);
ff6046
+                        if (r < 0)
ff6046
+                                return r;
ff6046
+                }
ff6046
+
ff6046
+                assert(a->range > 0);
ff6046
+
ff6046
+                if (badness != UINT64_MAX) {
ff6046
+                        assert(badness <= a->range);
ff6046
+
ff6046
+                        badness_sum += DIV_ROUND_UP(badness * a->weight, a->range);
ff6046
+                        weight_sum += a->weight;
ff6046
+                }
ff6046
+
ff6046
+                if (details_table) {
ff6046
+                        const char *checkmark, *description, *color = NULL;
ff6046
+                        TableCell *cell;
ff6046
+
ff6046
+                        if (badness == UINT64_MAX) {
ff6046
+                                checkmark = " ";
ff6046
+                                description = a->description_na;
ff6046
+                                color = NULL;
ff6046
+                        } else if (badness == a->range) {
ff6046
+                                checkmark = special_glyph(CROSS_MARK);
ff6046
+                                description = a->description_bad;
ff6046
+                                color = ansi_highlight_red();
ff6046
+                        } else if (badness == 0) {
ff6046
+                                checkmark = special_glyph(CHECK_MARK);
ff6046
+                                description = a->description_good;
ff6046
+                                color = ansi_highlight_green();
ff6046
+                        } else {
ff6046
+                                checkmark = special_glyph(CROSS_MARK);
ff6046
+                                description = NULL;
ff6046
+                                color = ansi_highlight_red();
ff6046
+                        }
ff6046
+
ff6046
+                        if (d)
ff6046
+                                description = d;
ff6046
+
ff6046
+                        r = table_add_cell_full(details_table, &cell, TABLE_STRING, checkmark, 1, 1, 0, 0, 0);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                        if (color)
ff6046
+                                (void) table_set_color(details_table, cell, color);
ff6046
+
ff6046
+                        r = table_add_cell(details_table, &cell, TABLE_STRING, a->id);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                        if (a->url)
ff6046
+                                (void) table_set_url(details_table, cell, a->url);
ff6046
+
ff6046
+                        r = table_add_cell(details_table, NULL, TABLE_STRING, description);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+
ff6046
+                        r = table_add_cell(details_table, &cell, TABLE_UINT64, &a->weight);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                        (void) table_set_align_percent(details_table, cell, 100);
ff6046
+
ff6046
+                        r = table_add_cell(details_table, &cell, TABLE_UINT64, &badness);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                        (void) table_set_align_percent(details_table, cell, 100);
ff6046
+
ff6046
+                        r = table_add_cell(details_table, &cell, TABLE_UINT64, &a->range);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                        (void) table_set_align_percent(details_table, cell, 100);
ff6046
+
ff6046
+                        r = table_add_cell(details_table, &cell, TABLE_EMPTY, NULL);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                        (void) table_set_align_percent(details_table, cell, 100);
ff6046
+                }
ff6046
+        }
ff6046
+
ff6046
+        if (details_table) {
ff6046
+                size_t row;
ff6046
+
ff6046
+                for (row = 1; row < table_get_rows(details_table); row++) {
ff6046
+                        char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
ff6046
+                        const uint64_t *weight, *badness, *range;
ff6046
+                        TableCell *cell;
ff6046
+                        uint64_t x;
ff6046
+
ff6046
+                        assert_se(weight = table_get_at(details_table, row, 3));
ff6046
+                        assert_se(badness = table_get_at(details_table, row, 4));
ff6046
+                        assert_se(range = table_get_at(details_table, row, 5));
ff6046
+
ff6046
+                        if (*badness == UINT64_MAX || *badness == 0)
ff6046
+                                continue;
ff6046
+
ff6046
+                        assert_se(cell = table_get_cell(details_table, row, 6));
ff6046
+
ff6046
+                        x = DIV_ROUND_UP(DIV_ROUND_UP(*badness * *weight * 100U, *range), weight_sum);
ff6046
+                        xsprintf(buf, "%" PRIu64 ".%" PRIu64, x / 10, x % 10);
ff6046
+
ff6046
+                        r = table_update(details_table, cell, TABLE_STRING, buf);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to update cell in table: %m");
ff6046
+                }
ff6046
+
ff6046
+                r = table_print(details_table, stdout);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to output table: %m");
ff6046
+        }
ff6046
+
ff6046
+        exposure = DIV_ROUND_UP(badness_sum * 100U, weight_sum);
ff6046
+
ff6046
+        for (i = 0; i < ELEMENTSOF(badness_table); i++)
ff6046
+                if (exposure >= badness_table[i].exposure)
ff6046
+                        break;
ff6046
+
ff6046
+        assert(i < ELEMENTSOF(badness_table));
ff6046
+
ff6046
+        if (details_table) {
ff6046
+                _cleanup_free_ char *clickable = NULL;
ff6046
+                const char *name;
ff6046
+
ff6046
+                /* If we shall output the details table, also print the brief summary underneath */
ff6046
+
ff6046
+                if (info->fragment_path) {
ff6046
+                        r = terminal_urlify_path(info->fragment_path, info->id, &clickable);
ff6046
+                        if (r < 0)
ff6046
+                                return log_oom();
ff6046
+
ff6046
+                        name = clickable;
ff6046
+                } else
ff6046
+                        name = info->id;
ff6046
+
ff6046
+                printf("\n%s %sOverall exposure level for %s%s: %s%" PRIu64 ".%" PRIu64 " %s%s %s\n",
ff6046
+                       special_glyph(ARROW),
ff6046
+                       ansi_highlight(),
ff6046
+                       name,
ff6046
+                       ansi_normal(),
ff6046
+                       colors_enabled() ? strempty(badness_table[i].color) : "",
ff6046
+                       exposure / 10, exposure % 10,
ff6046
+                       badness_table[i].name,
ff6046
+                       ansi_normal(),
ff6046
+                       special_glyph(badness_table[i].smiley));
ff6046
+        }
ff6046
+
ff6046
+        fflush(stdout);
ff6046
+
ff6046
+        if (overview_table) {
ff6046
+                char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
ff6046
+                TableCell *cell;
ff6046
+
ff6046
+                r = table_add_cell(overview_table, &cell, TABLE_STRING, info->id);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                if (info->fragment_path) {
ff6046
+                        _cleanup_free_ char *url = NULL;
ff6046
+
ff6046
+                        r = file_url_from_path(info->fragment_path, &url;;
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to generate URL from path: %m");
ff6046
+
ff6046
+                        (void) table_set_url(overview_table, cell, url);
ff6046
+                }
ff6046
+
ff6046
+                xsprintf(buf, "%" PRIu64 ".%" PRIu64, exposure / 10, exposure % 10);
ff6046
+                r = table_add_cell(overview_table, &cell, TABLE_STRING, buf);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                (void) table_set_align_percent(overview_table, cell, 100);
ff6046
+
ff6046
+                r = table_add_cell(overview_table, &cell, TABLE_STRING, badness_table[i].name);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+                (void) table_set_color(overview_table, cell, strempty(badness_table[i].color));
ff6046
+
ff6046
+                r = table_add_cell(overview_table, NULL, TABLE_STRING, special_glyph(badness_table[i].smiley));
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to add cell to table: %m");
ff6046
+        }
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int property_read_restrict_address_families(
ff6046
+                sd_bus *bus,
ff6046
+                const char *member,
ff6046
+                sd_bus_message *m,
ff6046
+                sd_bus_error *error,
ff6046
+                void *userdata) {
ff6046
+
ff6046
+        struct security_info *info = userdata;
ff6046
+        int whitelist, r;
ff6046
+
ff6046
+        assert(bus);
ff6046
+        assert(member);
ff6046
+        assert(m);
ff6046
+
ff6046
+        r = sd_bus_message_enter_container(m, 'r', "bas");
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        r = sd_bus_message_read(m, "b", &whitelist);
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        info->restrict_address_family_inet =
ff6046
+                info->restrict_address_family_unix =
ff6046
+                info->restrict_address_family_netlink =
ff6046
+                info->restrict_address_family_packet =
ff6046
+                info->restrict_address_family_other = whitelist;
ff6046
+
ff6046
+        r = sd_bus_message_enter_container(m, 'a', "s");
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        for (;;) {
ff6046
+                const char *name;
ff6046
+
ff6046
+                r = sd_bus_message_read(m, "s", &name);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
+
ff6046
+                if (STR_IN_SET(name, "AF_INET", "AF_INET6"))
ff6046
+                        info->restrict_address_family_inet = !whitelist;
ff6046
+                else if (streq(name, "AF_UNIX"))
ff6046
+                        info->restrict_address_family_unix = !whitelist;
ff6046
+                else if (streq(name, "AF_NETLINK"))
ff6046
+                        info->restrict_address_family_netlink = !whitelist;
ff6046
+                else if (streq(name, "AF_PACKET"))
ff6046
+                        info->restrict_address_family_packet = !whitelist;
ff6046
+                else
ff6046
+                        info->restrict_address_family_other = !whitelist;
ff6046
+        }
ff6046
+
ff6046
+        r = sd_bus_message_exit_container(m);
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        return sd_bus_message_exit_container(m);
ff6046
+}
ff6046
+
ff6046
+static int property_read_system_call_filter(
ff6046
+                sd_bus *bus,
ff6046
+                const char *member,
ff6046
+                sd_bus_message *m,
ff6046
+                sd_bus_error *error,
ff6046
+                void *userdata) {
ff6046
+
ff6046
+        struct security_info *info = userdata;
ff6046
+        int whitelist, r;
ff6046
+
ff6046
+        assert(bus);
ff6046
+        assert(member);
ff6046
+        assert(m);
ff6046
+
ff6046
+        r = sd_bus_message_enter_container(m, 'r', "bas");
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        r = sd_bus_message_read(m, "b", &whitelist);
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        info->system_call_filter_whitelist = whitelist;
ff6046
+
ff6046
+        r = sd_bus_message_enter_container(m, 'a', "s");
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        for (;;) {
ff6046
+                const char *name;
ff6046
+
ff6046
+                r = sd_bus_message_read(m, "s", &name);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
+
ff6046
+                r = set_ensure_allocated(&info->system_call_filter, &string_hash_ops);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+
ff6046
+                r = set_put_strdup(info->system_call_filter, name);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+        }
ff6046
+
ff6046
+        r = sd_bus_message_exit_container(m);
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        return sd_bus_message_exit_container(m);
ff6046
+}
ff6046
+
ff6046
+static int property_read_ip_address_allow(
ff6046
+                sd_bus *bus,
ff6046
+                const char *member,
ff6046
+                sd_bus_message *m,
ff6046
+                sd_bus_error *error,
ff6046
+                void *userdata) {
ff6046
+
ff6046
+        struct security_info *info = userdata;
ff6046
+        bool deny_ipv4 = false, deny_ipv6 = false;
ff6046
+        int r;
ff6046
+
ff6046
+        assert(bus);
ff6046
+        assert(member);
ff6046
+        assert(m);
ff6046
+
ff6046
+        r = sd_bus_message_enter_container(m, 'a', "(iayu)");
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        for (;;) {
ff6046
+                const void *data;
ff6046
+                size_t size;
ff6046
+                int32_t family;
ff6046
+                uint32_t prefixlen;
ff6046
+
ff6046
+                r = sd_bus_message_enter_container(m, 'r', "iayu");
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
+
ff6046
+                r = sd_bus_message_read(m, "i", &family);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+
ff6046
+                r = sd_bus_message_read_array(m, 'y', &data, &size);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+
ff6046
+                r = sd_bus_message_read(m, "u", &prefixlen);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+
ff6046
+                r = sd_bus_message_exit_container(m);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+
ff6046
+                if (streq(member, "IPAddressAllow")) {
ff6046
+                        union in_addr_union u;
ff6046
+
ff6046
+                        if (family == AF_INET && size == 4 && prefixlen == 8)
ff6046
+                                memcpy(&u.in, data, size);
ff6046
+                        else if (family == AF_INET6 && size == 16 && prefixlen == 128)
ff6046
+                                memcpy(&u.in6, data, size);
ff6046
+                        else {
ff6046
+                                info->ip_address_allow_other = true;
ff6046
+                                continue;
ff6046
+                        }
ff6046
+
ff6046
+                        if (in_addr_is_localhost(family, &u))
ff6046
+                                info->ip_address_allow_localhost = true;
ff6046
+                        else
ff6046
+                                info->ip_address_allow_other = true;
ff6046
+                } else {
ff6046
+                        assert(streq(member, "IPAddressDeny"));
ff6046
+
ff6046
+                        if (family == AF_INET && size == 4 && prefixlen == 0)
ff6046
+                                deny_ipv4 = true;
ff6046
+                        else if (family == AF_INET6 && size == 16 && prefixlen == 0)
ff6046
+                                deny_ipv6 = true;
ff6046
+                }
ff6046
+        }
ff6046
+
ff6046
+        info->ip_address_deny_all = deny_ipv4 && deny_ipv6;
ff6046
+
ff6046
+        return sd_bus_message_exit_container(m);
ff6046
+}
ff6046
+
ff6046
+static int property_read_device_allow(
ff6046
+                sd_bus *bus,
ff6046
+                const char *member,
ff6046
+                sd_bus_message *m,
ff6046
+                sd_bus_error *error,
ff6046
+                void *userdata) {
ff6046
+
ff6046
+        struct security_info *info = userdata;
ff6046
+        size_t n = 0;
ff6046
+        int r;
ff6046
+
ff6046
+        assert(bus);
ff6046
+        assert(member);
ff6046
+        assert(m);
ff6046
+
ff6046
+        r = sd_bus_message_enter_container(m, 'a', "(ss)");
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        for (;;) {
ff6046
+                const char *name, *policy;
ff6046
+
ff6046
+                r = sd_bus_message_read(m, "(ss)", &name, &policy);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
+
ff6046
+                n++;
ff6046
+        }
ff6046
+
ff6046
+        info->device_allow_non_empty = n > 0;
ff6046
+
ff6046
+        return sd_bus_message_exit_container(m);
ff6046
+}
ff6046
+
ff6046
+static int acquire_security_info(sd_bus *bus, const char *name, struct security_info *info, AnalyzeSecurityFlags flags) {
ff6046
+
ff6046
+        static const struct bus_properties_map security_map[] = {
ff6046
+                { "AmbientCapabilities",     "t",       NULL,                                    offsetof(struct security_info, ambient_capabilities)      },
ff6046
+                { "CapabilityBoundingSet",   "t",       NULL,                                    offsetof(struct security_info, capability_bounding_set)   },
ff6046
+                { "DefaultDependencies",     "b",       NULL,                                    offsetof(struct security_info, default_dependencies)      },
ff6046
+                { "Delegate",                "b",       NULL,                                    offsetof(struct security_info, delegate)                  },
ff6046
+                { "DeviceAllow",             "a(ss)",   property_read_device_allow,              0                                                         },
ff6046
+                { "DevicePolicy",            "s",       NULL,                                    offsetof(struct security_info, device_policy)             },
ff6046
+                { "DynamicUser",             "b",       NULL,                                    offsetof(struct security_info, dynamic_user)              },
ff6046
+                { "FragmentPath",            "s",       NULL,                                    offsetof(struct security_info, fragment_path)             },
ff6046
+                { "IPAddressAllow",          "a(iayu)", property_read_ip_address_allow,          0                                                         },
ff6046
+                { "IPAddressDeny",           "a(iayu)", property_read_ip_address_allow,          0                                                         },
ff6046
+                { "Id",                      "s",       NULL,                                    offsetof(struct security_info, id)                        },
ff6046
+                { "KeyringMode",             "s",       NULL,                                    offsetof(struct security_info, keyring_mode)              },
ff6046
+                { "LoadState",               "s",       NULL,                                    offsetof(struct security_info, load_state)                },
ff6046
+                { "LockPersonality",         "b",       NULL,                                    offsetof(struct security_info, lock_personality)          },
ff6046
+                { "MemoryDenyWriteExecute",  "b",       NULL,                                    offsetof(struct security_info, memory_deny_write_execute) },
ff6046
+                { "NoNewPrivileges",         "b",       NULL,                                    offsetof(struct security_info, no_new_privileges)         },
ff6046
+                { "NotifyAccess",            "s",       NULL,                                    offsetof(struct security_info, notify_access)             },
ff6046
+                { "PrivateDevices",          "b",       NULL,                                    offsetof(struct security_info, private_devices)           },
ff6046
+                { "PrivateMounts",           "b",       NULL,                                    offsetof(struct security_info, private_mounts)            },
ff6046
+                { "PrivateNetwork",          "b",       NULL,                                    offsetof(struct security_info, private_network)           },
ff6046
+                { "PrivateTmp",              "b",       NULL,                                    offsetof(struct security_info, private_tmp)               },
ff6046
+                { "PrivateUsers",            "b",       NULL,                                    offsetof(struct security_info, private_users)             },
ff6046
+                { "PrivateUsers",            "b",       NULL,                                    offsetof(struct security_info, private_users)             },
ff6046
+                { "ProtectControlGroups",    "b",       NULL,                                    offsetof(struct security_info, protect_control_groups)    },
ff6046
+                { "ProtectHome",             "s",       NULL,                                    offsetof(struct security_info, protect_home)              },
ff6046
+                { "ProtectKernelModules",    "b",       NULL,                                    offsetof(struct security_info, protect_kernel_modules)    },
ff6046
+                { "ProtectKernelTunables",   "b",       NULL,                                    offsetof(struct security_info, protect_kernel_tunables)   },
ff6046
+                { "ProtectSystem",           "s",       NULL,                                    offsetof(struct security_info, protect_system)            },
ff6046
+                { "RemoveIPC",               "b",       NULL,                                    offsetof(struct security_info, remove_ipc)                },
ff6046
+                { "RestrictAddressFamilies", "(bas)",   property_read_restrict_address_families, 0                                                         },
ff6046
+                { "RestrictNamespaces",      "t",       NULL,                                    offsetof(struct security_info, restrict_namespaces)       },
ff6046
+                { "RestrictRealtime",        "b",       NULL,                                    offsetof(struct security_info, restrict_realtime)         },
ff6046
+                { "RootDirectory",           "s",       NULL,                                    offsetof(struct security_info, root_directory)            },
ff6046
+                { "RootImage",               "s",       NULL,                                    offsetof(struct security_info, root_image)                },
ff6046
+                { "SupplementaryGroups",     "as",      NULL,                                    offsetof(struct security_info, supplementary_groups)      },
ff6046
+                { "SystemCallArchitectures", "as",      NULL,                                    offsetof(struct security_info, system_call_architectures) },
ff6046
+                { "SystemCallFilter",        "(as)",    property_read_system_call_filter,        0                                                         },
ff6046
+                { "Type",                    "s",       NULL,                                    offsetof(struct security_info, type)                      },
ff6046
+                { "UMask",                   "u",       NULL,                                    offsetof(struct security_info, _umask)                    },
ff6046
+                { "User",                    "s",       NULL,                                    offsetof(struct security_info, user)                      },
ff6046
+                {}
ff6046
+        };
ff6046
+
ff6046
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ff6046
+        _cleanup_free_ char *path = NULL;
ff6046
+        int r;
ff6046
+
ff6046
+        /* Note: this mangles *info on failure! */
ff6046
+
ff6046
+        assert(bus);
ff6046
+        assert(name);
ff6046
+        assert(info);
ff6046
+
ff6046
+        path = unit_dbus_path_from_name(name);
ff6046
+        if (!path)
ff6046
+                return log_oom();
ff6046
+
ff6046
+        r = bus_map_all_properties(bus,
ff6046
+                                   "org.freedesktop.systemd1",
ff6046
+                                   path,
ff6046
+                                   security_map,
ff6046
+                                   BUS_MAP_STRDUP|BUS_MAP_BOOLEAN_AS_BOOL,
ff6046
+                                   &error,
ff6046
+                                   NULL,
ff6046
+                                   info);
ff6046
+        if (r < 0)
ff6046
+                return log_error_errno(r, "Failed to get unit properties: %s", bus_error_message(&error, r));
ff6046
+
ff6046
+        if (!streq_ptr(info->load_state, "loaded")) {
ff6046
+
ff6046
+                if (FLAGS_SET(flags, ANALYZE_SECURITY_ONLY_LOADED))
ff6046
+                        return -EMEDIUMTYPE;
ff6046
+
ff6046
+                if (streq_ptr(info->load_state, "not-found"))
ff6046
+                        log_error("Unit %s not found, cannot analyze.", name);
ff6046
+                else if (streq_ptr(info->load_state, "masked"))
ff6046
+                        log_error("Unit %s is masked, cannot analyze.", name);
ff6046
+                else
ff6046
+                        log_error("Unit %s not loaded properly, cannot analyze.", name);
ff6046
+
ff6046
+                return -EINVAL;
ff6046
+        }
ff6046
+
ff6046
+        if (FLAGS_SET(flags, ANALYZE_SECURITY_ONLY_LONG_RUNNING) && streq_ptr(info->type, "oneshot"))
ff6046
+                return -EMEDIUMTYPE;
ff6046
+
ff6046
+        if (info->private_devices ||
ff6046
+            info->private_tmp ||
ff6046
+            info->protect_control_groups ||
ff6046
+            info->protect_kernel_tunables ||
ff6046
+            info->protect_kernel_modules ||
ff6046
+            !streq_ptr(info->protect_home, "no") ||
ff6046
+            !streq_ptr(info->protect_system, "no") ||
ff6046
+            info->root_image)
ff6046
+                info->private_mounts = true;
ff6046
+
ff6046
+        if (info->protect_kernel_modules)
ff6046
+                info->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE);
ff6046
+
ff6046
+        if (info->private_devices)
ff6046
+                info->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) |
ff6046
+                                                   (UINT64_C(1) << CAP_SYS_RAWIO));
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+static int analyze_security_one(sd_bus *bus, const char *name, Table* overview_table, AnalyzeSecurityFlags flags) {
ff6046
+        _cleanup_(security_info_free) struct security_info info = {
ff6046
+                .default_dependencies = true,
ff6046
+                .capability_bounding_set = UINT64_MAX,
ff6046
+                .restrict_namespaces = UINT64_MAX,
ff6046
+                ._umask = 0002,
ff6046
+        };
ff6046
+        int r;
ff6046
+
ff6046
+        assert(bus);
ff6046
+        assert(name);
ff6046
+
ff6046
+        r = acquire_security_info(bus, name, &info, flags);
ff6046
+        if (r == -EMEDIUMTYPE) /* Ignore this one because not loaded or Type is oneshot */
ff6046
+                return 0;
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        r = assess(&info, overview_table, flags);
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) {
ff6046
+        _cleanup_(table_unrefp) Table *overview_table = NULL;
ff6046
+        int ret = 0, r;
ff6046
+
ff6046
+        assert(bus);
ff6046
+
ff6046
+        if (strv_length(units) != 1) {
ff6046
+                overview_table = table_new("UNIT", "EXPOSURE", "PREDICATE", "HAPPY");
ff6046
+                if (!overview_table)
ff6046
+                        return log_oom();
ff6046
+        }
ff6046
+
ff6046
+        if (strv_isempty(units)) {
ff6046
+                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ff6046
+                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
ff6046
+                _cleanup_strv_free_ char **list = NULL;
ff6046
+                size_t allocated = 0, n = 0;
ff6046
+                char **i;
ff6046
+
ff6046
+                r = sd_bus_call_method(
ff6046
+                                bus,
ff6046
+                                "org.freedesktop.systemd1",
ff6046
+                                "/org/freedesktop/systemd1",
ff6046
+                                "org.freedesktop.systemd1.Manager",
ff6046
+                                "ListUnits",
ff6046
+                                &error, &reply,
ff6046
+                                NULL);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
ff6046
+
ff6046
+                r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
ff6046
+                if (r < 0)
ff6046
+                        return bus_log_parse_error(r);
ff6046
+
ff6046
+                for (;;) {
ff6046
+                        UnitInfo info;
ff6046
+                        char *copy = NULL;
ff6046
+
ff6046
+                        r = bus_parse_unit_info(reply, &info;;
ff6046
+                        if (r < 0)
ff6046
+                                return bus_log_parse_error(r);
ff6046
+                        if (r == 0)
ff6046
+                                break;
ff6046
+
ff6046
+                        if (!endswith(info.id, ".service"))
ff6046
+                                continue;
ff6046
+
ff6046
+                        if (!GREEDY_REALLOC(list, allocated, n+2))
ff6046
+                                return log_oom();
ff6046
+
ff6046
+                        copy = strdup(info.id);
ff6046
+                        if (!copy)
ff6046
+                                return log_oom();
ff6046
+
ff6046
+                        list[n++] = copy;
ff6046
+                        list[n] = NULL;
ff6046
+                }
ff6046
+
ff6046
+                strv_sort(list);
ff6046
+
ff6046
+                flags |= ANALYZE_SECURITY_SHORT|ANALYZE_SECURITY_ONLY_LOADED|ANALYZE_SECURITY_ONLY_LONG_RUNNING;
ff6046
+
ff6046
+                STRV_FOREACH(i, list) {
ff6046
+                        r = analyze_security_one(bus, *i, overview_table, flags);
ff6046
+                        if (r < 0 && ret >= 0)
ff6046
+                                ret = r;
ff6046
+                }
ff6046
+
ff6046
+        } else {
ff6046
+                char **i;
ff6046
+
ff6046
+                STRV_FOREACH(i, units) {
ff6046
+                        _cleanup_free_ char *mangled = NULL, *instance = NULL;
ff6046
+                        const char *name;
ff6046
+
ff6046
+                        if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT) && i != units) {
ff6046
+                                putc('\n', stdout);
ff6046
+                                fflush(stdout);
ff6046
+                        }
ff6046
+
ff6046
+                        r = unit_name_mangle_with_suffix(*i, 0, ".service", &mangled);
ff6046
+                        if (r < 0)
ff6046
+                                return log_error_errno(r, "Failed to mangle unit name '%s': %m", *i);
ff6046
+
ff6046
+                        if (!endswith(mangled, ".service")) {
ff6046
+                                log_error("Unit %s is not a service unit, refusing.", *i);
ff6046
+                                return -EINVAL;
ff6046
+                        }
ff6046
+
ff6046
+                        if (unit_name_is_valid(mangled, UNIT_NAME_TEMPLATE)) {
ff6046
+                                r = unit_name_replace_instance(mangled, "test-instance", &instance);
ff6046
+                                if (r < 0)
ff6046
+                                        return log_oom();
ff6046
+
ff6046
+                                name = instance;
ff6046
+                        } else
ff6046
+                                name = mangled;
ff6046
+
ff6046
+                        r = analyze_security_one(bus, name, overview_table, flags);
ff6046
+                        if (r < 0 && ret >= 0)
ff6046
+                                ret = r;
ff6046
+                }
ff6046
+        }
ff6046
+
ff6046
+        if (overview_table) {
ff6046
+                if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT)) {
ff6046
+                        putc('\n', stdout);
ff6046
+                        fflush(stdout);
ff6046
+                }
ff6046
+
ff6046
+                r = table_print(overview_table, stdout);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to output table: %m");
ff6046
+        }
ff6046
+
ff6046
+        return ret;
ff6046
+}
ff6046
diff --git a/src/analyze/analyze-security.h b/src/analyze/analyze-security.h
ff6046
new file mode 100644
ff6046
index 0000000000..c00ae7c80a
ff6046
--- /dev/null
ff6046
+++ b/src/analyze/analyze-security.h
ff6046
@@ -0,0 +1,12 @@
ff6046
+/* SPDX-License-Identifier: LGPL-2.1+ */
ff6046
+#pragma once
ff6046
+
ff6046
+#include "sd-bus.h"
ff6046
+
ff6046
+typedef enum AnalyzeSecurityFlags {
ff6046
+        ANALYZE_SECURITY_SHORT             = 1 << 0,
ff6046
+        ANALYZE_SECURITY_ONLY_LOADED       = 1 << 1,
ff6046
+        ANALYZE_SECURITY_ONLY_LONG_RUNNING = 1 << 2,
ff6046
+} AnalyzeSecurityFlags;
ff6046
+
ff6046
+int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags);
ff6046
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
ff6046
index dc7d2ab0f6..c30a133fc0 100644
ff6046
--- a/src/analyze/analyze.c
ff6046
+++ b/src/analyze/analyze.c
ff6046
@@ -11,6 +11,7 @@
ff6046
 #include "sd-bus.h"
ff6046
 
ff6046
 #include "alloc-util.h"
ff6046
+#include "analyze-security.h"
ff6046
 #include "analyze-verify.h"
ff6046
 #include "bus-error.h"
ff6046
 #include "bus-unit-util.h"
ff6046
@@ -1659,6 +1660,19 @@ static int do_verify(int argc, char *argv[], void *userdata) {
ff6046
         return verify_units(strv_skip(argv, 1), arg_scope, arg_man, arg_generators);
ff6046
 }
ff6046
 
ff6046
+static int do_security(int argc, char *argv[], void *userdata) {
ff6046
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
ff6046
+        int r;
ff6046
+
ff6046
+        r = acquire_bus(&bus, NULL);
ff6046
+        if (r < 0)
ff6046
+                return log_error_errno(r, "Failed to create bus connection: %m");
ff6046
+
ff6046
+        (void) pager_open(arg_no_pager, false);
ff6046
+
ff6046
+        return analyze_security(bus, strv_skip(argv, 1), 0);
ff6046
+}
ff6046
+
ff6046
 static int help(int argc, char *argv[], void *userdata) {
ff6046
 
ff6046
         (void) pager_open(arg_no_pager, false);
ff6046
@@ -1696,6 +1710,7 @@ static int help(int argc, char *argv[], void *userdata) {
ff6046
                "  verify FILE...           Check unit files for correctness\n"
ff6046
                "  calendar SPEC...         Validate repetitive calendar time events\n"
ff6046
                "  service-watchdogs [BOOL] Get/set service watchdog state\n"
ff6046
+               "  security [UNIT...]       Analyze security of unit\n"
ff6046
                , program_invocation_short_name);
ff6046
 
ff6046
         /* When updating this list, including descriptions, apply
ff6046
@@ -1884,6 +1899,7 @@ int main(int argc, char *argv[]) {
ff6046
                 { "verify",            2,        VERB_ANY, 0,            do_verify              },
ff6046
                 { "calendar",          2,        VERB_ANY, 0,            test_calendar          },
ff6046
                 { "service-watchdogs", VERB_ANY, 2,        0,            service_watchdogs      },
ff6046
+                { "security",          VERB_ANY, VERB_ANY, 0,            do_security            },
ff6046
                 {}
ff6046
         };
ff6046
 
ff6046
diff --git a/src/analyze/meson.build b/src/analyze/meson.build
ff6046
index 3a69a259b1..4db4dfa552 100644
ff6046
--- a/src/analyze/meson.build
ff6046
+++ b/src/analyze/meson.build
ff6046
@@ -4,4 +4,6 @@ systemd_analyze_sources = files('''
ff6046
         analyze.c
ff6046
         analyze-verify.c
ff6046
         analyze-verify.h
ff6046
+        analyze-security.c
ff6046
+        analyze-security.h
ff6046
 '''.split())
ff6046
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
ff6046
index 844b92f41c..c541e92b3c 100644
ff6046
--- a/src/basic/format-table.c
ff6046
+++ b/src/basic/format-table.c
ff6046
@@ -10,7 +10,6 @@
ff6046
 #include "gunicode.h"
ff6046
 #include "pager.h"
ff6046
 #include "parse-util.h"
ff6046
-#include "pretty-print.h"
ff6046
 #include "string-util.h"
ff6046
 #include "terminal-util.h"
ff6046
 #include "time-util.h"
ff6046
diff --git a/src/basic/macro.h b/src/basic/macro.h
ff6046
index 79ab02b27a..0fe6a62aa8 100644
ff6046
--- a/src/basic/macro.h
ff6046
+++ b/src/basic/macro.h
ff6046
@@ -249,12 +249,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
ff6046
  * computation should be possible in the given type. Therefore, we use
ff6046
  * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
ff6046
  * quotient and the remainder, so both should be equally fast. */
ff6046
-#define DIV_ROUND_UP(_x, _y)                                            \
ff6046
-        __extension__ ({                                                \
ff6046
-                const typeof(_x) __x = (_x);                            \
ff6046
-                const typeof(_y) __y = (_y);                            \
ff6046
-                (__x / __y + !!(__x % __y));                            \
ff6046
-        })
ff6046
+#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
ff6046
+#define __DIV_ROUND_UP(xq, x, yq, y)                                    \
ff6046
+        ({                                                              \
ff6046
+                const typeof(x) UNIQ_T(X, xq) = (x);                    \
ff6046
+                const typeof(y) UNIQ_T(Y, yq) = (y);                    \
ff6046
+                (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
ff6046
+         })
ff6046
 
ff6046
 #define assert_message_se(expr, message)                                \
ff6046
         do {                                                            \
ff6046
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
ff6046
index f4af0e6522..e2bbe8187d 100644
ff6046
--- a/src/basic/terminal-util.c
ff6046
+++ b/src/basic/terminal-util.c
ff6046
@@ -1320,10 +1320,38 @@ int terminal_urlify(const char *url, const char *text, char **ret) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-int terminal_urlify_path(const char *path, const char *text, char **ret) {
ff6046
+int file_url_from_path(const char *path, char **ret) {
ff6046
         _cleanup_free_ char *absolute = NULL;
ff6046
         struct utsname u;
ff6046
-        const char *url;
ff6046
+        char *url = NULL;
ff6046
+        int r;
ff6046
+
ff6046
+        if (uname(&u) < 0)
ff6046
+                return -errno;
ff6046
+
ff6046
+        if (!path_is_absolute(path)) {
ff6046
+                r = path_make_absolute_cwd(path, &absolute);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+
ff6046
+                path = absolute;
ff6046
+        }
ff6046
+
ff6046
+        /* As suggested by https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda, let's include the local
ff6046
+         * hostname here. Note that we don't use gethostname_malloc() or gethostname_strict() since we are interested
ff6046
+         * in the raw string the kernel has set, whatever it may be, under the assumption that terminals are not overly
ff6046
+         * careful with validating the strings either. */
ff6046
+
ff6046
+        url = strjoin("file://", u.nodename, path);
ff6046
+        if (!url)
ff6046
+                return -ENOMEM;
ff6046
+
ff6046
+        *ret = url;
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+int terminal_urlify_path(const char *path, const char *text, char **ret) {
ff6046
+        _cleanup_free_ char *url = NULL;
ff6046
         int r;
ff6046
 
ff6046
         assert(path);
ff6046
@@ -1348,27 +1376,14 @@ int terminal_urlify_path(const char *path, const char *text, char **ret) {
ff6046
                 return 0;
ff6046
         }
ff6046
 
ff6046
-        if (uname(&u) < 0)
ff6046
-                return -errno;
ff6046
-
ff6046
-        if (!path_is_absolute(path)) {
ff6046
-                r = path_make_absolute_cwd(path, &absolute);
ff6046
-                if (r < 0)
ff6046
-                        return r;
ff6046
-
ff6046
-                path = absolute;
ff6046
-        }
ff6046
-
ff6046
-        /* As suggested by https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda, let's include the local
ff6046
-         * hostname here. Note that we don't use gethostname_malloc() or gethostname_strict() since we are interested
ff6046
-         * in the raw string the kernel has set, whatever it may be, under the assumption that terminals are not overly
ff6046
-         * careful with validating the strings either. */
ff6046
-
ff6046
-        url = strjoina("file://", u.nodename, path);
ff6046
+        r = file_url_from_path(path, &url;;
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
 
ff6046
         return terminal_urlify(url, text, ret);
ff6046
 }
ff6046
 
ff6046
+
ff6046
 static int cat_file(const char *filename, bool newline) {
ff6046
         _cleanup_fclose_ FILE *f = NULL;
ff6046
         _cleanup_free_ char *urlified = NULL;
ff6046
diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h
ff6046
index 0055b72343..3f23ecfd3d 100644
ff6046
--- a/src/basic/terminal-util.h
ff6046
+++ b/src/basic/terminal-util.h
ff6046
@@ -154,6 +154,7 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
ff6046
 int vt_default_utf8(void);
ff6046
 int vt_reset_keyboard(int fd);
ff6046
 
ff6046
+int file_url_from_path(const char *path, char **ret);
ff6046
 int terminal_urlify(const char *url, const char *text, char **ret);
ff6046
 int terminal_urlify_path(const char *path, const char *text, char **ret);
ff6046