Commit Graph

2446 Commits

Author SHA1 Message Date
Simon McVittie
7b1cd20696 Replace flatpak_close_fds_workaround() with g_fdwalk_set_cloexec()
flatpak_close_fds_workaround() wasn't technically async-signal-safe,
because the requirement for sysconf() to be async-signal-safe was
removed in POSIX.1-2008.

It could also leave high fds open in some cases: in practice
sysconf(_SC_OPEN_MAX) returns the soft resource limit, but if our
resource limit has been reduced by an ancestor process, we could
conceivably still have fds open and inherited above that number.

We can fix this by using g_fdwalk_set_cloexec() with GLib >= 2.79.2,
or the backport in libglnx with older GLib. This uses close_range()
if possible, falling back to rummaging in /proc with async-signal-safe
syscalls.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 16:06:41 +00:00
Simon McVittie
ec796ec37f flatpak-bwrap: Don't leak runtime_dir_members
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Simon McVittie
14846313f9 flatpak_remote_commit_filter: Don't leak config GKeyFile
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Simon McVittie
a96834bb32 populate_commit_data_cache: Don't leak a floating GVariant
var_variant_dup_to_gvariant() returns a floating GVariant, and
g_variant_get_child_value() won't sink it, so we need to free it.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Simon McVittie
fd56fb85f8 populate_commit_data_cache: Don't leak child value
g_variant_get_child_value() returns a non-floating reference, so
g_variant_builder_add() will not sink it.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Simon McVittie
b97704dee6 dir: Don't store a pointer in a gsize
This is, strictly speaking, not allowed. On uncommon architectures like
CHERI, a pointer can be larger than a gsize.

This might also help to avoid AddressSanitizer losing track of
reachability, so that it won't think the array and its contents have
been leaked.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Weng Xuetian
0402e1614c Limit the usage of WAYLAND_SOCKET to an opt-in feature
1. For security context creation, only relies on WAYLAND_DISPLAY, do not
   use WAYLAND_SOCKET since the file descriptor defined by WAYLAND_SOCKET
   can be only consumed once.
2. Due to the incompatiblity between WAYLAND_SOCKET and the security
   context, add a new permission --socket=inherit-wayland-socket
   to limit the usage of WAYLAND_SOCKET to an opt-in feature. Only when
   this flag is set, WAYLAND_SOCKET will be passed to the sandbox.
3. When WAYLAND_SOCKET is not inherited, set FD_CLOEXEC to avoid it to
   be leaked the to sandbox.

Closes: #5614
2024-02-14 19:39:50 +00:00
Hubert Figuière
6e3cc82af3 Fix memory leaks
Build with address sanitizer and run the tests

Signed-off-by: Hubert Figuière <hub@figuiere.net>
2024-02-13 08:20:57 -06:00
Simon McVittie
48caf88a63 Always allow app to inherit redirected fds from flatpak-run(1)
As noticed on #5615, under normal circumstances, flatpak-run(1)
replaces itself with the bwrap process via execve(), and does not
close any fds that it might have inherited from its parent. This
allows for patterns like:

    flatpak run com.example.App --in-fd=3 --out-fd=5 3<foo 5>bar

However, using execve() is annoying when trying to analyze code
coverage, because the coverage instrumentation does not get the
opportunity to write out its data during exit, so it is possible to
set FLATPAK_TEST_COVERAGE=1 to make flatpak run the app as a child
process and wait for it. This puts us on the code path normally used
for apps launched in the background by flatpak_installation_launch_full(),
which *don't* inherit arbitrary fds from their parent.

Detect this situation and use a different child setup function,
avoiding closing fds that we were meant to inherit.

Fixes: 88a928ea "run: Avoid execve() when measuring test coverage"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-13 13:48:38 +00:00
Alice Mikhaylenko
1a5fd13ca5 dir: Fix glib criticals 2024-01-21 17:33:07 -06:00
Patrick Griffis
8c26798991 update: Make autopruned refs automatically removed
In order to maintain a system over time update automatically removes any EOL runtimes that are unused.

This extends it to also remove any autopruned refs. In practice this means removing no longer used driver versions as the system is updated.

Closes #5261
2024-01-07 18:51:46 -06:00
Dan Nicholson
3afdfd298b run: Ignore system bus failures in parental controls check
Currently if the parental controls check can't connect to the system
bus, apps are not allowed to run. However, apps are also allowed to run
if the malcontent (or accounts-service) D-Bus services aren't available.
Since it's trivial to meet that requirement by starting a temporary
dbus-daemon and setting `DBUS_SYSTEM_BUS_ADDRESS` to use it, not being
able to access the system bus at all is no less secure.

This primarily affects flatpak running in a container where D-Bus is
generally not available.

