Blame SOURCES/0124-seccomp-reduce-logging-about-failure-to-add-syscall-.patch

a3e2b5
From da8ea9abbacf381513896a7064a1fa0067b3d549 Mon Sep 17 00:00:00 2001
a3e2b5
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
a3e2b5
Date: Mon, 24 Sep 2018 16:59:12 +0200
a3e2b5
Subject: [PATCH] seccomp: reduce logging about failure to add syscall to
a3e2b5
 seccomp
a3e2b5
a3e2b5
Our logs are full of:
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call oldstat() / -10037, ignoring: Numerical argument out of domain
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call get_thread_area() / -10076, ignoring: Numerical argument out of domain
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call set_thread_area() / -10079, ignoring: Numerical argument out of domain
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call oldfstat() / -10034, ignoring: Numerical argument out of domain
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call oldolduname() / -10036, ignoring: Numerical argument out of domain
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call oldlstat() / -10035, ignoring: Numerical argument out of domain
a3e2b5
Sep 19 09:22:10 autopkgtest systemd[690]: Failed to add rule for system call waitpid() / -10073, ignoring: Numerical argument out of domain
a3e2b5
...
a3e2b5
This is pointless and makes debug logs hard to read. Let's keep the logs
a3e2b5
in test code, but disable it in nspawn and pid1. This is done through a function
a3e2b5
parameter because those functions operate recursively and it's not possible to
a3e2b5
make the caller to log meaningfully.
a3e2b5
a3e2b5
There should be no functional change, except the skipped debug logs.
a3e2b5
a3e2b5
(cherry-picked from commit b54f36c604472ffe08830ec4306fa2885b4a5424)
a3e2b5
a3e2b5
Resolves: #1658691
a3e2b5
---
a3e2b5
 src/core/execute.c          |  6 ++--
a3e2b5
 src/nspawn/nspawn-seccomp.c |  4 +--
a3e2b5
 src/shared/seccomp-util.c   | 57 ++++++++++++++++++++-----------------
a3e2b5
 src/shared/seccomp-util.h   |  6 ++--
a3e2b5
 src/test/test-seccomp.c     | 16 +++++------
a3e2b5
 5 files changed, 47 insertions(+), 42 deletions(-)
a3e2b5
a3e2b5
diff --git a/src/core/execute.c b/src/core/execute.c
a3e2b5
index 8ac69d1a0f..ffb92ddfc7 100644
a3e2b5
--- a/src/core/execute.c
a3e2b5
+++ b/src/core/execute.c
a3e2b5
@@ -1415,7 +1415,7 @@ static int apply_syscall_filter(const Unit* u, const ExecContext *c, bool needs_
a3e2b5
                         return r;
a3e2b5
         }
a3e2b5
 
a3e2b5
-        return seccomp_load_syscall_filter_set_raw(default_action, c->syscall_filter, action);
a3e2b5
+        return seccomp_load_syscall_filter_set_raw(default_action, c->syscall_filter, action, false);
a3e2b5
 }
a3e2b5
 
a3e2b5
 static int apply_syscall_archs(const Unit *u, const ExecContext *c) {
a3e2b5
@@ -1498,7 +1498,7 @@ static int apply_protect_kernel_modules(const Unit *u, const ExecContext *c) {
a3e2b5
         if (skip_seccomp_unavailable(u, "ProtectKernelModules="))
a3e2b5
                 return 0;
a3e2b5
 
a3e2b5
-        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_MODULE, SCMP_ACT_ERRNO(EPERM));
a3e2b5
+        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_MODULE, SCMP_ACT_ERRNO(EPERM), false);
a3e2b5
 }
a3e2b5
 
a3e2b5
 static int apply_private_devices(const Unit *u, const ExecContext *c) {
a3e2b5
@@ -1513,7 +1513,7 @@ static int apply_private_devices(const Unit *u, const ExecContext *c) {
a3e2b5
         if (skip_seccomp_unavailable(u, "PrivateDevices="))
a3e2b5
                 return 0;
a3e2b5
 
a3e2b5
-        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_RAW_IO, SCMP_ACT_ERRNO(EPERM));
a3e2b5
+        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_RAW_IO, SCMP_ACT_ERRNO(EPERM), false);
a3e2b5
 }
a3e2b5
 
