698723
From e706f5df66b7189a7df526aeeb45c86b8c4b057a Mon Sep 17 00:00:00 2001
698723
From: Lennart Poettering <lennart@poettering.net>
698723
Date: Mon, 2 Nov 2020 14:51:10 +0100
698723
Subject: [PATCH] seccomp: allow turning off of seccomp filtering via env var
698723
698723
Fixes: #17504
698723
698723
(While we are it, also move $SYSTEMD_SECCOMP_LOG= env var description
698723
into the right document section)
698723
698723
Also suggested in: https://github.com/systemd/systemd/issues/17245#issuecomment-704773603
698723
698723
(cherry picked from commit ce8f6d478e3f6c6a313fb19615aa5029bb18f86d)
698723
698723
Resolves: #1916835
698723
---
698723
 doc/ENVIRONMENT.md          |  3 +++
698723
 src/nspawn/nspawn-seccomp.c |  2 +-
698723
 src/shared/seccomp-util.c   | 19 +++++++++++++++----
698723
 3 files changed, 19 insertions(+), 5 deletions(-)
698723
698723
diff --git a/doc/ENVIRONMENT.md b/doc/ENVIRONMENT.md
698723
index 0e763b6302..36b649afe1 100644
698723
--- a/doc/ENVIRONMENT.md
698723
+++ b/doc/ENVIRONMENT.md
698723
@@ -117,3 +117,6 @@ systemd-sulogin-shell:
698723
 * `$SYSTEMD_SULOGIN_FORCE=1` — This skips asking for the root password if the
698723
   root password is not available (such as when the root account is locked).
698723
   See `sulogin(8)` for more details.
698723
+
698723
+* `$SYSTEMD_SECCOMP=0` – if set, seccomp filters will not be enforced, even if
698723
+  support for it is compiled in and available in the kernel.
698723
diff --git a/src/nspawn/nspawn-seccomp.c b/src/nspawn/nspawn-seccomp.c
698723
index b56c5b04a8..fba22644da 100644
698723
--- a/src/nspawn/nspawn-seccomp.c
698723
+++ b/src/nspawn/nspawn-seccomp.c
698723
@@ -172,7 +172,7 @@ int setup_seccomp(uint64_t cap_list_retain, char **syscall_whitelist, char **sys
698723
         int r;
698723
 
698723
         if (!is_seccomp_available()) {
698723
-                log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP filterering");
698723
+                log_debug("SECCOMP features not detected in the kernel or disabled at runtime, disabling SECCOMP filtering");
698723
                 return 0;
698723
         }
698723
 
698723
diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c
698723
index d91fb4e269..e903512d45 100644
698723
--- a/src/shared/seccomp-util.c
698723
+++ b/src/shared/seccomp-util.c
698723
@@ -12,6 +12,7 @@
698723
 
698723
 #include "af-list.h"
698723
 #include "alloc-util.h"
698723
+#include "env-util.h"
698723
 #include "macro.h"
698723
 #include "nsflags.h"
698723
 #include "process-util.h"
698723
@@ -244,10 +245,20 @@ static bool is_seccomp_filter_available(void) {
698723
 bool is_seccomp_available(void) {
698723
         static int cached_enabled = -1;
698723
 
698723
-        if (cached_enabled < 0)
698723
-                cached_enabled =
698723
-                        is_basic_seccomp_available() &&
698723
-                        is_seccomp_filter_available();
698723
+        if (cached_enabled < 0) {
698723
+                int b;
698723
+
698723
+                b = getenv_bool("SYSTEMD_SECCOMP");
698723
+                if (b != 0) {
698723
+                        if (b < 0 && b != -ENXIO) /* ENXIO: env var unset */
698723
+                                log_debug_errno(b, "Failed to parse $SYSTEMD_SECCOMP value, ignoring.");
698723
+
698723
+                        cached_enabled =
698723
+                                is_basic_seccomp_available() &&
698723
+                                is_seccomp_filter_available();
698723
+                } else
698723
+                        cached_enabled = false;
698723
+        }
698723
 
698723
         return cached_enabled;
698723
 }