CVE-2021-43860: Incorrect Default Permissions (CWE-276)
value: Flatpak is a Linux application sandboxing and distribution framework. Prior to versions 1.12.3 and 1.10.6, Flatpak doesn't properly validate that the permissions displayed to the user for an app at install time match the actual permissions granted to the app at runtime, in the case that there's a null byte in the metadata file of an app. Therefore apps can grant themselves permissions without the consent of the user. Flatpak shows permissions to the user during install by reading them from the "xa.metadata" key in the commit metadata. This cannot contain a null terminator, because it is an untrusted GVariant. Flatpak compares these permissions to the *actual* metadata, from the "metadata" file to ensure it wasn't lied to. However, the actual metadata contents are loaded in several places where they are read as simple C-style strings. That means that, if the metadata file includes a null terminator, only the content of the file from *before* the terminator gets compared to xa.metadata. Thus, any permissions that appear in the metadata file after a null terminator are applied at runtime but not shown to the user. So maliciously crafted apps can give themselves hidden permissions. Users who have Flatpaks installed from untrusted sources are at risk in case the Flatpak has a maliciously crafted metadata file, either initially or in an update. This issue is patched in versions 1.12.3 and 1.10.6. As a workaround, users can manually check the permissions of installed apps by checking the metadata file or the xa.metadata key on the commit metadata.
CWE description: During installation, installed file permissions are set to allow anyone to modify those files.
CVSS v3 base score: 8.6
Published: 2022-01-12T22:15Z
Repository: https://github.com/flatpak/flatpak
Language: C
Files changed: common/flatpak-dir.c
Commit date: 2022-01-12 19:48:16 +0100
Commit message: Require metadata in commit also for OCI remotes

This was disables a long time ago because the fedora remotes didn't
contain metadata, but that has been added since then. Requiring fixes
a security concern where an app claims to require no permissions (by
having no metadata in commit) but then actually requires permissions
in the installed app.

Diff: diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
index 05aa91a11..e8bf106cc 100644
--- a/common/flatpak-dir.c
+++ b/common/flatpak-dir.c
@@ -1793,11 +1793,10 @@ flatpak_get_user_base_dir_location (void)
 static gboolean
 validate_commit_metadata (GVariant   *commit_data,
                           const char *ref,
                           const char *required_metadata,
                           gsize       required_metadata_size,
-                          gboolean   require_xa_metadata,
                           GError   **error)
 {
   g_autoptr(GVariant) commit_metadata = NULL;
   g_autoptr(GVariant) xa_metadata_v = NULL;
   const char *xa_metadata = NULL;
@@ -1812,13 +1811,13 @@ validate_commit_metadata (GVariant   *commit_data,
                                               G_VARIANT_TYPE_STRING);
       if (xa_metadata_v)
         xa_metadata = g_variant_get_string (xa_metadata_v, &xa_metadata_size);
     }
 
-  if ((xa_metadata == NULL && require_xa_metadata) ||
-      (xa_metadata != NULL && (xa_metadata_size != required_metadata_size ||
-                               memcmp (xa_metadata, required_metadata, xa_metadata_size) != 0)))
+  if (xa_metadata == NULL ||
+      xa_metadata_size != required_metadata_size ||
+      memcmp (xa_metadata, required_metadata, xa_metadata_size) != 0)
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
                    _("Commit metadata for %s not matching expected metadata"), ref);
       return FALSE;
     }
@@ -5846,11 +5845,10 @@ flatpak_dir_pull (FlatpakDir                           *self,
       if (!ostree_repo_load_commit (repo, rev, &commit_data, NULL, error) ||
           !validate_commit_metadata (commit_data,
                                      ref,
                                      (const char *)g_bytes_get_data (require_metadata, NULL),
                                      g_bytes_get_size (require_metadata),
-                                     TRUE,
                                      error))
         goto out;
     }
 
   if (!flatpak_dir_pull_extra_data (self, repo,
@@ -8171,11 +8169,10 @@ flatpak_dir_deploy (FlatpakDir          *self,
   g_autoptr(GVariant) commit_metadata = NULL;
   g_auto(GLnxLockFile) lock = { 0, };
   g_autoptr(GFile) metadata_file = NULL;
   g_autofree char *metadata_contents = NULL;
   gsize metadata_size = 0;
-  gboolean is_oci;
   const char *flatpak;
 
   if (!flatpak_dir_ensure_repo (self, cancellable, error))
     return FALSE;
 
@@ -8397,16 +8394,13 @@ flatpak_dir_deploy (FlatpakDir          *self,
     }
 
   /* Check the metadata in the commit to make sure it matches the actual
    * deployed metadata, in case we relied on the one in the commit for
    * a decision
-   * Note: For historical reason we don't enforce commits to contain xa.metadata
-   * since this was lacking in fedora builds.
    */
-  is_oci = flatpak_dir_get_remote_oci (self, origin);
   if (!validate_commit_metadata (commit_data, flatpak_decomposed_get_ref (ref),
-                                 metadata_contents, metadata_size, !is_oci, error))
+                                 metadata_contents, metadata_size, error))
     return FALSE;
 
   dotref = g_file_resolve_relative_path (checkoutdir, "files/.ref");
   if (!g_file_replace_contents (dotref, "", 0, NULL, FALSE,
                                 G_FILE_CREATE_REPLACE_DESTINATION, NULL, cancellable, error))

Vulnerable code:                           gboolean   require_xa_metadata,
  if ((xa_metadata == NULL && require_xa_metadata) ||
      (xa_metadata != NULL && (xa_metadata_size != required_metadata_size ||
                               memcmp (xa_metadata, required_metadata, xa_metadata_size) != 0)))
                                     TRUE,
  gboolean is_oci;
   * Note: For historical reason we don't enforce commits to contain xa.metadata
   * since this was lacking in fedora builds.
  is_oci = flatpak_dir_get_remote_oci (self, origin);
                                 metadata_contents, metadata_size, !is_oci, error))
Fixed code:   if (xa_metadata == NULL ||
      xa_metadata_size != required_metadata_size ||
      memcmp (xa_metadata, required_metadata, xa_metadata_size) != 0)
                                 metadata_contents, metadata_size, error))