Skip to content

Avoid exporting reserved directories more thoroughly / improve error reporting - #5213

Merged
smcv merged 10 commits into
flatpak:mainfrom
smcv:avoid-exporting-reserved-dirs
Jan 12, 2023
Merged

smcv merged 10 commits into
flatpak:mainfrom
smcv:avoid-exporting-reserved-dirs

Conversation

@smcv

@smcv smcv commented Dec 12, 2022 •

Copy link
Copy Markdown
Collaborator

If we try to share certain reserved directories with the sandbox, that can break our ability to set up sandbox-specific things: the app in /app, the runtime in /usr and /etc, OS stuff in /dev and /proc, and Flatpak-specific information in /.flatpak-info, /run/flatpak and /run/host.

Previously, we would silently prevent descendants of most of these paths from being shared (#5035). We still prevent that, but now there is a warning so that users will be less mystified.

We did not previously prevent descendants of /.flatpak-info, /run/flatpak and /run/host from being shared, but now those three have joined the list.

We also did not previously prevent ancestors of the reserved paths from being shared, which in practice manifested in two ways: a symlink like /the-root -> / or /the-root -> . would prevent apps from being run successfully with --filesystem=host or --filesystem=/the-root (#1357), and --filesystem=/run would also fail. Now, both are prevented, with a warning in all cases except --filesystem=host:

F: Not sharing "/run" with sandbox: Path "/run/flatpak" is reserved by Flatpak
F: Not sharing "/the-root" with sandbox: Path "/.flatpak-info" is reserved by Flatpak

For --filesystem=host it seemed better for unsuitable top-level objects to be rejected silently (other than debug messages), so that there isn't a lot of noise for directories like /bin, /lib, /libx32 or merged-/usr compat symlinks like /bin -> usr/bin, /lib -> usr/lib, /libx32 -> usr/libx32.


@smcv smcv changed the title Avoid exporting reserved directories Avoid exporting reserved directories more thoroughly / improve error reporting Dec 12, 2022
@smcv
smcv requested review from alexlarsson and mwleeds December 12, 2022 16:35
@smcv smcv added bug sandbox issue related to sandbox setup labels Dec 12, 2022
@smcv

smcv commented Dec 12, 2022

Copy link
Copy Markdown
Collaborator Author

cc @RyuzakiKK

@smcv

smcv commented Dec 12, 2022

Copy link
Copy Markdown
Collaborator Author

Needs work: the warning "Unable to provide access to app-specific directory" is making some tests fail, because they're run with G_DEBUG=fatal-warnings, but flatpak info calls into this code for purposes other than actually running the app, which means the app-specific directory hasn't necessarily been created.

@smcv
smcv force-pushed the avoid-exporting-reserved-dirs branch from b97eab0 to 7566efa Compare December 14, 2022 14:02
@smcv
smcv marked this pull request as draft December 14, 2022 14:28
@smcv
smcv force-pushed the avoid-exporting-reserved-dirs branch from 7566efa to 2dfc2b3 Compare December 14, 2022 14:40
@smcv smcv self-assigned this Dec 14, 2022
@smcv
smcv marked this pull request as ready for review December 14, 2022 14:42
@smcv
smcv force-pushed the avoid-exporting-reserved-dirs branch 2 times, most recently from b704302 to f17bc58 Compare December 14, 2022 20:04
@smcv

smcv commented Dec 14, 2022

Copy link
Copy Markdown
Collaborator Author

This might have some minor conflicts with #5219 if that gets merged first, but is ready for review: cannot_export() already uses G_LOG_LEVEL_INFO, so the conflicts can easily be resolved by taking this branch's version of the changed code.

Comment thread tests/test-exports.c Resolved
@smcv
smcv force-pushed the avoid-exporting-reserved-dirs branch from f17bc58 to 8e93ddf Compare December 15, 2022 16:58
Comment thread common/flatpak-context.c Outdated
};

static void
cannot_export (FlatpakFilesystemMode mode,

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.

Tiny bikeshed. I'd find this function more clear if it noted that its just a logging function.

print_cannot_export_error, log_cannot_export_error, etc

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good thinking, I used log_cannot_export_error.

smcv added 4 commits January 12, 2023 14:46
This will reduce conflicts when new entries are added.

Helps: flatpak#5205
Signed-off-by: Simon McVittie <[email protected]>
These directories are reserved for Flatpak's own use.

Helps: flatpak#5205
Signed-off-by: Simon McVittie <[email protected]>
Just for completeness, in practice the host system will not have this.

Helps: flatpak#5205
Signed-off-by: Simon McVittie <[email protected]>
This is a step towards allowing its direct and indirect callers to decide
how serious the failure is, and debug or warn accordingly.

Helps: flatpak#5205
Signed-off-by: Simon McVittie <[email protected]>
@smcv
smcv force-pushed the avoid-exporting-reserved-dirs branch from 8e93ddf to 4c7ad7e Compare January 12, 2023 14:46
smcv added 6 commits January 12, 2023 14:52
This lets flatpak_context_export() or other callers decide how they want
to handle failure to export each path. For now, the callers in
FlatpakExports are still using g_debug() unconditionally, but we can now
have somewhat better test coverage.

Helps: flatpak#1357
Helps: flatpak#5035
Helps: flatpak#5205
Helps: flatpak#5207
Signed-off-by: Simon McVittie <[email protected]>
If the user gives us a override or command-line argument that we cannot
obey, like --filesystem=/usr/share/whatever or
--filesystem=/run/flatpak/whatever, then it's confusing that we silently
ignore it. We should give them an opportunity to see that their override
was ineffective.

However, there are a few situations where we still want to keep quiet.
If there is a --filesystem argument for something that simply doesn't
exist, we don't diagnose the failure to share it: that avoids creating
unnecessary noise for apps that opportunistically share locations that
might or might not exist, like the way the Steam app on Flathub asks
for access to $XDG_RUNTIME_DIR/app/com.discordapp.Discord.

Similarly, if we have been asked for --filesystem=host, the root
directory is very likely to contain symlinks into a reserved path, like
/lib -> usr/lib. We don't need a user-visible warning for that.

We actually use the equivalent of g_message() rather than g_warning(),
to avoid this being fatal during unit testing (in particular when we
do a `flatpak info` on an app that has never been run, which will
be unable to share its `.var/app` subdirectory). `app/flatpak-main.c`
currently displays them as equivalent to each other anyway.

Signed-off-by: Simon McVittie <[email protected]>
If $HOME is below a reserved path (for example `/usr/home/thompson`
for Unix traditionalists) or otherwise cannot be shared, or is a
symbolic link to somewhere that cannot be shared, then we will end
up running the app with $HOME not existing. This is unexpected, so
we should make more noise about it.

There are two situations here, both of which get a warning: if we have
--filesystem=home or --filesystem=host then we are trying to share the
real $HOME with the application, and if we do not, then we are trying
to create a directory at the location of the real $HOME and replicate
the chain of symlinks (if any) leading from $HOME to that location.

Unlike the previous commit, this is not expected to happen during unit
testing, so we do not use a g_warning() for this.

Diagnoses: flatpak#5035
Signed-off-by: Simon McVittie <[email protected]>
Previously, --filesystem=/run would prevent apps from starting by
breaking our ability to set up /run/flatpak and /run/host. Now it is
ignored, with a diagnostic message, resolving flatpak#5205 and flatpak#5207.

Similarly, --filesystem=/symlink-to-root (or --filesystem=host) would
have prevented apps from starting if a symlink like
`/symlink-to-root -> /` or `/symlink-to-root -> .` exists, and refusing
to export the target of that symlink avoids that failure mode,
resolving flatpak#1357.

Resolves: flatpak#1357
Resolves: flatpak#5205
Resolves: flatpak#5207
Signed-off-by: Simon McVittie <[email protected]>
@smcv
smcv force-pushed the avoid-exporting-reserved-dirs branch from 4c7ad7e to fa005cd Compare January 12, 2023 14:57
@smcv

smcv commented Jan 12, 2023

Copy link
Copy Markdown
Collaborator Author

I'll merge this when CI has finished.

@smcv
smcv merged commit 0aa0284 into flatpak:main Jan 12, 2023
@smcv smcv mentioned this pull request Feb 7, 2023
4 tasks done
smcv added a commit to smcv/flatpak that referenced this pull request Feb 8, 2023
smcv added a commit to smcv/flatpak that referenced this pull request Feb 8, 2023
smcv added a commit that referenced this pull request Feb 10, 2023
Signed-off-by: Simon McVittie <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ready-to-merge sandbox issue related to sandbox setup

Projects

None yet

2 participants