8d86bd
From 65d64ba146c30a5f205b650381f331fd8db2eb22 Mon Sep 17 00:00:00 2001
8d86bd
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d86bd
Date: Fri, 21 Aug 2020 17:23:48 +0200
8d86bd
Subject: [PATCH] nspawn: return ENOSYS by default, EPERM for "known" calls
8d86bd
8d86bd
(cherry picked from commit 3573e032f26724949e86626eace058d006b8bf70)
8d86bd
8d86bd
Resolves: #2040247
8d86bd
---
8d86bd
 src/nspawn/nspawn-seccomp.c | 20 +++++++++++++++-----
8d86bd
 1 file changed, 15 insertions(+), 5 deletions(-)
8d86bd
8d86bd
diff --git a/src/nspawn/nspawn-seccomp.c b/src/nspawn/nspawn-seccomp.c
8d86bd
index 2b4a65e875..563cda140e 100644
8d86bd
--- a/src/nspawn/nspawn-seccomp.c
8d86bd
+++ b/src/nspawn/nspawn-seccomp.c
8d86bd
@@ -20,7 +20,7 @@
8d86bd
 
8d86bd
 #if HAVE_SECCOMP
8d86bd
 
8d86bd
-static int seccomp_add_default_syscall_filter(
8d86bd
+static int add_syscall_filters(
8d86bd
                 scmp_filter_ctx ctx,
8d86bd
                 uint32_t arch,
8d86bd
                 uint64_t cap_list_retain,
8d86bd
@@ -140,6 +140,7 @@ static int seccomp_add_default_syscall_filter(
8d86bd
                  */
8d86bd
         };
8d86bd
 
8d86bd
+        _cleanup_strv_free_ char **added = NULL;
8d86bd
         int r;
8d86bd
         size_t i;
8d86bd
         char **p;
8d86bd
@@ -153,18 +154,25 @@ static int seccomp_add_default_syscall_filter(
8d86bd
                                                     SCMP_ACT_ALLOW,
8d86bd
                                                     syscall_blacklist,
8d86bd
                                                     false,
8d86bd
-                                                    NULL);
8d86bd
+                                                    &added);
8d86bd
                 if (r < 0)
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, NULL);
8d86bd
+                r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist, true, &added);
8d86bd
                 if (r < 0)
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
+        /* The default action is ENOSYS. Respond with EPERM to all other "known" but not allow-listed
8d86bd
+         * syscalls. */
8d86bd
+        r = seccomp_add_syscall_filter_item(ctx, "@known", SCMP_ACT_ERRNO(EPERM), added, true, NULL);
8d86bd
+        if (r < 0)
8d86bd
+                log_warning_errno(r, "Failed to add rule for @known set on %s, ignoring: %m",
8d86bd
+                                  seccomp_arch_to_string(arch));
8d86bd
+
8d86bd
         return 0;
8d86bd
 }
8d86bd
 
8d86bd
@@ -182,11 +190,13 @@ int setup_seccomp(uint64_t cap_list_retain, char **syscall_whitelist, char **sys
8d86bd
 
8d86bd
                 log_debug("Applying whitelist on architecture: %s", seccomp_arch_to_string(arch));
8d86bd
 
8d86bd
-                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ERRNO(EPERM));
8d86bd
+                /* We install ENOSYS as the default action, but it will only apply to syscalls which are not
8d86bd
+                 * in the @known set, see above. */
8d86bd
+                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ERRNO(ENOSYS));
8d86bd
                 if (r < 0)
8d86bd
                         return log_error_errno(r, "Failed to allocate seccomp object: %m");
8d86bd
 
8d86bd
-                r = seccomp_add_default_syscall_filter(seccomp, arch, cap_list_retain, syscall_whitelist, syscall_blacklist);
8d86bd
+                r = add_syscall_filters(seccomp, arch, cap_list_retain, syscall_whitelist, syscall_blacklist);
8d86bd
                 if (r < 0)
8d86bd
                         return r;
8d86bd