Skip to content

RFE: subsandboxes that can share /dev/shm, /tmp with main app, but not host #4091

Closed

Description

@smcv

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_DIR doesn't exist on systems that don't have systemd-logind, elogind or something similar
  • $HOME and $XDG_CACHE_HOME might be too long to put proper namespacing/uniqueness in an AF_UNIX socket name (which are limited to 108 bytes)
  • $HOME and $XDG_CACHE_HOME might be on NFS, SMB/CIFS or some other not-quite-POSIX filesystem that cannot accommodate AF_UNIX sockets or the standard trick of using an unlinked file as an anonymous memory buffer
  • $HOME and $XDG_CACHE_HOME might 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

  1. self-assigned this
    on Jan 22, 2021
  2. smcv commented on Jan 22, 2021

    @smcv
    CollaboratorAuthor

    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=ipc permission, so it already shares the IPC namespace with the host and therefore transitively shares it with the subsandboxes.

  3. smcv commented on Jan 22, 2021

    @smcv
    CollaboratorAuthor

    Unfortunately, we can't just look into the main app's filesystem via /proc/${pid}/root/tmp and /proc/${pid}/root/dev/shm, because root is a "magic symlink" with weird semantics. In some experiments with bwrap, even after I hack out the realpath() 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 with

    bwrap: Can't get type of source /proc/473968/root/dev/shm: Permission denied
    

    which 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 with EINVAL.

    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/shm and /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/shm and /tmp on 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-XXXXXX to be the main app's /dev/shm, and a temporary directory /tmp/flatpak-XXXXXX to 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).

  4. gasinvein commented on Jan 25, 2021

    @gasinvein
    Contributor

    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 $TMPDIR shared between instances, so such change could be useful not only for sub-sandboxes.

  5. smcv commented on Jan 25, 2021

    @smcv
    CollaboratorAuthor

    What if we create something like /tmp/user-$UID/flatpak-$FLATPAK_ID?

    Then an attacker with uid 1234 on a multi-user system creates /tmp/user-4321 and 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 /tmp and /dev/shm is to create an unpredictable path inside them.

  6. gasinvein commented on Jan 25, 2021

    @gasinvein
    Contributor

    Right. I was thinking of approach similar to /run/user/$UID, but forgot that /run/user is not world-writeable.

  7. smcv commented on Jan 25, 2021

    @smcv
    CollaboratorAuthor

    Also it worth noting that there are numerous flatpak apps that do need a $TMPDIR shared between instances

    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 set TMPDIR=/var/tmp in 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 /tmp and /dev/shm subdirectories 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-XXXXXX and $XDG_RUNTIME_DIR/flatpak/per-app-dev-shm/$FLATPAK_ID -> /dev/shm/flatpak-YYYYYY - but because g_get_user_runtime_dir() can return ~/.cache on 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.

  8. smcv commented on Jan 25, 2021

    @smcv
    CollaboratorAuthor

    I think many of the implementation questions on #4093 would still need to be answered if we're doing per-app-ID sharing.

  9. gasinvein commented on Jan 25, 2021

    @gasinvein
    Contributor

    These apps could just set TMPDIR=/var/tmp in their metadata?

    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 $TMPDIR env var. Thus, believe creating a sandboxed /tmp shared per app id would much better.

  10. Erick555 commented on Jan 26, 2021

    @Erick555
    Contributor

    Current flatpak recommendation is to set TMPDIR=$XDG_RUNTIME_DIR/app/$FLATPAK_ID which 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.

  11. smcv commented on Jan 26, 2021

    @smcv
    CollaboratorAuthor

    Current flatpak recommendation is to set TMPDIR=$XDG_RUNTIME_DIR/app/$FLATPAK_ID which is in memory

    ... except when it isn't (on non-systemd, non-elogind systems, it falls back to ~/.cache).

    Whatever will be decided it should be the default behaviour instead of option

    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.

  12. Erick555 commented on Jan 26, 2021

    @Erick555
    Contributor

    ... except when it isn't (on non-systemd, non-elogind systems, it falls back to ~/.cache).

    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 😄 .

  13. smcv commented on Jan 26, 2021

    @smcv
    CollaboratorAuthor

    I guess flatpak working reliably on such systems would be a miracle

    It works well enough that users of those systems complained that PulseAudio doesn't work (which I fixed: #4058).

  14. Erick555 commented on Jan 26, 2021

    @Erick555
    Contributor

    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.

  15. Erick555 commented on Jan 27, 2021

    @Erick555
    Contributor

    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 $TMPDIR env var. Thus, believe creating a sandboxed /tmp shared per app id would much better.

    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_ID to /tmp to handle apps that need shared /tmp but doesn't follow $TMPDIR variable. This will fix things you mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions