Skip to content

Share /tmp between instances of the same app - #4093

Merged
alexlarsson merged 8 commits into
flatpak:masterfrom
smcv:shareable-tmp
Apr 15, 2021
Merged

alexlarsson merged 8 commits into
flatpak:masterfrom
smcv:shareable-tmp

Conversation

@smcv

@smcv smcv commented Jan 25, 2021 •

Copy link
Copy Markdown
Collaborator

Part of #4091.

Design

This no longer depends on #4126, and instead contains the parts of #4126 that it needs, so that we can try to land this feature without being blocked by the concerns about symlink attacks in the XDG_RUNTIME_DIR.

The g_get_user_runtime_dir() on the host now looks like this:

real host g_get_user_runtime_dir()
    \- app
        \- com.example.App (gets mounted as XDG_RUNTIME_DIR/app/com.example.App as before)
    \- .flatpak
        \- 123 (instance-specific directory, already used)
        \- 456 (instance-specific directory, already used)
        \- com.example.App (per-app directory, not visible in sandbox, new)
            \- .ref (locked non-exclusively by app, new)
            \- tmp (gets mounted as /tmp in app sandbox, new)

The shared /tmp is only used for flatpak run, not for flatpak build (which reinvents a lot of flatpak_run_app()), and also not for apply_extra.

Whenever we garbage-collect the numbered per-instance directories, we check their app info. If an instance is disappearing, we attempt to garbage-collect the corresponding tmp if no apps are still using them.

The shared /tmp is backed by a directory in g_get_user_runtime_dir(), so that unrelated Flatpak apps that happen to have --filesystem=/tmp don't see it. g_get_user_runtime_dir() will usually be a tmpfs (except on systems using e.g. sysvinit to boot).

The per-app-ID directory and the .ref file are never garbage-collected, to avoid defeating the locking scheme: $XDG_RUNTIME_DIR/.flatpak/ will contain one subdirectory for each app that the user has run during the current session, each containing an empty .ref file. These will normally get cleaned up when the user fully logs out (at the transition from 1 to 0 concurrent login sessions).

Commits

