15526b450SMickaël Salaün.. SPDX-License-Identifier: GPL-2.0 25526b450SMickaël Salaün.. Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> 35526b450SMickaël Salaün.. Copyright © 2019-2020 ANSSI 45526b450SMickaël Salaün 55526b450SMickaël Salaün================================== 65526b450SMickaël SalaünLandlock LSM: kernel documentation 75526b450SMickaël Salaün================================== 85526b450SMickaël Salaün 95526b450SMickaël Salaün:Author: Mickaël Salaün 10*3e52e5b0SMickaël Salaün:Date: December 2022 115526b450SMickaël Salaün 125526b450SMickaël SalaünLandlock's goal is to create scoped access-control (i.e. sandboxing). To 135526b450SMickaël Salaünharden a whole system, this feature should be available to any process, 145526b450SMickaël Salaünincluding unprivileged ones. Because such process may be compromised or 155526b450SMickaël Salaünbackdoored (i.e. untrusted), Landlock's features must be safe to use from the 165526b450SMickaël Salaünkernel and other processes point of view. Landlock's interface must therefore 175526b450SMickaël Salaünexpose a minimal attack surface. 185526b450SMickaël Salaün 195526b450SMickaël SalaünLandlock is designed to be usable by unprivileged processes while following the 205526b450SMickaël Salaünsystem security policy enforced by other access control mechanisms (e.g. DAC, 215526b450SMickaël SalaünLSM). Indeed, a Landlock rule shall not interfere with other access-controls 225526b450SMickaël Salaünenforced on the system, only add more restrictions. 235526b450SMickaël Salaün 245526b450SMickaël SalaünAny user can enforce Landlock rulesets on their processes. They are merged and 255526b450SMickaël Salaünevaluated according to the inherited ones in a way that ensures that only more 265526b450SMickaël Salaünconstraints can be added. 275526b450SMickaël Salaün 28d3122273SMauro Carvalho ChehabUser space documentation can be found here: 29d3122273SMauro Carvalho ChehabDocumentation/userspace-api/landlock.rst. 305526b450SMickaël Salaün 315526b450SMickaël SalaünGuiding principles for safe access controls 325526b450SMickaël Salaün=========================================== 335526b450SMickaël Salaün 345526b450SMickaël Salaün* A Landlock rule shall be focused on access control on kernel objects instead 355526b450SMickaël Salaün of syscall filtering (i.e. syscall arguments), which is the purpose of 365526b450SMickaël Salaün seccomp-bpf. 375526b450SMickaël Salaün* To avoid multiple kinds of side-channel attacks (e.g. leak of security 385526b450SMickaël Salaün policies, CPU-based attacks), Landlock rules shall not be able to 395526b450SMickaël Salaün programmatically communicate with user space. 405526b450SMickaël Salaün* Kernel access check shall not slow down access request from unsandboxed 415526b450SMickaël Salaün processes. 425526b450SMickaël Salaün* Computation related to Landlock operations (e.g. enforcing a ruleset) shall 435526b450SMickaël Salaün only impact the processes requesting them. 44*3e52e5b0SMickaël Salaün* Resources (e.g. file descriptors) directly obtained from the kernel by a 45*3e52e5b0SMickaël Salaün sandboxed process shall retain their scoped accesses (at the time of resource 46*3e52e5b0SMickaël Salaün acquisition) whatever process use them. 47*3e52e5b0SMickaël Salaün Cf. `File descriptor access rights`_. 485526b450SMickaël Salaün 499e0c76b9SMickaël SalaünDesign choices 509e0c76b9SMickaël Salaün============== 519e0c76b9SMickaël Salaün 52*3e52e5b0SMickaël SalaünInode access rights 53*3e52e5b0SMickaël Salaün------------------- 549e0c76b9SMickaël Salaün 559e0c76b9SMickaël SalaünAll access rights are tied to an inode and what can be accessed through it. 5616023b05SMickaël SalaünReading the content of a directory does not imply to be allowed to read the 579e0c76b9SMickaël Salaüncontent of a listed inode. Indeed, a file name is local to its parent 589e0c76b9SMickaël Salaündirectory, and an inode can be referenced by multiple file names thanks to 599e0c76b9SMickaël Salaün(hard) links. Being able to unlink a file only has a direct impact on the 609e0c76b9SMickaël Salaündirectory, not the unlinked inode. This is the reason why 612fff00c8SMickaël Salaün``LANDLOCK_ACCESS_FS_REMOVE_FILE`` or ``LANDLOCK_ACCESS_FS_REFER`` are not 622fff00c8SMickaël Salaünallowed to be tied to files but only to directories. 639e0c76b9SMickaël Salaün 64*3e52e5b0SMickaël SalaünFile descriptor access rights 65*3e52e5b0SMickaël Salaün----------------------------- 66*3e52e5b0SMickaël Salaün 67*3e52e5b0SMickaël SalaünAccess rights are checked and tied to file descriptors at open time. The 68*3e52e5b0SMickaël Salaünunderlying principle is that equivalent sequences of operations should lead to 69*3e52e5b0SMickaël Salaünthe same results, when they are executed under the same Landlock domain. 70*3e52e5b0SMickaël Salaün 71*3e52e5b0SMickaël SalaünTaking the ``LANDLOCK_ACCESS_FS_TRUNCATE`` right as an example, it may be 72*3e52e5b0SMickaël Salaünallowed to open a file for writing without being allowed to 73*3e52e5b0SMickaël Salaün:manpage:`ftruncate` the resulting file descriptor if the related file 74*3e52e5b0SMickaël Salaünhierarchy doesn't grant such access right. The following sequences of 75*3e52e5b0SMickaël Salaünoperations have the same semantic and should then have the same result: 76*3e52e5b0SMickaël Salaün 77*3e52e5b0SMickaël Salaün* ``truncate(path);`` 78*3e52e5b0SMickaël Salaün* ``int fd = open(path, O_WRONLY); ftruncate(fd); close(fd);`` 79*3e52e5b0SMickaël Salaün 80*3e52e5b0SMickaël SalaünSimilarly to file access modes (e.g. ``O_RDWR``), Landlock access rights 81*3e52e5b0SMickaël Salaünattached to file descriptors are retained even if they are passed between 82*3e52e5b0SMickaël Salaünprocesses (e.g. through a Unix domain socket). Such access rights will then be 83*3e52e5b0SMickaël Salaünenforced even if the receiving process is not sandboxed by Landlock. Indeed, 84*3e52e5b0SMickaël Salaünthis is required to keep a consistent access control over the whole system, and 85*3e52e5b0SMickaël Salaünthis avoids unattended bypasses through file descriptor passing (i.e. confused 86*3e52e5b0SMickaël Salaündeputy attack). 87*3e52e5b0SMickaël Salaün 885526b450SMickaël SalaünTests 895526b450SMickaël Salaün===== 905526b450SMickaël Salaün 915526b450SMickaël SalaünUserspace tests for backward compatibility, ptrace restrictions and filesystem 925526b450SMickaël Salaünsupport can be found here: `tools/testing/selftests/landlock/`_. 935526b450SMickaël Salaün 945526b450SMickaël SalaünKernel structures 955526b450SMickaël Salaün================= 965526b450SMickaël Salaün 975526b450SMickaël SalaünObject 985526b450SMickaël Salaün------ 995526b450SMickaël Salaün 1005526b450SMickaël Salaün.. kernel-doc:: security/landlock/object.h 1015526b450SMickaël Salaün :identifiers: 1025526b450SMickaël Salaün 1035526b450SMickaël SalaünFilesystem 1045526b450SMickaël Salaün---------- 1055526b450SMickaël Salaün 1065526b450SMickaël Salaün.. kernel-doc:: security/landlock/fs.h 1075526b450SMickaël Salaün :identifiers: 1085526b450SMickaël Salaün 1095526b450SMickaël SalaünRuleset and domain 1105526b450SMickaël Salaün------------------ 1115526b450SMickaël Salaün 1125526b450SMickaël SalaünA domain is a read-only ruleset tied to a set of subjects (i.e. tasks' 1135526b450SMickaël Salaüncredentials). Each time a ruleset is enforced on a task, the current domain is 1145526b450SMickaël Salaünduplicated and the ruleset is imported as a new layer of rules in the new 1155526b450SMickaël Salaündomain. Indeed, once in a domain, each rule is tied to a layer level. To 1165526b450SMickaël Salaüngrant access to an object, at least one rule of each layer must allow the 1175526b450SMickaël Salaünrequested action on the object. A task can then only transit to a new domain 1185526b450SMickaël Salaünthat is the intersection of the constraints from the current domain and those 1195526b450SMickaël Salaünof a ruleset provided by the task. 1205526b450SMickaël Salaün 1215526b450SMickaël SalaünThe definition of a subject is implicit for a task sandboxing itself, which 1225526b450SMickaël Salaünmakes the reasoning much easier and helps avoid pitfalls. 1235526b450SMickaël Salaün 1245526b450SMickaël Salaün.. kernel-doc:: security/landlock/ruleset.h 1255526b450SMickaël Salaün :identifiers: 1265526b450SMickaël Salaün 1275526b450SMickaël Salaün.. Links 1285526b450SMickaël Salaün.. _tools/testing/selftests/landlock/: 1295526b450SMickaël Salaün https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/landlock/ 130