cross-post linking to SIGPLAN Rust post
[web.git] / ralf / _posts / 2019-03-09-firejail.md
1 ---
2 title: "Sandboxing All The Things with Firejail"
3 categories: sysadmin
4 ---
5
6 Sometimes, I run software that I trust less.
7 All software I use on a daily basis is open-source, but there is still closed-source software I run occasionally, like video games.
8 Unfortunately, the usual Unix security model does not protect against such software misbehaving, as illustrated by [this XKCD](https://www.xkcd.com/1200/).
9 There is no good reason that these applications should have access to all my personal information and secret keys that I have stored on my system, but without proper application isolation (like we are used to on mobile devices nowadays) nothing stops them from leaking all this data.
10 I didn't want to wait until such technology becomes the default on Linux desktops, so I did some research for ways to sandbox existing applications and ended up with [Firejail](https://github.com/netblue30/firejail).
11
12 <!-- MORE -->
13
14 Firejail is packaged for Debian and other distributions, so you should be able to install it with your distribution's package manager.
15 It runs an application in a sandbox, the details of which are configured in a *profile*.
16 Firejail comes with profiles for many applications.
17 For example, `firejail firefox` will start Firefox inside the sandbox.
18 This adds an extra layer of security around Firefox' security mechanisms.
19
20 However, I have found that my setup is sufficiently non-standard that I often have to customize the profiles.
21 The most complex piece of the profile is to configure which parts of the file system the application will be able to access.
22 Unfortunately, the configuration language can be quite hard to use---it is often difficult to find out why access to a particular file does not work.
23 Also, the whitelisting mechanism is fairly inflexible: you cannot control which directory gets whitelisted; instead, when you whitelist something in `/home/user/foo`, whitelisting gets turned on for all of `/home` (and similar for a bunch of other hard-coded "whitelisting roots").
24 But with some experimentation, you'll get things to work eventually.
25
26 For example, if there are extra directories that no sandboxed application should be able to access, you can add them in `/etc/firejail/disable-common.local`.
27 I am blacklisting all my personal files:
28 ```
29 # Personal files
30 blacklist ${HOME}/Documents
31 ```
32 This will affect all sandboxes, because `disable-common.inc` is included pretty much everywhere, and that includes `disable-common.local`.
33
34 ## Custom Profiles
35
36 Firejail cannot come with profiles for all applications, so sometimes you have to write your own.
37 For example, I use one profile for all video games as well as TeamSpeak, which is based on the profile for Steam and looks like this (I store it in `/etc/firejail/gaming.profile`):
38 ```
39 # Steam profile (applies to games/apps launched from Steam as well)
40 noblacklist ${HOME}/.steam
41 noblacklist ${HOME}/.steampath
42 noblacklist ${HOME}/.steampid
43 noblacklist ${HOME}/.local/share/steam
44 noblacklist ${HOME}/.wine
45 noblacklist ${HOME}/.ts3client
46 include /etc/firejail/disable-common.inc
47 include /etc/firejail/disable-programs.inc
48 include /etc/firejail/disable-passwdmgr.inc
49
50 caps.drop all
51 netfilter
52 nonewprivs
53 noroot
54 protocol unix,inet,inet6,netlink
55 # wine/games don't work properly with seccomp
56 #seccomp
57
58 # give access to some directories
59 read-write ${HOME}/.cache/winetricks
60 read-write ${HOME}/.ts3client
61 read-write ${HOME}/.steam
62 read-write ${HOME}/bin/teamspeak3
63 whitelist /mnt/store/r/games
64 # -> implicitly, the rest of /mnt is blocked
65 ```
66 Obviously, you will have to adjust the paths.
67 If one particular application needs something that got globally blacklisted, you can use `noblacklist`.
68 However, you might also have to `whitelist` it if something else sets up a whitelist for this directory.
69 Also, there seems to be no way to have something on the `blacklist` globally but then make it `read-only` for a particular sandbox only.
70 (This is what I mean by the configuration language being hard to use.)
71
72 Next, it is a good idea to test the profile.
73 You can start a shell inside the sandbox with:
74 ```
75 firejail --profile=/etc/firejail/gaming.profile bash
76 ```
77 Now you can do things like `ls ~/.ssh` to make sure your secret keys are not accessible in the sandbox.
78 Also test accessing the directories where the games are stored.
79 Use `exit` to leave the sandbox.
80
81 I have created a file `~/bin/gamejail` to conveniently start an application inside the sandbox:
82 ```
83 #!/bin/bash
84 exec firejail --profile=/etc/firejail/gaming.profile "$@"
85 ```
86 Now you just have to wrap the call to run the application inside `gamejail`, for example `gamejail steam`.
87
88 That's all I got.
89 I hope this little tutorial helps you to make your system a bit more secure by restricting application privileges.
90 Firejail is far from perfect, but it works quite well and it is extremely customizable.
91 In case you have trouble configuring the sandbox properly, I have found it very helpful to look at all the examples that are shipped in `/etc/firejail`.