8d86bd
From 42ed3377b5817f2c1f84e1bdca301ea51ecc3299 Mon Sep 17 00:00:00 2001
8d86bd
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d86bd
Date: Thu, 20 Sep 2018 14:19:41 +0200
8d86bd
Subject: [PATCH] seccomp: tighten checking of seccomp filter creation
8d86bd
8d86bd
In seccomp code, the code is changed to propagate errors which are about
8d86bd
anything other than unknown/unimplemented syscalls. I *think* such errors
8d86bd
should not happen in normal usage, but so far we would summarilly ignore all
8d86bd
errors, so that part is uncertain. If it turns out that other errors occur and
8d86bd
should be ignored, this should be added later.
8d86bd
8d86bd
In nspawn, we would count the number of added filters, but didn't use this for
8d86bd
anything. Drop that part.
8d86bd
8d86bd
The comments suggested that seccomp_add_syscall_filter_item() returned negative
8d86bd
if the syscall is unknown, but this wasn't true: it returns 0.
8d86bd
8d86bd
The error at this point can only be if the syscall was known but couldn't be
8d86bd
added. If the error comes from our internal whitelist in nspawn, treat this as
8d86bd
error, because it means that our internal table is wrong. If the error comes
8d86bd
from user arguments, warn and ignore. (If some syscall is not known at current
8d86bd
architecture, it is still silently ignored.)
8d86bd
8d86bd
(cherry picked from commit 7e86bd73a47f2b8dd3d9a743e69fb0117f450ad8)
8d86bd
8d86bd
Related: #2040247
8d86bd
---
8d86bd
 src/nspawn/nspawn-seccomp.c | 14 +++++---------
8d86bd
 src/shared/seccomp-util.c   | 26 ++++++++++++++++----------
8d86bd
 2 files changed, 21 insertions(+), 19 deletions(-)
8d86bd
8d86bd
diff --git a/src/nspawn/nspawn-seccomp.c b/src/nspawn/nspawn-seccomp.c
8d86bd
index fba22644da..17abfcec26 100644
8d86bd
--- a/src/nspawn/nspawn-seccomp.c
8d86bd
+++ b/src/nspawn/nspawn-seccomp.c
8d86bd
@@ -140,7 +140,7 @@ static int seccomp_add_default_syscall_filter(
8d86bd
                  */
8d86bd
         };
8d86bd
 
8d86bd
-        int r, c = 0;
8d86bd
+        int r;
8d86bd
         size_t i;
8d86bd
         char **p;
8d86bd
 
8d86bd
@@ -150,21 +150,17 @@ static int seccomp_add_default_syscall_filter(
8d86bd
 
8d86bd
                 r = seccomp_add_syscall_filter_item(ctx, whitelist[i].name, SCMP_ACT_ALLOW, syscall_blacklist, false);
8d86bd
                 if (r < 0)
8d86bd
-                        /* If the system call is not known on this architecture, then that's fine, let's ignore it */
8d86bd
-                        log_debug_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", whitelist[i].name, seccomp_arch_to_string(arch));
8d86bd
-                else
8d86bd
-                        c++;
8d86bd
+                        return log_error_errno(r, "Failed to add syscall filter item %s: %m", whitelist[i].name);
8d86bd
         }
8d86bd
 
8d86bd
         STRV_FOREACH(p, syscall_whitelist) {
8d86bd
                 r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist, false);
8d86bd
                 if (r < 0)
8d86bd
-                        log_debug_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", *p, seccomp_arch_to_string(arch));
8d86bd
-                else
8d86bd
-                        c++;
8d86bd
+                        log_warning_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m",
8d86bd
+                                          *p, seccomp_arch_to_string(arch));
8d86bd
         }
8d86bd
 
8d86bd
-        return c;
8d86bd
+        return 0;
8d86bd
 }
8d86bd
 
8d86bd
 int setup_seccomp(uint64_t cap_list_retain, char **syscall_whitelist, char **syscall_blacklist) {
8d86bd
diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c
8d86bd
index 4d2ba31d47..710a734715 100644
8d86bd
--- a/src/shared/seccomp-util.c
8d86bd
+++ b/src/shared/seccomp-util.c
8d86bd
@@ -907,9 +907,13 @@ int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name,
8d86bd
                 r = seccomp_rule_add_exact(seccomp, action, id, 0);
8d86bd
                 if (r < 0) {
8d86bd
                         /* If the system call is not known on this architecture, then that's fine, let's ignore it */
8d86bd
-                        if (log_missing)
8d86bd
-                                log_debug_errno(r, "Failed to add rule for system call %s() / %d, ignoring: %m",
8d86bd
-                                                name, id);
8d86bd
+                        bool ignore = r == -EDOM;
8d86bd
+
8d86bd
+                        if (!ignore || log_missing)
8d86bd
+                                log_debug_errno(r, "Failed to add rule for system call %s() / %d%s: %m",
8d86bd
+                                                name, id, ignore ? ", ignoring" : "");
8d86bd
+                        if (!ignore)
8d86bd
+                                return r;
8d86bd
                 }
8d86bd
 
8d86bd
                 return 0;
8d86bd
@@ -957,10 +961,8 @@ int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilter
8d86bd
                         return r;
8d86bd
 
8d86bd
                 r = seccomp_add_syscall_filter_set(seccomp, set, action, NULL, log_missing);
8d86bd
-                if (r < 0) {
8d86bd
-                        log_debug_errno(r, "Failed to add filter set, ignoring: %m");
8d86bd
-                        continue;
8d86bd
-                }
8d86bd
+                if (r < 0)
8d86bd
+                        return log_debug_errno(r, "Failed to add filter set: %m");
8d86bd
 
8d86bd
                 r = seccomp_load(seccomp);
8d86bd
                 if (IN_SET(r, -EPERM, -EACCES))
8d86bd
@@ -1005,11 +1007,15 @@ int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, u
8d86bd
                         if (r < 0) {
8d86bd
                                 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
8d86bd
                                 _cleanup_free_ char *n = NULL;
8d86bd
+                                bool ignore;
8d86bd
 
8d86bd
                                 n = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, id);
8d86bd
-                                if (log_missing)
8d86bd
-                                        log_debug_errno(r, "Failed to add rule for system call %s() / %d, ignoring: %m",
8d86bd
-                                                        strna(n), id);
8d86bd
+                                ignore = r == -EDOM;
8d86bd
+                                if (!ignore || log_missing)
8d86bd
+                                        log_debug_errno(r, "Failed to add rule for system call %s() / %d%s: %m",
8d86bd
+                                                        strna(n), id, ignore ? ", ignoring" : "");
8d86bd
+                                if (!ignore)
8d86bd
+                                        return r;
8d86bd
                         }
8d86bd
                 }
8d86bd