Blame SOURCES/flatpak-1.6.2-fix-CVE-2021-21261.patch

3af087
From 038655c9ee29ee659e5fede4d36871cf8ccef803 Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Mon, 7 Dec 2020 18:08:16 +0000
3af087
Subject: [PATCH 01/10] common: Add a backport of
3af087
 G_DBUS_METHOD_INVOCATION_HANDLED
3af087
3af087
This is syntactic sugar added in GLib 2.67.0, which makes it more clearly
3af087
correct when we return TRUE after a GDBus error.
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
---
3af087
 common/flatpak-utils-base-private.h | 6 ++++++
3af087
 1 file changed, 6 insertions(+)
3af087
3af087
diff --git a/common/flatpak-utils-base-private.h b/common/flatpak-utils-base-private.h
3af087
index 181a0c95..9d10a530 100644
3af087
--- a/common/flatpak-utils-base-private.h
3af087
+++ b/common/flatpak-utils-base-private.h
3af087
@@ -22,6 +22,12 @@
3af087
 #define __FLATPAK_UTILS_BASE_H__
3af087
 
3af087
 #include <glib.h>
3af087
+#include <gio/gio.h>
3af087
+
3af087
+#ifndef G_DBUS_METHOD_INVOCATION_HANDLED
3af087
+# define G_DBUS_METHOD_INVOCATION_HANDLED TRUE
3af087
+# define G_DBUS_METHOD_INVOCATION_UNHANDLED FALSE
3af087
+#endif
3af087
 
3af087
 char *flatpak_get_timezone (void);
3af087
 
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From 6ed3e235459f5fe8b58aecbab744166a5ee66f9d Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Sun, 10 Jan 2021 16:11:28 +0000
3af087
Subject: [PATCH 02/10] run: Convert all environment variables into bwrap
3af087
 arguments
3af087
3af087
This avoids some of them being filtered out by a setuid bwrap. It also
3af087
means that if they came from an untrusted source, they cannot be used
3af087
to inject arbitrary code into a non-setuid bwrap via mechanisms like
3af087
LD_PRELOAD.
3af087
3af087
Because they get bundled into a memfd or temporary file, they do not
3af087
actually appear in argv, ensuring that they remain inaccessible to
3af087
processes running under a different uid (which is important if their
3af087
values are tokens or other secrets).
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
---
3af087
 common/flatpak-bwrap-private.h |  3 +++
3af087
 common/flatpak-bwrap.c         | 43 ++++++++++++++++++++++++++++++++++
3af087
 common/flatpak-run.c           | 32 +++++++++++--------------
3af087
 3 files changed, 60 insertions(+), 18 deletions(-)
3af087
3af087
diff --git a/common/flatpak-bwrap-private.h b/common/flatpak-bwrap-private.h
3af087
index 92d6e9de..6c31b789 100644
3af087
--- a/common/flatpak-bwrap-private.h
3af087
+++ b/common/flatpak-bwrap-private.h
3af087
@@ -43,6 +43,8 @@ void          flatpak_bwrap_unset_env (FlatpakBwrap *bwrap,
3af087
                                        const char   *variable);
3af087
 void          flatpak_bwrap_add_arg (FlatpakBwrap *bwrap,
3af087
                                      const char   *arg);
3af087
+void          flatpak_bwrap_take_arg (FlatpakBwrap *bwrap,
3af087
+                                      char         *arg);
3af087
 void          flatpak_bwrap_add_noinherit_fd (FlatpakBwrap *bwrap,
3af087
                                               int           fd);
