Skip to content

Create a shared XDG_RUNTIME_DIR for each app-ID - #4126

Merged
alexlarsson merged 8 commits into
flatpak:masterfrom
smcv:xrd-per-app-id
Apr 16, 2021
Merged

alexlarsson merged 8 commits into
flatpak:masterfrom
smcv:xrd-per-app-id

Conversation

@smcv

@smcv smcv commented Feb 15, 2021 •

Copy link
Copy Markdown
Collaborator

Fixes #4120.

Design

The XDG_RUNTIME_DIR on the host now looks like this:

real host XDG_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, from #4093)
            \- tmp (gets mounted as /tmp in app sandbox, from #4093)
            \- xdg-run (gets mounted as XDG_RUNTIME_DIR in app sandbox, new)

In this implementation, we never garbage-collect xdg-run, just like the pre-existing $XDG_RUNTIME_DIR/app/$FLATPAK_ID. If we wanted to GC it, the procedure would be the same as for /tmp (#4093).

I have not removed the special handling of $XDG_RUNTIME_DIR/app/$FLATPAK_ID. Those are slightly redundant now, but they might still make sense as a location that is more directly user-visible and has the same path on the host and in the sandbox.

This is only done for flatpak run, not for flatpak build (which reinvents a lot of flatpak_run_app()), and not for apply_extra either.

To avoid tricky things happening with symlinks, and to avoid mounting a directory onto a location we have already half-populated, we no longer mount sockets etc. directly onto members of $XDG_RUNTIME_DIR. Instead, we mount them in /run/flatpak.

For things where there's no real reason to want to use $XDG_RUNTIME_DIR, like Xauthority, we just use /run/flatpak as the new canonical path, and the file/directory doesn't exist in $XDG_RUNTIME_DIR at all.

For things where the specification involves $XDG_RUNTIME_DIR, most notably the Wayland socket but also D-Bus and PulseAudio, we populate $XDG_RUNTIME_DIR with symlinks into /run/flatpak as our last piece of filesystem manipulation.

Commits

  • run: Delay allocation of per-app directory

    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 Share /tmp between instances of the same app #4093 that also implemented Multiple instances of the same app don't share XDG_RUNTIME_DIR #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.

  • run: Use a constant path for the reference to per-app directories

    It's a bit simpler to get a per-app XDG_RUNTIME_DIR safely if we avoid
    putting this in there. Nothing relies on it being in the
    XDG_RUNTIME_DIR.

  • run: Create Xauthority in /run/flatpak

    There's no real reason why this needs to be in XDG_RUNTIME_DIR: nothing
    relies on it being there, and applications find it via environment
    variable XAUTHORITY.

  • run: Create ssh-auth socket in /run/flatpak

    There's no real reason why this has to be in the XDG_RUNTIME_DIR:
    nothing looks for it via XDG_RUNTIME_DIR, it's located via environment
    variable SSH_AUTH_SOCK.

  • run: Put the AT-SPI bus socket in /run/flatpak

    There's no real reason why this has to be in the XDG_RUNTIME_DIR: it's
    located via environment variable AT_SPI_BUS_ADDRESS.

  • run: Normalize WAYLAND_DISPLAY to a reasonable name

    If it doesn't start with wayland- or contains a directory separator,
    then it's probably not something we want to be creating in the
    sandbox's XDG_RUNTIME_DIR.

  • run: Populate XDG_RUNTIME_DIR with symlinks into /run/flatpak

    If XDG_RUNTIME_DIR is under app control, as it will be with Multiple instances of the same app don't share XDG_RUNTIME_DIR #4120, we
    don't want to be mounting pieces of filesystem directly into it, because
    that will mean that the app could create a symlink that will cause us
    to create a mount point for it at the target of the symlink, potentially
    elsewhere in the host filesystem.

    Instead, we mount them in /run/flatpak, which is a per-instance
    directory entirely controlled by Flatpak; and then create (relative)
    symlinks in XDG_RUNTIME_DIR, pointing into /run/flatpak.

    In this commit, we still know that the XDG_RUNTIME_DIR is a
    per-instance tmpfs, so we can safely create the symlinks using
    the --symlink option. In a subsequent commit this will change to
    creating them in a shared XDG_RUNTIME_DIR, if any.

  • run: Create a shared XDG_RUNTIME_DIR for each app-ID

    Like $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this is shared between all
    instances of the app, except for subsandboxed instances created by
    flatpak-spawn --sandbox or equivalent. Unlike
    $XDG_RUNTIME_DIR/app/$FLATPAK_ID, it does not exist at an equivalent
    path on the host and in the sandboxed app.

    Resolves: Multiple instances of the same app don't share XDG_RUNTIME_DIR #4120

@Erick555

Copy link
Copy Markdown
Contributor

Minor nitpick: is xrd a recognizable acronym? If not I expect some confusion about what mystery xrd dir is for. Maybe using run will be more familiar here instead?

@smcv

smcv commented Feb 16, 2021

Copy link
Copy Markdown
Collaborator Author

Minor nitpick: is xrd a recognizable acronym?

Not really. I don't think run is necessarily any better though - I'd expect that to mean /run, particularly after #4093 adds tmp and dev-shm next to it. In most other projects I'd say runtime, but that already means something different in Flatpak, so it seems less good here. xdg-runtime-dir or XDG_RUNTIME_DIR is clear, but a bit long.

xdg-run or xdg-runtime seem like maybe a good balance of "not too long" and "says what it's for"?

We could bikeshed this forever, so I'm going to pick a replacement and stick to it, then wait for maintainers to approve or reject - I expect I'll need at least one round of fixups anyway.

@smcv

smcv commented Feb 16, 2021

Copy link
Copy Markdown
Collaborator Author

A design question that should be resolved by the Flatpak maintainers before merging this:

Consider my scenario from #4120, where we run instance 1, run instance 2 concurrently, exit from them both, and run instance 3. What should happen? Options are:

  1. Instances 1, 2 and 3 all share an XDG_RUNTIME_DIR, assuming you didn't log out and back in (causing the "real" XDG_RUNTIME_DIR on the host to be deleted and recreated) before running instance 3
  2. Instances 1 and 2 share an XDG_RUNTIME_DIR, but it is garbage-collected sometime after instance 2 exits, and then instance 3 gets a fresh XDG_RUNTIME_DIR, even if you didn't log out and back in

For now I've gone for the first option (don't ever GC private XDG_RUNTIME_DIR instances) because it matches the behaviour of the "real" XDG_RUNTIME_DIR on the host system, and also because it matches the behaviour that $XDG_RUNTIME_DIR/app/$FLATPAK_ID already has.

@smcv

smcv commented Feb 16, 2021 •

Copy link
Copy Markdown
Collaborator Author

One thing that is a bit weird in this branch is that if you run two instances of an app with different permissions (via flatpak override or flatpak run --socket=... or just upgrading the app), they can get different sockets mounted below $XDG_RUNTIME_DIR. If they are sharing the underlying $XDG_RUNTIME_DIR, then the one that doesn't have the socket will get a file (currently an empty regular file) appearing in the directory.

This is because bubblewrap needs to create something (more specifically, a non-directory non-symlink) to act as the mount point, and currently that something is an empty regular file with 0666 permissions.

Example (assume the first command, in the background, continues to run while the other two are run):

$ ./flatpak run --command=bash --socket=wayland org.gnome.Recipes -c 'sleep 60' &
$ ./flatpak run --command=bash --socket=wayland org.gnome.Recipes -c 'ls -al $XDG_RUNTIME_DIR/wayland-0'
srwxr-xr-x ... /run/user/1000/wayland-0
$ ./flatpak run --command=bash --nosocket=wayland org.gnome.Recipes -c 'ls -al $XDG_RUNTIME_DIR/wayland-0'
-rw-rw-rw- ... /run/user/1000/wayland-0

@smcv

smcv commented Feb 16, 2021

Copy link
Copy Markdown
Collaborator Author

and currently that something is an empty regular file with 0666 permissions

(See also containers/bubblewrap#405)

@smcv
smcv force-pushed the xrd-per-app-id branch 2 times, most recently from d245332 to 0caaa71 Compare February 16, 2021 19:58
@smcv
smcv marked this pull request as ready for review February 16, 2021 20:08
@smcv
smcv requested a review from alexlarsson February 16, 2021 20:44
@RyuzakiKK

Copy link
Copy Markdown
Contributor

I'm not a maintainer but LGTM.

@smcv

smcv commented Feb 23, 2021

Copy link
Copy Markdown
Collaborator Author

This seems to be working pretty well, but I'll need maintainer judgement on the right answer to the design question to proceed with this.

@smcv smcv added ready-for-review sandbox issue related to sandbox setup and removed ready-for-review labels Feb 25, 2021
@smcv

smcv commented Feb 26, 2021

Copy link
Copy Markdown
Collaborator Author

Looks like this causes Steam to not find CA certificates any more. Looking into that.

@smcv

smcv commented Feb 26, 2021

Copy link
Copy Markdown
Collaborator Author

Looks like this causes Steam to not find CA certificates any more. Looking into that.

Resolved by setting up the XDG_RUNTIME_DIR even earlier, so that it doesn't "shadow" the p11-kit/pkcs11 socket that validates certificates on the host.

@alexlarsson

alexlarsson commented Apr 6, 2021 •

Copy link
Copy Markdown
Member

I don't fundamentally disagree with an instance-shared rundir, and i think not GC:ing it is fine. However, I worry a bit about having the sandbox setup running against an app-controlled director.

For instance, suppose the app was running, and it made $xrd/pulse a symlink somewhere, then during setup flatpak run (or really bwrap) follow that symlink and create a file called "config" there. In particular, this setup will happen in the phase of bwrap where the old root is mounted at /oldroot, so if the symlink is constructed in the right way it could point outside of the sandbox being constructed.

Now, the exact filenames and content (mostly empty files) being created are fixed and limited, so I can't offhand think of a massive exploit using this thing, but its not giving me a warm fuzzy feeling. This is why the shared instance dir when it was added wasn't the entire x-r-d.

However, we could maybe go the other way, where everything is shared except some specific subdir of x-r-d (say /run/user/1000/instance) where we put a tmpfs (which can't be moved or renamed by the app). Then we move all the flatpak created stuff to that directory, and rejig the various env vars to point to those.

@smcv

smcv commented Apr 6, 2021

Copy link
Copy Markdown
Collaborator Author

For instance, suppose the app was running, and it made $xrd/pulse a symlink somewhere, then during setup flatpak run (or really bwrap) follow that symlink and create a file called "config" there

Hmm, that's a good point. See also containers/bubblewrap#390.

However, we could maybe go the other way, where everything is shared except some specific subdir of x-r-d (say /run/user/1000/instance) where we put a tmpfs (which can't be moved or renamed by the app). Then we move all the flatpak created stuff to that directory, and rejig the various env vars to point to those.

That's an interesting idea, I'll look into how implementable it is.

@smcv

smcv commented Apr 6, 2021

Copy link
Copy Markdown
Collaborator Author

We could mitigate both that concern and containers/bubblewrap#390 if bubblewrap had a mode that we could opt into which would either refuse to follow symlinks when creating the target of a mount, or refuse to auto-create the target of a mount at all? Then the new flatpak that has this feature could require the new bubblewrap.

Would you mind taking a look at containers/bubblewrap#405, containers/bubblewrap#406? I think a solution to containers/bubblewrap#390 would look quite similar to 406.

containers/bubblewrap#409 is also in a similar space.

@alexlarsson

Copy link
Copy Markdown
Member

We could mitigate both that concern and containers/bubblewrap#390 if bubblewrap had a mode that we could opt into which would either refuse to follow symlinks when creating the target of a mount, or refuse to auto-create the target of a mount at all? Then the new flatpak that has this feature could require the new bubblewrap.

I'm not sure that would work. It just means bubblewrap would have to --dir $xrd/pulse --bind-mount $xrd/pulse/config (or the bind mount fails), and I wouldn't be surprised if that worked if pulse was already a symlink to another existing dir. At the very least there is a large risk that somewhere there is a similar thing that works and you can access /oldroot somehow.

Another option other than the subdir is to always mount the various files (as empty data, or tmpfs:es), even if not used. Because if they are in place then the app cannot modify those pathnames. Of course, there is a risk that newly added files in there could pre-exist as only newer flatpaks would bind-mount them, but we could make sure to only add new files in some tmpfs subdir.

@alexlarsson

Copy link
Copy Markdown
Member

I'll try to take a look at the bubblewrap issues soon.

@alexlarsson

Copy link
Copy Markdown
Member

I'm not sure that would work. It just means bubblewrap would have to --dir $xrd/pulse --bind-mount $xrd/pulse/config (or the bind mount fails), and I wouldn't be surprised if that worked if pulse was already a symlink to another existing dir. At the very least there is a large risk that somewhere there is a similar thing that works and you can access /oldroot somehow.

Also, if you have running code with modification rights to the directory in question, validating whether something is a symlink or not before mounting is fundamentally racy anyway.

@smcv

smcv commented Apr 6, 2021 •

Copy link
Copy Markdown
Collaborator Author

Also, if you have running code with modification rights to the directory in question, validating whether something is a symlink or not before mounting is fundamentally racy anyway.

I think it's possible to implement securely. Validating whether something is a symlink obviously has a fundamental TOC/TOU issue, but we can create things in a way that will atomically check whether they are a symlink: mkdirat and symlinkat already fail with EEXIST for symlinks, and openat with O_NOFOLLOW will fail with ELOOP rather than following symlinks.

@smcv
smcv force-pushed the xrd-per-app-id branch 2 times, most recently from fb54808 to 835713c Compare April 7, 2021 11:29
@smcv

smcv commented Apr 8, 2021

Copy link
Copy Markdown
Collaborator Author

For instance, suppose the app was running, and it made $xrd/pulse a symlink somewhere, then during setup flatpak run (or really bwrap) follow that symlink and create a file called "config" there

I confirm that this can genuinely happen.

One thing we might be able to do about this:

  • Instead of bind-mounting various things directly into $XDG_RUNTIME_DIR, bind-mount them into a Flatpak-controlled directory, probably /run/flatpak

  • For the ones that don't really need to be in $XDG_RUNTIME_DIR anyway, just use /run/flatpak/... as their canonical path

  • For the ones that ought to be in $XDG_RUNTIME_DIR because an API specification says so (e.g. the Wayland and D-Bus sockets), drop a symlink into the shared $XDG_RUNTIME_DIR, which might succeed or fail, but will not result in traversing a symlink

    • open the shared directory as a dirfd
    • unlinkat (dirfd, "wayland-0")
    • symlinkat ("/run/flatpak/wayland-0", dirfd, "wayland-0")
    • if any of those steps fail, either warn and early-return, or warn and refuse to launch the app

@smcv
smcv marked this pull request as draft April 8, 2021 19:45
smcv added 8 commits April 15, 2021 19:56
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]>
It's a bit simpler to get a per-app XDG_RUNTIME_DIR safely if we avoid
putting this in there. Nothing relies on it being in the
XDG_RUNTIME_DIR.

Signed-off-by: Simon McVittie <[email protected]>
There's no real reason why this needs to be in XDG_RUNTIME_DIR: nothing
relies on it being there, and applications find it via environment
variable XAUTHORITY.

Signed-off-by: Simon McVittie <[email protected]>
There's no real reason why this has to be in the XDG_RUNTIME_DIR:
nothing looks for it via XDG_RUNTIME_DIR, it's located via environment
variable SSH_AUTH_SOCK.

Signed-off-by: Simon McVittie <[email protected]>
There's no real reason why this has to be in the XDG_RUNTIME_DIR: it's
located via environment variable AT_SPI_BUS_ADDRESS.

Signed-off-by: Simon McVittie <[email protected]>
If it doesn't start with wayland- or contains a directory separator,
then it's probably not something we want to be creating in the
sandbox's XDG_RUNTIME_DIR.

Signed-off-by: Simon McVittie <[email protected]>
If XDG_RUNTIME_DIR is under app control, as it will be with flatpak#4120, we
don't want to be mounting pieces of filesystem directly into it, because
that will mean that the app could create a symlink that will cause us
to create a mount point for it at the target of the symlink, potentially
elsewhere in the host filesystem.

Instead, we mount them in /run/flatpak, which is a per-instance
directory entirely controlled by Flatpak; and then create (relative)
symlinks in XDG_RUNTIME_DIR, pointing into /run/flatpak.

In this commit, we still know that the XDG_RUNTIME_DIR is a
per-instance tmpfs, so we can safely create the symlinks using
the --symlink option. In a subsequent commit this will change to
creating them in a shared XDG_RUNTIME_DIR, if any.

Signed-off-by: Simon McVittie <[email protected]>
Like $XDG_RUNTIME_DIR/app/$FLATPAK_ID, this is shared between all
instances of the app, except for subsandboxed instances created by
flatpak-spawn --sandbox or equivalent. Unlike
$XDG_RUNTIME_DIR/app/$FLATPAK_ID, it does not exist at an equivalent
path on the host and in the sandboxed app.

Resolves: flatpak#4120
Signed-off-by: Simon McVittie <[email protected]>
@smcv

smcv commented Apr 15, 2021

Copy link
Copy Markdown
Collaborator Author

However, we could maybe go the other way, where everything is shared except some specific subdir of x-r-d (say /run/user/1000/instance) where we put a tmpfs (which can't be moved or renamed by the app). Then we move all the flatpak created stuff to that directory, and rejig the various env vars to point to those.

I've basically done this, except instead of using a subdirectory of the $XDG_RUNTIME_DIR, I used /run/flatpak (which in fact already existed, we put the ld.so.conf.d in there).

I need to try this against Steam, but I think it's ready for re-review.

@smcv

smcv commented Apr 15, 2021

Copy link
Copy Markdown
Collaborator Author

I need to try this against Steam, but I think it's ready for re-review.

Yes, seems to work.

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.

Multiple instances of the same app don't share XDG_RUNTIME_DIR

4 participants