Commit Graph

2134 Commits

Author SHA1 Message Date
Alexander Larsson
adfa816cd4 custom thread mainloop: finish pending sources when removing
We sometimes set a custom per-thread mainloop because and then spin it
manually to fake a sync call on a thread using async calls. Primarily
this happens with the soup streaming calls. In this case, eventually
we finish the main loop iteration (because, say, the download is done)
so we stop iterating the mainloop and return from the fake sync code.

However, that might not necessarily be the only thing queued on the
main context. I ran into a situation where it seems like libsoup did
some call to a thread-pool during the async call, and the next time i
used soup aync everything froze. It looks like there is some threaded
soup service that returned a response on the old context, and since
that never got handled (since that context is now dead) it now doesn't
work.

To solve this situation we're now iterating the main context until
there are no pending sources before killing the main context.
2021-03-19 10:59:04 +01:00
Alexander Larsson
1b130c5cc3 http utils: push custom thread local main context
We're calling async soup APIs with SOUP_SESSION_USE_THREAD_CONTEXT
set, which means that libsoup async APIs will run async callbacks on
the loop of the thread-default main context. We then manually spin
this main context, because we're supposed to look like a sync call and
the async stuff is just internally.

This is not really right, because normally there isn't any custom
mainloop context registred, which means we're spinning the main thread
context on some other thread, as well as queuing soup sorces on
it. This can't be any good!

Rather than doing this we actually create and push our own main
context that we then spin isolated from the default mainloop.
2021-03-19 10:59:04 +01:00
Alexander Larsson
534cf643a6 Revert "Revert "flatpak-utils-http: Don’t use GMainLoop""
This reverts commit 14f062593f.
2021-03-19 10:59:04 +01:00
Simon McVittie
14f062593f Revert "flatpak-utils-http: Don’t use GMainLoop"
This reverts commit c1561b1ded,
which seems to trigger a hang in the
test-oci-registry@system.wrap test-case.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-03-17 15:35:34 +01:00
Simon McVittie
bb52aeb342 Merge pull request #4076 from pwithnall/no-main-loops
flatpak-utils-http: Don’t use GMainLoop
2021-03-12 12:44:22 +00:00
Simon McVittie
46b3ede524 dir: Refuse to export .desktop files with suspicious uses of @@ tokens
This is either a malicious/compromised app trying to do an attack, or
a mistake that will break handling of %f, %u and so on. Either way,
if we refuse to export the .desktop file, resulting in installation
failing, then it makes the rejection more obvious than quietly
removing the magic tokens.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-03-10 10:45:30 +01:00
Simon McVittie
1e7e8fdb24 dir: Reserve the whole @@ prefix
If we add new features analogous to file forwarding later, we might
find that we need a different magic token. Let's reserve the whole
@@* namespace so we can call it @@something-else.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-03-10 10:45:30 +01:00
Ryan Gonzalez
652a28ffab Disallow @@ and @@u usage in desktop files
Fixes #4146.
2021-03-10 10:45:30 +01:00
Bastien Nocera
e67847e253 common: Fix Spawn portal method not working with UTF-8 files
When the portal's Spawn method is used with the environment cleared,
it's very likely that the "flatpak run" that ends up being run will be
in an environment without UTF-8 support.

If one of the files or directories we try to expose to the sub-sandbox
contains UTF-8/non-ASCII characters, then "flatpak run" would fail with:
error: Invalid byte sequence in conversion input

This is caused by GOption trying to parse the --filesystem option for
flatpak, as, when using the G_OPTION_ARG_CALLBACK argument type, GOption
will split the option name from its value, and try to convert the value
to UTF-8. Which will fail because there's no UTF-8.