3af087
 void          flatpak_bwrap_add_fd (FlatpakBwrap *bwrap,
3af087
@@ -73,6 +75,7 @@ void          flatpak_bwrap_add_bind_arg (FlatpakBwrap *bwrap,
3af087
                                           const char   *type,
3af087
                                           const char   *src,
3af087
                                           const char   *dest);
3af087
+void          flatpak_bwrap_envp_to_args (FlatpakBwrap *bwrap);
3af087
 gboolean      flatpak_bwrap_bundle_args (FlatpakBwrap *bwrap,
3af087
                                          int           start,
3af087
                                          int           end,
3af087
diff --git a/common/flatpak-bwrap.c b/common/flatpak-bwrap.c
3af087
index 7e5d38d1..d383d25f 100644
3af087
--- a/common/flatpak-bwrap.c
3af087
+++ b/common/flatpak-bwrap.c
3af087
@@ -109,6 +109,18 @@ flatpak_bwrap_add_arg (FlatpakBwrap *bwrap, const char *arg)
3af087
   g_ptr_array_add (bwrap->argv, g_strdup (arg));
3af087
 }
3af087
 
3af087
+/*
3af087
+ * flatpak_bwrap_take_arg:
3af087
+ * @arg: (transfer full): Take ownership of this argument
3af087
+ *
3af087
+ * Add @arg to @bwrap's argv, taking ownership of the pointer.
3af087
+ */
3af087
+void
3af087
+flatpak_bwrap_take_arg (FlatpakBwrap *bwrap, char *arg)
3af087
+{
3af087
+  g_ptr_array_add (bwrap->argv, arg);
3af087
+}
3af087
+
3af087
 void
3af087
 flatpak_bwrap_finish (FlatpakBwrap *bwrap)
3af087
 {
3af087
@@ -274,6 +286,37 @@ flatpak_bwrap_add_bind_arg (FlatpakBwrap *bwrap,
3af087
     }
3af087
 }
3af087
 
3af087
+/*
3af087
+ * Convert bwrap->envp into a series of --setenv arguments for bwrap(1),
3af087
+ * assumed to be applied to an empty environment. Reset envp to be an
3af087
+ * empty environment.
3af087
+ */
3af087
+void
3af087
+flatpak_bwrap_envp_to_args (FlatpakBwrap *bwrap)
3af087
+{
3af087
+  gsize i;
3af087
+
3af087
+  for (i = 0; bwrap->envp[i] != NULL; i++)
3af087
+    {
3af087
+      char *key_val = bwrap->envp[i];
3af087
+      char *eq = strchr (key_val, '=');
3af087
+
3af087
+      if (eq)
3af087
+        {
3af087
+          flatpak_bwrap_add_arg (bwrap, "--setenv");
3af087
+          flatpak_bwrap_take_arg (bwrap, g_strndup (key_val, eq - key_val));
3af087
+          flatpak_bwrap_add_arg (bwrap, eq + 1);
3af087
+        }
3af087
+      else
3af087
+        {
3af087
+          g_warn_if_reached ();
3af087
+        }
3af087
+    }
3af087
+
3af087
+  g_strfreev (g_steal_pointer (&bwrap->envp));
3af087
+  bwrap->envp = g_strdupv (flatpak_bwrap_empty_env);
3af087
+}
3af087
+
3af087
 gboolean
3af087
 flatpak_bwrap_bundle_args (FlatpakBwrap *bwrap,
3af087
                            int           start,
3af087
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
3af087
index 51c002ff..9383610b 100644
3af087
--- a/common/flatpak-run.c
3af087
+++ b/common/flatpak-run.c
3af087
@@ -1314,24 +1314,6 @@ flatpak_run_add_environment_args (FlatpakBwrap    *bwrap,
3af087
   flatpak_run_add_system_dbus_args (bwrap, proxy_arg_bwrap, context, flags);
3af087
   flatpak_run_add_a11y_dbus_args (bwrap, proxy_arg_bwrap, context, flags);
3af087
 
3af087
-  if (g_environ_getenv (bwrap->envp, "LD_LIBRARY_PATH") != NULL)
3af087
-    {
3af087
-      /* LD_LIBRARY_PATH is overridden for setuid helper, so pass it as cmdline arg */
3af087
-      flatpak_bwrap_add_args (bwrap,
3af087
-                              "--setenv", "LD_LIBRARY_PATH", g_environ_getenv (bwrap->envp, "LD_LIBRARY_PATH"),
3af087
-                              NULL);
3af087
-      flatpak_bwrap_unset_env (bwrap, "LD_LIBRARY_PATH");
3af087
-    }
3af087
-
3af087
-  if (g_environ_getenv (bwrap->envp, "TMPDIR") != NULL)
3af087
-    {
3af087
-      /* TMPDIR is overridden for setuid helper, so pass it as cmdline arg */
3af087
-      flatpak_bwrap_add_args (bwrap,
3af087
-                              "--setenv", "TMPDIR", g_environ_getenv (bwrap->envp, "TMPDIR"),
3af087
-                              NULL);
3af087
-      flatpak_bwrap_unset_env (bwrap, "TMPDIR");
3af087
-    }
3af087
-
3af087
   /* Must run this before spawning the dbus proxy, to ensure it
3af087
      ends up in the app cgroup */
3af087
   if (!flatpak_run_in_transient_unit (app_id, &my_error))
3af087
@@ -3852,6 +3834,8 @@ flatpak_run_app (const char     *app_ref,
3af087
       command = default_command;
3af087
     }
3af087
 
3af087
+  flatpak_bwrap_envp_to_args (bwrap);
3af087
+
3af087
   if (!flatpak_bwrap_bundle_args (bwrap, 1, -1, FALSE, error))
3af087
     return FALSE;
3af087
 
3af087
@@ -3882,6 +3866,12 @@ flatpak_run_app (const char     *app_ref,
3af087
       /* We use LEAVE_DESCRIPTORS_OPEN to work around dead-lock, see flatpak_close_fds_workaround */
3af087
       spawn_flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN;
3af087
 
3af087
+      /* flatpak_bwrap_envp_to_args() moved the environment variables to
3af087
+       * be set into --setenv instructions in argv, so the environment
3af087
+       * in which the bwrap command runs must be empty. */
3af087
+      g_assert (bwrap->envp != NULL);
3af087
+      g_assert (bwrap->envp[0] == NULL);
3af087
+
3af087
       if (!g_spawn_async (NULL,
3af087
                           (char **) bwrap->argv->pdata,
3af087
                           bwrap->envp,
3af087
@@ -3909,6 +3899,12 @@ flatpak_run_app (const char     *app_ref,
3af087
        * we do want to allow inheriting fds into flatpak run. */
3af087
       flatpak_bwrap_child_setup (bwrap->fds, FALSE);
3af087
 
3af087
+      /* flatpak_bwrap_envp_to_args() moved the environment variables to
3af087
+       * be set into --setenv instructions in argv, so the environment
3af087
+       * in which the bwrap command runs must be empty. */
3af087
+      g_assert (bwrap->envp != NULL);
3af087
+      g_assert (bwrap->envp[0] == NULL);
3af087
+
3af087
       if (execvpe (flatpak_get_bwrap (), (char **) bwrap->argv->pdata, bwrap->envp) == -1)
3af087
         {
3af087
           g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (errno),
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From a1dec4885218f40b03044c82245d5b362c0affeb Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Mon, 11 Jan 2021 12:14:48 +0000
3af087
Subject: [PATCH 03/10] tests: Expand coverage for environment variable
3af087
 overrides
3af087
3af087
This checks that `flatpak run --env=` takes precedence over
3af087
`flatpak override --env=`, and that environment variables don't get
3af087
onto the bwrap command-line (which would be information disclosure
3af087
if their values are secret).
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
---
3af087
 tests/test-override.sh | 68 +++++++++++++++++++++++++++++++++++++++++-
3af087
 1 file changed, 67 insertions(+), 1 deletion(-)
3af087
3af087
diff --git a/tests/test-override.sh b/tests/test-override.sh
3af087
index 93009ee3..1213bbd4 100755
3af087
--- a/tests/test-override.sh
3af087
+++ b/tests/test-override.sh
3af087
@@ -12,7 +12,7 @@ reset_overrides () {
3af087
     assert_file_empty info
3af087
 }
3af087
 
3af087
-echo "1..13"
3af087
+echo "1..15"
3af087
 
3af087
 setup_repo
3af087
 install_repo
3af087
@@ -65,14 +65,80 @@ reset_overrides
3af087
 
3af087
 ${FLATPAK} override --user --env=FOO=BAR org.test.Hello
3af087
 ${FLATPAK} override --user --env=BAR= org.test.Hello
3af087
+# TODO: A future commit will add a way to avoid this ever being present in argv
3af087
+${FLATPAK} override --user --env=SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6 org.test.Hello
3af087
+# TMPDIR and TZDIR are filtered out by ld.so for setuid processes,
3af087
+# so setting these gives us a way to verify that we can pass them through
3af087
+# a setuid bwrap (without special-casing them, as we previously did for
3af087
+# TMPDIR).
3af087
+${FLATPAK} override --user --env=TMPDIR=/nonexistent/tmp org.test.Hello
3af087
+${FLATPAK} override --user --env=TZDIR=/nonexistent/tz org.test.Hello
3af087
 ${FLATPAK} override --user --show org.test.Hello > override
3af087
 
3af087
 assert_file_has_content override "^\[Environment\]$"
3af087
 assert_file_has_content override "^FOO=BAR$"
3af087
 assert_file_has_content override "^BAR=$"
3af087
+assert_file_has_content override "^SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6$"
3af087
+assert_file_has_content override "^TMPDIR=/nonexistent/tmp$"
3af087
+assert_file_has_content override "^TZDIR=/nonexistent/tz$"
3af087
 
3af087
 echo "ok override --env"
3af087
 
3af087
+if skip_one_without_bwrap "sandbox environment variables"; then
3af087
+  :
3af087
+else
3af087
+  ${FLATPAK} run --command=bash org.test.Hello \
3af087
+      -c 'echo "FOO=$FOO"; echo "BAR=$BAR"; echo "SECRET_TOKEN=$SECRET_TOKEN"; echo "TMPDIR=$TMPDIR"; echo "TZDIR=$TZDIR"' > out
3af087
+  assert_file_has_content out '^FOO=BAR$'
3af087
+  assert_file_has_content out '^BAR=$'
3af087
+  assert_file_has_content out '^SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6$'
3af087
+  # The variables that would be filtered out by a setuid bwrap get set
3af087
+  assert_file_has_content out '^TZDIR=/nonexistent/tz$'
3af087
+  assert_file_has_content out '^TMPDIR=/nonexistent/tmp$'
3af087
+  ${FLATPAK} run --command=cat org.test.Hello -- /proc/1/cmdline > out
3af087
+  # The secret doesn't end up in bubblewrap's cmdline where other users
3af087
+  # could see it
3af087
+  assert_not_file_has_content out 3047225e-5e38-4357-b21c-eac83b7e8ea6
3af087
+
3af087
+  ok "sandbox environment variables"
3af087
+fi
3af087
+
3af087
+reset_overrides
3af087
+
3af087
+if skip_one_without_bwrap "temporary environment variables"; then
3af087
+  :
3af087
+else
3af087
+  ${FLATPAK} override --user --env=FOO=wrong org.test.Hello
3af087
+  ${FLATPAK} override --user --env=BAR=wrong org.test.Hello
3af087
+  ${FLATPAK} override --user --env=SECRET_TOKEN=wrong org.test.Hello
3af087
+  ${FLATPAK} override --user --env=TMPDIR=/nonexistent/wrong org.test.Hello
3af087
+  ${FLATPAK} override --user --env=TZDIR=/nonexistent/wrong org.test.Hello
3af087
+  ${FLATPAK} override --user --show org.test.Hello > override
3af087
+
3af087
+  ${FLATPAK} run --command=bash \
3af087
+      --env=FOO=BAR \
3af087
+      --env=BAR= \
3af087
+      --env=SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6 \
3af087
+      --env=TMPDIR=/nonexistent/tmp \
3af087
+      --env=TZDIR=/nonexistent/tz \
3af087
+      org.test.Hello \
3af087
+      -c 'echo "FOO=$FOO"; echo "BAR=$BAR"; echo "SECRET_TOKEN=$SECRET_TOKEN"; echo "TMPDIR=$TMPDIR"; echo "TZDIR=$TZDIR"' > out
3af087
+  # The versions from `flatpak run` overrule `flatpak override`
3af087
+  assert_file_has_content out '^FOO=BAR$'
3af087
+  assert_file_has_content out '^BAR=$'
3af087
+  assert_file_has_content out '^SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6$'
3af087
+  assert_file_has_content out '^TZDIR=/nonexistent/tz$'
3af087
+  assert_file_has_content out '^TMPDIR=/nonexistent/tmp$'
3af087
+  ${FLATPAK} run --command=cat org.test.Hello -- /proc/1/cmdline > out
3af087
+  # The secret doesn't end up in bubblewrap's cmdline where other users
3af087
+  # could see it
3af087
+  assert_not_file_has_content out 3047225e-5e38-4357-b21c-eac83b7e8ea6
3af087
+
3af087
+  ok "temporary environment variables"
3af087
+fi
3af087
+
3af087
+reset_overrides
3af087
+
3af087
 ${FLATPAK} override --user --filesystem=home org.test.Hello
3af087
 ${FLATPAK} override --user --filesystem=xdg-desktop/foo:create org.test.Hello
3af087
 ${FLATPAK} override --user --filesystem=xdg-config:ro org.test.Hello
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From 1b80c139f02cff6eeec39ecdf1a8f531ab6d7d9b Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Sun, 10 Jan 2021 16:18:58 +0000
3af087
Subject: [PATCH 04/10] context: Add --env-fd option
3af087
3af087
This allows environment variables to be added to the context without
3af087
making their values visible to processes running under a different uid,
3af087
which might be significant if the variable's value is a token or some
3af087
other secret value.
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
---
3af087
 common/flatpak-context.c     | 60 ++++++++++++++++++++++++++++++++++++
3af087
 doc/flatpak-build-finish.xml | 18 +++++++++++
3af087
 doc/flatpak-build.xml        | 18 +++++++++++
3af087
 doc/flatpak-override.xml     | 18 +++++++++++
3af087
 doc/flatpak-run.xml          | 18 +++++++++++
3af087
 5 files changed, 132 insertions(+)
3af087
3af087
diff --git a/common/flatpak-context.c b/common/flatpak-context.c
3af087
index 462134aa..7e1bc46c 100644
3af087
--- a/common/flatpak-context.c
3af087
+++ b/common/flatpak-context.c
3af087
@@ -1039,6 +1039,65 @@ option_env_cb (const gchar *option_name,
3af087
   return TRUE;
3af087
 }
3af087
 
3af087
+static gboolean
3af087
+option_env_fd_cb (const gchar *option_name,
3af087
+                  const gchar *value,
3af087
+                  gpointer     data,
3af087
+                  GError     **error)
3af087
+{
3af087
+  FlatpakContext *context = data;
3af087
+  g_autoptr(GBytes) env_block = NULL;
3af087
+  gsize remaining;
3af087
+  const char *p;
3af087
+  guint64 fd;
3af087
+  gchar *endptr;
3af087
+
3af087
+  fd = g_ascii_strtoull (value, &endptr, 10);
3af087
+
3af087
+  if (endptr == NULL || *endptr != '\0' || fd > G_MAXINT)
3af087
+    return glnx_throw (error, "Not a valid file descriptor: %s", value);
3af087
+
3af087
+  env_block = glnx_fd_readall_bytes ((int) fd, NULL, error);
3af087
+
3af087
+  if (env_block == NULL)
3af087
+    return FALSE;
3af087
+
3af087
+  p = g_bytes_get_data (env_block, &remaining);
3af087
+
3af087
+  /* env_block might not be \0-terminated */
3af087
+  while (remaining > 0)
3af087
+    {
3af087
+      size_t len = strnlen (p, remaining);
3af087
+      const char *equals;
3af087
+
3af087
+      g_assert (len <= remaining);
3af087
+
3af087
+      equals = memchr (p, '=', len);
3af087
+
3af087
+      if (equals == NULL || equals == p)
3af087
+        return glnx_throw (error,
3af087
+                           "Environment variable must be given in the form VARIABLE=VALUE, not %.*s", (int) len, p);
3af087
+
3af087
+      flatpak_context_set_env_var (context,
3af087
+                                   g_strndup (p, equals - p),
3af087
+                                   g_strndup (equals + 1, len - (equals - p) - 1));
3af087
+      p += len;
3af087
+      remaining -= len;
3af087
+
3af087
+      if (remaining > 0)
3af087
+        {
3af087
+          g_assert (*p == '\0');
3af087
+          p += 1;
3af087
+          remaining -= 1;
3af087
+        }
3af087
+    }
3af087
+
3af087
+  if (fd >= 3)
3af087
+    close (fd);
3af087
+
3af087
+  return TRUE;
3af087
+}
3af087
+
3af087
 static gboolean
3af087
 option_own_name_cb (const gchar *option_name,
3af087
                     const gchar *value,
3af087
@@ -1236,6 +1295,7 @@ static GOptionEntry context_options[] = {
3af087
   { "filesystem", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_filesystem_cb, N_("Expose filesystem to app (:ro for read-only)"), N_("FILESYSTEM[:ro]") },
3af087
   { "nofilesystem", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_nofilesystem_cb, N_("Don't expose filesystem to app"), N_("FILESYSTEM") },
3af087
   { "env", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_env_cb, N_("Set environment variable"), N_("VAR=VALUE") },
3af087
+  { "env-fd", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_env_fd_cb, N_("Read environment variables in env -0 format from FD"), N_("FD") },
3af087
   { "own-name", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_own_name_cb, N_("Allow app to own name on the session bus"), N_("DBUS_NAME") },
3af087
   { "talk-name", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_talk_name_cb, N_("Allow app to talk to name on the session bus"), N_("DBUS_NAME") },
3af087
   { "no-talk-name", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK, &option_no_talk_name_cb, N_("Don't allow app to talk to name on the session bus"), N_("DBUS_NAME") },
3af087
diff --git a/doc/flatpak-build-finish.xml b/doc/flatpak-build-finish.xml
3af087
index d92eeb4d..2e01cd3e 100644
3af087
--- a/doc/flatpak-build-finish.xml
3af087
+++ b/doc/flatpak-build-finish.xml
3af087
@@ -286,6 +286,24 @@ key=v1;v2;
3af087
                 </para></listitem>
3af087
             </varlistentry>
3af087
 
3af087
+            <varlistentry>
3af087
+                <term><option>--env-fd=<replaceable>FD</replaceable></option></term>
3af087
+
3af087
+                <listitem><para>
3af087
+                    Read environment variables from the file descriptor
3af087
+                    <replaceable>FD</replaceable>, and set them as if
3af087
+                    via <option>--env</option>. This can be used to avoid
3af087
+                    environment variables and their values becoming visible
3af087
+                    to other users.
3af087
+                </para><para>
3af087
+                    Each environment variable is in the form
3af087
+                    <replaceable>VAR</replaceable>=<replaceable>VALUE</replaceable>
3af087
+                    followed by a zero byte. This is the same format used by
3af087
+                    <literal>env -0</literal> and
3af087
+                    <filename>/proc/*/environ</filename>.
3af087
+                </para></listitem>
3af087
+            </varlistentry>
3af087
+
3af087
             <varlistentry>
3af087
                 <term><option>--own-name=NAME</option></term>
3af087
 
3af087
diff --git a/doc/flatpak-build.xml b/doc/flatpak-build.xml
3af087
index 55e3ec89..e3eb9bc8 100644
3af087
--- a/doc/flatpak-build.xml
3af087
+++ b/doc/flatpak-build.xml
3af087
@@ -288,6 +288,24 @@ key=v1;v2;
3af087
                 </para></listitem>
3af087
             </varlistentry>
3af087
 
3af087
+            <varlistentry>
3af087
+                <term><option>--env-fd=<replaceable>FD</replaceable></option></term>
3af087
+
3af087
+                <listitem><para>
3af087
+                    Read environment variables from the file descriptor
3af087
+                    <replaceable>FD</replaceable>, and set them as if
3af087
+                    via <option>--env</option>. This can be used to avoid
3af087
+                    environment variables and their values becoming visible
3af087
+                    to other users.
3af087
+                </para><para>
3af087
+                    Each environment variable is in the form
3af087
+                    <replaceable>VAR</replaceable>=<replaceable>VALUE</replaceable>
3af087
+                    followed by a zero byte. This is the same format used by
3af087
+                    <literal>env -0</literal> and
3af087
+                    <filename>/proc/*/environ</filename>.
3af087
+                </para></listitem>
3af087
+            </varlistentry>
3af087
+
3af087
             <varlistentry>
3af087
                 <term><option>--own-name=NAME</option></term>
3af087
 
3af087
diff --git a/doc/flatpak-override.xml b/doc/flatpak-override.xml
3af087
index 8f131575..137e1251 100644
3af087
--- a/doc/flatpak-override.xml
3af087
+++ b/doc/flatpak-override.xml
3af087
@@ -262,6 +262,24 @@ key=v1;v2;
3af087
                 </para></listitem>
3af087
             </varlistentry>
3af087
 
3af087
+            <varlistentry>
3af087
+                <term><option>--env-fd=<replaceable>FD</replaceable></option></term>
3af087
+
3af087
+                <listitem><para>
3af087
+                    Read environment variables from the file descriptor
3af087
+                    <replaceable>FD</replaceable>, and set them as if
3af087
+                    via <option>--env</option>. This can be used to avoid
3af087
+                    environment variables and their values becoming visible
3af087
+                    to other users.
3af087
+                </para><para>
3af087
+                    Each environment variable is in the form
3af087
+                    <replaceable>VAR</replaceable>=<replaceable>VALUE</replaceable>
3af087
+                    followed by a zero byte. This is the same format used by
3af087
+                    <literal>env -0</literal> and
3af087
+                    <filename>/proc/*/environ</filename>.
3af087
+                </para></listitem>
3af087
+            </varlistentry>
3af087
+
3af087
             <varlistentry>
3af087
                 <term><option>--own-name=NAME</option></term>
3af087
 
3af087
diff --git a/doc/flatpak-run.xml b/doc/flatpak-run.xml
3af087
index 5077c1a9..ed157341 100644
3af087
--- a/doc/flatpak-run.xml
3af087
+++ b/doc/flatpak-run.xml
3af087
@@ -402,6 +402,24 @@ key=v1;v2;
3af087
                 </para></listitem>
3af087
             </varlistentry>
3af087
 
3af087
+            <varlistentry>
3af087
+                <term><option>--env-fd=<replaceable>FD</replaceable></option></term>
3af087
+
3af087
+                <listitem><para>
3af087
+                    Read environment variables from the file descriptor
3af087
+                    <replaceable>FD</replaceable>, and set them as if
3af087
+                    via <option>--env</option>. This can be used to avoid
3af087
+                    environment variables and their values becoming visible
3af087
+                    to other users.
3af087
+                </para><para>
3af087
+                    Each environment variable is in the form
3af087
+                    <replaceable>VAR</replaceable>=<replaceable>VALUE</replaceable>
3af087
+                    followed by a zero byte. This is the same format used by
3af087
+                    <literal>env -0</literal> and
3af087
+                    <filename>/proc/*/environ</filename>.
3af087
+                </para></listitem>
3af087
+            </varlistentry>
3af087
+
3af087
             <varlistentry>
3af087
                 <term><option>--own-name=NAME</option></term>
3af087
 
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From 6c244791c912fe0c5ae2e140f251dc02c46cc0aa Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Tue, 12 Jan 2021 10:11:51 +0000
3af087
Subject: [PATCH 05/10] portal: Convert --env in extra-args into --env-fd
3af087
3af087
This hides overridden variables from the command-line, which means
3af087
processes running under other uids can't see them in /proc/*/cmdline,
3af087
which might be important if they contain secrets.
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
---
3af087
 portal/flatpak-portal.c | 51 ++++++++++++++++++++++++++++++++++++++++-
3af087
 1 file changed, 50 insertions(+), 1 deletion(-)
3af087
3af087
diff --git a/portal/flatpak-portal.c b/portal/flatpak-portal.c
3af087
index 060987b7..03ea58cc 100644
3af087
--- a/portal/flatpak-portal.c
3af087
+++ b/portal/flatpak-portal.c
3af087
@@ -249,6 +249,7 @@ typedef struct
3af087
   int         fd_map_len;
3af087
   gboolean    set_tty;
3af087
   int         tty;
3af087
+  int         env_fd;
3af087
 } ChildSetupData;
3af087
 
3af087
 static void
3af087
@@ -267,6 +268,9 @@ child_setup_func (gpointer user_data)
3af087
 
3af087
   flatpak_close_fds_workaround (3);
3af087
 
3af087
+  if (data->env_fd != -1)
3af087
+    drop_cloexec (data->env_fd);
3af087
+
3af087
   /* Unblock all signals */
3af087
   sigemptyset (&set);
3af087
   if (pthread_sigmask (SIG_SETMASK, &set, NULL) == -1)
3af087
@@ -553,6 +557,9 @@ handle_spawn (PortalFlatpak         *object,
3af087
   gboolean sandboxed;
3af087
   gboolean devel;
3af087
   gboolean expose_pids;
3af087
+  g_autoptr(GString) env_string = g_string_new ("");
3af087
+
3af087
+  child_setup_data.env_fd = -1;
3af087
 
3af087
   if (fd_list != NULL)
3af087
     fds = g_unix_fd_list_peek_fds (fd_list, &fds_len);
3af087
@@ -805,7 +812,49 @@ handle_spawn (PortalFlatpak         *object,
3af087
   else
3af087
     {
3af087
       for (i = 0; extra_args != NULL && extra_args[i] != NULL; i++)
3af087
-        g_ptr_array_add (flatpak_argv, g_strdup (extra_args[i]));
3af087
+        {
3af087
+          if (g_str_has_prefix (extra_args[i], "--env="))
3af087
+            {
3af087
+              const char *var_val = extra_args[i] + strlen ("--env=");
3af087
+
3af087
+              if (var_val[0] == '\0' || var_val[0] == '=')
3af087
+                {
3af087
+                  g_warning ("Environment variable in extra-args has empty name");
3af087
+                  continue;
3af087
+                }
3af087
+
3af087
+              if (strchr (var_val, '=') == NULL)
3af087
+                {
3af087
+                  g_warning ("Environment variable in extra-args has no value");
3af087
+                  continue;
3af087
+                }
3af087
+
3af087
+              g_string_append (env_string, var_val);
3af087
+              g_string_append_c (env_string, '\0');
3af087
+            }
3af087
+          else
3af087
+            {
3af087
+              g_ptr_array_add (flatpak_argv, g_strdup (extra_args[i]));
3af087
+            }
3af087
+        }
3af087
+    }
3af087
+
3af087
+  if (env_string->len > 0)
3af087
+    {
3af087
+      g_auto(GLnxTmpfile) env_tmpf  = { 0, };
3af087
+
3af087
+      if (!flatpak_buffer_to_sealed_memfd_or_tmpfile (&env_tmpf, "environ",
3af087
+                                                      env_string->str,
3af087
+                                                      env_string->len, &error))
3af087
+        {
3af087
+          g_dbus_method_invocation_return_gerror (invocation, error);
3af087
+          return G_DBUS_METHOD_INVOCATION_HANDLED;
3af087
+        }
3af087
+
3af087
+      child_setup_data.env_fd = glnx_steal_fd (&env_tmpf.fd);
3af087
+      g_ptr_array_add (flatpak_argv,
3af087
+                       g_strdup_printf ("--env-fd=%d",
3af087
+                                        child_setup_data.env_fd));
3af087
     }
3af087
 
3af087
   expose_pids = (arg_flags & FLATPAK_SPAWN_FLAGS_EXPOSE_PIDS) != 0;
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From f1725cd4fc6164d33f7a92bba673e8718655c1f1 Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Mon, 11 Jan 2021 12:25:50 +0000
3af087
Subject: [PATCH 06/10] tests: Exercise --env-fd
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
---
3af087
 tests/test-override.sh | 18 ++++++++++--------
3af087
 1 file changed, 10 insertions(+), 8 deletions(-)
3af087
3af087
diff --git a/tests/test-override.sh b/tests/test-override.sh
3af087
index 1213bbd4..1c7dafd3 100755
3af087
--- a/tests/test-override.sh
3af087
+++ b/tests/test-override.sh
3af087
@@ -65,14 +65,16 @@ reset_overrides
3af087
 
3af087
 ${FLATPAK} override --user --env=FOO=BAR org.test.Hello
3af087
 ${FLATPAK} override --user --env=BAR= org.test.Hello
3af087
-# TODO: A future commit will add a way to avoid this ever being present in argv
3af087
-${FLATPAK} override --user --env=SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6 org.test.Hello
3af087
+# --env-fd with terminating \0 (strictly as documented).
3af087
+printf '%s\0' "SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6" > env.3
3af087
+# --env-fd without terminating \0 (which we also accept).
3af087
 # TMPDIR and TZDIR are filtered out by ld.so for setuid processes,
3af087
 # so setting these gives us a way to verify that we can pass them through
3af087
 # a setuid bwrap (without special-casing them, as we previously did for
3af087
 # TMPDIR).
3af087
-${FLATPAK} override --user --env=TMPDIR=/nonexistent/tmp org.test.Hello
3af087
-${FLATPAK} override --user --env=TZDIR=/nonexistent/tz org.test.Hello
3af087
+printf '%s\0%s' "TMPDIR=/nonexistent/tmp" "TZDIR=/nonexistent/tz" > env.4
3af087
+${FLATPAK} override --user --env-fd=3 --env-fd=4 org.test.Hello \
3af087
+    3
3af087
 ${FLATPAK} override --user --show org.test.Hello > override
3af087
 
3af087
 assert_file_has_content override "^\[Environment\]$"
3af087
@@ -118,11 +120,11 @@ else
3af087
   ${FLATPAK} run --command=bash \
3af087
       --env=FOO=BAR \
3af087
       --env=BAR= \
3af087
-      --env=SECRET_TOKEN=3047225e-5e38-4357-b21c-eac83b7e8ea6 \
3af087
-      --env=TMPDIR=/nonexistent/tmp \
3af087
-      --env=TZDIR=/nonexistent/tz \
3af087
+      --env-fd=3 \
3af087
+      --env-fd=4 \
3af087
       org.test.Hello \
3af087
-      -c 'echo "FOO=$FOO"; echo "BAR=$BAR"; echo "SECRET_TOKEN=$SECRET_TOKEN"; echo "TMPDIR=$TMPDIR"; echo "TZDIR=$TZDIR"' > out
3af087
+      -c 'echo "FOO=$FOO"; echo "BAR=$BAR"; echo "SECRET_TOKEN=$SECRET_TOKEN"; echo "TMPDIR=$TMPDIR"; echo "TZDIR=$TZDIR"' \
3af087
+      3<env.3 4<env.4 > out
3af087
   # The versions from `flatpak run` overrule `flatpak override`
3af087
   assert_file_has_content out '^FOO=BAR$'
3af087
   assert_file_has_content out '^BAR=$'
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From adcb3b2608caa1ab6647f59e31a9084ec0b66bbb Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Sun, 10 Jan 2021 16:25:29 +0000
3af087
Subject: [PATCH 07/10] portal: Do not use caller-supplied variables in
3af087
 environment
3af087
3af087
If the caller specifies a variable that can be used to inject arbitrary
3af087
code into processes, we must not allow it to enter the environment
3af087
block used to run `flatpak run`, which runs unsandboxed.
3af087
3af087
This change requires the previous commit "context: Add --env-fd option",
3af087
which adds infrastructure used here.
3af087
3af087
To be secure, this change also requires the previous commit
3af087
"run: Convert all environment variables into bwrap arguments", which
3af087
protects a non-setuid bwrap(1) from the same attack.
3af087
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
---
3af087
 portal/flatpak-portal.c | 28 +++++++++++++++++++++++++++-
3af087
 1 file changed, 27 insertions(+), 1 deletion(-)
3af087
3af087
diff --git a/portal/flatpak-portal.c b/portal/flatpak-portal.c
3af087
index 03ea58cc..fe8a514f 100644
3af087
--- a/portal/flatpak-portal.c
3af087
+++ b/portal/flatpak-portal.c
3af087
@@ -760,6 +760,13 @@ handle_spawn (PortalFlatpak         *object,
3af087
   else
3af087
     env = g_get_environ ();
3af087
 
3af087
+  /* Let the environment variables given by the caller override the ones
3af087
+   * from extra_args. Don't add them to @env, because they are controlled
3af087
+   * by our caller, which might be trying to use them to inject code into
3af087
+   * flatpak(1); add them to the environment block instead.
3af087
+   *
3af087
+   * We don't use --env= here, so that if the values are something that
3af087
+   * should not be exposed to other uids, they can remain confidential. */
3af087
   n_envs = g_variant_n_children (arg_envs);
3af087
   for (i = 0; i < n_envs; i++)
3af087
     {
3af087
@@ -767,7 +774,26 @@ handle_spawn (PortalFlatpak         *object,
3af087
       const char *val = NULL;
3af087
       g_variant_get_child (arg_envs, i, "{&s&s}", &var, &val;;
3af087
 
3af087
-      env = g_environ_setenv (env, var, val, TRUE);
3af087
+      if (var[0] == '\0')
3af087
+        {
3af087
+          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
3af087
+                                                 G_DBUS_ERROR_INVALID_ARGS,
3af087
+                                                 "Environment variable cannot have empty name");
3af087
+          return G_DBUS_METHOD_INVOCATION_HANDLED;
3af087
+        }
3af087
+
3af087
+      if (strchr (var, '=') != NULL)
3af087
+        {
3af087
+          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
3af087
+                                                 G_DBUS_ERROR_INVALID_ARGS,
3af087
+                                                 "Environment variable name cannot contain '='");
3af087
+          return G_DBUS_METHOD_INVOCATION_HANDLED;
3af087
+        }
3af087
+
3af087
+      g_string_append (env_string, var);
3af087
+      g_string_append_c (env_string, '=');
3af087
+      g_string_append (env_string, val);
3af087
+      g_string_append_c (env_string, '\0');
3af087
     }
3af087
 
3af087
   g_ptr_array_add (flatpak_argv, g_strdup ("flatpak"));
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From 1fb13b40cea72ded0ca804a990e84b12454a30a1 Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Mon, 11 Jan 2021 12:48:01 +0000
3af087
Subject: [PATCH 08/10] tests: Assert that --env= does not go in `flatpak run`
3af087
 or bwrap environ
3af087
3af087
For the portal's use of --env-fd= to be safe, we want the environment
3af087
variables that it sets to end up in the environment for the program
3af087
that is run by `bwrap` as process 2, but they must not go into the
3af087
environment that gets used to run `flatpak run` or `bwrap`. Assert
3af087
that this is the case.
3af087
3af087
For completeness, we're testing both --env= and --env-fd= here,
3af087
even though the earlier commit
3af087
"portal: Do not use caller-supplied variables in environment"
3af087
always uses --env-fd=.
3af087
3af087
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
---
3af087
 tests/Makefile.am.inc  | 10 ++++++++++
3af087
 tests/libpreload.c     | 31 +++++++++++++++++++++++++++++++
3af087
 tests/test-override.sh | 18 ++++++++++++++++++
3af087
 3 files changed, 59 insertions(+)
3af087
 create mode 100644 tests/libpreload.c
3af087
3af087
diff --git a/tests/Makefile.am.inc b/tests/Makefile.am.inc
3af087
index 15f52148..c95ed3bc 100644
3af087
--- a/tests/Makefile.am.inc
3af087
+++ b/tests/Makefile.am.inc
3af087
@@ -156,6 +156,16 @@ dist_installed_test_data = \
3af087
 	tests/org.flatpak.Authenticator.test.service.in \
3af087
 	$(NULL)
3af087
 
3af087
+test_ltlibraries = tests/libpreload.la
3af087
+
3af087
+tests_libpreload_la_SOURCES = tests/libpreload.c
3af087
+tests_libpreload_la_LDFLAGS = \
3af087
+	-avoid-version \
3af087
+	-module \
3af087
+	-no-undefined \
3af087
+	-rpath $(installed_testdir) \
3af087
+	$(NULL)
3af087
+
3af087
 installed_test_keyringdir = $(installed_testdir)/test-keyring
3af087
 installed_test_keyring2dir = $(installed_testdir)/test-keyring2
3af087
 
3af087
diff --git a/tests/libpreload.c b/tests/libpreload.c
3af087
new file mode 100644
3af087
index 00000000..a640a945
3af087
--- /dev/null
3af087
+++ b/tests/libpreload.c
3af087
@@ -0,0 +1,31 @@
3af087
+/*
3af087
+ * Copyright 2021 Collabora Ltd.
3af087
+ * SPDX-License-Identifier: LGPL-2-or-later
3af087
+ */
3af087
+
3af087
+#include <stdio.h>
3af087
+#include <stdlib.h>
3af087
+#include <sys/types.h>
3af087
+#include <sys/stat.h>
3af087
+#include <unistd.h>
3af087
+
3af087
+__attribute__((constructor)) static void
3af087
+ctor (void)
3af087
+{
3af087
+  pid_t me = getpid ();
3af087
+  struct stat buf;
3af087
+
3af087
+  fprintf (stderr, "LD_PRELOAD module got loaded by process %d\n", me);
3af087
+
3af087
+  if (stat ("/.flatpak-info", &buf) == 0)
3af087
+    {
3af087
+      fprintf (stderr, "OK: pid %d is in a Flatpak sandbox\n", me);
3af087
+    }
3af087
+  else
3af087
+    {
3af087
+      /* If the --env=LD_PRELOAD had come from a call to flatpak-portal,
3af087
+       * then this would be a sandbox escape (GHSA-4ppf-fxf6-vxg2). */
3af087
+      fprintf (stderr, "Error: pid %d is not in a Flatpak sandbox\n", me);
3af087
+      abort ();
3af087
+    }
3af087
+}
3af087
diff --git a/tests/test-override.sh b/tests/test-override.sh
3af087
index 1c7dafd3..47416a6d 100755
3af087
--- a/tests/test-override.sh
3af087
+++ b/tests/test-override.sh
3af087
@@ -3,6 +3,11 @@
3af087
 set -euo pipefail
3af087
 
3af087
 . $(dirname $0)/libtest.sh
3af087
+if [ -e "${test_builddir}/.libs/libpreload.so" ]; then
3af087
+    install "${test_builddir}/.libs/libpreload.so" "${test_tmpdir}"
3af087
+else
3af087
+    install "${test_builddir}/libpreload.so" "${test_tmpdir}"
3af087
+fi
3af087
 
3af087
 skip_revokefs_without_fuse
3af087
 
3af087
@@ -118,6 +123,7 @@ else
3af087
   ${FLATPAK} override --user --show org.test.Hello > override
3af087
 
3af087
   ${FLATPAK} run --command=bash \
3af087
+      --filesystem="${test_tmpdir}" \
3af087
       --env=FOO=BAR \
3af087
       --env=BAR= \
3af087
       --env-fd=3 \
3af087
@@ -136,6 +142,18 @@ else
3af087
   # could see it
3af087
   assert_not_file_has_content out 3047225e-5e38-4357-b21c-eac83b7e8ea6
3af087
 
3af087
+  # libpreload.so will abort() if it gets loaded into the `flatpak run`
3af087
+  # or `bwrap` processes, so if this succeeds, everything's OK
3af087
+  ${FLATPAK} run --command=bash \
3af087
+      --filesystem="${test_tmpdir}" \
3af087
+      --env=LD_PRELOAD="${test_tmpdir}/libpreload.so" \
3af087
+      org.test.Hello -c ''
3af087
+  printf '%s\0' "LD_PRELOAD=${test_tmpdir}/libpreload.so" > env.ldpreload
3af087
+  ${FLATPAK} run --command=bash \
3af087
+      --filesystem="${test_tmpdir}" \
3af087
+      --env-fd=3 \
3af087
+      org.test.Hello -c '' 3
3af087
+
3af087
   ok "temporary environment variables"
3af087
 fi
3af087
 
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From e4a3720e49aa38d0ed07106663499f957c070847 Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Mon, 18 Jan 2021 17:52:13 +0000
3af087
Subject: [PATCH 09/10] build: Convert environment into a sequence of bwrap
3af087
 arguments
3af087
3af087
This means we can systematically pass the environment variables
3af087
through bwrap(1), even if it is setuid and thus is filtering out
3af087
security-sensitive environment variables. bwrap itself ends up being
3af087
run with an empty environment instead.
3af087
3af087
This fixes a regression when CVE-2021-21261 was fixed: before the
3af087
CVE fixes, LD_LIBRARY_PATH would have been passed through like this
3af087
and appeared in the `flatpak build` shell, but during the CVE fixes,
3af087
the special case that protected LD_LIBRARY_PATH was removed in favour
3af087
of the more general flatpak_bwrap_envp_to_args(). That reasoning only
3af087
works if we use flatpak_bwrap_envp_to_args(), consistently, everywhere
3af087
that we run the potentially-setuid bwrap.
3af087
3af087
Fixes: 6d1773d2 "run: Convert all environment variables into bwrap arguments"
3af087
Resolves: https://github.com/flatpak/flatpak/issues/4080
3af087
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=980323
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
---
3af087
 app/flatpak-builtins-build.c | 2 ++
3af087
 1 file changed, 2 insertions(+)
3af087
3af087
diff --git a/app/flatpak-builtins-build.c b/app/flatpak-builtins-build.c
3af087
index 5ecd2ef3..8616f3c8 100644
3af087
--- a/app/flatpak-builtins-build.c
3af087
+++ b/app/flatpak-builtins-build.c
3af087
@@ -569,6 +569,8 @@ flatpak_builtin_build (int argc, char **argv, GCancellable *cancellable, GError
3af087
                               NULL);
3af087
     }
3af087
 
3af087
+  flatpak_bwrap_envp_to_args (bwrap);
3af087
+
3af087
   if (!flatpak_bwrap_bundle_args (bwrap, 1, -1, FALSE, error))
3af087
     return FALSE;
3af087
 
3af087
-- 
3af087
2.29.2
3af087
3af087
3af087
From f830b97e81a626a43b160ccb5dad4fe934ab03fa Mon Sep 17 00:00:00 2001
3af087
From: Simon McVittie <smcv@collabora.com>
3af087
Date: Mon, 18 Jan 2021 18:07:38 +0000
3af087
Subject: [PATCH 10/10] dir: Pass environment via bwrap --setenv when running
3af087
 apply_extra
3af087
3af087
This means we can systematically pass the environment variables
3af087
through bwrap(1), even if it is setuid and thus is filtering out
3af087
security-sensitive environment variables. bwrap ends up being
3af087
run with an empty environment instead.
3af087
3af087
As with the previous commit, this regressed while fixing CVE-2021-21261.
3af087
3af087
Fixes: 6d1773d2 "run: Convert all environment variables into bwrap arguments"
3af087
Signed-off-by: Simon McVittie <smcv@collabora.com>
3af087
---
3af087
 common/flatpak-dir.c | 2 ++
3af087
 1 file changed, 2 insertions(+)
3af087
3af087
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
3af087
index c3ab8bcf..030b8a31 100644
3af087
--- a/common/flatpak-dir.c
3af087
+++ b/common/flatpak-dir.c
3af087
@@ -7751,6 +7751,8 @@ apply_extra_data (FlatpakDir   *self,
3af087
                                          app_context, NULL, NULL, NULL, cancellable, error))
3af087
     return FALSE;
3af087
 
3af087
+  flatpak_bwrap_envp_to_args (bwrap);
3af087
+
3af087
   flatpak_bwrap_add_arg (bwrap, "/app/bin/apply_extra");
3af087
 
3af087
   flatpak_bwrap_finish (bwrap);
3af087
-- 
3af087
2.29.2
3af087