Blame SOURCES/flatpak-1.0.9-fix-CVE-2021-41133.patch

b4368f
From 6d8318a8d4fd82a5b75b6d2c595f54e54310ebd6 Mon Sep 17 00:00:00 2001
b4368f
From: Matthew Leeds <matthew.leeds@endlessm.com>
b4368f
Date: Mon, 8 Jun 2020 18:38:12 -0700
b4368f
Subject: [PATCH 01/13] tree-wide: Replace usages of whitelist/blacklist
b4368f
b4368f
The terms whitelist and blacklist are hurtful to some people, and per
b4368f
our code of conduct Flatpak is an inclusive community. Replace them with
b4368f
allowlist and blocklist which are also more clear. This terminology
b4368f
change is being implemented more broadly in the software industry; see
b4368f
e.g. https://go-review.googlesource.com/c/go/+/236857/
b4368f
b4368f
[Backported to 1.2.x to make subsequent security fixes apply without
b4368f
conflicts: don't touch the documentation, only the code. -smcv]
b4368f
b4368f
(cherry picked from commit a994cdb30e78c52d10a5c86bcc86783b86d11648)
b4368f
(cherry picked from commit 9776116698f0fdb9abc4c278aeb8b89ce8303d46)
b4368f
---
b4368f
 common/flatpak-run.c | 42 +++++++++++++++++++++---------------------
b4368f
 1 file changed, 21 insertions(+), 21 deletions(-)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index ea5571bd489b..1098ea7204fe 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2082,8 +2082,8 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
    * can do, and we should support code portability between different
b4368f
    * container tools.
b4368f
    *
b4368f
-   * This syscall blacklist is copied from linux-user-chroot, which was in turn
b4368f
-   * clearly influenced by the Sandstorm.io blacklist.
b4368f
+   * This syscall blocklist is copied from linux-user-chroot, which was in turn
b4368f
+   * clearly influenced by the Sandstorm.io blocklist.
b4368f
    *
b4368f
    * If you make any changes here, I suggest sending the changes along
b4368f
    * to other sandbox maintainers.  Using the libseccomp list is also
b4368f
@@ -2091,7 +2091,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
    * https://groups.google.com/forum/#!topic/libseccomp
b4368f
    *
b4368f
    * A non-exhaustive list of links to container tooling that might
b4368f
-   * want to share this blacklist:
b4368f
+   * want to share this blocklist:
b4368f
    *
b4368f
    *  https://github.com/sandstorm-io/sandstorm
b4368f
    *    in src/sandstorm/supervisor.c++
b4368f
@@ -2106,7 +2106,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
   {
b4368f
     int                  scall;
b4368f
     struct scmp_arg_cmp *arg;
b4368f
-  } syscall_blacklist[] = {
b4368f
+  } syscall_blocklist[] = {
b4368f
     /* Block dmesg */
b4368f
     {SCMP_SYS (syslog)},
b4368f
     /* Useless old syscall */
b4368f
@@ -2145,7 +2145,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
   {
b4368f
     int                  scall;
b4368f
     struct scmp_arg_cmp *arg;
b4368f
-  } syscall_nondevel_blacklist[] = {
b4368f
+  } syscall_nondevel_blocklist[] = {
b4368f
     /* Profiling operations; we expect these to be done by tools from outside
b4368f
      * the sandbox.  In particular perf has been the source of many CVEs.
b4368f
      */
b4368f
@@ -2154,12 +2154,12 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
     {SCMP_SYS (personality), &SCMP_A0 (SCMP_CMP_NE, allowed_personality)},
b4368f
     {SCMP_SYS (ptrace)}
b4368f
   };
b4368f
-  /* Blacklist all but unix, inet, inet6 and netlink */
b4368f
+  /* Blocklist all but unix, inet, inet6 and netlink */
b4368f
   struct
b4368f
   {
b4368f
     int             family;
b4368f
     FlatpakRunFlags flags_mask;
b4368f
-  } socket_family_whitelist[] = {
b4368f
+  } socket_family_allowlist[] = {
b4368f
     /* NOTE: Keep in numerical order */
b4368f
     { AF_UNSPEC, 0 },
b4368f
     { AF_LOCAL, 0 },
b4368f
@@ -2234,11 +2234,11 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
    * leak system stuff or secrets from other apps.
b4368f
    */
b4368f
 
b4368f
-  for (i = 0; i < G_N_ELEMENTS (syscall_blacklist); i++)
b4368f
+  for (i = 0; i < G_N_ELEMENTS (syscall_blocklist); i++)
b4368f
     {
b4368f
-      int scall = syscall_blacklist[i].scall;
b4368f
-      if (syscall_blacklist[i].arg)
b4368f
-        r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 1, *syscall_blacklist[i].arg);
b4368f
+      int scall = syscall_blocklist[i].scall;
b4368f
+      if (syscall_blocklist[i].arg)
b4368f
+        r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 1, *syscall_blocklist[i].arg);
b4368f
       else
b4368f
         r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 0);
b4368f
       if (r < 0 && r == -EFAULT /* unknown syscall */)
b4368f
@@ -2247,11 +2247,11 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
 
b4368f
   if (!devel)
b4368f
     {
b4368f
-      for (i = 0; i < G_N_ELEMENTS (syscall_nondevel_blacklist); i++)
b4368f
+      for (i = 0; i < G_N_ELEMENTS (syscall_nondevel_blocklist); i++)
b4368f
         {
b4368f
-          int scall = syscall_nondevel_blacklist[i].scall;
b4368f
-          if (syscall_nondevel_blacklist[i].arg)
b4368f
-            r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 1, *syscall_nondevel_blacklist[i].arg);
b4368f
+          int scall = syscall_nondevel_blocklist[i].scall;
b4368f
+          if (syscall_nondevel_blocklist[i].arg)
b4368f
+            r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 1, *syscall_nondevel_blocklist[i].arg);
b4368f
           else
b4368f
             r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 0);
b4368f
 
b4368f
@@ -2264,23 +2264,23 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
    * However, we need to user seccomp_rule_add_exact to avoid libseccomp doing
b4368f
    * something else: https://github.com/seccomp/libseccomp/issues/8 */
b4368f
   last_allowed_family = -1;
b4368f
-  for (i = 0; i < G_N_ELEMENTS (socket_family_whitelist); i++)
b4368f
+  for (i = 0; i < G_N_ELEMENTS (socket_family_allowlist); i++)
b4368f
     {
b4368f
-      int family = socket_family_whitelist[i].family;
b4368f
+      int family = socket_family_allowlist[i].family;
b4368f
       int disallowed;
b4368f
 
b4368f
-      if (socket_family_whitelist[i].flags_mask != 0 &&
b4368f
-          (socket_family_whitelist[i].flags_mask & run_flags) != socket_family_whitelist[i].flags_mask)
b4368f
+      if (socket_family_allowlist[i].flags_mask != 0 &&
b4368f
+          (socket_family_allowlist[i].flags_mask & run_flags) != socket_family_allowlist[i].flags_mask)
b4368f
         continue;
b4368f
 
b4368f
       for (disallowed = last_allowed_family + 1; disallowed < family; disallowed++)
b4368f
         {
b4368f
-          /* Blacklist the in-between valid families */
b4368f
+          /* Blocklist the in-between valid families */
b4368f
           seccomp_rule_add_exact (seccomp, SCMP_ACT_ERRNO (EAFNOSUPPORT), SCMP_SYS (socket), 1, SCMP_A0 (SCMP_CMP_EQ, disallowed));
b4368f
         }
b4368f
       last_allowed_family = family;
b4368f
     }
b4368f
-  /* Blacklist the rest */
b4368f
+  /* Blocklist the rest */
b4368f
   seccomp_rule_add_exact (seccomp, SCMP_ACT_ERRNO (EAFNOSUPPORT), SCMP_SYS (socket), 1, SCMP_A0 (SCMP_CMP_GE, last_allowed_family + 1));
b4368f
 
b4368f
   if (!glnx_open_anonymous_tmpfile (O_RDWR | O_CLOEXEC, &seccomp_tmpf, error))
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 9f578cfb5b5cf3cdeb91b79f7dd9076bd2862830 Mon Sep 17 00:00:00 2001
b4368f
From: Julian Andres Klode <julian.klode@canonical.com>
b4368f
Date: Wed, 5 Aug 2020 16:28:50 +0200
b4368f
Subject: [PATCH 02/13] Fix argument order of clone() for s390x in seccomp
b4368f
 filter
b4368f
b4368f
clone() is a mad syscall with about 4 different argument orders. While
b4368f
most of them agree that argument 0 is flags, s390 and s390x have the
b4368f
flags argument second - A0 is the child stack pointer there.
b4368f
b4368f
[smcv: Add an explanatory comment; also test __CRIS__ for completeness]
b4368f
b4368f
Bug-Debian: https://bugs.debian.org/964541
b4368f
Bug-Ubuntu: https://launchpad.net/bugs/1886814
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 8ba141c38f85c8ad82d0ad6d9bde503ec4a971b6)
b4368f
(cherry picked from commit ad32f848d5b7126a16f15fbfe0ec0a1e4f4b66c3)
b4368f
---
b4368f
 common/flatpak-run.c | 7 +++++++
b4368f
 1 file changed, 7 insertions(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 1098ea7204fe..50dab684b050 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2135,7 +2135,14 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
     {SCMP_SYS (unshare)},
b4368f
     {SCMP_SYS (mount)},
b4368f
     {SCMP_SYS (pivot_root)},
b4368f
+#if defined(__s390__) || defined(__s390x__) || defined(__CRIS__)
b4368f
+    /* Architectures with CONFIG_CLONE_BACKWARDS2: the child stack
b4368f
+     * and flags arguments are reversed so the flags come second */
b4368f
+    {SCMP_SYS (clone), &SCMP_A1 (SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER)},
b4368f
+#else
b4368f
+    /* Normally the flags come first */
b4368f
     {SCMP_SYS (clone), &SCMP_A0 (SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER)},
b4368f
+#endif
b4368f
 
b4368f
     /* Don't allow faking input to the controlling tty (CVE-2017-5226) */
b4368f
     {SCMP_SYS (ioctl), &SCMP_A1 (SCMP_CMP_MASKED_EQ, 0xFFFFFFFFu, (int) TIOCSTI)},
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From f41f46aaf57bcecaeb0885d7cbf6a33ab2cf3ca6 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 11:53:23 +0100
b4368f
Subject: [PATCH 03/13] run: Add an errno value to seccomp filters
b4368f
b4368f
At the moment, if we block a syscall we always make it fail with EPERM,
b4368f
but this is risky: user-space libraries can start to use new replacements
b4368f
for old syscalls at any time, and will often treat EPERM as a fatal error.
b4368f
For new syscalls, we should make the syscall fail with ENOSYS, which is
b4368f
indistinguishable from running on an older kernel and will cause fallback
b4368f
to an older implementation, for example clone3() to clone().
b4368f
b4368f
In future we should probably move from EPERM to ENOSYS for some of the
b4368f
syscalls we already block, but for now keep the status quo.
b4368f
b4368f
This is a prerequisite for fixing the vulnerability tracked as
b4368f
GHSA-67h7-w3jq-vh4q.
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit e26ac7586c392b5eb35ff4609fe232c52523b2cf)
b4368f
(cherry picked from commit fa00b38504ebef43dec74dee2e91af837f4bc7da)
b4368f
---
b4368f
 common/flatpak-run.c | 62 +++++++++++++++++++++++++-------------------
b4368f
 1 file changed, 36 insertions(+), 26 deletions(-)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 50dab684b050..f7f40100bd2b 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2105,61 +2105,63 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
   struct