It won't however do that if we tell the option parser that the value is
a filename using G_OPTION_FLAG_FILENAME, so set it.
2021-02-23 14:18:57 +01:00
Phaedrus Leeds
e2a2e38c6b Merge pull request #4130 from RyuzakiKK/xauthlocalhostname
run: Propagate X11 cookies with an address equals to XAUTHLOCALHOSTNAME
2021-02-18 11:45:50 -08:00
Phaedrus Leeds
af2b8ca236 Merge pull request #4133 from RyuzakiKK/unused_vars
Remove unused variables
2021-02-18 11:43:00 -08:00
Ludovico de Nittis
0d27b27744 run: Remove unused GAsyncResult variable
Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
2021-02-18 11:39:45 +01:00
Ludovico de Nittis
8d9be910e5 dir: Remove unused AutoPolkitDetails variable
Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
2021-02-18 11:39:23 +01:00
Ludovico de Nittis
fd6a3b79f4 run: Propagate X11 cookies with an address equals to XAUTHLOCALHOSTNAME
OpenSUSE inherits the hostname value from DHCP without updating its X11
authentication cookie, and it keeps the initial value in
`XAUTHLOCALHOSTNAME`.
To avoid breaking the X11 applications, OpenSUSE patches libxcb so that
it also considers the value in `XAUTHLOCALHOSTNAME` as another possible
hostname.

https://bugzilla.opensuse.org/show_bug.cgi?id=262309

To cope with that behavior we need to check `XAUTHLOCALHOSTNAME` too
and, if we have a cookie with that address, propagate it inside the
container adjusting its address to the canonical hostname
`unames.nodename`.

Fixes: #4043

Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
2021-02-17 18:03:00 +01:00
Kalev Lember
426284759c Add G_BEGIN_DECLS/G_END_DECLS to public headers
This ensures that we correctly specify C linkage when including flatpak
headers from C++ code.

This should fix fallout from glib's change to include C++ code in its
headers, see https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1935
for discussion.

Fixes https://github.com/flatpak/flatpak/issues/4117
2021-02-12 15:58:09 +01:00
Phaedrus Leeds
2a111e5042 common: Add API for listing related refs of installed ref
Currently if a user of libflatpak wants to list the related refs (such
as extensions and plugins) of something, they have three options:
1. They can parse the metadata manually with e.g.
   flatpak_remote_ref_get_metadata() and then key-file operations, but
   this means re-implementing parts of libflatpak and using key file
   strings that are not actually public (FLATPAK_METADATA_KEY_...).
2. They can use flatpak_installation_list_installed_related_refs_sync()
   but this only works for installed related refs not remote ones.
3. They can use flatpak_installation_list_remote_related_refs_sync() but
   this lists all remotely available related refs, including ones that
   may not be compatible with the installed version of the main ref
   (because they don't match any of the values in the "versions"
   metadata key).

So since none of these provide a way to get the remote related refs
corresponding to an installed application, add new API for that. For the
motivation of this see
https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1132
2021-02-10 09:17:53 +01:00
Phaedrus Leeds
dbe81c90c2 common: Tweak error code docs
FLATPAK_ERROR_ALREADY_INSTALLED can be returned from, for example,
flatpak_installation_add_remote(), so document that it applies to
remotes not just apps and runtimes.
2021-02-09 09:36:59 +01:00
Phaedrus Leeds
0920e959b5 common: Update FlatpakRef docs with respect to uniqueness
See these links for context on this change:
- https://lists.freedesktop.org/archives/flatpak/2021-January/002087.html
- https://gitlab.gnome.org/GNOME/gnome-software/-/merge_requests/583#note_1006763
2021-02-09 09:34:47 +01:00
Philip Withnall
ae457239f5 flatpak-installation: Fix a leak of the content rating hash table
`flatpak_deploy_data_get_appdata_content_rating()` returns ownership of
the container.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2021-02-03 17:13:01 +00:00
Simon McVittie
59a8be5805 run: Remove unused minimal environment
We instantiated this but didn't use it for anything.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-21 14:16:44 +01:00
Simon McVittie
baa5affb5e run: Pass environment variables via bwrap --setenv when running ldconfig
This means we can systematically pass the environment variables
through bwrap(1), even if it is setuid and thus is filtering out
security-sensitive environment variables. bwrap ends up being
run with an empty environment instead.

This did not regress in 6d1773d "run: Convert all environment variables
into bwrap arguments", because the LD_LIBRARY_PATH special case in
flatpak_run_add_environment_args() was already not used here; but it's
a bug fix along the same lines as fixing the regression.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-21 14:16:44 +01:00
Simon McVittie
fb473cad80 dir: Pass environment via bwrap --setenv when running apply_extra
This means we can systematically pass the environment variables
through bwrap(1), even if it is setuid and thus is filtering out
security-sensitive environment variables. bwrap ends up being
run with an empty environment instead.

