Skip to content

dir: Don't allow collection ID collisions - #2601

Closed
mwleeds wants to merge 1 commit into
flatpak:masterfrom
mwleeds:disallow-collection-collisions
Closed

mwleeds wants to merge 1 commit into
flatpak:masterfrom
mwleeds:disallow-collection-collisions

Conversation

@mwleeds

@mwleeds mwleeds commented Jan 16, 2019

Copy link
Copy Markdown
Contributor

With Flatpak you should only have to trust each remote to provide good
updates for the apps provided by it. However the P2P support in flatpak
considers each remote to be equally trustworthy, which opens a possible
attack vector. For example if I have a flathub remote configured and
apps installed from it and I also have a remote "sketchy-remote"
configured which I have one app installed from, I expect the Flathub
apps to update from Flathub (or to update from LAN/USB sources with
Flathub GPG signatures) and not from the sketchy-remote.

The way this attack would play out is that the sketchy-remote would
deploy the same collection ID as the victim remote (in this case
org.flathub.Stable) in order to serve updates for it. So this commit
disallows any operation on remotes that would leave two with the same
collection ID but differing GPG keys. The case of two remotes having
matching collection IDs and GPG keys is allowed for e.g. mirrors. This
is implemented in flatpak_dir_modify_remote() so that it applies to both
adding new remotes and modifying existing ones, which can happen via
the CLI, via library API, or by setting metadata keys on the server
side.

Unfortunately this implementation is not perfect, because there's not
always a one-to-one mapping between configured remotes and GPG keyrings.
On Endless OS some remotes have keyrings in /usr/share/keyrings/ rather
than /var/lib/flatpak/repo/remote_name.trustedkeys.gpg as do remotes
added by Flatpak. Therefore it might make sense to have other
mitigations for this issue beyond this commit.

Partial fix for #1447

@matthiasclasen

Copy link
Copy Markdown
Collaborator

Remotes can also be added using ostree cli and library api, I think ?

I think it would be great to have a 'are there conflicting remotes?' in more places. E.g, should we not check this before trying to install or update anything ?

@mwleeds

mwleeds commented Jan 16, 2019

Copy link
Copy Markdown
Contributor Author

Remotes can also be added using ostree cli and library api, I think ?

That's true, although I want to check with @dbnicholson before making changes there because I believe he's working on changing the libostree API for modifying remotes.

I think it would be great to have a 'are there conflicting remotes?' in more places. E.g, should we not check this before trying to install or update anything ?

I suppose we could but I think you'd only get around this check by editing the repo config directly which kind of implies "I know I might shoot myself in the foot". The check is also a bit costly in terms of I/O in the case that two remotes legitimately have the same collection ID.

@matthiasclasen

Copy link
Copy Markdown
Collaborator

ok, seems like a good start anyway.

I'll leave it to alex to decide if he wants this in 1.2.

@mwleeds

mwleeds commented Jan 16, 2019

Copy link
Copy Markdown
Contributor Author

I don't know if anyone uses /etc/flatpak/remotes.d/ but that would be another way of getting around this check, I think. If a file is dropped in that directory Flatpak doesn't have an opportunity to verify it and just uses it during the next operation, I think. But I'm not sure what should be done about that.

@matthiasclasen

Copy link
Copy Markdown
Collaborator

Right, that is because we're passing system-remotes-dir when creating the OstreeRepo object. We could not do that and parse the remotes ourselves instead

Comment thread common/flatpak-dir.c
continue;
}

if (!g_bytes_equal (keyring_bytes, other_keyring_bytes))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry about this check. The keyring doesn't have to be identical for the gpg keys to match. For example any of the keyrings could contain two keys, or there might be some difference in version in the gpg keyring format or other gratious difference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how does this play with key updates (via metadata). Do those always end up with identical copeis of the keyring?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how does this play with key updates (via metadata). Do those always end up with identical copeis of the keyring?

This check doesn't happen for a keyring update because we don't compare a remote to its own config

I worry about this check. The keyring doesn't have to be identical for the gpg keys to match. For example any of the keyrings could contain two keys, or there might be some difference in version in the gpg keyring format or other gratious difference.

