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