a3e2b5
 static int apply_restrict_namespaces(const Unit *u, const ExecContext *c) {
a3e2b5
diff --git a/src/nspawn/nspawn-seccomp.c b/src/nspawn/nspawn-seccomp.c
a3e2b5
index eb1964bb6d..b56c5b04a8 100644
a3e2b5
--- a/src/nspawn/nspawn-seccomp.c
a3e2b5
+++ b/src/nspawn/nspawn-seccomp.c
a3e2b5
@@ -148,7 +148,7 @@ static int seccomp_add_default_syscall_filter(
a3e2b5
                 if (whitelist[i].capability != 0 && (cap_list_retain & (1ULL << whitelist[i].capability)) == 0)
a3e2b5
                         continue;
a3e2b5
 
a3e2b5
-                r = seccomp_add_syscall_filter_item(ctx, whitelist[i].name, SCMP_ACT_ALLOW, syscall_blacklist);
a3e2b5
+                r = seccomp_add_syscall_filter_item(ctx, whitelist[i].name, SCMP_ACT_ALLOW, syscall_blacklist, false);
a3e2b5
                 if (r < 0)
a3e2b5
                         /* If the system call is not known on this architecture, then that's fine, let's ignore it */
a3e2b5
                         log_debug_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", whitelist[i].name, seccomp_arch_to_string(arch));
a3e2b5
@@ -157,7 +157,7 @@ static int seccomp_add_default_syscall_filter(
a3e2b5
         }
a3e2b5
 
a3e2b5
         STRV_FOREACH(p, syscall_whitelist) {
a3e2b5
-                r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist);
a3e2b5
+                r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist, false);
a3e2b5
                 if (r < 0)
a3e2b5
                         log_debug_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", *p, seccomp_arch_to_string(arch));
a3e2b5
                 else
a3e2b5
diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c
a3e2b5
index c433cb90dc..92910acf0e 100644
a3e2b5
--- a/src/shared/seccomp-util.c
a3e2b5
+++ b/src/shared/seccomp-util.c
a3e2b5
@@ -857,11 +857,9 @@ const SyscallFilterSet *syscall_filter_set_find(const char *name) {
a3e2b5
         return NULL;
a3e2b5
 }
a3e2b5
 
a3e2b5
-static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp, const SyscallFilterSet *set, uint32_t action, char **exclude);
a3e2b5
-
a3e2b5
-int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name, uint32_t action, char **exclude) {
a3e2b5
-        int r;
a3e2b5
+static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp, const SyscallFilterSet *set, uint32_t action, char **exclude, bool log_missing);
a3e2b5
 
a3e2b5
+int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name, uint32_t action, char **exclude, bool log_missing) {
a3e2b5
         assert(seccomp);
a3e2b5
         assert(name);
a3e2b5
 
a3e2b5
@@ -877,32 +875,36 @@ int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name,
a3e2b5
                         return -EINVAL;
a3e2b5
                 }
a3e2b5
 
a3e2b5
-                r = seccomp_add_syscall_filter_set(seccomp, other, action, exclude);
a3e2b5
-                if (r < 0)
a3e2b5
-                        return r;
a3e2b5
+                return seccomp_add_syscall_filter_set(seccomp, other, action, exclude, log_missing);
a3e2b5
+
a3e2b5
         } else {
a3e2b5
-                int id;
a3e2b5
+                int id, r;
a3e2b5
 
a3e2b5
                 id = seccomp_syscall_resolve_name(name);
a3e2b5
                 if (id == __NR_SCMP_ERROR) {
a3e2b5
-                        log_debug("System call %s is not known, ignoring.", name);
a3e2b5
+                        if (log_missing)
a3e2b5
+                                log_debug("System call %s is not known, ignoring.", name);
a3e2b5
                         return 0;
a3e2b5
                 }
a3e2b5
 
a3e2b5
                 r = seccomp_rule_add_exact(seccomp, action, id, 0);
a3e2b5
-                if (r < 0)
a3e2b5
+                if (r < 0) {
a3e2b5
                         /* If the system call is not known on this architecture, then that's fine, let's ignore it */
a3e2b5
-                        log_debug_errno(r, "Failed to add rule for system call %s() / %d, ignoring: %m", name, id);
a3e2b5
-        }
a3e2b5
+                        if (log_missing)
a3e2b5
+                                log_debug_errno(r, "Failed to add rule for system call %s() / %d, ignoring: %m",
a3e2b5
+                                                name, id);
a3e2b5
+                }
a3e2b5
 
a3e2b5
-        return 0;
a3e2b5
+                return 0;
a3e2b5
+        }
a3e2b5
 }
a3e2b5
 