As with the previous commit, this regressed while fixing CVE-2021-21261.

Fixes: 6d1773d2 "run: Convert all environment variables into bwrap arguments"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-21 14:16:44 +01:00
Philip Withnall
8f24958d2a flatpak-dir: Set subpaths when calling Deploy() if not set
This adds `NULL` protection to the `subpaths` argument to
`flatpak_dir_system_helper_call_deploy()`, just like already exists for
other arguments.

It’s a second layer of defence against bugs like fixed in the previous
commit.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2021-01-20 21:44:15 +00:00
Philip Withnall
e274bafc01 flatpak-dir: Fix crash on updating app with no deploy data or subpaths
If an app had no deploy data, and `flatpak_dir_update()` was called with
`opt_subpaths == NULL`, then `arg_subpaths` passed to
`flatpak_dir_system_helper_call_deploy()` would be `NULL`, which causes
an assertion failure from an internal `g_variant_new()` call:
```
g_variant_new_strv: assertion 'length == 0 || strv != NULL' failed
```

Fix that by setting the `subpaths` to an empty array if nothing else is
specified.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2021-01-20 21:40:39 +00:00
Simon McVittie
4108e02245 context: Add --unset-env option and a corresponding override
This follows up from GHSA-4ppf-fxf6-vxg2 to fix missing functionality
that I noticed while resolving that vulnerability, but is not required
for fixing the vulnerability.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-14 09:33:24 +01:00
Simon McVittie
b0aea9c053 run: Allow setting environment variables to empty strings
Some consumers of environment variables distinguish between present
with an empty value and absent. For example, if an environment variable
represents a search path like VK_ICD_FILENAMES, unsetting it often
results in use of a default, but setting it to the empty string results
in not searching any locations, which is sometimes what is desired.

The shell syntax "${BAR-unset}" expands to the value of ${BAR} if it
is set to anything (even an empty string), or to "unset" if not.
We can use that in the unit test to check that BAR is set to the
empty string in this case.

This follows up from GHSA-4ppf-fxf6-vxg2 to fix an issue that I noticed
while resolving that vulnerability, but is not required for fixing the
vulnerability.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-14 09:33:24 +01:00
Simon McVittie
6e5ae7a109 context: Add --env-fd option
This allows environment variables to be added to the context without
making their values visible to processes running under a different uid,
which might be significant if the variable's value is a token or some
other secret value.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
2021-01-14 09:33:24 +01:00
Simon McVittie
6d1773d2a5 run: Convert all environment variables into bwrap arguments
This avoids some of them being filtered out by a setuid bwrap. It also
means that if they came from an untrusted source, they cannot be used
to inject arbitrary code into a non-setuid bwrap via mechanisms like
LD_PRELOAD.

Because they get bundled into a memfd or temporary file, they do not
actually appear in argv, ensuring that they remain inaccessible to
processes running under a different uid (which is important if their
values are tokens or other secrets).

Signed-off-by: Simon McVittie <smcv@collabora.com>
Part-of: https://github.com/flatpak/flatpak/security/advisories/GHSA-4ppf-fxf6-vxg2
2021-01-14 09:33:24 +01:00
Philip Withnall
c1561b1ded flatpak-utils-http: Don’t use GMainLoop
Iterate the `GMainContext` manually instead. This makes the exit
condition from the loop much more obvious, and eliminates the potential
class of problems where `g_main_loop_quit()` is called before
`g_main_loop_run()` and has no effect.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2021-01-13 19:32:58 +00:00
Simon McVittie
fe95ef6fad run: More accurately match PulseAudio's logic for finding its socket
On systems with no XDG_RUNTIME_DIR, PulseAudio falls back to creating
a temporary directory in /tmp, then creating a symlink in
legacy directory ~/.pulse or its more modern replacement ~/.config/pulse.
The symlink is named according to the machine ID, falling back to the
hostname.

Resolves: #4058
Might-resolve: #2169
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-12 11:02:14 +01:00
Simon McVittie
bbf6debec2 Add an option to share the pid namespace with the parent flatpak
As with flatpak run --parent-expose-pids, this will only work if we have
a working, non-setuid bwrap. Systems where user namespace creation is
restricted and bwrap needs to be setuid (Debian 10, RHEL/CentOS 7,
Arch Linux linux-hardened kernel) will have degraded functionality.