I don't think we have to worry about the keyrings having different sets of keys, because a mirror would use the same set of keys. But I'm not sure what can be done about the file format differing. We could potentially try to verify some signatures (say, on ostree-metadata) that are valid for the existing remote and see if they're valid for the one being modified, but then we have to assume we have some signed data, or do network I/O here to get it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, why would a mirror have the same keys in a different format?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it turns out this doesn't properly detect the case where the GPG keys are the same. I downloaded flathub.flatpakrepo, added a DeployCollectionID=org.flathub.Stable line to it, and tried to add it as another remote but got this error. So it seems we do need a better way to compare GPG keys.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how does this play with key updates (via metadata). Do those always end up with identical copeis of the keyring?

This check doesn't happen for a keyring update because we don't compare a remote to its own config

Sure, but you have a flathub remote, with gpg key 1, now it rotates to gpg key 2, importing it into the local keyring. Now your local gpg key list is updated, and may not look bitwise identical to "upstream gpg key 2". At this point a third party repo may claim to use the same gpg key as the "upstream gpg key 2" and we will not match this as bad.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to continue down this path I think the way to do it would be to use gpgme and specifically the keyid attribute documented here, and see if the remote being modified would have a (possibly empty) subset of the keys associated with the existing remote with the same collection ID (which would be an error) or the same set of keys (which would not be an error).

However I think that would break keyring updates (via xa.gpg-keys) in the case where you have a mirror remote configured (say you have both flathub and flathub-mirror), because you wouldn't update both in one atomic transaction. From the perspective of flatpak_dir_modify_remote() it would look like you're changing the set of keys associated with flathub to differ from the set associated with flathub-mirror (even though in reality that would only be the case until flathub-mirror gets the same update).

Therefore I'll try the strategy described in the last sentence here which seems more robust.

@alexlarsson

Copy link
Copy Markdown
Member

Hmm, this also ties in to a discussion we had on irc today. If we're going to do a subset flathub like freehub, they might have different urls but same collection id. (Same gpg key too though.)

@mwleeds

mwleeds commented Jan 17, 2019

Copy link
Copy Markdown
Contributor Author

Hmm, this also ties in to a discussion we had on irc today. If we're going to do a subset flathub like freehub, they might have different urls but same collection id. (Same gpg key too though.)

I thought the idea was to use a separate collection ID for freehub (librehub?). Otherwise the info from the two repositories wouldn't really be separate in the repo metadata or appstream data. For example, (org.flathub.Stable, appstream/x86_64) should resolve to the same thing no matter where it comes from, so the two repos couldn't serve different versions of it.

@alexlarsson

Copy link
Copy Markdown
Member

Yeah, we're mostly speculating about how do to the freehub stuff, but it seems like we'll have to have different collection ids..

With Flatpak you should only have to trust each remote to provide good
updates for the apps provided by it. However the P2P support in flatpak
considers each remote to be equally trustworthy, which opens a possible
attack vector. For example if I have a flathub remote configured and
apps installed from it and I also have a remote "sketchy-remote"
configured which I have one app installed from, I expect the Flathub
apps to update from Flathub (or to update from LAN/USB sources with
Flathub GPG signatures) and not from the sketchy-remote.

The way this attack would play out is that the sketchy-remote would
deploy the same collection ID as the victim remote (in this case
org.flathub.Stable) in order to serve updates for it. So this commit
disallows any operation on remotes that would leave two with the same
collection ID but differing GPG keys. The case of two remotes having
matching collection IDs and GPG keys is allowed for e.g. mirrors. This
is implemented in flatpak_dir_modify_remote() so that it applies to both
adding new remotes and modifying existing ones, which can happen via
the CLI, via library API, or by setting metadata keys on the server
side.

Unfortunately this implementation is not perfect, because there's not
always a one-to-one mapping between configured remotes and GPG keyrings.
On Endless OS some remotes have keyrings in /usr/share/keyrings/ rather
than /var/lib/flatpak/repo/remote_name.trustedkeys.gpg as do remotes
added by Flatpak. Therefore it might make sense to have other
mitigations for this issue beyond this commit.

