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