Repository navigation
Description
Linux distribution and version
any
Flatpak version
1.10.1
Description of the problem
If an app creates a subsandbox (as Steam will want to do for #3797), the main app and each subsandbox get their own instance of /tmp (a separate tmpfs), unless the app has the --filesystem=/tmp permission, which would weaken the sandboxing.
Similarly, the main app and each subsandbox get their own instance of /dev/shm, unless the app has the --device=shm permission.
This is a problem because /tmp and /dev/shm are among the few locations that portable programs can use for IPC:
$XDG_RUNTIME_DIRdoesn't exist on systems that don't have systemd-logind, elogind or something similar$HOMEand$XDG_CACHE_HOMEmight be too long to put proper namespacing/uniqueness in anAF_UNIXsocket name (which are limited to 108 bytes)$HOMEand$XDG_CACHE_HOMEmight be on NFS, SMB/CIFS or some other not-quite-POSIX filesystem that cannot accommodateAF_UNIXsockets or the standard trick of using an unlinked file as an anonymous memory buffer$HOMEand$XDG_CACHE_HOMEmight be on NFS, SMB/CIFS or something else that is slow- as implemented in glibc, POSIX shared memory (
shm_open(3)) always uses/dev/shm/and cannot be redirected elsewhere
and it is reasonable for a subsandboxed program to want to communicate with the main app that initiated the sandbox.
For example, Steam creates a socket /tmp/steam_chrome_overlay_${something}, which game processes use to render HTML in the Steam Overlay. Steam also uses POSIX shm (shm_open(3)) or some other mechanism involving /dev/shm, although I don't know precisely what that's used for.
Prior to #3797, all Steam components and Steam games in a Flatpak'd Steam ran in the same bwrap instance, so they automatically and transparently all had the same /tmp and /dev/shm; but when we start putting games and possibly other components into subsandboxes, for them to continue to communicate, they should be able to share /tmp and /dev/shm.
Steps to reproduce
host$ flatpak run --command=bash --nofilesystem=/tmp --nodevice=shm ${any_app}
[📦 app ~]$ touch /tmp/THIS_IS_MY_PRIVATE_TMP
[📦 app ~]$ touch /dev/shm/THIS_IS_MY_PRIVATE_DEV_SHM
[📦 app ~]$ flatpak-spawn ls /tmp
(nothing listed)
[📦 app ~]$ flatpak-spawn ls /dev/shm
(nothing listed)
The desired result is that the subsandbox's /tmp is the same directory as the app's /tmp (demonstrated by it containing the flag-file THIS_IS_MY_PRIVATE_TMP), or that there is some option on the app and/or flatpak-spawn that can be used to make that happen; and the same for /dev/shm.
Workaround
If the app is given the --filesystem=/tmp permission, then it will share the host's /tmp, and the subsandboxes will also share the host's /tmp. By transitivity, this ends up with the app and the subsandboxes having the same /tmp. However, this is an undesirable exposure of host files, such as temporary downloaded files in /tmp/mozilla*, in the sandbox.
The same is true for /dev/shm and --device=shm.
Activity
smcv commentedon Jan 22, 2021
Vaguely related to this, there is currently no way to make a subsandbox share its IPC namespace (System V message queues, System V semaphores, System V shared memory, POSIX message queues) with the main app - analogous to #4060, but for ipcns rather than pidns. Both Flatpak and bubblewrap currently lack this feature.
However, this is not an immediate problem for Steam, because Steam already has the
--share=ipcpermission, so it already shares the IPC namespace with the host and therefore transitively shares it with the subsandboxes.smcv commentedon Jan 22, 2021
Unfortunately, we can't just look into the main app's filesystem via
/proc/${pid}/root/tmpand/proc/${pid}/root/dev/shm, becauserootis a "magic symlink" with weird semantics. In some experiments withbwrap, even after I hack out therealpath()canonicalization (which won't work correctly for magic symlinks), I still can't use/proc/${pid}/root/...as the source for a bind-mount operation: it fails withwhich I suspect is because that directory only exists in a parallel mount namespace for which the bwrap process does not have privileges.
Similarly, we can't fd-pass a file descriptor out to the portal and use
/proc/self/fd/...as the source for the bind-mount, because mount fails withEINVAL.I think this probably means the only way to make this work is going to be to allocate a pair of "real" directories on the host system, and mount them on the main app's
/dev/shmand/tmp, either unconditionally or for apps that opt-in; and then when we create a subsandbox, if appropriate flags are set, find the directories that represent the calling app's/dev/shmand/tmpon the host, and mount those into it instead of creating new directories.The most elegant way to do this would probably be to create a temporary directory
/dev/shm/flatpak-XXXXXXto be the main app's/dev/shm, and a temporary directory/tmp/flatpak-XXXXXXto be the main app's/tmp- that way, filesystem space limits would do the expected thing. However, to do this securely we have to give them securely-unique and unpredictable names, which makes them harder to manage.Or, we could create them in
g_get_user_runtime_dir(), like we do for instance info - if we do that, they could potentially have predictable names, but space allocation could be a problem.Either way we will need some reference-counting so that they are only deleted when both the main app and any subsandboxes have exited, because subsandboxes can legitimately persist beyond the lifetime of the main app if I understand correctly (although I don't think Steam actually needs that property).
run, portal: Allow sharing /dev/shm and /tmp with subsandboxes
run, portal: Allow sharing /dev/shm and /tmp with subsandboxes
gasinvein commentedon Jan 25, 2021
What if we create something like
/tmp/user-$UID/flatpak-$FLATPAK_ID? User ID and flatpak app ID combination should be unique for the same host, or I'm missing something?Also it worth noting that there are numerous flatpak apps that do need a
$TMPDIRshared between instances, so such change could be useful not only for sub-sandboxes.smcv commentedon Jan 25, 2021
Then an attacker with uid 1234 on a multi-user system creates
/tmp/user-4321and uses it to attack uid 4321. Depending how careful the checks inside Flatpak are, either uid 1234 gets partial control over uid 4321 (privilege escalation), or uid 4321 can't run Flatpak apps (denial of service). Either way you get a security vulnerability and a CVE ID.Sorry, the only way it's safe to interact with world-writeable directories like
/tmpand/dev/shmis to create an unpredictable path inside them.gasinvein commentedon Jan 25, 2021
Right. I was thinking of approach similar to
/run/user/$UID, but forgot that/run/useris not world-writeable.smcv commentedon Jan 25, 2021
A couple of ways to achieve this:
One possibility, orthogonal to this RFE, is to use
/var/tmp=~/.var/app/$FLATPAK_ID/cache/tmp, which is persistent on-disk and is shared between instances (and between reboots). These apps could just setTMPDIR=/var/tmpin their metadata?Another possibility is to expand the scope of this RFE to do something a lot like what I asked for, but instead of creating and destroying
/tmpand/dev/shmsubdirectories per instance (with each instance's subsandboxes sharing with their parent), create and destroy them per$FLATPAK_ID.The implementation on #4093 is already not a million miles away from how this would have to be implemented. For example, they could be tracked by creating symlinks
$XDG_RUNTIME_DIR/flatpak/per-app-tmp/$FLATPAK_ID -> /tmp/flatpak-XXXXXXand$XDG_RUNTIME_DIR/flatpak/per-app-dev-shm/$FLATPAK_ID -> /dev/shm/flatpak-YYYYYY- but becauseg_get_user_runtime_dir()can return~/.cacheon non-systemd systems, we might see stale symlinks after a reboot, so we'd have to be extra-careful to make sure that the temp directory that is pointed to still exists and is owned by our uid. I think we'd probably want to clean them up whenever an instance exits and no surviving instance is holding a lock, a lot like what I do in #4093.smcv commentedon Jan 25, 2021
I think many of the implementation questions on #4093 would still need to be answered if we're doing per-app-ID sharing.
gasinvein commentedon Jan 25, 2021
That's what they do in genereal, but there are two problems with this: 1. we substitute transient in-memory directory with a persistent on-disk one and 2. not all apps respect
$TMPDIRenv var. Thus, believe creating a sandboxed/tmpshared per app id would much better.Erick555 commentedon Jan 26, 2021
Current flatpak recommendation is to set
TMPDIR=$XDG_RUNTIME_DIR/app/$FLATPAK_IDwhich is in memory. Whatever will be decided it should be the default behaviour instead of option, all electron apps need to share /tmp and fixing this on flathub is never ending cat and mouse game.smcv commentedon Jan 26, 2021
... except when it isn't (on non-systemd, non-elogind systems, it falls back to
~/.cache).I'm going to prototype this as an option, but if Flatpak maintainers prefer it to be on by default, deleting conditionals is easy.
Making it a feature flag has the nice side-effect that it's automatically disabled for subsandboxes that were launched with
--sandbox.Erick555 commentedon Jan 26, 2021
I guess flatpak working reliably on such systems would be a miracle and having persistent or not persistent /tmp would be the least concern there 😄 .
Implement per-app shared /dev/shm and /tmp directories
smcv commentedon Jan 26, 2021
It works well enough that users of those systems complained that PulseAudio doesn't work (which I fixed: #4058).
Erick555 commentedon Jan 26, 2021
I would say that getting pulseaudio to work after just few years doesn't contradict my point 😄 . It's nice that someone pays attention and fixes things but I'm afraid this may be not the last fix needed.
Erick555 commentedon Jan 27, 2021
Actually I think we need both setting
TMPDIR=$XDG_RUNTIME_DIR/app/$FLATPAK_ID(to handle temp files sharing within different apps like ff does) and bind mount$XDG_RUNTIME_DIR/app/$FLATPAK_IDto /tmp to handle apps that need shared /tmp but doesn't follow $TMPDIR variable. This will fix things you mentioned.Implement per-app shared /dev/shm and /tmp directories
Implement per-app shared XDG_RUNTIME_DIR, /dev/shm and /tmp directories
Implement per-app shared XDG_RUNTIME_DIR, /dev/shm and /tmp directories
Implement per-app shared XDG_RUNTIME_DIR, /dev/shm and /tmp directories