Repository navigation
Create a shared XDG_RUNTIME_DIR for each app-ID - #4126
Conversation
|
Minor nitpick: is |
Not really. I don't think
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. |
|
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:
For now I've gone for the first option (don't ever GC private |
|
One thing that is a bit weird in this branch is that if you run two instances of an app with different permissions (via 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): |
(See also containers/bubblewrap#405) |
d245332 to
0caaa71
Compare
|
I'm not a maintainer but LGTM. |
|
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. |
|
Looks like this causes Steam to not find CA certificates any more. Looking into that. |
Resolved by setting up the |
|
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 |
Hmm, that's a good point. See also containers/bubblewrap#390.
That's an interesting idea, I'll look into how implementable it is. |
|
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. |
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. |
|
I'll try to take a look at the bubblewrap issues soon. |
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: |
fb54808 to
835713c
Compare
I confirm that this can genuinely happen. One thing we might be able to do about this:
|
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]>
I've basically done this, except instead of using a subdirectory of the I need to try this against Steam, but I think it's ready for re-review. |
Yes, seems to work. |
Fixes #4120.
Design
The
XDG_RUNTIME_DIRon the host now looks like this: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 forflatpak build(which reinvents a lot offlatpak_run_app()), and not forapply_extraeither.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, likeXauthority, we just use/run/flatpakas the new canonical path, and the file/directory doesn't exist in$XDG_RUNTIME_DIRat 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_DIRwith symlinks into/run/flatpakas 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