Partial fix for flatpak#1447
@mwleeds
mwleeds force-pushed the disallow-collection-collisions branch from bd2b0b2 to e8f55fc Compare January 18, 2019 00:23
@mwleeds mwleeds added the WIP label Jan 24, 2019
@mwleeds

mwleeds commented Feb 5, 2019

Copy link
Copy Markdown
Contributor Author

I'm giving up on this approach; see #2601 (comment)

@mwleeds mwleeds closed this Feb 5, 2019
mwleeds added a commit to mwleeds/ostree that referenced this pull request Feb 6, 2019
Currently the P2P code requires you to trust every remote you have
configured to the same extent, because a remote controlled by a
malicious actor can serve updates to refs (such as Flatpak apps)
installed from other remotes.[1] The way this attack would play out is
that the malicious remote would deploy the same collection ID as the
victim remote, and would then be able to serve updates for it.

One possible remedy would be to make it an error to configure remotes
such that two have the same collection ID but differing GPG keys. I
attempted to do that in Flatpak[2] but it proved difficult because it is
valid to configure two remotes with the same collection ID, and they may
then each want to update their keyrings which wouldn't happen
atomically.

Another potential solution I've considered is to add a `trusted-remotes`
option to ostree_repo_find_remotes_async() which would dictate which
keyring to use when pulling each ref. However the
ostree_repo_finder_resolve_async() API would still remain vulnerable,
and changing that would require rewriting a large chunk of libostree's
P2P support.

So this commit represents a third attempt at mitigating this security
hole, namely to have the client specify which remote to use for GPG
verification at pull time. This way the pull will fail if the commits
are signed with anything other than the keys we actually trust to serve
updates.

This is implemented as an option "override-keyring-remotes" for
ostree_repo_pull_from_remotes_async() which dictates the remote to be
used for GPG verification of each OstreeRepoFinderResult. If set, this
value will become the value of the "override-remote-name" option of
ostree_repo_pull_with_options(). Currently "override-remote-name"
affects only local pulls and in that case affects both the name to be
used in the refspec and the remote used for GPG verification. This
commit changes the meaning so that it dictates what remote to use for
GPG verification regardless of whether the pull is local or not. This
seems cleaner than adding a new option and shouldn't be a backwards
compatibility concern because it only makes sense to use a remote in a
refspec name if it can also be used for GPG verification.

This solution is still not perfect because (a) commit metadata will have
already been pulled from the malicious remote during the execution of
ostree_repo_find_remotes_async() which would not be removed on pull
failure and (b) if a global keyring directory is being used rather than
a separate keyring for each remote the pull will still succeed even if
it's not from the intended remote.

[1] flatpak/flatpak#1447
[2] flatpak/flatpak#2601
mwleeds added a commit to mwleeds/ostree that referenced this pull request Feb 14, 2019
Currently the P2P code requires you to trust every remote you have
configured to the same extent, because a remote controlled by a
malicious actor can serve updates to refs (such as Flatpak apps)
installed from other remotes.[1] The way this attack would play out is
that the malicious remote would deploy the same collection ID as the
victim remote, and would then be able to serve updates for it.

One possible remedy would be to make it an error to configure remotes
such that two have the same collection ID but differing GPG keys. I
attempted to do that in Flatpak[2] but it proved difficult because it is
valid to configure two remotes with the same collection ID, and they may
then each want to update their keyrings which wouldn't happen
atomically.

Another potential solution I've considered is to add a `trusted-remotes`
option to ostree_repo_find_remotes_async() which would dictate which
keyring to use when pulling each ref. However the
ostree_repo_finder_resolve_async() API would still remain vulnerable,
and changing that would require rewriting a large chunk of libostree's
P2P support.

So this commit represents a third attempt at mitigating this security
hole, namely to have the client specify which remote to use for GPG
verification at pull time. This way the pull will fail if the commits
are signed with anything other than the keys we actually trust to serve
updates.