Best reviewed commit-by-commit, I think.

  • utils: Add flatpak_str_is_integer() (previously part of Create a shared XDG_RUNTIME_DIR for each app-ID #4126)

  • run: Mark some preconditions around the app ID

  • test-instance: Provide app-IDs for our mock apps

    Previously, this only had to consider two situations: either an instance
    is still running (alive), or it is not (dead).

    When we start sharing directories between all instances of a particular
    app-ID (Multiple instances of the same app don't share XDG_RUNTIME_DIR #4120, Share /tmp between instances of the same app #4093), we'll also need to consider whether instances
    share an app-ID, expanding the test to three situations: either an
    instance is still running (alive), or it has exited but shares its
    app-ID with a different instance that is still running (the app is
    alive but the instance is dead, abbreviated here as alive_dead),
    or it has exited and does not share its app-ID with any running
    instances (dead).

  • instance: Create and destroy per-app-ID subdirs of XRD/.flatpak

    If we want to provide a per-app-ID XDG_RUNTIME_DIR (Multiple instances of the same app don't share XDG_RUNTIME_DIR #4120) or a
    per-app-ID /tmp or /dev/shm (Share /tmp between instances of the same app #4093) then we'll need somewhere to put
    them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere
    that is not accessible to the app, so that we can trust its contents.

  • context: Factor out flatpak_context_get_exports_full()

    This combines the functionality of flatpak_context_get_exports() and its
    open-coded version in flatpak_context_append_bwrap_filesystem().

  • context: Build xdg_dirs_conf as an "out" argument

    flatpak_context_get_exports_full() previously copied the interface of
    flatpak_context_export(), which appended entries to a caller-supplied
    GString, but it's a more GLib-style API if we use an "out" argument.

  • common: Separate creation of FlatpakExports from append_bwrap_filesystem

    A subsequent commit will need to look at the FlatpakExports before
    we are ready to append their arguments to the FlatpakBwrap.

  • run: Share /tmp between all instances of an app-ID

    This allows apps that use /tmp as an IPC rendezvous point, such as those
    that embed Chromium-derived browsers, to communicate between instances;
    this would not previously have worked without --filesystem=/tmp, which
    is a significant weakening of the sandbox.

    It also allows /tmp to be shared with subsandboxes (if they are not
    sandboxed more strictly).

    The temporary directory is actually created in XDG_RUNTIME_DIR,
    to avoid it becoming visible to unrelated apps that happen to have
    --filesystem=/tmp.

@smcv
smcv requested a review from alexlarsson January 25, 2021 17:33
Comment thread app/flatpak-builtins-run.c Resolved
Comment thread common/flatpak-context.c Resolved
Comment thread common/flatpak-instance.c Resolved
Comment thread portal/flatpak-portal.c Resolved
Comment thread tests/test-exports.c Resolved
@smcv

smcv commented Jan 25, 2021

Copy link
Copy Markdown
Collaborator Author

v2 (ec77505): documented the new fields in flatpak-metadata(5).

@smcv smcv changed the title Make /dev/shm, /tmp shareable with sandboxes Make /dev/shm, /tmp shareable between instances Jan 26, 2021
@smcv

smcv commented Jan 26, 2021

Copy link
Copy Markdown
Collaborator Author

v3 (e0fb9ce): rewrite to share these directories between all instances, not just a main app and subsandboxes.

Comment thread common/flatpak-instance.c Resolved
Comment thread common/flatpak-instance.c Resolved
Comment thread common/flatpak-context-private.h Resolved
Comment thread common/flatpak-instance.c Resolved
@smcv

smcv commented Feb 12, 2021

Copy link
Copy Markdown
Collaborator Author

Latest version also resolves #4120, but probably needs some more thought.

@gasinvein

Copy link
Copy Markdown
Contributor

Looks like this went far beyond pressure-vessel support and now is going to fix a bunch of long-lasting issues. That's really cool.

@Erick555

Copy link
Copy Markdown
Contributor

So is this is still opt-in?

@smcv

smcv commented Feb 13, 2021

Copy link
Copy Markdown
Collaborator Author

So is this is still opt-in?

Yes, because opt-in is more code than not opt-in. If I test and submit a PR where it's opt-in, but maintainers want it to be applied to all apps, then I can delete the feature flag quickly and the PR will be ready. If I test and submit a PR where it's applied to all apps, but maintainers want it to be opt-in, it would take me longer to add the feature flag back in.

I do not have any particular opinion on whether it should be opt-in or not, I'm going by what @alexlarsson told me to do.

@Erick555

Erick555 commented Feb 13, 2021 •

Copy link
Copy Markdown
Contributor

I do not have any particular opinion on whether it should be opt-in or not

Do you think it's sufficient fixing stuff like #4120 only per opt-in basis? Main argument against opt-in is that it doesn't really fix anything without additional effort to change every app separately while that effort may be easily avoided without a downside. Do you disagree with this?

As Filippe wrote above this work is more about fixing long standing bugs/issues rather than introducing new feature.

@gasinvein

Copy link
Copy Markdown
Contributor

@Erick555 I guess opt-in is still much better than nothing. And indeed this can be reconsidered and made default in future.

@smcv

smcv commented Feb 13, 2021

Copy link
Copy Markdown
Collaborator Author

Do you think it's sufficient fixing stuff like #4120 only per opt-in basis?

That one is actually not opt-in as currently implemented, just if (!sandbox), so the only instances that don't share the XDG_RUNTIME_DIR will be subsandboxes started by flatpak-spawn --sandbox.

At the moment /tmp and /dev/shm are still behind a feature flag, rather than if (!sandbox). Again, that isn't because I think they necessarily should be opt-in, but because I'm trying to make it as easy as possible for Flatpak's busy maintainer to review it and decide whether that extra code is worthwhile or not - and trying to make sure that after a decision is made, I can have the implementation ready for merge as soon as possible.

I'm fairly sure I'm creating the per-app-ID XDG_RUNTIME_DIR in a more complicated way than I needed to in this version of the branch, but I ran out of time on Friday - I'll come back to it next week.

@Erick555

Copy link
Copy Markdown
Contributor

Fine, when you find some time please update description from first post to show current state of this work, thx.

smcv added a commit to smcv/flatpak that referenced this pull request Feb 16, 2021
If we want to provide a per-app-ID XDG_RUNTIME_DIR (flatpak#4120) or a
per-app-ID /tmp or /dev/shm (flatpak#4093) then we'll need somewhere to put
them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere
that is *not* accessible to the app, so that we can trust its contents.

Signed-off-by: Simon McVittie <[email protected]>
@smcv

smcv commented Feb 16, 2021

Copy link
Copy Markdown
Collaborator Author

I'm fairly sure I'm creating the per-app-ID XDG_RUNTIME_DIR in a more complicated way than I needed to

Fixed, and moved to #4126.

@smcv
smcv marked this pull request as ready for review February 16, 2021 20:48
@smcv

smcv commented Feb 17, 2021

Copy link
Copy Markdown
Collaborator Author

With some more testing: this seems to work as intended. I need to do more on the pressure-vessel side to make it compatible with Steam, but I think this part is solved, so this change is ready to be reviewed.

Comment thread common/flatpak-run.c Resolved
@alexlarsson

Copy link
Copy Markdown
Member

Other than the out value this looks good to me.

smcv added a commit to smcv/flatpak that referenced this pull request Apr 13, 2021
Previously, this only had to consider two situations: either an instance
is still running (alive), or it is not (dead).

When we start sharing directories between all instances of a particular
app-ID (flatpak#4120, flatpak#4093), we'll also need to consider whether instances
share an app-ID, expanding the test to three situations: either an
instance is still running (alive), or it has exited but shares its
app-ID with a different instance that is still running (the app is
alive but the instance is dead, abbreviated here as alive_dead),
or it has exited and does not share its app-ID with any running
instances (dead).

Signed-off-by: Simon McVittie <[email protected]>
smcv added a commit to smcv/flatpak that referenced this pull request Apr 13, 2021
If we want to provide a per-app-ID XDG_RUNTIME_DIR (flatpak#4120) or a
per-app-ID /tmp or /dev/shm (flatpak#4093) then we'll need somewhere to put
them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere
that is *not* accessible to the app, so that we can trust its contents.

Signed-off-by: Simon McVittie <[email protected]>
smcv added 3 commits April 13, 2021 13:43
Previously, this only had to consider two situations: either an instance
is still running (alive), or it is not (dead).

When we start sharing directories between all instances of a particular
app-ID (flatpak#4120, flatpak#4093), we'll also need to consider whether instances
share an app-ID, expanding the test to three situations: either an
instance is still running (alive), or it has exited but shares its
app-ID with a different instance that is still running (the app is
alive but the instance is dead, abbreviated here as alive_dead),
or it has exited and does not share its app-ID with any running
instances (dead).

Signed-off-by: Simon McVittie <[email protected]>
smcv added a commit to smcv/flatpak that referenced this pull request Apr 13, 2021
If we want to provide a per-app-ID XDG_RUNTIME_DIR (flatpak#4120) or a
per-app-ID /tmp or /dev/shm (flatpak#4093) then we'll need somewhere to put
them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere
that is *not* accessible to the app, so that we can trust its contents.

Signed-off-by: Simon McVittie <[email protected]>
Comment thread common/flatpak-instance.c Resolved
smcv added 5 commits April 13, 2021 19:10
If we want to provide a per-app-ID XDG_RUNTIME_DIR (flatpak#4120) or a
per-app-ID /tmp or /dev/shm (flatpak#4093) then we'll need somewhere to put
them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere
that is *not* accessible to the app, so that we can trust its contents.

Signed-off-by: Simon McVittie <[email protected]>
This combines the functionality of flatpak_context_get_exports() and its
open-coded version in flatpak_context_append_bwrap_filesystem().

Signed-off-by: Simon McVittie <[email protected]>
flatpak_context_get_exports_full() previously copied the interface of
flatpak_context_export(), which appended entries to a caller-supplied
GString, but it's a more GLib-style API if we use an "out" argument.

Signed-off-by: Simon McVittie <[email protected]>
A subsequent commit will need to look at the FlatpakExports before
we are ready to append their arguments to the FlatpakBwrap.

Signed-off-by: Simon McVittie <[email protected]>
This allows apps that use /tmp as an IPC rendezvous point, such as those
that embed Chromium-derived browsers, to communicate between instances;
this would not previously have worked without --filesystem=/tmp, which
is a significant weakening of the sandbox.

It also allows /tmp to be shared with subsandboxes (if they are not
sandboxed more strictly).

The temporary directory is actually created in XDG_RUNTIME_DIR,
to avoid it becoming visible to unrelated apps that happen to have
--filesystem=/tmp.

Signed-off-by: Simon McVittie <[email protected]>
@smcv

smcv commented Apr 14, 2021

Copy link
Copy Markdown
Collaborator Author

Ready for re-review.

If you're happy with the first three commits "utils: Add flatpak_str_is_integer()", "run: Mark some preconditions around the app ID" and "test-instance: Provide app-IDs for our mock apps" but work is still needed on subsequent commits, please let me know and I can get those first three merged.

@alexlarsson
alexlarsson merged commit b65b3f6 into flatpak:master Apr 15, 2021
alexlarsson pushed a commit that referenced this pull request Apr 15, 2021
Previously, this only had to consider two situations: either an instance
is still running (alive), or it is not (dead).

When we start sharing directories between all instances of a particular
app-ID (#4120, #4093), we'll also need to consider whether instances
share an app-ID, expanding the test to three situations: either an
instance is still running (alive), or it has exited but shares its
app-ID with a different instance that is still running (the app is
alive but the instance is dead, abbreviated here as alive_dead),
or it has exited and does not share its app-ID with any running
instances (dead).

Signed-off-by: Simon McVittie <[email protected]>
alexlarsson pushed a commit that referenced this pull request Apr 15, 2021
If we want to provide a per-app-ID XDG_RUNTIME_DIR (#4120) or a
per-app-ID /tmp or /dev/shm (#4093) then we'll need somewhere to put
them. Unlike $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this should be somewhere
that is *not* accessible to the app, so that we can trust its contents.

Signed-off-by: Simon McVittie <[email protected]>
smcv added a commit to smcv/flatpak that referenced this pull request Apr 15, 2021
If we call this after flatpak_run_add_app_info_args(), then the
garbage-collection code will have a chance to run, cleaning up after a
previous instance of the same app.

In a previous implementation of flatpak#4093 that also implemented flatpak#4120, we
had to allocate the per-app directory this early to avoid shadowing the
XDG_RUNTIME_DIR allocated in flatpak_run_add_app_info_args(), but I'm
taking a different approach to that now.

Signed-off-by: Simon McVittie <[email protected]>
alexlarsson pushed a commit that referenced this pull request Apr 16, 2021
If we call this after flatpak_run_add_app_info_args(), then the
garbage-collection code will have a chance to run, cleaning up after a
previous instance of the same app.

In a previous implementation of #4093 that also implemented #4120, we
had to allocate the per-app directory this early to avoid shadowing the
XDG_RUNTIME_DIR allocated in flatpak_run_add_app_info_args(), but I'm
taking a different approach to that now.

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

ready-for-review sandbox issue related to sandbox setup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants