984f77
From eaad892c513806801e3d2055788fa202372b3f15 Mon Sep 17 00:00:00 2001
984f77
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
984f77
Date: Fri, 21 Aug 2020 17:21:04 +0200
984f77
Subject: [PATCH] shared/seccomp-util: added functionality to make list of
984f77
 filtred syscalls
984f77
984f77
While at it, start removing the "seccomp_" prefix from our
984f77
own functions. It is used by libseccomp.
984f77
984f77
(cherry picked from commit 000c05207d68658b76af9e1caf9aa3a4e3fa697b)
984f77
984f77
Related: #2040247
984f77
---
984f77
 src/nspawn/nspawn-seccomp.c |  9 +++++++--
984f77
 src/shared/seccomp-util.c   | 39 ++++++++++++++++++++++++++++++-------
984f77
 src/shared/seccomp-util.h   |  8 +++++++-
984f77
 3 files changed, 46 insertions(+), 10 deletions(-)
984f77
984f77
diff --git a/src/nspawn/nspawn-seccomp.c b/src/nspawn/nspawn-seccomp.c
984f77
index 17abfcec26..2b4a65e875 100644
984f77
--- a/src/nspawn/nspawn-seccomp.c
984f77
+++ b/src/nspawn/nspawn-seccomp.c
984f77
@@ -148,13 +148,18 @@ static int seccomp_add_default_syscall_filter(
984f77
                 if (whitelist[i].capability != 0 && (cap_list_retain & (1ULL << whitelist[i].capability)) == 0)
984f77
                         continue;
984f77
 
984f77
-                r = seccomp_add_syscall_filter_item(ctx, whitelist[i].name, SCMP_ACT_ALLOW, syscall_blacklist, false);
984f77
+                r = seccomp_add_syscall_filter_item(ctx,
984f77
+                                                    whitelist[i].name,
984f77
+                                                    SCMP_ACT_ALLOW,
984f77
+                                                    syscall_blacklist,
984f77
+                                                    false,
984f77
+                                                    NULL);
984f77
                 if (r < 0)
984f77
                         return log_error_errno(r, "Failed to add syscall filter item %s: %m", whitelist[i].name);
984f77
         }
984f77
 
984f77
         STRV_FOREACH(p, syscall_whitelist) {
984f77
-                r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist, false);
984f77
+                r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist, false, NULL);
984f77
                 if (r < 0)
984f77
                         log_warning_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m",
984f77
                                           *p, seccomp_arch_to_string(arch));
984f77
diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c
984f77
index 710a734715..56075d92e0 100644
984f77
--- a/src/shared/seccomp-util.c
984f77
+++ b/src/shared/seccomp-util.c
984f77
@@ -874,15 +874,31 @@ const SyscallFilterSet *syscall_filter_set_find(const char *name) {
984f77
         return NULL;
984f77
 }
984f77
 
984f77
-static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp, const SyscallFilterSet *set, uint32_t action, char **exclude, bool log_missing);
984f77
+static int add_syscall_filter_set(
984f77
+                scmp_filter_ctx seccomp,
984f77
+                const SyscallFilterSet *set,
984f77
+                uint32_t action,
984f77
+                char **exclude,
984f77
+                bool log_missing,
984f77
+                char ***added);
984f77
+
984f77
+int seccomp_add_syscall_filter_item(
984f77
+                scmp_filter_ctx *seccomp,
984f77
+                const char *name,
984f77
+                uint32_t action,
984f77
+                char **exclude,
984f77
+                bool log_missing,
984f77
+                char ***added) {
984f77
 
984f77
-int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name, uint32_t action, char **exclude, bool log_missing) {
984f77
         assert(seccomp);
984f77
         assert(name);
984f77
 
984f77
         if (strv_contains(exclude, name))
984f77
                 return 0;
984f77
 
984f77
+        /* Any syscalls that are handled are added to the *added strv. The pointer
984f77
+         * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
984f77
+
984f77
         if (name[0] == '@') {
984f77
                 const SyscallFilterSet *other;
984f77
 
984f77
@@ -892,7 +908,7 @@ int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name,
984f77
                         return -EINVAL;
984f77
                 }
984f77
 
984f77
-                return seccomp_add_syscall_filter_set(seccomp, other, action, exclude, log_missing);
984f77
+                return add_syscall_filter_set(seccomp, other, action, exclude, log_missing, added);
984f77
 
984f77
         } else {
984f77
                 int id, r;
984f77
@@ -916,25 +932,34 @@ int seccomp_add_syscall_filter_item(scmp_filter_ctx *seccomp, const char *name,
984f77
                                 return r;
984f77
                 }
984f77
 
984f77
+                if (added) {
984f77
+                        r = strv_extend(added, name);
984f77
+                        if (r < 0)
984f77
+                                return r;
984f77
+                }
984f77
+
984f77
                 return 0;
984f77
         }
984f77
 }
984f77
 
984f77
-static int seccomp_add_syscall_filter_set(
984f77
+static int add_syscall_filter_set(
984f77
                 scmp_filter_ctx seccomp,
984f77
                 const SyscallFilterSet *set,
984f77
                 uint32_t action,
984f77
                 char **exclude,
984f77
-                bool log_missing) {
984f77
+                bool log_missing,
984f77
+                char ***added) {
984f77
 
984f77
         const char *sys;
984f77
         int r;
984f77
 
984f77
+        /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
984f77
+
984f77
         assert(seccomp);
984f77
         assert(set);
984f77
 
984f77
         NULSTR_FOREACH(sys, set->value) {
984f77
-                r = seccomp_add_syscall_filter_item(seccomp, sys, action, exclude, log_missing);
984f77
+                r = seccomp_add_syscall_filter_item(seccomp, sys, action, exclude, log_missing, added);
984f77
                 if (r < 0)
984f77
                         return r;
984f77
         }
984f77
@@ -960,7 +985,7 @@ int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilter
984f77
                 if (r < 0)
984f77
                         return r;
984f77
 
984f77
-                r = seccomp_add_syscall_filter_set(seccomp, set, action, NULL, log_missing);
984f77
+                r = add_syscall_filter_set(seccomp, set, action, NULL, log_missing, NULL);
984f77
                 if (r < 0)
984f77
                         return log_debug_errno(r, "Failed to add filter set: %m");
984f77
 
984f77
diff --git a/src/shared/seccomp-util.h b/src/shared/seccomp-util.h
984f77
index 541ba1e067..291b2bffe0 100644
984f77
--- a/src/shared/seccomp-util.h
984f77
+++ b/src/shared/seccomp-util.h
984f77
@@ -59,7 +59,13 @@ const SyscallFilterSet *syscall_filter_set_find(const char *name);
984f77
 
984f77
 int seccomp_filter_set_add(Hashmap *s, bool b, const SyscallFilterSet *set);
984f77
 
984f77
-int seccomp_add_syscall_filter_item(scmp_filter_ctx *ctx, const char *name, uint32_t action, char **exclude, bool log_missing);
984f77
+int seccomp_add_syscall_filter_item(
984f77
+                scmp_filter_ctx *ctx,
984f77
+                const char *name,
984f77
+                uint32_t action,
984f77
+                char **exclude,
984f77
+                bool log_missing,
984f77
+                char ***added);
984f77
 
984f77
 int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action, bool log_missing);
984f77
 int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* set, uint32_t action, bool log_missing);