Blame SOURCES/flatpak-builder-CVE-2022-21682.patch

a1ba12
From dd05ea86a4701a33cc4d271edf0a36b5c972e2e1 Mon Sep 17 00:00:00 2001
a1ba12
From: Simon McVittie <smcv@collabora.com>
a1ba12
Date: Mon, 17 Jan 2022 21:59:02 +0000
a1ba12
Subject: [PATCH 1/2] Disable filesystem access with --nofilesystem=host:reset
a1ba12
a1ba12
This requires <https://github.com/flatpak/flatpak/pull/4678>.
a1ba12
a1ba12
In addition to counteracting an earlier --filesystem=host, in Flatpak
a1ba12
versions that support it, the new --nofilesystem=host:reset removes all
a1ba12
filesystem access that might have been inherited from the app manifest
a1ba12
or overrides. This prevents CVE-2022-21682, while avoiding behaviour
a1ba12
changes in Flatpak for non-builder use cases.
a1ba12
a1ba12
In older Flatpak versions, this option acts as --filesystem=host with an
a1ba12
unknown mode suffix, which is ignored (with a warning, which is harmless
a1ba12
but will hopefully nudge people towards upgrading Flatpak to a version
a1ba12
that enables CVE-2022-21682 to be avoided). flatpak-builder will still
a1ba12
be vulnerable to CVE-2022-21682 in this case.
a1ba12
a1ba12
Signed-off-by: Simon McVittie <smcv@collabora.com>
a1ba12
(cherry picked from commit 2d1b6799e782d5e5577072aa7bbfed00ddf0b087)
a1ba12
---
a1ba12
 src/builder-main.c         | 2 +-
a1ba12
 src/builder-manifest.c     | 4 ++--
a1ba12
 src/builder-module.c       | 2 +-
a1ba12
 src/builder-source-shell.c | 2 +-
a1ba12
 4 files changed, 5 insertions(+), 5 deletions(-)
