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

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