Skip to content

App updates may not come from the intended remote #1447

Closed

Description

@mwleeds

You should not be able to update an app such that its collection ID changes, but you can.

$ flatpak info com.endlessm.wiki_art.en
Ref: app/com.endlessm.wiki_art.en/x86_64/eos3
ID: com.endlessm.wiki_art.en
Arch: x86_64
Branch: eos3
Origin: eos-apps
Collection ID: com.endlessm.Dev.Apps
Date: 2018-01-26 21:32:56 +0000
Subject: Export com.endlessm.wiki_art.en
Commit: 160436e78fce0a5f4576c5baa06b0492971faebccd91839d1b67dc06ece106de
Parent: fd3d1be2bcc34d2200fbc1b47857714a47270fd741c3bc677a10e0771bd04198
Location: /var/lib/flatpak/app/com.endlessm.wiki_art.en/x86_64/eos3/160436e78fce0a5f4576c5baa06b0492971faebccd91839d1b67dc06ece106de
Installed size: 13.1 MB
Runtime: com.endlessm.apps.Platform/x86_64/3
$ cat /ostree/repo/config
[core]
repo_version=1
mode=bare
add-remotes-config-dir=false
min-free-space-percent=0

[remote "eos"]
url=https://ostree.endlessm.com/ostree/eos-amd64
branches=os/eos/amd64/eos3;
xa.disable=true

[remote "flathub"]
url=https://dl.flathub.org/repo/
xa.title=Flathub
gpg-verify=true
gpg-verify-summary=true

[remote "eos-apps"]
url=https://ostree.endlessm.com/ostree/eos-apps
xa.default-branch=eos3
gpg-verify=true
gpg-verify-summary=false
xa.title=Endless Applications
collection-id=com.endlessm.Apps

[remote "eos-runtimes"]
url=https://ostree.endlessm.com/ostree/eos-amd64
gpg-verify=true
gpg-verify-summary=false
xa.title=Endless OS and Runtimes
collection-id=com.endlessm.Os

[remote "eos-sdk"]
url=https://ostree.endlessm.com/ostree/eos-sdk
gpg-verify=true
gpg-verify-summary=false
xa.title=Endless Runtime and SDK
collection-id=com.endlessm.Sdk

Running G_MESSAGES_DEBUG=all flatpak update com.endlessm.wiki_art.en:
flatpak-update-diff-collection-try2.log

$ flatpak info com.endlessm.wiki_art.en
Ref: app/com.endlessm.wiki_art.en/x86_64/eos3
ID: com.endlessm.wiki_art.en
Arch: x86_64
Branch: eos3
Origin: eos-apps
Collection ID: com.endlessm.Apps
Date: 2018-01-26 21:32:56 +0000
Subject: Export com.endlessm.wiki_art.en
Commit: 59b85f128df8a8841257f67c38b314d4a8450a8853036c53d32427795db3cb52
Parent: d7f0c3937383db6688266bebd42adc4a2a0e76ef1d26f4aa03118a16f3ee2bdc
Location: /var/lib/flatpak/app/com.endlessm.wiki_art.en/x86_64/eos3/59b85f128df8a8841257f67c38b314d4a8450a8853036c53d32427795db3cb52
Installed size: 13.1 MB
Runtime: com.endlessm.apps.Platform/x86_64/3