b4368f
   {
b4368f
     int                  scall;
b4368f
+    int                  errnum;
b4368f
     struct scmp_arg_cmp *arg;
b4368f
   } syscall_blocklist[] = {
b4368f
     /* Block dmesg */
b4368f
-    {SCMP_SYS (syslog)},
b4368f
+    {SCMP_SYS (syslog), EPERM},
b4368f
     /* Useless old syscall */
b4368f
-    {SCMP_SYS (uselib)},
b4368f
+    {SCMP_SYS (uselib), EPERM},
b4368f
     /* Don't allow disabling accounting */
b4368f
-    {SCMP_SYS (acct)},
b4368f
+    {SCMP_SYS (acct), EPERM},
b4368f
     /* 16-bit code is unnecessary in the sandbox, and modify_ldt is a
b4368f
        historic source of interesting information leaks. */
b4368f
-    {SCMP_SYS (modify_ldt)},
b4368f
+    {SCMP_SYS (modify_ldt), EPERM},
b4368f
     /* Don't allow reading current quota use */
b4368f
-    {SCMP_SYS (quotactl)},
b4368f
+    {SCMP_SYS (quotactl), EPERM},
b4368f
 
b4368f
     /* Don't allow access to the kernel keyring */
b4368f
-    {SCMP_SYS (add_key)},
b4368f
-    {SCMP_SYS (keyctl)},
b4368f
-    {SCMP_SYS (request_key)},
b4368f
+    {SCMP_SYS (add_key), EPERM},
b4368f
+    {SCMP_SYS (keyctl), EPERM},
b4368f
+    {SCMP_SYS (request_key), EPERM},
b4368f
 
b4368f
     /* Scary VM/NUMA ops */
b4368f
-    {SCMP_SYS (move_pages)},
b4368f
-    {SCMP_SYS (mbind)},
b4368f
-    {SCMP_SYS (get_mempolicy)},
b4368f
-    {SCMP_SYS (set_mempolicy)},
b4368f
-    {SCMP_SYS (migrate_pages)},
b4368f
+    {SCMP_SYS (move_pages), EPERM},
b4368f
+    {SCMP_SYS (mbind), EPERM},
b4368f
+    {SCMP_SYS (get_mempolicy), EPERM},
b4368f
+    {SCMP_SYS (set_mempolicy), EPERM},
b4368f
+    {SCMP_SYS (migrate_pages), EPERM},
b4368f
 
b4368f
     /* Don't allow subnamespace setups: */
b4368f
-    {SCMP_SYS (unshare)},
b4368f
-    {SCMP_SYS (mount)},
b4368f
-    {SCMP_SYS (pivot_root)},
b4368f
+    {SCMP_SYS (unshare), EPERM},
b4368f
+    {SCMP_SYS (mount), EPERM},
b4368f
+    {SCMP_SYS (pivot_root), EPERM},
b4368f
 #if defined(__s390__) || defined(__s390x__) || defined(__CRIS__)
b4368f
     /* Architectures with CONFIG_CLONE_BACKWARDS2: the child stack
b4368f
      * and flags arguments are reversed so the flags come second */
b4368f
-    {SCMP_SYS (clone), &SCMP_A1 (SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER)},
b4368f
+    {SCMP_SYS (clone), EPERM, &SCMP_A1 (SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER)},
b4368f
 #else
b4368f
     /* Normally the flags come first */
b4368f
-    {SCMP_SYS (clone), &SCMP_A0 (SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER)},
b4368f
+    {SCMP_SYS (clone), EPERM, &SCMP_A0 (SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER)},
b4368f
 #endif
b4368f
 
b4368f
     /* Don't allow faking input to the controlling tty (CVE-2017-5226) */
b4368f
-    {SCMP_SYS (ioctl), &SCMP_A1 (SCMP_CMP_MASKED_EQ, 0xFFFFFFFFu, (int) TIOCSTI)},
b4368f
+    {SCMP_SYS (ioctl), EPERM, &SCMP_A1 (SCMP_CMP_MASKED_EQ, 0xFFFFFFFFu, (int) TIOCSTI)},
b4368f
   };
b4368f
 
b4368f
   struct
b4368f
   {
b4368f
     int                  scall;
b4368f
+    int                  errnum;
b4368f
     struct scmp_arg_cmp *arg;
b4368f
   } syscall_nondevel_blocklist[] = {
b4368f
     /* Profiling operations; we expect these to be done by tools from outside
b4368f
      * the sandbox.  In particular perf has been the source of many CVEs.
b4368f
      */
b4368f
-    {SCMP_SYS (perf_event_open)},
b4368f
+    {SCMP_SYS (perf_event_open), EPERM},
b4368f
     /* Don't allow you to switch to bsd emulation or whatnot */
b4368f
-    {SCMP_SYS (personality), &SCMP_A0 (SCMP_CMP_NE, allowed_personality)},
b4368f
-    {SCMP_SYS (ptrace)}
b4368f
+    {SCMP_SYS (personality), EPERM, &SCMP_A0 (SCMP_CMP_NE, allowed_personality)},
b4368f
+    {SCMP_SYS (ptrace), EPERM}
b4368f
   };
b4368f
   /* Blocklist all but unix, inet, inet6 and netlink */
b4368f
   struct
b4368f
@@ -2244,10 +2246,14 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
   for (i = 0; i < G_N_ELEMENTS (syscall_blocklist); i++)
b4368f
     {
b4368f
       int scall = syscall_blocklist[i].scall;
b4368f
+      int errnum = syscall_blocklist[i].errnum;
b4368f
+
b4368f
+      g_return_val_if_fail (errnum == EPERM || errnum == ENOSYS, FALSE);
b4368f
+
b4368f
       if (syscall_blocklist[i].arg)
b4368f
-        r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 1, *syscall_blocklist[i].arg);
b4368f
+        r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 1, *syscall_blocklist[i].arg);
b4368f
       else
b4368f
-        r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 0);
b4368f
+        r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 0);
b4368f
       if (r < 0 && r == -EFAULT /* unknown syscall */)
b4368f
         return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d"), scall);
b4368f
     }
b4368f
@@ -2257,10 +2263,14 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
       for (i = 0; i < G_N_ELEMENTS (syscall_nondevel_blocklist); i++)
b4368f
         {
b4368f
           int scall = syscall_nondevel_blocklist[i].scall;
b4368f
+          int errnum = syscall_nondevel_blocklist[i].errnum;
b4368f
+
b4368f
+          g_return_val_if_fail (errnum == EPERM || errnum == ENOSYS, FALSE);
b4368f
+
b4368f
           if (syscall_nondevel_blocklist[i].arg)
b4368f
-            r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 1, *syscall_nondevel_blocklist[i].arg);
b4368f
+            r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 1, *syscall_nondevel_blocklist[i].arg);
b4368f
           else
b4368f
-            r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (EPERM), scall, 0);
b4368f
+            r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 0);
b4368f
 
b4368f
           if (r < 0 && r == -EFAULT /* unknown syscall */)
b4368f
             return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d"), scall);
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 96c181058292a4b4c9326b81e7fc8ba3bb052395 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 12:44:04 +0100
b4368f
Subject: [PATCH 04/13] run: Add cross-references for some other seccomp
b4368f
 syscall filters
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 89ae9fe74c6d445bb1b3a40e568d77cf5de47e48)
b4368f
(cherry picked from commit ab95bdb1b3c82de80848f0f2a385878a68e97350)
b4368f
---
b4368f
 common/flatpak-run.c | 4 ++++
b4368f
 1 file changed, 4 insertions(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index f7f40100bd2b..4846324af304 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2100,6 +2100,10 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
    *  https://git.gnome.org/browse/linux-user-chroot
b4368f
    *    in src/setup-seccomp.c
b4368f
    *
b4368f
+   * Other useful resources:
b4368f
+   * https://github.com/systemd/systemd/blob/HEAD/src/shared/seccomp-util.c
b4368f
+   * https://github.com/moby/moby/blob/HEAD/profiles/seccomp/default.json
b4368f
+   *
b4368f
    **** END NOTE ON CODE SHARING
b4368f
    */
b4368f
   struct
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 8351001f4a52466f6629390e6b0e94e2e15da4e6 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 14:17:04 +0100
b4368f
Subject: [PATCH 05/13] common: Add a list of recently-added Linux syscalls
b4368f
b4368f
Historically, syscalls could take arbitrarily-different values on
b4368f
different architectures, but new syscalls are added with syscall numbers
b4368f
that align on each architecture.
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 26b12484eb8a6219b9e7aa287b298a894b2f34ca)
b4368f
(cherry picked from commit e019d04faba1fb812996e8404b5ad05efb1bf439)
b4368f
---
b4368f
 common/Makefile.am.inc            |   1 +
b4368f
 common/flatpak-run.c              |   2 +
b4368f
 common/flatpak-syscalls-private.h | 197 ++++++++++++++++++++++++++++++
b4368f
 3 files changed, 200 insertions(+)
b4368f
 create mode 100644 common/flatpak-syscalls-private.h
b4368f
b4368f
diff --git a/common/Makefile.am.inc b/common/Makefile.am.inc
b4368f
index 794bd4e348ff..623ef78be2c4 100644
b4368f
--- a/common/Makefile.am.inc
b4368f
+++ b/common/Makefile.am.inc
b4368f
@@ -124,6 +124,7 @@ libflatpak_common_la_SOURCES = \
b4368f
 	common/flatpak-installation.c \
b4368f
 	common/flatpak-instance-private.h \
b4368f
 	common/flatpak-instance.c \
b4368f
+	common/flatpak-syscalls-private.h \
b4368f
 	common/valgrind-private.h \
b4368f
 	$(NULL)
b4368f
 
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 4846324af304..5e655c13e7d2 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -33,6 +33,8 @@
b4368f
 #include <unistd.h>
b4368f
 #include <gio/gunixfdlist.h>
b4368f
 
b4368f
+#include "flatpak-syscalls-private.h"
b4368f
+
b4368f
 #ifdef ENABLE_SECCOMP
b4368f
 #include <seccomp.h>
b4368f
 #endif