a3e2b5
 static int seccomp_add_syscall_filter_set(
a3e2b5
                 scmp_filter_ctx seccomp,
a3e2b5
                 const SyscallFilterSet *set,
a3e2b5
                 uint32_t action,
a3e2b5
-                char **exclude) {
a3e2b5
+                char **exclude,
a3e2b5
+                bool log_missing) {
a3e2b5
 
a3e2b5
         const char *sys;
a3e2b5
         int r;
a3e2b5
@@ -911,7 +913,7 @@ static int seccomp_add_syscall_filter_set(
a3e2b5
         assert(set);
a3e2b5
 
a3e2b5
         NULSTR_FOREACH(sys, set->value) {
a3e2b5
-                r = seccomp_add_syscall_filter_item(seccomp, sys, action, exclude);
a3e2b5
+                r = seccomp_add_syscall_filter_item(seccomp, sys, action, exclude, log_missing);
a3e2b5
                 if (r < 0)
a3e2b5
                         return r;
a3e2b5
         }
a3e2b5
@@ -919,7 +921,7 @@ static int seccomp_add_syscall_filter_set(
a3e2b5
         return 0;
a3e2b5
 }
a3e2b5
 
a3e2b5
-int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action) {
a3e2b5
+int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action, bool log_missing) {
a3e2b5
         uint32_t arch;
a3e2b5
         int r;
a3e2b5
 
a3e2b5
@@ -937,7 +939,7 @@ int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilter
a3e2b5
                 if (r < 0)
a3e2b5
                         return r;
a3e2b5
 
a3e2b5
-                r = seccomp_add_syscall_filter_set(seccomp, set, action, NULL);
a3e2b5
+                r = seccomp_add_syscall_filter_set(seccomp, set, action, NULL, log_missing);
a3e2b5
                 if (r < 0) {
a3e2b5
                         log_debug_errno(r, "Failed to add filter set, ignoring: %m");
a3e2b5
                         continue;
a3e2b5
@@ -953,7 +955,7 @@ int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilter
a3e2b5
         return 0;
a3e2b5
 }
a3e2b5
 
a3e2b5
-int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, uint32_t action) {
a3e2b5
+int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, uint32_t action, bool log_missing) {
a3e2b5
         uint32_t arch;
a3e2b5
         int r;
a3e2b5
 
a3e2b5
@@ -966,7 +968,7 @@ int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, u
a3e2b5
         SECCOMP_FOREACH_LOCAL_ARCH(arch) {
a3e2b5
                 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
a3e2b5
                 Iterator i;
a3e2b5
-                void *id, *val;
a3e2b5
+                void *syscall_id, *val;
a3e2b5
 
a3e2b5
                 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch));
a3e2b5
 
a3e2b5
@@ -974,20 +976,23 @@ int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, u
a3e2b5
                 if (r < 0)
a3e2b5
                         return r;
a3e2b5
 
a3e2b5
-                HASHMAP_FOREACH_KEY(val, id, set, i) {
a3e2b5
+                HASHMAP_FOREACH_KEY(val, syscall_id, set, i) {
a3e2b5
                         uint32_t a = action;
a3e2b5
-                        int e = PTR_TO_INT(val);
a3e2b5
+                        int id = PTR_TO_INT(syscall_id) - 1;
a3e2b5
+                        int error = PTR_TO_INT(val);
a3e2b5
 
a3e2b5
-                        if (action != SCMP_ACT_ALLOW && e >= 0)
a3e2b5
-                                a = SCMP_ACT_ERRNO(e);
a3e2b5
+                        if (action != SCMP_ACT_ALLOW && error >= 0)
a3e2b5
+                                a = SCMP_ACT_ERRNO(error);
a3e2b5
 
a3e2b5
-                        r = seccomp_rule_add_exact(seccomp, a, PTR_TO_INT(id) - 1, 0);
a3e2b5
+                        r = seccomp_rule_add_exact(seccomp, a, id, 0);
a3e2b5
                         if (r < 0) {
a3e2b5
                                 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
a3e2b5
                                 _cleanup_free_ char *n = NULL;
a3e2b5
 
a3e2b5
-                                n = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1);
a3e2b5
-                                log_debug_errno(r, "Failed to add rule for system call %s() / %d, ignoring: %m", strna(n), PTR_TO_INT(id) - 1);
a3e2b5
+                                n = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, id);
a3e2b5
+                                if (log_missing)
a3e2b5
+                                        log_debug_errno(r, "Failed to add rule for system call %s() / %d, ignoring: %m",
a3e2b5
+                                                        strna(n), id);
a3e2b5
                         }
a3e2b5
                 }
a3e2b5
 
a3e2b5
diff --git a/src/shared/seccomp-util.h b/src/shared/seccomp-util.h
a3e2b5
index eac857afb9..d8a36c4e21 100644
a3e2b5
--- a/src/shared/seccomp-util.h
a3e2b5
+++ b/src/shared/seccomp-util.h
a3e2b5
@@ -58,10 +58,10 @@ const SyscallFilterSet *syscall_filter_set_find(const char *name);
a3e2b5
 
a3e2b5
 int seccomp_filter_set_add(Hashmap *s, bool b, const SyscallFilterSet *set);
a3e2b5
 
a3e2b5
-int seccomp_add_syscall_filter_item(scmp_filter_ctx *ctx, const char *name, uint32_t action, char **exclude);
a3e2b5
+int seccomp_add_syscall_filter_item(scmp_filter_ctx *ctx, const char *name, uint32_t action, char **exclude, bool log_missing);
a3e2b5
 