Activity

  1. mwleeds commented on Feb 22, 2018

    @mwleeds
    ContributorAuthor

    Steps to reproduce: (sorry these aren't really possible for people outside Endless)

    1. Flash a computer with eos-eos3.3-amd64-amd64.180221-070058.base.iso
    2. Run flatpak remote-modify --collection-id=com.endlessm.Apps eos-apps; flatpak remote-modify --collection-id=com.endlessm.Sdk eos-sdk; flatpak remote-modify --collection-id=com.endlessm.Os eos-runtimes
    3. Update WikiArt, either with the flatpak CLI or GNOME Software
  2. pwithnall commented on Feb 28, 2018

    @pwithnall
    Collaborator

    To be clear, I think this is an issue because an attacker could trick you into adding a remote for some shiny new app, but that remote then also serves updates to other apps which you didn’t expect.

    I guess I would be happier about the collection ID changing if flatpak errored out if the old and new versions of the app you’re updating are signed with different GPG keys. What currently happens in that situation?

  3. mwleeds commented on Mar 7, 2018

    @mwleeds
    ContributorAuthor

    To be clear, I think this is an issue because an attacker could trick you into adding a remote for some shiny new app, but that remote then also serves updates to other apps which you didn’t expect.

    I guess I would be happier about the collection ID changing if flatpak errored out if the old and new versions of the app you’re updating are signed with different GPG keys. What currently happens in that situation?

    I think the short answer to your question is that attack is only possible when --user is used. When the system helper is used for deployment, handle_deploy calls flatpak_dir_pull_untrusted_local which calls ostree_repo_verify_commit_for_remote using the remote of the ref being updated, so that keyring is used.

    When --user was used, flatpak relies on ostree's GPG verification during the pull. In that case (and for users of ostree other than flatpak) the attack is only possible if the sketchy remote is configured with the same collection ID as the legitimate one. OstreeRepoFinderConfig only checks remotes with the same collection ID as the ref and that remote's keyring is then used by find_remotes_cb (and in the pull code if it makes it that far).

    For the similar attack vector of advertising malicious refs over LAN or USB, the answer is similar. Since OstreeRepoFinderMount and OstreeRepoFinderAvahi both use ostree_repo_resolve_keyring_for_collection(), the only way a ref could be verified with the "wrong" keyring is if two remotes have the same collection ID, and in that case the behavior depends on the order of the remotes.

    So the status quo isn't great. Some options are:

    1. Make it an error to have more than one remote with the same collection ID (which I guess means you can't configure mirrors, but ostree already has metalink support).
    2. Make it an error to update a ref in a way that changes the keyring used to sign it (which seems like it should be done at the level of ostree not flatpak, and implies there can never be a mechanism for key rotation).
    3. Accept the current behavior, and have front-ends like GNOME Software warn the user if a new remote is duplicating an existing collection ID (but you'd also have to consider the case of the collection ID being deployed after the remote is added).

    cc: @alexlarsson @cgwalters

  4. pwithnall commented on Mar 8, 2018

    @pwithnall
    Collaborator

    Thanks for investigating.

    So the status quo isn't great.

    From the sounds of it, the status quo is pretty good. As I understand it (correct me if I’m wrong), with --user the sketchy remote has to have the same collection ID as the existing remote for that app in order to be able to provide upgrades for an app. Without --user, the sketchy remote can have a different collection ID, but must have the same GPG key as the existing remote.

    To double-check, is it the case that an app can be upgraded to a version from a repository with a different collection ID if (and only if) the other repository uses the same GPG keys as the old one?

    Some options are:

    1. Make it an error to have more than one remote with the same collection ID (which I guess means you can't configure mirrors, but ostree already has metalink support).

    A non-starter. Configuring two remotes to have the same collection ID is part of a valid migration path for old repository configurations to new ones (the other part of that approach being to merge the repositories on the server and redirect their URIs to the same place).

    1. Make it an error to update a ref in a way that changes the keyring used to sign it (which seems like it should be done at the level of ostree not flatpak, and implies there can never be a mechanism for key rotation).

    I suspect that updating a ref from a commit signed with one key to a commit signed with a different key should be an error, yes, unless explicitly allowed by the user or by the old key (for example, signing a new set of GPG keys with the old key and adding them to commit metadata).

    I think flatpak has a mechanism for GPG key rotation already by adding a xa.gpg-keys key to the ostree-metadata.

    1. Accept the current behavior, and have front-ends like GNOME Software warn the user if a new remote is duplicating an existing collection ID (but you'd also have to consider the case of the collection ID being deployed after the remote is added).

    We possibly should do that.

  5. ramcq commented on Mar 8, 2018

    @ramcq
    Contributor

    I believe on the way to deciding on collection IDs, we discussed and rejected the idea that GPG keys should be used as a way to identify commits, on the basis that they are non distinct and can validly change over the lifetime of a repo.

    A deployed Flatpak currently stores the name of the remote it came from. A locally configured remote binds together some information, such as the URL we pull from by default, a collection ID we will permit for alternative sources (essentially: match these possible mirrors) and the GPG key which must be present when we pull from that remote.

    We shouldn't be looking elsewhere. I don't think we need to look at the last commit - and nor do we want to. If we've automatically changed to a new key in the repo configuration, or the user has changed it (omg old key compromised and actively expoited by hackers) then it would be incorrect to allow a signed commit with the old key, even if we have an app installed with that old key.

    As long as we always enforce the locally-configured GPG key for remote, this isn't an attack vector, I don't think. I think if --user pull allows us to pick up (even if they are validly signed for /that/ remote) commits from a different remote with the same collection ID, that's a bug which should be fixed.

    (So I don't believe we need to warn about clashing collection IDs in the eg GNOME Software UI for human beings, but we might want to print a warning on the command line.)

  6. mwleeds commented on Dec 7, 2018

    @mwleeds
    ContributorAuthor

    Despite this issue stagnating for a while I think there is a real problem and I have an idea for a solution. The problem is that the configured remotes aren't all trusted equally, but the P2P code (ostree_repo_find_remotes_async/ostree_repo_pull_from_remotes_async) doesn't respect this. For example if I install an app from flathub I expect the updates for it to be signed with the GPG key on the flathub remote, regardless of if there's another remote that has its collection ID set to org.flathub.Stable (perhaps a remote controlled by a malicious actor). This set of constraints runs counter to the way OstreeRepoFinderConfig works.

    So the solution I'm proposing is to add an option that can be passed to ostree_repo_find_remotes_async, something like origin-remote or trusted-remote that could be set to flathub for example. Then OstreeRepoFinderConfig would only return that remote, and the pull code would only use the GPG keys on that remote. As I said above, I believe this will only effectively be a change in behavior for --user pulls.

    I haven't written the code yet, but that makes sense right?

  7. matthiasclasen commented on Dec 7, 2018

    @matthiasclasen
    Collaborator

    Why do you need to call ostree_repo_find_remotes_async when you already know that it will return flathub ?

  8. mwleeds commented on Dec 7, 2018

    @mwleeds
    ContributorAuthor

    Because it may also return USB drives or LAN peers

  9. matthiasclasen commented on Dec 8, 2018

    @matthiasclasen
    Collaborator

    wait, then .. you don't know what it'll return after all. Maybe your description of the 'trusted remote' concept was just not clear enough. This trusted remote is used for

    • GPG keys
    • collection ID

    Anything else ?

  10. mwleeds commented on Dec 8, 2018

    @mwleeds
    ContributorAuthor

    Yeah I think just those two. When I install something from Flathub, I expect that when it's updated, the update will be signed with the Flathub GPG keys, regardless of whether it comes from Flathub, a USB drive, or a LAN peer. Or if it's signed with a new key, that key should be in xa.gpg-keys in a metadata object signed with the old keys. I don't expect the flathub app to be updated from remote "sketchy-remote" that also has collection-id=org.flathub.Stable and a different set of GPG keys (or from a USB drive with commits signed with the sketchy GPG keys).

    So ostree_repo_find_remotes_async should return results that meet these conditions:

    • An OstreeRepoFinderResult object representing the configured remote, if the refs are found there (in other words if we're online)
    • Zero or more OstreeRepoFinderResult objects for any configured mirror remotes (ones with the same collection ID and GPG key)
    • Zero or more OstreeRepoFinderResult objects representing dynamic remotes (USB/LAN), with the OstreeRemote->keyring value set to the keyring of the trusted remote. If the refs provided are signed with any other keyring, this should be a non-fatal error in ostree_repo_pull_from_remotes_async
  11. mwleeds commented on Dec 12, 2018

    @mwleeds
    ContributorAuthor

    I discussed this with @pwithnall over email and here's the alternative fix being considered:

    Make it an error to configure a remote to have the same collection ID as an existing remote (either at the time the remote is added by the user or at the time the configuration is being updated based on metadata keys) unless the GPG keys of the two remotes also match. It would also have to be an error to update the GPG keys associated with a remote in such a way that the remotes would have matching collection IDs and differing GPG keys (in case for example the old keys are compromised, a malicious actor could switch to the compromised keys, deploy the collection ID org.flathub.Stable, and then switch back to their own keys).

    I think the above fix needs to be implemented because otherwise once flatpak/ostree switch to using refs/mirrors more than refs/remotes, you would end up with issues if refs/mirrors/org.flathub.Stable/ostree-metadata could be signed with GPG Key A or GPG Key B depending on which remote it was pulled from.

    But even with that it might make sense to implement the trusted-remote support for the purpose of layered security, and in case users of libostree other than flatpak have the same model of not all remotes being equally trustworthy.

  12. mwleeds commented on Jan 15, 2019

    @mwleeds
    ContributorAuthor

    I made a few attempts at fixing this today but none of them panned out. Here's what I tried:

    1. First I tried adding a trusted-remotes option to ostree_repo_find_remotes_async and implementing it in find_remotes_cb. This doesn't seem like a great solution because this problem would still exist in other places like ostree_repo_finder_resolve_async.
    2. Then I tried adding an interface, OstreeRepoFinder2 which replaces ostree_repo_finder_resolve_async with ostree_repo_finder_resolve_with_options_async (and similarly for the related functions) so that we could have an extensible options GVariant that could accept trusted-remotes but this breaks so much internal API that it would require a significant rewrite to avoid breaking ABI. For example one of the members of OstreeRepoFinderResult is an OstreeRepoFinder object so I think everything that uses OstreeRepoFinderResult would need to be changed.
    3. Then I looked into adding a check in flatpak_dir_modify_remote to prevent adding or modifying a remote such that its collection ID would match an existing one but its GPG key would not. The primary problem with that is that there's not necessarily a one-to-one mapping between configured remotes and keyrings (usually it's just remote_name.trustedkeys.gpg in the repo but on Endless some keyrings are in /usr/share/keyrings/ instead). So there's no way to reliably check what keyring is used by an existing remote.

    I'm open to ideas if anyone can think of a better solution, or perhaps I should just forge ahead with the first option. Flatpak doesn't seem to use the ostree_repo_finder_resolve_* interfaces. Perhaps the third option is viable too since there's no way to use /usr/share/keyrings/ in the normal process of adding flatpak remotes.

  13. 25 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions