link to sloonz's script
[web.git] / personal / _posts / 2024-04-14-bubblebox.md
1 ---
2 title: "Sandboxing All The Things with Flatpak and BubbleBox"
3 categories: sysadmin
4 ---
5
6 A few years ago, I have [blogged]({% post_url 2019-03-09-firejail %}) about my approach to sandboxing less-trusted applications that I have to or want to run on my main machine.
7 The approach has changed since then, so it is time for an update.
8
9 <!-- MORE -->
10
11 Over time I grew increasingly frustrated with Firejail: configurations would frequently break on updates,
12 and debugging Firejail profiles is extremely hard. When considering all the included files, we are talking
13 about many hundred lines of configuration with a subtle interplay of allowlists and blocklists.
14 Even when I knew which folder I wanted to give access to, it was often non-trivial to ensure that
15 this access would actually be possible.
16
17 Now I am instead using a combination of two different approaches: Flatpak and BubbleBox.
18
19 ## Flatpak
20
21 The easiest sandbox to maintain is the sandbox maintained by someone else.
22 So when a Flatpak exists for software I want to or have to use, such as Signal or Zoom, that is generally my preferred approach.
23
24 Unfortunately, Flatpaks can come with extremely liberal default profiles that make the sandbox mostly pointless.
25 The following global overrides help ensure that this does not happen:
26 ```
27 [Context]
28 sockets=!gpg-agent;!pcsc;!ssh-auth;!system-bus;!session-bus
29 filesystems=~/.XCompose:ro;xdg-config/fontconfig:ro;!~/.gnupg;!~/.ssh;!xdg-documents;!home;!host
30
31 [Session Bus Policy]
32 org.freedesktop.Flatpak=none
33 org.freedesktop.secrets=none
34 ```
35
36 I also use [Flatseal], an amazing application that helps to check which permissions applications get, and change them if necessary.
37
38 [Flatseal]: https://flathub.org/apps/com.github.tchx84.Flatseal
39
40 ## BubbleBox
41
42 However, not all software exists as Flatpak.
43 Also, sometimes I want software to run basically on my host system (i.e., to use the regular `/usr`), just without access to literally *everything* in my home directory.
44 Examples of this are Factorio and VSCodium.
45 The latter doesn't work in Flatpak as I want to use it with LaTeX, and realistically this means it needs to run the LaTeX installed via `apt`.
46 The official recommendation is to effectively disable the Flatpak sandbox, but that entirely defeats the point, so I went looking for alternatives.
47
48 [bubblewrap] provides a very convenient solution: it can start an application in its own private filesystem namespace with full control over which part of the host file system is accessible from inside the sandbox.
49 I wrote a small wrapper around bubblewrap to make this configuration a bit more convenient to write and manage;
50 this project is called [BubbleBox].
51 This week-end I finally got around to adding support for [xdg-dbus-proxy] so that sandboxed applications can now access particular D-Bus functions without having access to the entire bus (which is in general not safe to expose to a sandboxed application).
52 That means it's finally time to blog about this project, so here we go -- if you are interested, check out [BubbleBox];
53 the project page explains how you can use it to set up your own sandboxing.[^1]
54
55 [^1]: One day I should probably rewrite this in Rust... maybe this will be my test project for when [cargo-script](https://rust-lang.github.io/rfcs/3424-cargo-script.html) becomes available.
56
57 I should also note that this is not the only bubblewrap-based sandboxing solution.
58 [bubblejail] is fairly similar but provides a configuration GUI and a good set of default provides;
59 it was a very useful resource when figuring out the right bubblewrap flags to make complex GUI applications work properly.
60 (Incidentally, "bubblejail" is also how I called my own script originally, but then I realized that the name is already taken.)
61 Joachim Breitner also recently [blogged](https://www.joachim-breitner.de/blog/812-Convenient_sandboxed_development_environment) about his own bubblewrap-based sandboxing script.
62 sloonz has a similar [script](https://gist.github.com/sloonz/4b7f5f575a96b6fe338534dbc2480a5d) as well, with a nice yaml-based configuration format and [great explanations](https://sloonz.github.io/posts/sandboxing-1/) for what all the flags exactly do.
63 Had their script existed when I started what eventually became BubbleBox, I would have used it as a starting point.
64 But it was also fun to figure out my own solution.
65
66 Using bubblewrap and xdg-dbus-proxy for this was an absolute joy.
67 Both of these components came out of the Flatpak project, but the authors realized that they could be independently useful,
68 so in best Unix tradition they turned them into tools that provide all the required mechanism without hard-coding any sort of policy.
69 Despite doing highly non-trivial tasks, they are both pretty easy to use and compose and very well-documented.
70 Thanks a lot to everyone involved!
71
72 [bubblewrap]: https://github.com/containers/bubblewrap
73 [BubbleBox]: {{ site.baseurl }}/projects/bubblebox
74 [xdg-dbus-proxy]: https://github.com/flatpak/xdg-dbus-proxy
75 [bubblejail]: https://github.com/igo95862/bubblejail