b4368f
diff --git a/common/flatpak-syscalls-private.h b/common/flatpak-syscalls-private.h
b4368f
new file mode 100644
b4368f
index 000000000000..04eb38ce3631
b4368f
--- /dev/null
b4368f
+++ b/common/flatpak-syscalls-private.h
b4368f
@@ -0,0 +1,197 @@
b4368f
+/*
b4368f
+ * Copyright 2021 Collabora Ltd.
b4368f
+ * SPDX-License-Identifier: LGPL-2.1-or-later
b4368f
+ *
b4368f
+ * This program is free software; you can redistribute it and/or
b4368f
+ * modify it under the terms of the GNU Lesser General Public
b4368f
+ * License as published by the Free Software Foundation; either
b4368f
+ * version 2.1 of the License, or (at your option) any later version.
b4368f
+ *
b4368f
+ * This library is distributed in the hope that it will be useful,
b4368f
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
b4368f
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b4368f
+ * Lesser General Public License for more details.
b4368f
+ *
b4368f
+ * You should have received a copy of the GNU Lesser General Public
b4368f
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
b4368f
+ */
b4368f
+
b4368f
+#pragma once
b4368f
+
b4368f
+#include <sys/syscall.h>
b4368f
+
b4368f
+#if defined(_MIPS_SIM)
b4368f
+# if _MIPS_SIM == _MIPS_SIM_ABI32
b4368f
+#   define FLATPAK_MISSING_SYSCALL_BASE 4000
b4368f
+# elif _MIPS_SIM == _MIPS_SIM_ABI64
b4368f
+#   define FLATPAK_MISSING_SYSCALL_BASE 5000
b4368f
+# elif _MIPS_SIM == _MIPS_SIM_NABI32
b4368f
+#   define FLATPAK_MISSING_SYSCALL_BASE 6000
b4368f
+# else
b4368f
+#   error "Unknown MIPS ABI"
b4368f
+# endif
b4368f
+#endif
b4368f
+
b4368f
+#if defined(__ia64__)
b4368f
+# define FLATPAK_MISSING_SYSCALL_BASE 1024
b4368f
+#endif
b4368f
+
b4368f
+#if defined(__alpha__)
b4368f
+# define FLATPAK_MISSING_SYSCALL_BASE 110
b4368f
+#endif
b4368f
+
b4368f
+#if defined(__x86_64__) && defined(__ILP32__)
b4368f
+# define FLATPAK_MISSING_SYSCALL_BASE 0x40000000
b4368f
+#endif
b4368f
+
b4368f
+/*
b4368f
+ * FLATPAK_MISSING_SYSCALL_BASE:
b4368f
+ *
b4368f
+ * Number to add to the syscall numbers of recently-added syscalls
b4368f
+ * to get the appropriate syscall for the current ABI.
b4368f
+ */
b4368f
+#ifndef FLATPAK_MISSING_SYSCALL_BASE
b4368f
+# define FLATPAK_MISSING_SYSCALL_BASE 0
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_open_tree
b4368f
+# define __NR_open_tree (FLATPAK_MISSING_SYSCALL_BASE + 428)
b4368f
+#endif
b4368f
+#ifndef __SNR_open_tree
b4368f
+# define __SNR_open_tree __NR_open_tree
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_move_mount
b4368f
+# define __NR_move_mount (FLATPAK_MISSING_SYSCALL_BASE + 429)
b4368f
+#endif
b4368f
+#ifndef __SNR_move_mount
b4368f
+# define __SNR_move_mount __NR_move_mount
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_fsopen
b4368f
+# define __NR_fsopen (FLATPAK_MISSING_SYSCALL_BASE + 430)
b4368f
+#endif
b4368f
+#ifndef __SNR_fsopen
b4368f
+# define __SNR_fsopen __NR_fsopen
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_fsconfig
b4368f
+# define __NR_fsconfig (FLATPAK_MISSING_SYSCALL_BASE + 431)
b4368f
+#endif
b4368f
+#ifndef __SNR_fsconfig
b4368f
+# define __SNR_fsconfig __NR_fsconfig
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_fsmount
b4368f
+# define __NR_fsmount (FLATPAK_MISSING_SYSCALL_BASE + 432)
b4368f
+#endif
b4368f
+#ifndef __SNR_fsmount
b4368f
+# define __SNR_fsmount __NR_fsmount
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_fspick
b4368f
+# define __NR_fspick (FLATPAK_MISSING_SYSCALL_BASE + 433)
b4368f
+#endif
b4368f
+#ifndef __SNR_fspick
b4368f
+# define __SNR_fspick __NR_fspick
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_pidfd_open
b4368f
+# define __NR_pidfd_open (FLATPAK_MISSING_SYSCALL_BASE + 434)
b4368f
+#endif
b4368f
+#ifndef __SNR_pidfd_open
b4368f
+# define __SNR_pidfd_open __NR_pidfd_open
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_clone3
b4368f
+# define __NR_clone3 (FLATPAK_MISSING_SYSCALL_BASE + 435)
b4368f
+#endif
b4368f
+#ifndef __SNR_clone3
b4368f
+# define __SNR_clone3 __NR_clone3
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_close_range
b4368f
+# define __NR_close_range (FLATPAK_MISSING_SYSCALL_BASE + 436)
b4368f
+#endif
b4368f
+#ifndef __SNR_close_range
b4368f
+# define __SNR_close_range __NR_close_range
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_openat2
b4368f
+# define __NR_openat2 (FLATPAK_MISSING_SYSCALL_BASE + 437)
b4368f
+#endif
b4368f
+#ifndef __SNR_openat2
b4368f
+# define __SNR_openat2 __NR_openat2
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_pidfd_getfd
b4368f
+# define __NR_pidfd_getfd (FLATPAK_MISSING_SYSCALL_BASE + 438)
b4368f
+#endif
b4368f
+#ifndef __SNR_pidfd_getfd
b4368f
+# define __SNR_pidfd_getfd __NR_pidfd_getfd
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_faccessat2
b4368f
+# define __NR_faccessat2 (FLATPAK_MISSING_SYSCALL_BASE + 439)
b4368f
+#endif
b4368f
+#ifndef __SNR_faccessat2
b4368f
+# define __SNR_faccessat2 __NR_faccessat2
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_process_madvise
b4368f
+# define __NR_process_madvise (FLATPAK_MISSING_SYSCALL_BASE + 440)
b4368f
+#endif
b4368f
+#ifndef __SNR_process_madvise
b4368f
+# define __SNR_process_madvise __NR_process_madvise
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_epoll_pwait2
b4368f
+# define __NR_epoll_pwait2 (FLATPAK_MISSING_SYSCALL_BASE + 441)
b4368f
+#endif
b4368f
+#ifndef __SNR_epoll_pwait2
b4368f
+# define __SNR_epoll_pwait2 __NR_epoll_pwait2
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_mount_setattr
b4368f
+# define __NR_mount_setattr (FLATPAK_MISSING_SYSCALL_BASE + 442)
b4368f
+#endif
b4368f
+#ifndef __SNR_mount_setattr
b4368f
+# define __SNR_mount_setattr __NR_mount_setattr
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_quotactl_fd
b4368f
+# define __NR_quotactl_fd (FLATPAK_MISSING_SYSCALL_BASE + 443)
b4368f
+#endif
b4368f
+#ifndef __SNR_quotactl_fd
b4368f
+# define __SNR_quotactl_fd __NR_quotactl_fd
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_landlock_create_ruleset
b4368f
+# define __NR_landlock_create_ruleset (FLATPAK_MISSING_SYSCALL_BASE + 444)
b4368f
+#endif
b4368f
+#ifndef __SNR_landlock_create_ruleset
b4368f
+# define __SNR_landlock_create_ruleset __NR_landlock_create_ruleset
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_landlock_add_rule
b4368f
+# define __NR_landlock_add_rule (FLATPAK_MISSING_SYSCALL_BASE + 445)
b4368f
+#endif
b4368f
+#ifndef __SNR_landlock_add_rule
b4368f
+# define __SNR_landlock_add_rule __NR_landlock_add_rule
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_landlock_restrict_self
b4368f
+# define __NR_landlock_restrict_self (FLATPAK_MISSING_SYSCALL_BASE + 446)
b4368f
+#endif
b4368f
+#ifndef __SNR_landlock_restrict_self
b4368f
+# define __SNR_landlock_restrict_self __NR_landlock_restrict_self
b4368f
+#endif
b4368f
+
b4368f
+#ifndef __NR_memfd_secret
b4368f
+# define __NR_memfd_secret (FLATPAK_MISSING_SYSCALL_BASE + 447)
b4368f
+#endif
b4368f
+#ifndef __SNR_memfd_secret
b4368f
+# define __SNR_memfd_secret __NR_memfd_secret
b4368f
+#endif
b4368f
+
b4368f
+/* Last updated: Linux 5.14, syscall numbers < 448 */
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From bd5735dac3ed31b1a95d999cbf0b117d6c23ad61 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 11:59:00 +0100
b4368f
Subject: [PATCH 06/13] run: Block clone3() in sandbox
b4368f
b4368f
clone3() can be used to implement clone() with CLONE_NEWUSER, allowing
b4368f
a sandboxed process to get CAP_SYS_ADMIN in a new namespace and
b4368f
manipulate its root directory. We need to block this so that AF_UNIX-based
b4368f
socket servers (X11, Wayland, etc.) can rely on
b4368f
/proc/PID/root/.flatpak-info existing for all Flatpak-sandboxed apps.
b4368f
b4368f
Partially fixes GHSA-67h7-w3jq-vh4q.
b4368f
b4368f
Thanks: an anonymous reporter
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit a10f52a7565c549612c92b8e736a6698a53db330)
b4368f
(cherry picked from commit 6be11da1b95d2751468edddba4fc2fddf0ae7d9d)
b4368f
---
b4368f
 common/flatpak-run.c | 6 ++++++