Fixes: #5076
2023-12-08 10:51:30 +00:00
Dan Nicholson
9e58442804 run: Ensure container environment variable is used
If flatpak is run within a container manager that sets the `container`
environment variable, it will override the attempted `flatpak` value.
2023-11-30 13:54:31 -06:00
Simon McVittie
9bf3620aef common: Move locale utils to their own small translation unit
This reduces the size of flatpak-utils and flatpak-dir, which are both
too large.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-11-14 18:39:22 +00:00
Simon McVittie
f3278b3720 dir: Expose individual locale code paths for testing
This means we can still test the old code path, even when a newer version
of AccountsService that will take the newer code path is available.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-11-14 18:39:22 +00:00
Simon McVittie
974eadafdb dir: Expose flatpak_get_user_locales, flatpak_get_system_locales for testing
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-11-14 18:39:22 +00:00
Simon McVittie
a2f6659c1c dir: Don't crash if accountsservice cannot be contacted
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-11-14 18:39:02 +00:00
Georges Basile Stavracas Neto
9d63abef0a dir: Always call g_once_init_leave() for languages
Otherwise we leave the g_once_init_enter() call unpaired.

Closes https://github.com/flatpak/flatpak/issues/5588
Fixes 14c548301c
2023-11-14 16:47:35 +00:00
Georges Basile Stavracas Neto
fac93e69d5 dir: Change scope of localed_proxy variable
Move it to where it's actually used.
2023-11-14 16:47:35 +00:00
Dor Askayo
df4e98fc15 instance: Add flatpak_instance_get_run_environ()
This private function returns the environment in which flatpak-run
was executed for a given FlatpakInstance.
2023-10-27 17:09:52 +01:00
Dor Askayo
5a7ea354d3 context: Use flatpak_parse_env_block()
No change in behavior, except minor wording of the error message.
2023-10-27 17:09:52 +01:00
Dor Askayo
2804cd4116 utils: Add flatpak_parse_env_block()
This function can be used to parse environment variable blocks in
"env -0" format from a buffer. It performs a few format checks during
its parsing and returns an error if an issue is found.

When successful, it returns a string array containing each individual
environment variable parsed from the buffer, in the same format as
the return value of g_get_environ().

The implementation of this function is based on
flatpak_context_parse_env_block().
2023-10-27 17:09:52 +01:00
Dor Askayo
c262e5ff0b run: Save flatpak-run environment
Save the environment in which flatpak-run was executed in the private
instance directory.

The environment is saved in "env -0" format.
2023-10-27 17:09:52 +01:00
Dor Askayo
c5a4ec0fa9 instance: Create private instance directory
Unlike the instance directory, whose directory structure is
considered public API and is mounted in the sandbox, the private
instance directory is meant to hold private data or metadata about
an instance for use by internal components.

The private instance directory is not meant to be shared with any
external component, and provides no guarantees about its structure
or contents.

While the public instance directory is named "<instance-id>", the
private instance directory is named "<instance-id>-private". Both the
public and private instance directories share the same parent
directory.

The private instance directory relies on the same lock file as the
public instance directory, and both are garbage-collected together.
2023-10-27 17:09:52 +01:00
Dor Askayo
2ba322b1c0 instance: Extract lock creation logic and reverse checks
This allows extending the instance ID allocation logic with less
impact on readability.

No change in behavior.
2023-10-27 17:09:52 +01:00
Sebastian Wick
08090f90c7 flatpak-run-wayland: Change security-context sandbox engine name
To align with work in D-Bus, the security-context's sandbox engine names
are now supposed to follow the reverse-DNS style.

This change should be fine because there has not been a release of
flatpak with security-context support, yet.
2023-10-24 14:16:48 +01:00
Bastien Nocera
14c548301c dir: Get user's languages from AccountsService
Instead of getting it from locale environment variables as
g_get_language_names() and derivates do.
2023-10-13 14:44:46 -03:00
Bastien Nocera
dfba3853c7 dir: Use AccountsService to get system languages
Use the new GetUsersLanguages() method from AccountsService to get the
list of all the locales that each user is interested in.

See https://gitlab.freedesktop.org/accountsservice/accountsservice/-/merge_requests/99

Closes: #5006
2023-10-13 14:44:46 -03:00
Steveice10
b8d8d80c61 flatpak-run: Unset VK_DRIVER_FILES and VK_ICD_FILENAMES
These environment variables inform the Vulkan loader on where to find driver files.
Since they typically point to locations on the host filesystem, any application that
attempts to load Vulkan within the flatpak sandbox would break with these set.
2023-10-08 11:33:57 -05:00
Dor Askayo
1dc334c8a9 instance: Use glnx_opendirat() where possible
Doing so adds the following flags to the openat() call:
O_RDONLY | O_NONBLOCK | O_NOCTTY

And removes the following flag: O_PATH

Also let libglnx handle the error message formatting.
2023-09-22 14:32:17 +01:00
Philip Withnall
d6b10c26ef utils-http: Add transfer speed timeout for libcurl HTTP downloads
There was already a timeout set using `CURLOPT_CONNECTTIMEOUT`, but that
only affects the initial connection. Once a HTTP connection is
established, the timeout is ineffective.

That means that once a HTTP connection is established, there is no bound
on the length of time a download can take. Sometimes, downloads drop to
very low speeds (for Networking Reasons) and we need a way to give up on
a download if that happens.