This is implemented as an option "ref-keyring-map" for
ostree_repo_pull_from_remotes_async() and
ostree_repo_pull_with_options() which dictates the remote to be used for
GPG verification of each collection-ref. I think specifying a keyring
remote for each ref is better than specifying a remote for each
OstreeRepoFinderResult, because there are some edge cases where a result
could serve updates to refs which were installed from more than one
remote.

I'll soon submit a PR for Flatpak to use the new "ref-keyring-map"
option, along with a unit test.

[1] flatpak/flatpak#1447
[2] flatpak/flatpak#2601
mwleeds added a commit to mwleeds/ostree that referenced this pull request Feb 15, 2019
Currently the P2P code requires you to trust every remote you have
configured to the same extent, because a remote controlled by a
malicious actor can serve updates to refs (such as Flatpak apps)
installed from other remotes.[1] The way this attack would play out is
that the malicious remote would deploy the same collection ID as the
victim remote, and would then be able to serve updates for it.

One possible remedy would be to make it an error to configure remotes
such that two have the same collection ID but differing GPG keys. I
attempted to do that in Flatpak[2] but it proved difficult because it is
valid to configure two remotes with the same collection ID, and they may
then each want to update their keyrings which wouldn't happen
atomically.

Another potential solution I've considered is to add a `trusted-remotes`
option to ostree_repo_find_remotes_async() which would dictate which
keyring to use when pulling each ref. However the
ostree_repo_finder_resolve_async() API would still remain vulnerable,
and changing that would require rewriting a large chunk of libostree's
P2P support.

So this commit represents a third attempt at mitigating this security
hole, namely to have the client specify which remote to use for GPG
verification at pull time. This way the pull will fail if the commits
are signed with anything other than the keys we actually trust to serve
updates.

This is implemented as an option "ref-keyring-map" for
ostree_repo_pull_from_remotes_async() and
ostree_repo_pull_with_options() which dictates the remote to be used for
GPG verification of each collection-ref. I think specifying a keyring
remote for each ref is better than specifying a remote for each
OstreeRepoFinderResult, because there are some edge cases where a result
could serve updates to refs which were installed from more than one
remote.

The PR to make Flatpak use this new option is here[3].

[1] flatpak/flatpak#1447
[2] flatpak/flatpak#2601
[3] flatpak/flatpak#2705
rh-atomic-bot pushed a commit to ostreedev/ostree that referenced this pull request Mar 29, 2019
Currently the P2P code requires you to trust every remote you have
configured to the same extent, because a remote controlled by a
malicious actor can serve updates to refs (such as Flatpak apps)
installed from other remotes.[1] The way this attack would play out is
that the malicious remote would deploy the same collection ID as the
victim remote, and would then be able to serve updates for it.

One possible remedy would be to make it an error to configure remotes
such that two have the same collection ID but differing GPG keys. I
attempted to do that in Flatpak[2] but it proved difficult because it is
valid to configure two remotes with the same collection ID, and they may
then each want to update their keyrings which wouldn't happen
atomically.

Another potential solution I've considered is to add a `trusted-remotes`
option to ostree_repo_find_remotes_async() which would dictate which
keyring to use when pulling each ref. However the
ostree_repo_finder_resolve_async() API would still remain vulnerable,
and changing that would require rewriting a large chunk of libostree's
P2P support.

So this commit represents a third attempt at mitigating this security
hole, namely to have the client specify which remote to use for GPG
verification at pull time. This way the pull will fail if the commits
are signed with anything other than the keys we actually trust to serve
updates.

This is implemented as an option "ref-keyring-map" for
ostree_repo_pull_from_remotes_async() and
ostree_repo_pull_with_options() which dictates the remote to be used for
GPG verification of each collection-ref. I think specifying a keyring
remote for each ref is better than specifying a remote for each
OstreeRepoFinderResult, because there are some edge cases where a result
could serve updates to refs which were installed from more than one
remote.

The PR to make Flatpak use this new option is here[3].

[1] flatpak/flatpak#1447
[2] flatpak/flatpak#2601
[3] flatpak/flatpak#2705

Closes: #1810
Approved by: cgwalters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants