Skip to content

Make /dev/shm shareable between app instances - #4214

Merged
alexlarsson merged 1 commit into
flatpak:masterfrom
smcv:shareable-devshm
Apr 16, 2021
Merged

alexlarsson merged 1 commit into
flatpak:masterfrom
smcv:shareable-devshm

Conversation

@smcv

@smcv smcv commented Apr 8, 2021 •

Copy link
Copy Markdown
Collaborator

Fixes #4091.

Design

The interesting subset of the host filesystem is now:

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, new in #4093)
            \- .ref (locked non-exclusively by app, new in #4093)
            \- tmp (gets mounted as /tmp in app sandbox, new in #4093)
            \- dev-shm (symlink to /dev/shm/flatpak-com.example.App-abcdef, new)

real host /dev/shm
    \- flatpak-com.example.App-abcdef (or similar unpredictable name, new)
        \- .flatpak-tmpdir

As with #4093, the shared /dev/shm 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 dev-shm if no apps are still using it. The shared /dev/shm is currently backed by a directory /dev/shm/flatpak-com.example.App-XXXXXX, so that it's basically guaranteed to be genuinely a tmpfs.

The format of the temporary directory /dev/shm/flatpak-com.example.App-XXXXXX is checked, to defend against being tricked into deleting a directory that we didn't mean to. The app name is included in the directory name, so that if an app uses an excessive amount of space, users can blame the app and not Flatpak itself.

The flag file .flatpak-tmpdir is also checked for, again as a safety-catch against deleting unintended directories. This is loosely based on the .testtmp used in the libostree and Flatpak unit tests.

Having a shared /dev/shm is guarded by a feature flag as @alexlarsson requested: we don't expect so many apps to need this.

Commits

  • run: Add option to share /dev/shm between instances of an app-ID

    Similar to /tmp, applications might well use /dev/shm as an IPC
    rendezvous between instances, which wouldn't have worked without
    --device=shm until now.

    Because /dev/shm has specific characteristics (in particular it's
    meant to always be a tmpfs), we offload the actual storage into a
    subdirectory of the real /dev/shm. Because /dev/shm is a shared
    directory between all uids, we have to be extra-careful how we
    do this, which is why the test coverage here is important.

    This is done on an opt-in basis because of its extra complexity.

@smcv smcv added needs work sandbox issue related to sandbox setup labels Apr 8, 2021
@smcv
smcv force-pushed the shareable-devshm branch from e84a966 to e1e9b33 Compare April 8, 2021 13:41
Comment thread common/flatpak-instance.c
* or %FALSE with @error set if it does not.
*/
gboolean
flatpak_instance_claim_per_app_temp_directory (const char *app_id,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with this function's earlier existence in #4093, it's security-sensitive, so please review carefully.

I added test coverage for all the branches except fstat() failing (which I don't know how to trigger on-demand). To get full test coverage for it, you need to run tests as non-root, and pre-create /tmp/flatpak-com.example.App-OwnedByRoot owned by root (which I'm going to try to add to the CI).

Comment thread common/flatpak-instance.c Resolved
Comment thread common/flatpak-run.c Resolved
Comment thread common/flatpak-run.c Resolved
Comment thread common/flatpak-run.c Resolved
Similar to /tmp, applications might well use /dev/shm as an IPC
rendezvous between instances, which wouldn't have worked without
--device=shm until now.

Because /dev/shm has specific characteristics (in particular it's
meant to always be a tmpfs), we offload the actual storage into a
subdirectory of the real /dev/shm. Because /dev/shm is a shared
directory between all uids, we have to be extra-careful how we
do this, which is why the test coverage here is important.

This is done on an opt-in basis because of its extra complexity.

Signed-off-by: Simon McVittie <[email protected]>
@smcv
smcv force-pushed the shareable-devshm branch from 9998c4d to 829f756 Compare April 15, 2021 17:15
@alexlarsson
alexlarsson merged commit cb47d83 into flatpak:master Apr 16, 2021
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.

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

2 participants