So, set a low speed limit of 10KB/s for 60s. If a curl download goes
more slowly than this, it will be considered timed out, and the
`CURLE_OPERATION_TIMEDOUT` code path will be taken.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: https://github.com/flatpak/flatpak/issues/5519
2023-09-15 12:36:25 +01:00
Simon McVittie
e6bd149c25 build: Generate Wayland glue code as private
The `code` argument to wayland-scanner is deprecated in favour of
`private-code`, which marks the symbols as private, avoiding them
leaking into the ABI of `libflatpak.so.0`.

`private-code` was new in wayland-scanner 1.15, which is available in
relatively old LTS distributions like CentOS 7, Debian 10 and
Ubuntu 18.04, and is much older than wayland-protocols 1.32.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-09-10 13:58:53 +01:00
Simon McVittie
bb8afb0328 build: Link Wayland code into full libflatpak-common only
This is only needed in flatpak-run-wayland.c, so we don't need it when
linking ancillary daemons that don't need any of flatpak-run, such as
the portal, session helper, system helper and OCI authenticator.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-09-10 13:58:53 +01:00
Forest
738a0b14de Add --device=input permission
This new permission exposes the host's /dev/input directory, providing
minimal game controller support without resorting to --device=all.
2023-09-08 19:03:24 +01:00
Simon McVittie
d42d3e9772 dir: Use g_strv_equal()
This was new in GLib 2.60, but libglnx now provides a backport, so we
can use it unconditionally.

We still need a wrapper because g_strv_equal() is not NULL-safe, but
it's a very thin wrapper now.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-09-04 13:25:32 +01:00
Simon McVittie
5009857b5a Use g_steal_fd()
This was new in GLib 2.70, but libglnx now provides a backport, so we
can use it unconditionally.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-09-04 13:25:32 +01:00
Simon Ser
f0e626a4b6 common: add support for Wayland security context
This exposes a reliable way for Wayland compositors to get
identifying information about a client. Compositors can then
apply security policies if desirable.

See: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/68
2023-08-24 12:17:53 +02:00
Simon Ser
b4822e2230 common: split Wayland socket setup to separate file
More complicated setup logic will be added next commit.
2023-08-24 12:17:53 +02:00
Simon Ser
522dd8b911 common: introduce flatpak_bwrap_add_sync_fd
The same logic will be used for Wayland security context.
2023-08-24 12:17:53 +02:00
Simon McVittie
ae2579637a app: Move terminal-related utility functions from common into app
These functions are to do with being an interactive, terminal-oriented
CLI/TUI, so it would be inappropriate for library code in libflatpak
to call them, and it would also be inappropriate for daemons like the
session and system helpers to call them.

In fact all calls to these were already isolated to app/, so we can
easily move the terminal-related utilities themselves into app/.

As well as shrinking libflatpak, this makes it obvious that the system
helper does not actually need to call flatpak_disable_fancy_output():
it does not link any code that would be affected by that API call.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:19:00 +02:00
Simon McVittie
36bc1bd382 common: Break circular dependency between context and dir/run
By giving the context a bit more knowledge of which directories Flatpak
uses for what, we can stop it from depending on the dir/run modules,
which themselves depend on FlatpakContext.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
a898065a66 common: Move metadata to its own header
This cuts more circular dependencies between flatpak-run and
flatpak-exports.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
846e109fb2 common: Explicitly include context's own header
Previously we were relying on flatpak-utils-private.h having a circular
dependency on this, but I'm intending to remove that during future
refactoring.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
bf8e91cedc common: Stop including utils-http in utils
utils-http is conceptually larger than utils.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
f6cf1b2bbb common: Remove unused functions
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
5522d19f4f common: Move flatpak_is_app_runtime_or_appstream_ref to ref-utils
This matches where it was declared.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
8032ca9d69 common: Move flatpak_abs_usrmerged_dirs to FlatpakExports
This will help to break circular dependencies between FlatpakExports
and flatpak-run.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
f79f90e9c0 common: Move declaration of get_compat_arch_reverse back to -utils
The implementation never actually moved to -ref-utils.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Sebastian Wick
bbac52e6af dir: Document the apply_extra_data run flags
They are the same as `flatpak run --sandbox` with two exceptions:

  * `FLATPAK_RUN_FLAG_MULTIARCH` might be required so we just add it
    always
  * `FLATPAK_RUN_FLAG_NO_PROC` is added to prevent sandbox escapes via
    `/proc/self/exe`

Signed-off-by: Sebastian Wick <sebastian.wick@redhat.com>
2023-07-03 19:47:00 +02:00
Simon McVittie
dadd737381 common: Fix an uninitialized variable
This could be uninitialized if the app has --socket=fallback-x11 but
not --socket=wayland, although that's a combination that doesn't make
a great deal of sense. Regression introduced during recent refactoring.

Fixes: db7a8bb5 "common: Split up socket setup from flatpak-run into multiple files"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-05-17 15:15:31 -05:00