57dc01000a5d2aaecdc511cc6d49068f47daa7e6
[bubblebox.git] / profiles.py
1 from bubblebox import *
2
3 # Various default sandbox settings
4 DEFAULT = group(
5   # namespace unsharing
6   # cannot unshare IPC as that breaks some wine applications
7   bwrap_flags("--unshare-user", "--unshare-pid", "--unshare-cgroup"),
8   # A different hostname is useful to be able to see when we are inside the sandbox.
9   # However, some applications will not like this unless the hostname also exists in `/etc/hosts`!
10   bwrap_flags("--unshare-uts", "--hostname", "bubblebox"),
11   # Make sure the sandbox cannot inject commands into the host terminal.
12   # TODO: This flag breaks some CLI applications, like job control in shells.
13   # Consider using SECCOMP instead.
14   # Possible code to use for that: <https://gist.github.com/sloonz/4b7f5f575a96b6fe338534dbc2480a5d#file-sandbox-py-L129>
15   # There is also a good list of possible-syscalls-to-block at
16   # <https://github.com/flatpak/flatpak/blob/f16e064fd9454fb8f754b769ad1ffce0e42b51db/common/flatpak-run.c#L1791>.
17   bwrap_flags("--new-session"),
18   # basic directories
19   bwrap_flags("--proc", "/proc", "--dev", "/dev", "--dir", "/tmp", "--dir", "/var", "--dir", "/run", "--symlink", "../run", "/var/run"),
20   # an empty XDG_RUNTIME_DIR
21   bwrap_flags("--perms", "0700", "--dir", XDG_RUNTIME_DIR),
22   # merged-usr symlinks
23   bwrap_flags("--symlink", "usr/lib", "/lib", "--symlink", "usr/lib64", "/lib64", "--symlink", "usr/bin", "/bin", "--symlink", "usr/sbin", "/sbin"),
24   # folders we always need access to
25   host_access({ ("/usr", "/sys", "/etc"): Access.Read }),
26   # make a basic shell work
27   home_access({
28     (".bashrc", ".bash_aliases", ".profile"): Access.Read,
29     "bin": Access.Read,
30   }),
31 )
32
33 def X11():
34   display = os.environ["DISPLAY"].removeprefix(":").split('.')[0]
35   return host_access({
36       "/tmp/.X11-unix/": {
37         "X"+display: Access.Read,
38       },
39       os.environ["XAUTHORITY"]: Access.Read,
40   })
41
42 # https://github.com/igo95862/bubblejail/blob/master/src/bubblejail/services.py is a good source of paths that need allowing.
43 # We do not give access to pipewire, that needs a portal (https://docs.pipewire.org/page_portal.html).
44 def DESKTOP(name):
45   return group(
46     DEFAULT,
47     # Share XDG_RUNTIME_DIR among all instances of this sandbox
48     shared_runtime_dir(name),
49     # Access to display servers, hardware acceleration, and audio
50     host_access({
51       "dev": {
52         ("dri", "snd"): Access.Device,
53       },
54       XDG_RUNTIME_DIR: {
55         (os.environ["WAYLAND_DISPLAY"], "pulse"): Access.Read,
56       },
57     }),
58     X11(),
59     # Access to some key user configuration.
60     # We set GSETTINGS_BACKEND to make GTK3 apps use the config file in ~/.config/glib-2.0.
61     # (The "right" solution here is probably the settings portal...)
62     home_access({
63       (".config/fontconfig", ".config/glib-2.0", ".XCompose", ".local/share/applications"): Access.Read,
64     }),
65     bwrap_flags("--setenv", "GSETTINGS_BACKEND", "keyfile"),
66     # Access to basic d-bus services (that are hopefully safe to expose...)
67     dbus_proxy_flags(
68       "--call=org.kde.StatusNotifierWatcher=@/StatusNotifierWatcher",
69       "--call=org.freedesktop.Notifications=@/org/freedesktop/Notifications",
70       "--call=org.freedesktop.ScreenSaver=@/org/freedesktop/ScreenSaver",
71       "--call=org.freedesktop.ScreenSaver=@/ScreenSaver",
72       "--talk=org.freedesktop.portal.*",
73     ),
74     # Make it possible to open websites in Firefox
75     home_access({ ".mozilla/firefox/profiles.ini": Access.Read }),
76     dbus_proxy_flags("--call=org.mozilla.firefox.*=@/org/mozilla/firefox/Remote"),
77   )