984f77
From f3c6abebbe4718085fcf17ed3ab0690e379fbb7e Mon Sep 17 00:00:00 2001
984f77
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
984f77
Date: Wed, 19 Aug 2020 17:43:23 +0200
984f77
Subject: [PATCH] shared: add @known syscall list
984f77
984f77
(cherry picked from commit 95aac01259db689dac7d8e5bfafb60e8c70cd734)
984f77
984f77
Related: #2040247
984f77
---
984f77
 man/systemd.exec.xml                |  4 ++++
984f77
 src/shared/generate-syscall-list.py |  5 +++++
984f77
 src/shared/meson.build              | 11 +++++++++++
984f77
 src/shared/seccomp-util.c           |  6 ++++++
984f77
 src/shared/seccomp-util.h           |  3 ++-
984f77
 src/test/test-seccomp.c             | 19 ++++++++++++-------
984f77
 6 files changed, 40 insertions(+), 8 deletions(-)
984f77
 create mode 100755 src/shared/generate-syscall-list.py
984f77
984f77
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
984f77
index dc88cf9781..b04b4ba552 100644
984f77
--- a/man/systemd.exec.xml
984f77
+++ b/man/systemd.exec.xml
984f77
@@ -1556,6 +1556,10 @@ RestrictNamespaces=~cgroup net</programlisting>
984f77
                 <entry>@timer</entry>
984f77
                 <entry>System calls for scheduling operations by time (<citerefentry project='man-pages'><refentrytitle>alarm</refentrytitle><manvolnum>2</manvolnum></citerefentry>, <citerefentry project='man-pages'><refentrytitle>timer_create</refentrytitle><manvolnum>2</manvolnum></citerefentry>, …)</entry>
984f77
               </row>
984f77
+              <row>
984f77
+                <entry>@known</entry>
984f77
+                <entry>All system calls defined by the kernel. This list is defined statically in systemd based on a kernel version that was available when this systmed version was released. It will become progressively more out-of-date as the kernel is updated.</entry>
984f77
+              </row>
984f77
             
984f77
           </tgroup>
984f77
         