b4368f
 1 file changed, 6 insertions(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 5e655c13e7d2..2cc06239df9e 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2153,6 +2153,12 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
 
b4368f
     /* Don't allow faking input to the controlling tty (CVE-2017-5226) */
b4368f
     {SCMP_SYS (ioctl), EPERM, &SCMP_A1 (SCMP_CMP_MASKED_EQ, 0xFFFFFFFFu, (int) TIOCSTI)},
b4368f
+
b4368f
+    /* seccomp can't look into clone3()'s struct clone_args to check whether
b4368f
+     * the flags are OK, so we have no choice but to block clone3().
b4368f
+     * Return ENOSYS so user-space will fall back to clone().
b4368f
+     * (GHSA-67h7-w3jq-vh4q; see also https://github.com/moby/moby/commit/9f6b562d) */
b4368f
+    {SCMP_SYS (clone3), ENOSYS},
b4368f
   };
b4368f
 
b4368f
   struct
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 29c93b8a47ee73d2f2ff905004c41409a153cd57 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 12:45:54 +0100
b4368f
Subject: [PATCH 07/13] run: Disallow recently-added mount-manipulation
b4368f
 syscalls
b4368f
b4368f
If we don't allow mount() then we shouldn't allow these either.
b4368f
b4368f
Partially fixes GHSA-67h7-w3jq-vh4q.
b4368f
b4368f
Thanks: an anonymous reporter
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 9766ee05b1425db397d2cf23afd24c7f6146a69f)
b4368f
(cherry picked from commit 5ffa56fe76354392c74eb5c8fcf6e7f8bf7fdea7)
b4368f
---
b4368f
 common/flatpak-run.c | 12 ++++++++++++
b4368f
 1 file changed, 12 insertions(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 2cc06239df9e..1ae758892051 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2159,6 +2159,18 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
      * Return ENOSYS so user-space will fall back to clone().
b4368f
      * (GHSA-67h7-w3jq-vh4q; see also https://github.com/moby/moby/commit/9f6b562d) */
b4368f
     {SCMP_SYS (clone3), ENOSYS},
b4368f
+
b4368f
+    /* New mount manipulation APIs can also change our VFS. There's no
b4368f
+     * legitimate reason to do these in the sandbox, so block all of them
b4368f
+     * rather than thinking about which ones might be dangerous.
b4368f
+     * (GHSA-67h7-w3jq-vh4q) */
b4368f
+    {SCMP_SYS (open_tree), ENOSYS},
b4368f
+    {SCMP_SYS (move_mount), ENOSYS},
b4368f
+    {SCMP_SYS (fsopen), ENOSYS},
b4368f
+    {SCMP_SYS (fsconfig), ENOSYS},
b4368f
+    {SCMP_SYS (fsmount), ENOSYS},
b4368f
+    {SCMP_SYS (fspick), ENOSYS},
b4368f
+    {SCMP_SYS (mount_setattr), ENOSYS},
b4368f
   };
b4368f
 
b4368f
   struct
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 96b960d29ce4acf6bc983b97d7ff85e20fe1f2bb Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 14:19:31 +0100
b4368f
Subject: [PATCH 08/13] run: Block setns()
b4368f
b4368f
If we don't allow unshare() or clone() with CLONE_NEWUSER, we also
b4368f
shouldn't allow joining an existing (but different) namespace.
b4368f
b4368f
Partially fixes GHSA-67h7-w3jq-vh4q.
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 4c34815784e9ffda5733225c7d95824f96375e36)
b4368f
(cherry picked from commit ab5232e6c3d896f72a623e798b8e6dfa6efcfd9b)
b4368f
---
b4368f
 common/flatpak-run.c | 1 +
b4368f
 1 file changed, 1 insertion(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 1ae758892051..0cae23d7809c 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2140,6 +2140,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
 
b4368f
     /* Don't allow subnamespace setups: */
b4368f
     {SCMP_SYS (unshare), EPERM},
b4368f
+    {SCMP_SYS (setns), EPERM},
b4368f
     {SCMP_SYS (mount), EPERM},
b4368f
     {SCMP_SYS (pivot_root), EPERM},
b4368f
 #if defined(__s390__) || defined(__s390x__) || defined(__CRIS__)
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 8d22d36cb8861d3391015f267222d4742bb6357c Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 14:20:29 +0100
b4368f
Subject: [PATCH 09/13] run: Don't allow unmounting filesystems
b4368f
b4368f
If we don't allow mounting filesystems, we shouldn't allow unmounting
b4368f
either.
b4368f
b4368f
Partially fixes GHSA-67h7-w3jq-vh4q.
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 1330662f33a55e88bfe18e76de28b7922d91a999)
b4368f
(cherry picked from commit aa570112217ae6dfccaa8fe5db55e29b76c9db80)
b4368f
---
b4368f
 common/flatpak-run.c | 2 ++