a1ba12
a1ba12
diff --git a/src/builder-main.c b/src/builder-main.c
a1ba12
index a177f4b0c8b6..dc6f3e97603a 100644
a1ba12
--- a/src/builder-main.c
a1ba12
+++ b/src/builder-main.c
a1ba12
@@ -942,7 +942,7 @@ main (int    argc,
a1ba12
         "flatpak",
a1ba12
         "build",
a1ba12
         "--die-with-parent",
a1ba12
-        "--nofilesystem=host",
a1ba12
+        "--nofilesystem=host:reset",
a1ba12
         fs_app_dir,
a1ba12
         fs_cache,
a1ba12
         "--share=network",
a1ba12
diff --git a/src/builder-manifest.c b/src/builder-manifest.c
a1ba12
index 62e7096674fa..ae83e493db52 100644
a1ba12
--- a/src/builder-manifest.c
a1ba12
+++ b/src/builder-manifest.c
a1ba12
@@ -2124,7 +2124,7 @@ command (GFile      *app_dir,
a1ba12
   g_ptr_array_add (args, g_strdup ("build"));
a1ba12
 
a1ba12
   g_ptr_array_add (args, g_strdup ("--die-with-parent"));
a1ba12
-  g_ptr_array_add (args, g_strdup ("--nofilesystem=host"));
a1ba12
+  g_ptr_array_add (args, g_strdup ("--nofilesystem=host:reset"));
a1ba12
   if (extra_args)
a1ba12
     {
a1ba12
       for (i = 0; extra_args[i] != NULL; i++)
a1ba12
@@ -2304,7 +2304,7 @@ appstream_compose (GFile   *app_dir,
a1ba12
   g_ptr_array_add (args, g_strdup ("flatpak"));
a1ba12
   g_ptr_array_add (args, g_strdup ("build"));
a1ba12
   g_ptr_array_add (args, g_strdup ("--die-with-parent"));
a1ba12
-  g_ptr_array_add (args, g_strdup ("--nofilesystem=host"));
a1ba12
+  g_ptr_array_add (args, g_strdup ("--nofilesystem=host:reset"));
a1ba12
   g_ptr_array_add (args, g_file_get_path (app_dir));
a1ba12
   g_ptr_array_add (args, g_strdup ("appstream-compose"));
a1ba12
 
a1ba12
diff --git a/src/builder-module.c b/src/builder-module.c
a1ba12
index 8d1819a3e530..862c247e2fb2 100644
a1ba12
--- a/src/builder-module.c
a1ba12
+++ b/src/builder-module.c
a1ba12
@@ -1177,7 +1177,7 @@ setup_build_args (GFile          *app_dir,
a1ba12
     builddir = "/run/build/";
a1ba12
 
a1ba12
   g_ptr_array_add (args, g_strdup_printf ("--env=FLATPAK_BUILDER_BUILDDIR=%s%s", builddir, module_name));
a1ba12
-  g_ptr_array_add (args, g_strdup ("--nofilesystem=host"));
a1ba12
+  g_ptr_array_add (args, g_strdup ("--nofilesystem=host:reset"));
a1ba12
 
a1ba12
   /* We mount the canonical location, because bind-mounts of symlinks don't really work */
a1ba12
   g_ptr_array_add (args, g_strdup_printf ("--filesystem=%s", source_dir_path_canonical));
a1ba12
diff --git a/src/builder-source-shell.c b/src/builder-source-shell.c
a1ba12
index 152257b12476..8132a5c49d8a 100644
a1ba12
--- a/src/builder-source-shell.c
a1ba12
+++ b/src/builder-source-shell.c
a1ba12
@@ -136,7 +136,7 @@ run_script (BuilderContext *context,
a1ba12
 
a1ba12
   source_dir_path_canonical = realpath (source_dir_path, NULL);
a1ba12
 
a1ba12
-  g_ptr_array_add (args, g_strdup ("--nofilesystem=host"));
a1ba12
+  g_ptr_array_add (args, g_strdup ("--nofilesystem=host:reset"));
a1ba12
   g_ptr_array_add (args, g_strdup_printf ("--filesystem=%s", source_dir_path_canonical));
a1ba12
 
a1ba12
   if (env)
a1ba12
-- 
a1ba12
2.35.1
a1ba12
a1ba12
a1ba12
From 26cab8d7aae2146fa95cf5ad28e286f8034d97dc Mon Sep 17 00:00:00 2001
a1ba12
From: Alexander Larsson <alexl@redhat.com>
a1ba12
Date: Tue, 18 Jan 2022 09:58:29 +0100
a1ba12
Subject: [PATCH 2/2] Allow --nofilesystem=host:reset in flatpak-builder --run
a1ba12
a1ba12
This adds support for the new host:reset mode. We don't verify
a1ba12
that the argument is used as carefully as flatpak does, but any
a1ba12
issue will be reported later when passed to flatpak.
a1ba12
a1ba12
Co-authored-by: Simon McVittie <smcv@collabora.com>
a1ba12
(cherry picked from commit 27aa2f5e4508d03178bf905ee7099d6d69a79aa4)
a1ba12
---
a1ba12
 src/builder-flatpak-utils.c | 23 +++++++++++++++++++++--
a1ba12
 1 file changed, 21 insertions(+), 2 deletions(-)
a1ba12
a1ba12
diff --git a/src/builder-flatpak-utils.c b/src/builder-flatpak-utils.c
a1ba12
index 53191016047f..89352cdc2fd5 100644
a1ba12
--- a/src/builder-flatpak-utils.c
a1ba12
+++ b/src/builder-flatpak-utils.c
a1ba12
@@ -1196,6 +1196,7 @@ typedef enum {
a1ba12
 
a1ba12
 /* In numerical order of more privs */
a1ba12
 typedef enum {
a1ba12
+  FLATPAK_FILESYSTEM_MODE_NONE         = 0,
a1ba12
   FLATPAK_FILESYSTEM_MODE_READ_ONLY    = 1,
a1ba12
   FLATPAK_FILESYSTEM_MODE_READ_WRITE   = 2,
a1ba12
   FLATPAK_FILESYSTEM_MODE_CREATE       = 3,
a1ba12
@@ -1770,6 +1771,13 @@ parse_filesystem_flags (const char *filesystem, FlatpakFilesystemMode *mode)
a1ba12
       if (mode)
a1ba12
         *mode = FLATPAK_FILESYSTEM_MODE_CREATE;
a1ba12
     }
a1ba12
+  else if (g_str_equal (filesystem, "host:reset"))
a1ba12
+    {
a1ba12
+      filesystem = "host-reset";
a1ba12
+
a1ba12
+      if (mode)
a1ba12
+        *mode = FLATPAK_FILESYSTEM_MODE_NONE;
a1ba12
+    }
a1ba12
 
a1ba12
   return g_strndup (filesystem, len);
a1ba12
 }
a1ba12
@@ -1810,9 +1818,12 @@ static void
a1ba12
 flatpak_context_remove_filesystem (FlatpakContext *context,
a1ba12
                                    const char     *what)
a1ba12
 {
a1ba12
+  FlatpakFilesystemMode mode;
a1ba12
+  g_autofree char *fs = parse_filesystem_flags (what, &mode);
a1ba12
+
a1ba12
   g_hash_table_insert (context->filesystems,
a1ba12
-                       parse_filesystem_flags (what, NULL),
a1ba12
-                       NULL);
a1ba12
+                       g_steal_pointer (&fs),
a1ba12
+                       GINT_TO_POINTER (mode));
a1ba12
 }
a1ba12
 
a1ba12
 static gboolean
a1ba12
@@ -2222,11 +2233,19 @@ flatpak_context_to_args (FlatpakContext *context,
a1ba12
       g_ptr_array_add (args, g_strdup_printf ("--system-%s-name=%s", flatpak_policy_to_string (policy), name));
a1ba12
     }
a1ba12
 
a1ba12
+  if (g_hash_table_lookup_extended (context->filesystems, "host-reset", NULL, NULL))
a1ba12
+    {
a1ba12
+      g_ptr_array_add (args, g_strdup ("--nofilesystem=host:reset"));
a1ba12
+    }
a1ba12
+
a1ba12
   g_hash_table_iter_init (&iter, context->filesystems);
a1ba12
   while (g_hash_table_iter_next (&iter, &key, &value))
a1ba12
     {
a1ba12
       FlatpakFilesystemMode mode = GPOINTER_TO_INT (value);
a1ba12
 
a1ba12
+      if (g_str_equal (key, "host-reset"))
a1ba12
+        continue;
a1ba12
+
a1ba12
       if (mode == FLATPAK_FILESYSTEM_MODE_READ_ONLY)
a1ba12
         g_ptr_array_add (args, g_strdup_printf ("--filesystem=%s:ro", (char *)key));
a1ba12
       else if (mode == FLATPAK_FILESYSTEM_MODE_READ_WRITE)
a1ba12
-- 
a1ba12
2.35.1
a1ba12