984f77
diff --git a/src/shared/generate-syscall-list.py b/src/shared/generate-syscall-list.py
984f77
new file mode 100755
984f77
index 0000000000..13a6ae9241
984f77
--- /dev/null
984f77
+++ b/src/shared/generate-syscall-list.py
984f77
@@ -0,0 +1,5 @@
984f77
+#!/usr/bin/env python
984f77
+import sys
984f77
+
984f77
+for line in open(sys.argv[1]):
984f77
+    print('"{}\\0"'.format(line.strip()))
984f77
diff --git a/src/shared/meson.build b/src/shared/meson.build
984f77
index c9dd0a3a4e..fed08571d1 100644
984f77
--- a/src/shared/meson.build
984f77
+++ b/src/shared/meson.build
984f77
@@ -109,6 +109,16 @@ shared_sources = files('''
984f77
 test_tables_h = files('test-tables.h')
984f77
 shared_sources += [test_tables_h]
984f77
 
984f77
+generate_syscall_list = find_program('generate-syscall-list.py')
984f77
+fname = 'syscall-list.h'
984f77
+syscall_list_h = custom_target(
984f77
+        fname,
984f77
+        input : 'syscall-names.text',
984f77
+        output : fname,
984f77
+        command : [generate_syscall_list,
984f77
+                   '@INPUT@'],
984f77
+        capture : true)
984f77
+
984f77
 if conf.get('HAVE_ACL') == 1
984f77
         shared_sources += files('acl-util.c')
984f77
 endif
984f77
@@ -119,6 +129,7 @@ endif
984f77
 
984f77
 if conf.get('HAVE_SECCOMP') == 1
984f77
         shared_sources += files('seccomp-util.c')
984f77
+        shared_sources += syscall_list_h
984f77
 endif
984f77
 
984f77
 if conf.get('HAVE_LIBIPTC') == 1
984f77
diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c
984f77
index c57c409433..c2b2f2da92 100644
984f77
--- a/src/shared/seccomp-util.c
984f77
+++ b/src/shared/seccomp-util.c
984f77
@@ -855,6 +855,12 @@ const SyscallFilterSet syscall_filter_sets[_SYSCALL_FILTER_SET_MAX] = {
984f77
                 "timerfd_settime\0"
984f77
                 "times\0"
984f77
         },
984f77
+        [SYSCALL_FILTER_SET_KNOWN] = {
984f77
+                .name = "@known",
984f77
+                .help = "All known syscalls declared in the kernel",
984f77
+                .value =
984f77
+#include "syscall-list.h"
984f77
+        },
984f77
 };
984f77
 
984f77
 const SyscallFilterSet *syscall_filter_set_find(const char *name) {
984f77
diff --git a/src/shared/seccomp-util.h b/src/shared/seccomp-util.h
984f77
index 602f092255..541ba1e067 100644
984f77
--- a/src/shared/seccomp-util.h
984f77
+++ b/src/shared/seccomp-util.h
984f77
@@ -21,7 +21,7 @@ typedef struct SyscallFilterSet {
984f77
 } SyscallFilterSet;
984f77
 
984f77
 enum {
984f77
-        /* Please leave DEFAULT first, but sort the rest alphabetically */
984f77
+        /* Please leave DEFAULT first and KNOWN last, but sort the rest alphabetically */
984f77
         SYSCALL_FILTER_SET_DEFAULT,
984f77
         SYSCALL_FILTER_SET_AIO,
984f77
         SYSCALL_FILTER_SET_BASIC_IO,
984f77
@@ -49,6 +49,7 @@ enum {
984f77
         SYSCALL_FILTER_SET_SYNC,
984f77
         SYSCALL_FILTER_SET_SYSTEM_SERVICE,
984f77
         SYSCALL_FILTER_SET_TIMER,
984f77
+        SYSCALL_FILTER_SET_KNOWN,
984f77
         _SYSCALL_FILTER_SET_MAX
984f77
 };
984f77
 
984f77
diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c
984f77
index 6ec04c4c55..286f01b5ce 100644
984f77
--- a/src/test/test-seccomp.c
984f77
+++ b/src/test/test-seccomp.c
984f77
@@ -106,8 +106,10 @@ static void test_filter_sets(void) {
984f77
                 if (pid == 0) { /* Child? */
984f77
                         int fd;
984f77
 
984f77
-                        /* If we look at the default set (or one that includes it), whitelist instead of blacklist */
984f77
-                        if (IN_SET(i, SYSCALL_FILTER_SET_DEFAULT, SYSCALL_FILTER_SET_SYSTEM_SERVICE))
984f77
+                        /* If we look at the default set (or one that includes it), allow-list instead of deny-list */
984f77
+                        if (IN_SET(i, SYSCALL_FILTER_SET_DEFAULT,
984f77
+                                      SYSCALL_FILTER_SET_SYSTEM_SERVICE,
984f77
+                                      SYSCALL_FILTER_SET_KNOWN))
984f77
                                 r = seccomp_load_syscall_filter_set(SCMP_ACT_ERRNO(EUCLEAN), syscall_filter_sets + i, SCMP_ACT_ALLOW, true);
984f77
                         else
984f77
                                 r = seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + i, SCMP_ACT_ERRNO(EUCLEAN), true);
984f77
@@ -639,20 +641,23 @@ static void test_lock_personality(void) {
984f77
 }
984f77
 
984f77
 static void test_filter_sets_ordered(void) {
984f77
-        size_t i;
984f77
-
984f77
         /* Ensure "@default" always remains at the beginning of the list */
984f77
         assert_se(SYSCALL_FILTER_SET_DEFAULT == 0);
984f77
         assert_se(streq(syscall_filter_sets[0].name, "@default"));
984f77
 
984f77
-        for (i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) {
984f77
+        /* Ensure "@known" always remains at the end of the list */
984f77
+        assert_se(SYSCALL_FILTER_SET_KNOWN == _SYSCALL_FILTER_SET_MAX - 1);
984f77
+        assert_se(streq(syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].name, "@known"));
984f77
+
984f77
+        for (size_t i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) {
984f77
                 const char *k, *p = NULL;
984f77
 
984f77
                 /* Make sure each group has a description */
984f77
                 assert_se(!isempty(syscall_filter_sets[0].help));
984f77
 
984f77
-                /* Make sure the groups are ordered alphabetically, except for the first entry */
984f77
-                assert_se(i < 2 || strcmp(syscall_filter_sets[i-1].name, syscall_filter_sets[i].name) < 0);
984f77
+                /* Make sure the groups are ordered alphabetically, except for the first and last entries */
984f77
+                assert_se(i < 2 || i == _SYSCALL_FILTER_SET_MAX - 1 ||
984f77
+                          strcmp(syscall_filter_sets[i-1].name, syscall_filter_sets[i].name) < 0);
984f77
 
984f77
                 NULSTR_FOREACH(k, syscall_filter_sets[i].value) {
984f77