b4368f
 1 file changed, 2 insertions(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 0cae23d7809c..c4bd6fa84079 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2142,6 +2142,8 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
     {SCMP_SYS (unshare), EPERM},
b4368f
     {SCMP_SYS (setns), EPERM},
b4368f
     {SCMP_SYS (mount), EPERM},
b4368f
+    {SCMP_SYS (umount), EPERM},
b4368f
+    {SCMP_SYS (umount2), EPERM},
b4368f
     {SCMP_SYS (pivot_root), EPERM},
b4368f
 #if defined(__s390__) || defined(__s390x__) || defined(__CRIS__)
b4368f
     /* Architectures with CONFIG_CLONE_BACKWARDS2: the child stack
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 07ded57a9e8b6b3afe50fea2fed0e1d0d9484bb4 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Wed, 1 Sep 2021 14:21:04 +0100
b4368f
Subject: [PATCH 10/13] run: Don't allow chroot()
b4368f
b4368f
If we don't allow pivot_root() then there seems no reason why we should
b4368f
allow chroot().
b4368f
b4368f
Partially fixes GHSA-67h7-w3jq-vh4q.
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 462fca2c666e0cd2b60d6d2593a7216a83047aaf)
b4368f
(cherry picked from commit 8d4281f19a56901c0801e3b2b1cc0bc3e0519d49)
b4368f
---
b4368f
 common/flatpak-run.c | 1 +
b4368f
 1 file changed, 1 insertion(+)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index c4bd6fa84079..b8d1cb0b8dc9 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2145,6 +2145,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
     {SCMP_SYS (umount), EPERM},
b4368f
     {SCMP_SYS (umount2), EPERM},
b4368f
     {SCMP_SYS (pivot_root), EPERM},
b4368f
+    {SCMP_SYS (chroot), EPERM},
b4368f
 #if defined(__s390__) || defined(__s390x__) || defined(__CRIS__)
b4368f
     /* Architectures with CONFIG_CLONE_BACKWARDS2: the child stack
b4368f
      * and flags arguments are reversed so the flags come second */
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 512680add1be8689e013565a3ace45e372d1a665 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Fri, 8 Oct 2021 17:05:07 +0100
b4368f
Subject: [PATCH 11/13] run: Handle unknown syscalls as intended
b4368f
b4368f
The error-handling here was
b4368f
b4368f
    if (r < 0 && r == -EFAULT)
b4368f
b4368f
but Alex says it was almost certainly intended to be
b4368f
b4368f
    if (r < 0 && r != -EFAULT)
b4368f
b4368f
so that syscalls not known to libseccomp are not a fatal error.
b4368f
b4368f
Instead of literally making that change, emit a debug message on -EFAULT
b4368f
so we can see what is going on.
b4368f
b4368f
This temporarily weakens our defence against CVE-2021-41133
b4368f
(GHSA-67h7-w3jq-vh4q) in order to avoid regressions: if the installed
b4368f
version of libseccomp does not know about the recently-added syscalls,
b4368f
but the kernel does, then we will not prevent non-native executables
b4368f
from using those syscalls.
b4368f
b4368f
Resolves: https://github.com/flatpak/flatpak/issues/4458
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit d419fa67038370e4f4c3ce8c3b5f672d4876cfc8)
b4368f
(cherry picked from commit 270701f900c8612cf1fc5e6f5a6e2eb6459708c1)
b4368f
(cherry picked from commit a0055e4f849d5bb100f2af7e33f02ef9ac3fbdee)
b4368f
(cherry picked from commit ed2c15d9964b17a7f6b64d3956a9adfbfd2b0834)
b4368f
(cherry picked from commit 02498d636f8d3048cebde74bb1eb308149b8dd0b)
b4368f
---
b4368f
 common/flatpak-run.c | 17 +++++++++++++++--
b4368f
 1 file changed, 15 insertions(+), 2 deletions(-)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index b8d1cb0b8dc9..7ad2626c9e3d 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2282,7 +2282,16 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
         r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 1, *syscall_blocklist[i].arg);
b4368f
       else
b4368f
         r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 0);
b4368f
-      if (r < 0 && r == -EFAULT /* unknown syscall */)
b4368f
+
b4368f
+      /* EFAULT means "internal libseccomp error", but in practice we get
b4368f
+       * this for syscall numbers added via flatpak-syscalls-private.h
b4368f
+       * when trying to filter them on a non-native architecture, because
b4368f
+       * libseccomp cannot map the syscall number to a name and back to a
b4368f
+       * number for the non-native architecture. */
b4368f
+      if (r == -EFAULT)
b4368f
+        flatpak_debug2 ("Unable to block syscall %d: syscall not known to libseccomp?",
b4368f
+                        scall);
b4368f
+      else if (r < 0)
b4368f
         return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d"), scall);
b4368f
     }
b4368f
 
b4368f
@@ -2300,7 +2309,11 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
           else
b4368f
             r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 0);
b4368f
 
b4368f
-          if (r < 0 && r == -EFAULT /* unknown syscall */)
b4368f
+          /* See above for the meaning of EFAULT. */
b4368f
+          if (errno == EFAULT)
b4368f
+            flatpak_debug2 ("Unable to block syscall %d: syscall not known to libseccomp?",
b4368f
+                            scall);
b4368f
+          else if (r < 0)
b4368f
             return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d"), scall);
b4368f
         }
b4368f
     }
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From fd836c2eec26015efe904d48a81f13705bd578d5 Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Fri, 8 Oct 2021 19:00:13 +0100
b4368f
Subject: [PATCH 12/13] Fix handling of syscalls only allowed by --devel
b4368f
b4368f
This was incorrectly looking at errno instead of -r.
b4368f
b4368f
Fixes: 0b38b0f0 "run: Handle unknown syscalls as intended"
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 3fc8c672676ae016f8e7cc90481b2feecbad9861)
b4368f
(cherry picked from commit 97e128c2c1520202486b5e165e1734cbb421568a)
b4368f
(cherry picked from commit da503e0d903f275e02c8932069a0badbf3946812)
b4368f
(cherry picked from commit 68163fbc0b956e21fa6b2d5d854f0d1f81294d5d)
b4368f
(cherry picked from commit cfc72b9694e295696743ed52361e29bc1f575d60)
b4368f
---
b4368f
 common/flatpak-run.c | 2 +-
b4368f
 1 file changed, 1 insertion(+), 1 deletion(-)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 7ad2626c9e3d..6ad11f5b25a9 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2310,7 +2310,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
             r = seccomp_rule_add (seccomp, SCMP_ACT_ERRNO (errnum), scall, 0);
b4368f
 
b4368f
           /* See above for the meaning of EFAULT. */
b4368f
-          if (errno == EFAULT)
b4368f
+          if (r == -EFAULT)
b4368f
             flatpak_debug2 ("Unable to block syscall %d: syscall not known to libseccomp?",
b4368f
                             scall);
