Skip to content

Franz Franz

goFranz // personal journal all writing
Tools · guix

Sandboxing Desktop Apps on Guix

41 apps in my panther channel now have a bubblewrap variant that stops them from going rogue.

When I wrote up what I added to Guix System, one gap stayed open: everything in that post protects the system from a process, and almost nothing protects my files from a process running as me. A PDF viewer can read ~/.ssh. A chat client can read the browser profile, the password store and every token in the environment. The AppArmor profile at the end of that post covers one app, by hand.

Guix has guix shell --container, which is good for a one-off, but nothing you install that stays confined when you start it from the app menu. So I started adding that to my panther channel. There are 41 sandboxed apps so far.

The approach

A package gets a second variant, <name>-sandboxed, that runs the same program inside bubblewrap. The variant holds only launchers and desktop entries pointing at the unmodified package.

What the app can reach is a policy. Here’s an example for a document viewer:

(define-public papers-sandboxed
  (sandboxed papers
             (sandbox-policy
              (read-only-paths '(".cache/fontconfig"))
              (file-arguments? #t)
              (wayland? #t)
              (x11? #t)
              (gpu? #t)
              (dbus-rules %portal-settings-dbus-rules))))

Every field defaults to off, so this viewer gets a display, the GPU, the theme setting, and the file I opened it with (read-only). No network, no home directory, no session bus beyond that one portal interface.

Everything else is the same for all 41:

  • An empty home on a tmpfs, plus the paths the policy binds. Firefox gets ~/.mozilla/firefox and ~/Downloads; it doesn’t get ~/Documents.
  • Its own PID, IPC, network and user namespaces. 13 of the 41 have no network at all (GIMP, LibreOffice, mpv, the viewers).
  • A fixed list of environment variables. Whatever else is in the session, tokens included, is dropped.
  • No session bus, unless the policy lists rules; then it’s a filtered one through xdg-dbus-proxy.
  • The store, read-only.

These are the same building blocks Flatpak uses. The difference is that there’s no second runtime to download, and the policy isn’t written by the app’s author: it’s about ten lines of Scheme in the channel, and you can change it in your own config:

(use-modules (px sandbox)
             (px packages graphics))

(sandboxed vitrine
           (sandbox-policy
            (read-only-paths '(".config/vitrine" "/mnt/photos"))
            (writable-paths '(".local/share/vitrine" ".cache/vitrine"))
            (wayland? #t)
            (gpu? #t)))

sandboxed takes any package, so this isn’t limited to the 41. (sandboxed curl) gives you a curl with no network and none of your files (not useful, but it shows the default).

How it’s going

The launcher is under 400 lines. Most of the work was finding out what desktop apps assume about their surroundings:

  • GTK apps write settings through dconf, which the sandbox can’t reach, and some crash when the write fails. GSettings now stays in memory.
  • A second launch of Firefox or Signal looks for the running instance’s socket. All instances of an app share one runtime directory for that; /tmp stays private to each.
  • Chromium names its tray icon after its PID, which is always 2 inside the sandbox. That made the D-Bus rule easy to write.
  • Some programs find $HOME through getpwuid, so the sandbox has a passwd file with exactly one entry.
  • The original package’s D-Bus and systemd units aren’t linked into the variant; they’d start the program unsandboxed.

Chromium, Firefox and the Electron apps were the ones I expected trouble with, because they build their own renderer sandbox from user namespaces. They work; the policy has a field that allows it.

Where it stops

  • The kernel has to allow unprivileged user namespaces. On Guix that’s the default, and the unprivileged guix-daemon needs it anyway.
  • The whole store is readable, not just the app’s closure, and there’s no seccomp filter. This limits what an app can read and reach; it’s not a defence against a kernel exploit.
  • On X11 the sandbox covers files and network, but not the display: any X11 client can read every other one’s keyboard and screen. Wayland doesn’t have this problem.
  • There’s no file-chooser portal yet, so “Open” only shows what the policy binds. For the viewers that means opening files from the file manager or the command line.
  • The variant and the original provide the same program names. Install one, not both.

The policies are also mine, tested on my machines (niri on Wayland, mostly). If an app needs a path I didn’t think of, it won’t see it.

To try it:

$ guix search sandboxed
$ guix install papers-sandboxed
$ ls -A /proc/$(pgrep -n bwrap)/root/home/$USER

The last command lists what the newest sandbox sees of your home. The full field reference is in the README.