Update application framework documentation
[AGL/documentation.git] / docs / 06_Component_Documentation / Application_Framework / 04_Application_Sandboxing.md
1 ---
2 title: Application Sandboxing
3 ---
4
5 One of the motivations for leveraging systemd in `applaunchd` was to allow the use of its
6 advanced features such as [sandboxing](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Sandboxing),
7 [system call filtering](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#System%20Call%20Filtering),
8 [process limits](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Process%20Properties), and
9 Linux CGroup configuration via [slices](https://www.freedesktop.org/software/systemd/man/systemd.slice.html).
10 The scope of potential systemd configurations involving these options is broad, and so far AGL has focused on providing
11 some simple examples of basic sandboxing integrated with the application templates discussed above.
12
13 ## Network Access Restriction
14
15 The systemd [`PrivateNetwork`](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateNetwork=)
16 service configuration option can be used to disable network access for the service started by a unit file.
17 The `applaunchd` packaging installs a systemd drop-in configuration file named `no-network.conf` in the
18 directory `/usr/lib/systemd/system/sandboxing` that may be used to disable network access with `PrivateNetwork`.
19 To use it, `no-network.conf` should be symlinked in an appropriate unit file drop-in override directory per
20 systemd naming expectations (see systemd unit file
21 [man page](https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Description)).
22 The following is a `BitBake` recipe snippet showing installation of the drop-in for an application named `foo`
23 that is using the generic application template:
24
25 ```text
26 ...
27
28 inherit agl-app
29
30 AGL_APP_NAME = "Foo"
31
32 do_install() {
33     # Enable systemd sandboxing override as a demonstration
34     install -d ${D}${systemd_system_unitdir}/agl-app@${AGL_APP_ID}.service.d/
35     ln -sf ${systemd_system_unitdir}/sandboxing/no-network.conf ${D}${systemd_system_unitdir}/agl-app@${AGL_APP_ID}.service.d/
36 }
37
38 FILES:${PN} = " \
39     ${sysconfdir}/systemd/system/agl-app@${AGL_APP_ID}.service.d \
40 "
41
42 ...
43 ```
44
45 This results in a `/usr/lib/systemd/system/agl-app@foo.service.d/no-network.conf` symlink being created
46 in the `foo` packaging, disabling network access when `foo` is started by `applaunchd` or directly via
47 `systemctl` on the command line.
48
49 ## Private Temporary Directories
50
51 The systemd [`PrivateTmp`](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateTmp=)
52 service configuration option can be used to prevent access to the host `/tmp` and `/var/tmp` directories
53 for the service started by a unit file.  The service will instead have private temporary mounts of these
54 directories in a new mount namespace.  The `applaunchd` packaging installs a systemd drop-in configuration
55 file named `private-tmp.conf` in the directory `/usr/lib/systemd/system/sandboxing` that may be used to
56 create private temporary directory mounts with `PrivateTmp`. To use it, `private-tmp.conf` should be
57 symlinked in an appropriate unit file drop-in override directory per systemd naming expectations (see
58 systemd unit file [man page](https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Description)).
59 See above for an example `BitBake` recipe snippet showing installation of a drop-in file when using the
60 generic application template.
61
62 ## Other Sandboxing Options
63
64 In addition to the above, some other notable systemd sandbox options worth further consideration for
65 applications include:
66
67 - [PrivateDevices](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateDevices=)  
68   This option could be used to remove access to device files in /dev for applications that do not
69   require them, but it is likely that `DeviceAllow` may also need to be used to enable functionality in
70   some applications.
71 - [ProtectSystem](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=)  
72   The default and `full` modes of this option likely can be enabled with little impact to most
73   applications, as they should not need write access to the directories that are made read-only.
74   Using the `strict` option would need investigation into interactions with usage of directory
75   hierarchies like `/run`.
76 - [ProtectHome](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHome=)  
77   This option seems worthwhile if the system ends up with more than one active user for running
78   applications, but interactions with default application caching and configuration locations
79   needs to be investigated.  It is likely that enforcing use of locations outside of `/home`
80   for these would need to be settled upon and documented for application developers.  For
81   example using the [XDG directory scheme](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
82   similar to the previous AGL application framework.
83 - [ReadWritePaths, etc.](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ReadWritePaths=)  
84    These options provide a more fine-grained approach to some of the above ones, and could be
85    used to enable something like an application only seeing its own files and the contents of
86    a specific set of directories like `/etc` and `/usr/lib`.  Implementing such would likely
87    require settling on a custom application installation hierarchy as was done in the previous
88    AGL application framework.
89
90 The above list is not exhaustive, and does not include some other likely possibilities involving
91 system call filtering and resource limits.  Future AGL releases may expand the examples provided
92 with `applaunchd` along these lines.