b4368f
           else if (r < 0)
b4368f
-- 
b4368f
2.31.1
b4368f
b4368f
b4368f
From 79a8fb7cf8ed8ac23dce5c3fdbd61e8fdc49110c Mon Sep 17 00:00:00 2001
b4368f
From: Simon McVittie <smcv@collabora.com>
b4368f
Date: Fri, 8 Oct 2021 19:06:13 +0100
b4368f
Subject: [PATCH 13/13] run: Improve error handling/diagnostics for calls into
b4368f
 libseccomp
b4368f
b4368f
Signed-off-by: Simon McVittie <smcv@collabora.com>
b4368f
(cherry picked from commit 53bde36585b88a2b96bf896ed79b40ccb6a72c54)
b4368f
(cherry picked from commit bd2c58fc27fa5e31029339dbce8eea10717015f3)
b4368f
(cherry picked from commit adaa0259e807bee49d18495108fb0c4c6856213c)
b4368f
(cherry picked from commit 2c82e9cc6e053fd3ec419da5f67a50d1ee50bf72)
b4368f
(cherry picked from commit ad0e32e3477611383df1e7a6ad01276ad35c422d)
b4368f
---
b4368f
 common/flatpak-run.c | 46 ++++++++++++++++++++++++++++++++++++++------
b4368f
 1 file changed, 40 insertions(+), 6 deletions(-)
b4368f
b4368f
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
b4368f
index 6ad11f5b25a9..fa9862a047ad 100644
b4368f
--- a/common/flatpak-run.c
b4368f
+++ b/common/flatpak-run.c
b4368f
@@ -2054,6 +2054,38 @@ static const uint32_t seccomp_x86_64_extra_arches[] = { SCMP_ARCH_X86, 0, };
b4368f
 static const uint32_t seccomp_aarch64_extra_arches[] = { SCMP_ARCH_ARM, 0 };
b4368f
 #endif
b4368f
 
b4368f
+/*
b4368f
+ * @negative_errno: Result code as returned by libseccomp functions
b4368f
+ *
b4368f
+ * Translate a libseccomp error code into an error message. libseccomp
b4368f
+ * mostly returns negative `errno` values such as `-ENOMEM`, but some
b4368f
+ * standard `errno` values are used for non-standard purposes where their
b4368f
+ * `strerror()` would be misleading.
b4368f
+ *
b4368f
+ * Returns: a string version of @negative_errno if possible
b4368f
+ */
b4368f
+static const char *
b4368f
+flatpak_seccomp_strerror (int negative_errno)
b4368f
+{
b4368f
+  g_return_val_if_fail (negative_errno < 0, "Non-negative error value from libseccomp?");
b4368f
+  g_return_val_if_fail (negative_errno > INT_MIN, "Out of range error value from libseccomp?");
b4368f
+
b4368f
+  switch (negative_errno)
b4368f
+    {
b4368f
+      case -EDOM:
b4368f
+        return "Architecture specific failure";
b4368f
+
b4368f
+      case -EFAULT:
b4368f
+        return "Internal libseccomp failure (unknown syscall?)";
b4368f
+
b4368f
+      case -ECANCELED:
b4368f
+        return "System failure beyond the control of libseccomp";
b4368f
+    }
b4368f
+
b4368f
+  /* e.g. -ENOMEM: the result of strerror() is good enough */
b4368f
+  return g_strerror (-negative_errno);
b4368f
+}
b4368f
+
b4368f
 static inline void
b4368f
 cleanup_seccomp (void *p)
b4368f
 {
b4368f
@@ -2251,7 +2283,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
              couldn't continue running. */
b4368f
           r = seccomp_arch_add (seccomp, arch_id);
b4368f
           if (r < 0 && r != -EEXIST)
b4368f
-            return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to add architecture to seccomp filter"));
b4368f
+            return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to add architecture to seccomp filter: %s"), flatpak_seccomp_strerror (r));
b4368f
 
b4368f
           if (multiarch && extra_arches != NULL)
b4368f
             {
b4368f
@@ -2260,7 +2292,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
                 {
b4368f
                   r = seccomp_arch_add (seccomp, extra_arches[i]);
b4368f
                   if (r < 0 && r != -EEXIST)
b4368f
-                    return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to add multiarch architecture to seccomp filter"));
b4368f
+                    return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to add multiarch architecture to seccomp filter: %s"), flatpak_seccomp_strerror (r));
b4368f
                 }
b4368f
             }
b4368f
         }
b4368f
@@ -2292,7 +2324,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
         flatpak_debug2 ("Unable to block syscall %d: syscall not known to libseccomp?",
b4368f
                         scall);
b4368f
       else if (r < 0)
b4368f
-        return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d"), scall);
b4368f
+        return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d: %s"), scall, flatpak_seccomp_strerror (r));
b4368f
     }
b4368f
 
b4368f
   if (!devel)
b4368f
@@ -2314,7 +2346,7 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
             flatpak_debug2 ("Unable to block syscall %d: syscall not known to libseccomp?",
b4368f
                             scall);
b4368f
           else if (r < 0)
b4368f
-            return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d"), scall);
b4368f
+            return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to block syscall %d: %s"), scall, flatpak_seccomp_strerror (r));
b4368f
         }
b4368f
     }
b4368f
 
b4368f
@@ -2344,8 +2376,10 @@ setup_seccomp (FlatpakBwrap   *bwrap,
b4368f
   if (!glnx_open_anonymous_tmpfile (O_RDWR | O_CLOEXEC, &seccomp_tmpf, error))
b4368f
     return FALSE;
b4368f
 
b4368f
-  if (seccomp_export_bpf (seccomp, seccomp_tmpf.fd) != 0)
b4368f
-    return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to export bpf"));
b4368f
+  r = seccomp_export_bpf (seccomp, seccomp_tmpf.fd);
b4368f
+
b4368f
+  if (r != 0)
b4368f
+    return flatpak_fail_error (error, FLATPAK_ERROR_SETUP_FAILED, _("Failed to export bpf: %s"), flatpak_seccomp_strerror (r));
b4368f
 
b4368f
   lseek (seccomp_tmpf.fd, 0, SEEK_SET);
b4368f
 
b4368f
-- 
b4368f
2.31.1
b4368f