CVE-2021-43860 (C, published 2022-01-12T22:15Z)
Description: 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.
Severity: MEDIUM (CVSS v3 base score: 8.6)
Weakness: CWE-276 \u2014 Incorrect Default Permissions
During installation, installed file permissions are set to allow anyone to modify those files.
Repository: https://github.com/flatpak/flatpak
Files changed:
common/flatpak-dir.c
common/flatpak-transaction.c
common/flatpak-utils.c
Commit message: Fix metadata file contents after null terminators being ignored

In particular, if a null terminator is placed inside the metadata file,
Flatpak will only compare the text *before* it to the value of
xa.metadata, but the full file will be parsed when permissions are set
at runtime. This means that any app can include a null terminator in its
permissions metadata, and Flatpak will only show the user the
permissions *preceding* the terminator during install, but the
permissions *after* the terminator are applied at runtime.

Fixes GHSA-qpjc-vq3c-572j / CVE-2021-43860

Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>


Vulnerable code:
    g_variant_lookup (commit_metadata, "xa.metadata", "&s", &xa_metadata);
      (xa_metadata != NULL && g_strcmp0 (required_metadata, xa_metadata) != 0))
                                 &metadata_contents, NULL, NULL, error))
      if (!g_key_file_load_from_data (keyfile, metadata_contents, -1, 0, error))
          !validate_commit_metadata (commit_data, ref, (const char *)g_bytes_get_data (require_metadata, NULL), TRUE, error))
        return FALSE;
                            &metadata_contents, NULL, NULL, NULL))
                                      -1,
                                 metadata_contents, !is_oci, error))
    op->external_metadata = g_bytes_new (external_metadata, strlen (external_metadata) + 1);
  return g_bytes_new_take (g_steal_pointer (&metadata_contents), metadata_contents_length + 1);
    metadata_bytes = g_bytes_new (xa_metadata, strlen (xa_metadata) + 1);
  metadata_bytes = g_bytes_new (metadata, strlen (metadata) + 1);
      /* Null terminate */
      g_output_stream_write (G_OUTPUT_STREAM (data_stream), "\0", 1, NULL, NULL);

        strcmp (metadata_contents, g_memory_output_stream_get_data (data_stream)) == 0;

Fixed code:
                          gsize       required_metadata_size,
  g_autoptr(GVariant) xa_metadata_v = NULL;
  gsize xa_metadata_size = 0;
    {
      xa_metadata_v = g_variant_lookup_value (commit_metadata,
                                              "xa.metadata",
                                              G_VARIANT_TYPE_STRING);
      if (xa_metadata_v)
        xa_metadata = g_variant_get_string (xa_metadata_v, &xa_metadata_size);
    }
      (xa_metadata != NULL && (xa_metadata_size != required_metadata_size ||
                               memcmp (xa_metadata, required_metadata, xa_metadata_size) != 0)))
      gsize metadata_size = 0;
                                 &metadata_contents, &metadata_size, NULL, error))
      if (!g_key_file_load_from_data (keyfile, metadata_contents, metadata_size, 0, 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;
  gsize metadata_size = 0;
                            &metadata_contents,
                            &metadata_size, NULL, NULL))
                                      metadata_size,
                                 metadata_contents, metadata_size, !is_oci, error))
    op->external_metadata = g_bytes_new (external_metadata, strlen (external_metadata));
  return g_bytes_new_take (g_steal_pointer (&metadata_contents), metadata_contents_length);
    metadata_bytes = g_bytes_new (xa_metadata, strlen (xa_metadata));
  metadata_bytes = g_bytes_new (metadata, strlen (metadata));
  gsize metadata_size = 0;
  metadata_size = strlen (metadata_contents);

        metadata_size == g_memory_output_stream_get_data_size (data_stream) &&
        memcmp (metadata_contents, g_memory_output_stream_get_data (data_stream), metadata_size) == 0;