This option is similar to --expose-pids, except that instead of making
the subsandbox use a nested pid namespace inside the parent's, it makes
the subsandbox share the parent's pid namespace as-is, so that process
IDs in the parent and the subsandbox are interchangeable. This will
be useful if the parent and the subsandbox communicate via protocols
that assume a global view of the process ID namespace, for example
passing process IDs across an AF_UNIX socket or in shared memory.

In particular, this will be useful for Steam's pressure-vessel container
tool: the IPC between the Steam client and the "game overlay" loaded into
Steam games uses process IDs, and becomes confused if they don't match up.

This weakens the security boundary between a subsandbox and the parent,
but that's OK in some cases, especially if the subsandbox is being used
as a way to get a different runtime /usr (flatpak-spawn --latest-version
or #4018) rather than as a security boundary.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-12 09:55:23 +01:00
Michael Catanzaro
e5da98ff4b Mount resolved varlink socket in sandbox if network access is allowed
If network access is allowed, then we should probably allow name
resolution too.

This should be enough to make nss-resolve work inside flatpak. However,
it cannot be tested with GNOME runtimes, because GNOME runtimes do not
contain systemd. It also cannot be tested with the Fedora 33 flatpak
runtime, because this runtime contains systemd 246, where nss-resolve
uses D-Bus rather than varlink to communicate with systemd-resolved. And
there is no rawhide runtime, and will be no Fedora 34 runtime until F34
is branched. So currently it's not possible to actually test this
without building a custom runtime, which I have not attempted to do. I
have built flatpak myself and verified the resolved socket is mounted
properly inside the sandbox, but it would be better to test if it
actually works with a runtime that contains systemd 247.

Related: https://bugzilla.redhat.com/show_bug.cgi?id=1912131
2021-01-12 09:50:40 +01:00
Simon McVittie
eaa65989db run: Try using /var/run/pulse/native as PulseAudio socket
This brings us closer to matching what happens in libpulse.so.0.
This path is the default for a system-wide PulseAudio instance, which
is discouraged on non-embedded systems but is easy to check for.
In particular, SteamOS, an "appliance"-style gaming OS, uses this
socket.

Adapted from Steam's pressure-vessel container tool, which reuses
various Flatpak modules, including this function.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-01-12 09:42:43 +01:00
Michael Catanzaro
9b34768fa7 Fix build with GCC 11
See:
fab561f8d0
2021-01-11 11:05:57 +01:00
Alexander Larsson
ebf01791dc Download the new digested summary index sig if there
We still load the old one as a backup.
2020-12-22 13:49:59 +01:00
Alexander Larsson
f479c0f1a4 update-repo: Create an digested version of the summary index signature
In addition to the old summary.idx.sig we now create an identical file
in `summaries/$DIGEST.idx.sig`, where the digest is of the summary
index itself. This means it will have a unique name and there will
never be issues where downloading the index and signature will race
during an update.

We keep the new and the previous digested sigs every update to avoid
removing the previous one while its in use.

As suggested by simon in https://github.com/flatpak/flatpak/issues/3983
and https://github.com/ostreedev/ostree/issues/2250
2020-12-22 13:49:59 +01:00
Alexander Larsson
f87ffe26a8 Fix leak in upgrade_deploy_data() 2020-12-21 10:30:22 +01:00
Phaedrus Leeds
84f6e1c61f common: Upgrade deploy data when necessary
Now that we have version 4 of deploy data, upgrade to it before trying
to use any of the pieces of data it guarantees the presence of.
2020-12-21 10:30:22 +01:00
Phaedrus Leeds
9ab02b38bc Ensure deploy files have complete metadata
Currently if a deploy file was created (because a flatpak is installed)
using an old Flatpak version (such as 0.10.1 that's in Endless 3.3.16),
it will not include every metadata key, such as "eol" and "eolr". Those
keys were added before deploy version 1. Then when the deploy file is
upgraded to version 3, the metadata is only copied so eol/eolr are still
missing even though they exist in the commit metadata.

So this commit adds deploy version 4 which ensures all metadata keys in
the deploy file are present (when they are present in the source data).
All existing deploy files will be upgraded with this code.

This change is important because we use the end-of-life information to
decide whether an unused runtime should be uninstalled, so without this
change we are not freeing up disk space that we should be.
2020-12-21 10:30:22 +01:00
David Hewitt
d762a2f536 flatpak-dir: Allow root to bypass parental controls
In elementary OS, we're building our images with some flatpaks pre-installed. To do this, we run noninteractive `flatpak install` commands in the chroot as part of the build scripts. We've bumped into some build failures recently after switching to a version of flatpak with malcontent support built in.

After some debugging, it turns out installations are failing the parental controls checks with errors like `Failed to install org.gnome.Epiphany: Could not connect: No such file or directory` message. I'm assuming this is a failure to connect to the system bus in this method (since there isn't one in the chroot).

Is it reasonable to assume that UID 0 should be allowed to install whatever they want regardless of parental controls? I believe this would fix our issue too.
2020-12-21 09:46:31 +01:00
Phaedrus Leeds
c31dc54594 instance: Add missing nullable annotation 2020-12-21 09:11:08 +01:00
David Hewitt
e1c094be15 flatpak-transaction: Mark commit param on operation_done nullable 2020-12-21 09:09:51 +01:00
Corentin Noël
348ccb9565 flatpak-installed-ref: Add annotation to content rating table 2020-12-14 11:25:16 +01:00
Alexander Larsson
b3321e9179 extra-data: Resolve runtime from the target installation
When installing to an installation we need to find the runtime to use
for the apply-extra-data script from the installation we're targeting,
because that is the one that FlatpakTransaction guaranteed has the
required dependencies (although its possible they came from the
default dependency source too, i.e. the system repos).

In particular, we run into this issue if nothing is installed
anywhere, and then we install an extra-data app into a custom
directory. The transaction will download the runtime, and it
will not be anywhere else. Without this change flatpak only
looked for the dependency in the systam an regular user dirs
where it isn't.
2020-12-09 16:12:25 +01:00
Philip Withnall
a9f5c124c1 flatpak-installation: Add default dep sources to list installed refs
It seems that all `FlatpakTransaction`s add the default dep sources, so
the internal transaction used to list installed refs for updates should
do the same.

This fixes a bug where
`flatpak_installation_list_installed_refs_for_update()` would return an
error saying “The application x requires the runtime y which was not
found” if the app was installed in the user repo, the runtime was
installed in the system repo, and no remote was configured (or one was
configured `xa.noenumerate=true`) in the user repo to provide the
runtime. If a remote was configured, the error wouldn’t be returned, but
the app would be spuriously listed for an update as its runtime couldn’t
be found.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-12-09 09:05:13 +01:00
Simon McVittie
cc7f863707 common: Add a backport of G_DBUS_METHOD_INVOCATION_HANDLED
This is syntactic sugar added in GLib 2.67.0, which makes it more clearly
correct when we return TRUE after a GDBus error.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-12-09 08:34:19 +01:00
Alexander Larsson
97d1eb8478 Update common/flatpak-exports.c
Co-authored-by: Simon McVittie <smcv@debian.org>
2020-12-09 08:31:45 +01:00
Casey Jao
3aaea7d206 Expose /var/usrlocal if "--filesystem=host" is specified.
As /usr/local points to ../var/usrlocal on Silverblue,
/run/host/usr/local was previously a broken link inside the
sandbox. This patch checks if /var/usrlocal exists and bind-mounts it
to /run/host/var/usrlocal.

See bug #4010.
2020-12-09 08:31:45 +01:00
Alexander Larsson
a03c2739df Add back the ostree.commit.timestamp per-ref metadata for summary
The old summary had a ostree.commit.timestamp in the per-ref metadata
dict. However, since this was not used anymore by flatpak I removed it.
However, it turns out that flathub has infra that depends on this,
so I'm adding it back.

We reuse the data in the old summary for unchanged refs when
rebuilding the summary, to avoid having to read all the commits. In
the new world the new format summaries are used for this, which means
we have to keep the timestamp in that too. However, to not be
unnecessary large its now using a shorter key name, as this is
duplicated for each ref in the summary.
2020-11-20 15:20:15 +01:00
Alexander Larsson
ecda7757bb Fix multiarch support
A typo was causing us to pass the app id as arch, breaking (at least)
multiarch support.
2020-11-20 15:08:58 +01:00