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-transaction.c
Commit message: Transaction: Fail the resolve if xa.metadata invalid or missing

If we fail to parse xa.metadata from the summary cache or the commit
xa.metadata we fail the resolve.

If xa.metadata is missing in the commit we fail the resolve (it is
always set in the summary cache, because summary update converts
missing xa.metadata to "", so we either get that, or cache miss which
leads to resolving from the commit.

This means that op->resolved_metadata is always set during install and
updates, which means we will show the app permissions. The transaction
will also always make sure that this data actually matches what gets
deployed.

Before this change an invalid metadata in the summary cache could lead
to a NULL resolved_metadata, which means we wouldn't print the app
permissions, yet we would still deploy some metadata file that could
have permissions. (NOTE: It would fail to deploy unless the
xa.metadata in the commit matched the metadata file, but in this
corner case we would't compare the summary and commit metadata, so
they may differ.)


Vulnerable code:
static void
                  GBytes                      *old_metadata)
      if (g_key_file_load_from_bytes (metakey, metadata, G_KEY_FILE_NONE, NULL))
        {
          op->resolved_metadata = g_bytes_ref (metadata);
          op->resolved_metakey = g_steal_pointer (&metakey);
        }
      else
        g_message ("Warning: Failed to parse metadata for %s\n", flatpak_decomposed_get_ref (op->ref));
        g_message ("Warning: Failed to parse old metadata for %s\n", flatpak_decomposed_get_ref (op->ref));
static void
                GBytes *metadata_bytes)
  mark_op_resolved (op, checksum, sideload_path, metadata_bytes, old_metadata_bytes);
static void
                        GVariant *commit_data)
    g_message ("Warning: No xa.metadata in local commit %s ref %s", checksum, flatpak_decomposed_get_ref (op->ref));
  else
    metadata_bytes = g_bytes_new (xa_metadata, strlen (xa_metadata));
  resolve_op_end (self, op, checksum, sideload_path, metadata_bytes);
                              FlatpakRemoteState *state)
  resolve_op_end (self, op, checksum, sideload_path, metadata_bytes);
  return TRUE;
          mark_op_resolved (op, op->resolved_commit, NULL, NULL, NULL);
          mark_op_resolved (op, checksum, NULL, metadata_bytes, NULL);
          mark_op_resolved (op, op->commit, NULL, op->external_metadata, NULL);
          resolve_op_from_commit (self, op, checksum, NULL, commit_data);
          if (!try_resolve_op_from_metadata (self, op, checksum, sideload_path, state))
              /* Else try to load the commit object.
              resolve_op_from_commit (self, op, checksum, sideload_path, commit_data);

Fixed code:
static gboolean
                  GBytes                      *old_metadata,
                  GError                     **error)
      if (!g_key_file_load_from_bytes (metakey, metadata, G_KEY_FILE_NONE, NULL))
        return flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA,
                                   "Metadata for %s is invalid", flatpak_decomposed_get_ref (op->ref));

      op->resolved_metadata = g_bytes_ref (metadata);
      op->resolved_metakey = g_steal_pointer (&metakey);
        {
          /* This shouldn't happen, but a NULL old metadata is safe (all permisssions are considered new) */
          g_message ("Warning: Failed to parse old metadata for %s\n", flatpak_decomposed_get_ref (op->ref));
        }

  return TRUE;
static gboolean
                GBytes *metadata_bytes,
                GError **error)
  if (!mark_op_resolved (op, checksum, sideload_path, metadata_bytes, old_metadata_bytes, error))
    return FALSE;
  return TRUE;
static gboolean
                        GVariant *commit_data,
                        GError **error)
    return flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA,
                               "No xa.metadata in local commit %s ref %s",
                               checksum, flatpak_decomposed_get_ref (op->ref));

  metadata_bytes = g_bytes_new (xa_metadata, strlen (xa_metadata));
  return resolve_op_end (self, op, checksum, sideload_path, metadata_bytes, error);
/* NOTE: In case of non-available summary this returns FALSE with a
 * NULL error, but for other error cases it will be set.
 */
                              FlatpakRemoteState *state,
                              GError **error)
  return resolve_op_end (self, op, checksum, sideload_path, metadata_bytes, error);
          if (!mark_op_resolved (op, op->resolved_commit, NULL, NULL, NULL, error))
            return FALSE;
          if (!mark_op_resolved (op, checksum, NULL, metadata_bytes, NULL, error))
            return FALSE;
          if (!mark_op_resolved (op, op->commit, NULL, op->external_metadata, NULL, error))
            return FALSE;
          if (!resolve_op_from_commit (self, op, checksum, NULL, commit_data, error))
            return FALSE;
          if (!try_resolve_op_from_metadata (self, op, checksum, sideload_path, state, &local_error))
              if (local_error)
                {
                  /* Actual error, not just missing from summary */
                  g_propagate_error (error, g_steal_pointer (&local_error));
                  return FALSE;
                }

              /* Missing from summary, try to load the commit object.
              if (!resolve_op_from_commit (self, op, checksum, sideload_path, commit_data, error))
                return FALSE;