a3e2b5
-int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action);
a3e2b5
-int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, uint32_t action);
a3e2b5
+int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action, bool log_missing);
a3e2b5
+int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, uint32_t action, bool log_missing);
a3e2b5
 
a3e2b5
 typedef enum SeccompParseFlags {
a3e2b5
         SECCOMP_PARSE_INVERT     = 1 << 0,
a3e2b5
diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c
a3e2b5
index d82cb5c1c5..d177515ac7 100644
a3e2b5
--- a/src/test/test-seccomp.c
a3e2b5
+++ b/src/test/test-seccomp.c
a3e2b5
@@ -104,11 +104,11 @@ static void test_filter_sets(void) {
a3e2b5
                 if (pid == 0) { /* Child? */
a3e2b5
                         int fd;
a3e2b5
 
a3e2b5
-                        /* if we look at the default set (or one that includes it), whitelist instead of blacklist */
a3e2b5
+                        /* If we look at the default set (or one that includes it), whitelist instead of blacklist */
a3e2b5
                         if (IN_SET(i, SYSCALL_FILTER_SET_DEFAULT, SYSCALL_FILTER_SET_SYSTEM_SERVICE))
a3e2b5
-                                r = seccomp_load_syscall_filter_set(SCMP_ACT_ERRNO(EUCLEAN), syscall_filter_sets + i, SCMP_ACT_ALLOW);
a3e2b5
+                                r = seccomp_load_syscall_filter_set(SCMP_ACT_ERRNO(EUCLEAN), syscall_filter_sets + i, SCMP_ACT_ALLOW, true);
a3e2b5
                         else
a3e2b5
-                                r = seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + i, SCMP_ACT_ERRNO(EUCLEAN));
a3e2b5
+                                r = seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + i, SCMP_ACT_ERRNO(EUCLEAN), true);
a3e2b5
                         if (r < 0)
a3e2b5
                                 _exit(EXIT_FAILURE);
a3e2b5
 
a3e2b5
@@ -515,7 +515,7 @@ static void test_load_syscall_filter_set_raw(void) {
a3e2b5
                 assert_se(access("/", F_OK) >= 0);
a3e2b5
                 assert_se(poll(NULL, 0, 0) == 0);
a3e2b5
 
a3e2b5
-                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, NULL, SCMP_ACT_KILL) >= 0);
a3e2b5
+                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, NULL, SCMP_ACT_KILL, true) >= 0);
a3e2b5
                 assert_se(access("/", F_OK) >= 0);
a3e2b5
                 assert_se(poll(NULL, 0, 0) == 0);
a3e2b5
 
a3e2b5
@@ -526,7 +526,7 @@ static void test_load_syscall_filter_set_raw(void) {
a3e2b5
                 assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat + 1), INT_TO_PTR(-1)) >= 0);
a3e2b5
 #endif
a3e2b5
 
a3e2b5
-                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN)) >= 0);
a3e2b5
+                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN), true) >= 0);
a3e2b5
 
a3e2b5
                 assert_se(access("/", F_OK) < 0);
a3e2b5
                 assert_se(errno == EUCLEAN);
a3e2b5
@@ -542,7 +542,7 @@ static void test_load_syscall_filter_set_raw(void) {
a3e2b5
                 assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat + 1), INT_TO_PTR(EILSEQ)) >= 0);
a3e2b5
 #endif
a3e2b5
 
a3e2b5
-                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN)) >= 0);
a3e2b5
+                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN), true) >= 0);
a3e2b5
 
a3e2b5
                 assert_se(access("/", F_OK) < 0);
a3e2b5
                 assert_se(errno == EILSEQ);
a3e2b5
@@ -558,7 +558,7 @@ static void test_load_syscall_filter_set_raw(void) {
a3e2b5
                 assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_ppoll + 1), INT_TO_PTR(-1)) >= 0);
a3e2b5
 #endif
a3e2b5
 
a3e2b5
-                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUNATCH)) >= 0);
a3e2b5
+                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUNATCH), true) >= 0);
a3e2b5
 
a3e2b5
                 assert_se(access("/", F_OK) < 0);
a3e2b5
                 assert_se(errno == EILSEQ);
a3e2b5
@@ -575,7 +575,7 @@ static void test_load_syscall_filter_set_raw(void) {
a3e2b5
                 assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_ppoll + 1), INT_TO_PTR(EILSEQ)) >= 0);
a3e2b5
 #endif
a3e2b5
 
a3e2b5
-                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUNATCH)) >= 0);
a3e2b5
+                assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUNATCH), true) >= 0);
a3e2b5
 
a3e2b5
                 assert_se(access("/", F_OK) < 0);
a3e2b5
                 assert_se(errno == EILSEQ);