xref: /linux/Documentation/userspace-api/landlock.rst (revision f490e205bcbada6eb6dca8b75a2511685e6bd0f0)
1.. SPDX-License-Identifier: GPL-2.0
2.. Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
3.. Copyright © 2019-2020 ANSSI
4.. Copyright © 2021-2022 Microsoft Corporation
5
6=====================================
7Landlock: unprivileged access control
8=====================================
9
10:Author: Mickaël Salaün
11:Date: September 2024
12
13The goal of Landlock is to enable to restrict ambient rights (e.g. global
14filesystem or network access) for a set of processes.  Because Landlock
15is a stackable LSM, it makes possible to create safe security sandboxes as new
16security layers in addition to the existing system-wide access-controls. This
17kind of sandbox is expected to help mitigate the security impact of bugs or
18unexpected/malicious behaviors in user space applications.  Landlock empowers
19any process, including unprivileged ones, to securely restrict themselves.
20
21We can quickly make sure that Landlock is enabled in the running system by
22looking for "landlock: Up and running" in kernel logs (as root):
23``dmesg | grep landlock || journalctl -kb -g landlock`` .
24Developers can also easily check for Landlock support with a
25:ref:`related system call <landlock_abi_versions>`.
26If Landlock is not currently supported, we need to
27:ref:`configure the kernel appropriately <kernel_support>`.
28
29Landlock rules
30==============
31
32A Landlock rule describes an action on an object which the process intends to
33perform.  A set of rules is aggregated in a ruleset, which can then restrict
34the thread enforcing it, and its future children.
35
36The two existing types of rules are:
37
38Filesystem rules
39    For these rules, the object is a file hierarchy,
40    and the related filesystem actions are defined with
41    `filesystem access rights`.
42
43Network rules (since ABI v4)
44    For these rules, the object is a TCP port,
45    and the related actions are defined with `network access rights`.
46
47Defining and enforcing a security policy
48----------------------------------------
49
50We first need to define the ruleset that will contain our rules.
51
52For this example, the ruleset will contain rules that only allow filesystem
53read actions and establish a specific TCP connection. Filesystem write
54actions and other TCP actions will be denied.
55
56The ruleset then needs to handle both these kinds of actions.  This is
57required for backward and forward compatibility (i.e. the kernel and user
58space may not know each other's supported restrictions), hence the need
59to be explicit about the denied-by-default access rights.
60
61.. code-block:: c
62
63    struct landlock_ruleset_attr ruleset_attr = {
64        .handled_access_fs =
65            LANDLOCK_ACCESS_FS_EXECUTE |
66            LANDLOCK_ACCESS_FS_WRITE_FILE |
67            LANDLOCK_ACCESS_FS_READ_FILE |
68            LANDLOCK_ACCESS_FS_READ_DIR |
69            LANDLOCK_ACCESS_FS_REMOVE_DIR |
70            LANDLOCK_ACCESS_FS_REMOVE_FILE |
71            LANDLOCK_ACCESS_FS_MAKE_CHAR |
72            LANDLOCK_ACCESS_FS_MAKE_DIR |
73            LANDLOCK_ACCESS_FS_MAKE_REG |
74            LANDLOCK_ACCESS_FS_MAKE_SOCK |
75            LANDLOCK_ACCESS_FS_MAKE_FIFO |
76            LANDLOCK_ACCESS_FS_MAKE_BLOCK |
77            LANDLOCK_ACCESS_FS_MAKE_SYM |
78            LANDLOCK_ACCESS_FS_REFER |
79            LANDLOCK_ACCESS_FS_TRUNCATE |
80            LANDLOCK_ACCESS_FS_IOCTL_DEV,
81        .handled_access_net =
82            LANDLOCK_ACCESS_NET_BIND_TCP |
83            LANDLOCK_ACCESS_NET_CONNECT_TCP,
84        .scoped =
85            LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
86    };
87
88Because we may not know on which kernel version an application will be
89executed, it is safer to follow a best-effort security approach.  Indeed, we
90should try to protect users as much as possible whatever the kernel they are
91using.
92
93To be compatible with older Linux versions, we detect the available Landlock ABI
94version, and only use the available subset of access rights:
95
96.. code-block:: c
97
98    int abi;
99
100    abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
101    if (abi < 0) {
102        /* Degrades gracefully if Landlock is not handled. */
103        perror("The running kernel does not enable to use Landlock");
104        return 0;
105    }
106    switch (abi) {
107    case 1:
108        /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */
109        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
110        __attribute__((fallthrough));
111    case 2:
112        /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
113        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
114        __attribute__((fallthrough));
115    case 3:
116        /* Removes network support for ABI < 4 */
117        ruleset_attr.handled_access_net &=
118            ~(LANDLOCK_ACCESS_NET_BIND_TCP |
119              LANDLOCK_ACCESS_NET_CONNECT_TCP);
120        __attribute__((fallthrough));
121    case 4:
122        /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
123        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
124        __attribute__((fallthrough));
125    case 5:
126        /* Removes LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET for ABI < 6 */
127        ruleset_attr.scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
128    }
129
130This enables to create an inclusive ruleset that will contain our rules.
131
132.. code-block:: c
133
134    int ruleset_fd;
135
136    ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
137    if (ruleset_fd < 0) {
138        perror("Failed to create a ruleset");
139        return 1;
140    }
141
142We can now add a new rule to this ruleset thanks to the returned file
143descriptor referring to this ruleset.  The rule will only allow reading the
144file hierarchy ``/usr``.  Without another rule, write actions would then be
145denied by the ruleset.  To add ``/usr`` to the ruleset, we open it with the
146``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with this file
147descriptor.
148
149.. code-block:: c
150
151    int err;
152    struct landlock_path_beneath_attr path_beneath = {
153        .allowed_access =
154            LANDLOCK_ACCESS_FS_EXECUTE |
155            LANDLOCK_ACCESS_FS_READ_FILE |
156            LANDLOCK_ACCESS_FS_READ_DIR,
157    };
158
159    path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
160    if (path_beneath.parent_fd < 0) {
161        perror("Failed to open file");
162        close(ruleset_fd);
163        return 1;
164    }
165    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
166                            &path_beneath, 0);
167    close(path_beneath.parent_fd);
168    if (err) {
169        perror("Failed to update ruleset");
170        close(ruleset_fd);
171        return 1;
172    }
173
174It may also be required to create rules following the same logic as explained
175for the ruleset creation, by filtering access rights according to the Landlock
176ABI version.  In this example, this is not required because all of the requested
177``allowed_access`` rights are already available in ABI 1.
178
179For network access-control, we can add a set of rules that allow to use a port
180number for a specific action: HTTPS connections.
181
182.. code-block:: c
183
184    struct landlock_net_port_attr net_port = {
185        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
186        .port = 443,
187    };
188
189    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
190                            &net_port, 0);
191
192The next step is to restrict the current thread from gaining more privileges
193(e.g. through a SUID binary).  We now have a ruleset with the first rule
194allowing read access to ``/usr`` while denying all other handled accesses for
195the filesystem, and a second rule allowing HTTPS connections.
196
197.. code-block:: c
198
199    if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
200        perror("Failed to restrict privileges");
201        close(ruleset_fd);
202        return 1;
203    }
204
205The current thread is now ready to sandbox itself with the ruleset.
206
207.. code-block:: c
208
209    if (landlock_restrict_self(ruleset_fd, 0)) {
210        perror("Failed to enforce ruleset");
211        close(ruleset_fd);
212        return 1;
213    }
214    close(ruleset_fd);
215
216If the ``landlock_restrict_self`` system call succeeds, the current thread is
217now restricted and this policy will be enforced on all its subsequently created
218children as well.  Once a thread is landlocked, there is no way to remove its
219security policy; only adding more restrictions is allowed.  These threads are
220now in a new Landlock domain, merge of their parent one (if any) with the new
221ruleset.
222
223Full working code can be found in `samples/landlock/sandboxer.c`_.
224
225Good practices
226--------------
227
228It is recommended setting access rights to file hierarchy leaves as much as
229possible.  For instance, it is better to be able to have ``~/doc/`` as a
230read-only hierarchy and ``~/tmp/`` as a read-write hierarchy, compared to
231``~/`` as a read-only hierarchy and ``~/tmp/`` as a read-write hierarchy.
232Following this good practice leads to self-sufficient hierarchies that do not
233depend on their location (i.e. parent directories).  This is particularly
234relevant when we want to allow linking or renaming.  Indeed, having consistent
235access rights per directory enables to change the location of such directory
236without relying on the destination directory access rights (except those that
237are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
238documentation).
239
240Having self-sufficient hierarchies also helps to tighten the required access
241rights to the minimal set of data.  This also helps avoid sinkhole directories,
242i.e.  directories where data can be linked to but not linked from.  However,
243this depends on data organization, which might not be controlled by developers.
244In this case, granting read-write access to ``~/tmp/``, instead of write-only
245access, would potentially allow to move ``~/tmp/`` to a non-readable directory
246and still keep the ability to list the content of ``~/tmp/``.
247
248Layers of file path access rights
249---------------------------------
250
251Each time a thread enforces a ruleset on itself, it updates its Landlock domain
252with a new layer of policy.  Indeed, this complementary policy is stacked with
253the potentially other rulesets already restricting this thread.  A sandboxed
254thread can then safely add more constraints to itself with a new enforced
255ruleset.
256
257One policy layer grants access to a file path if at least one of its rules
258encountered on the path grants the access.  A sandboxed thread can only access
259a file path if all its enforced policy layers grant the access as well as all
260the other system access controls (e.g. filesystem DAC, other LSM policies,
261etc.).
262
263Bind mounts and OverlayFS
264-------------------------
265
266Landlock enables to restrict access to file hierarchies, which means that these
267access rights can be propagated with bind mounts (cf.
268Documentation/filesystems/sharedsubtree.rst) but not with
269Documentation/filesystems/overlayfs.rst.
270
271A bind mount mirrors a source file hierarchy to a destination.  The destination
272hierarchy is then composed of the exact same files, on which Landlock rules can
273be tied, either via the source or the destination path.  These rules restrict
274access when they are encountered on a path, which means that they can restrict
275access to multiple file hierarchies at the same time, whether these hierarchies
276are the result of bind mounts or not.
277
278An OverlayFS mount point consists of upper and lower layers.  These layers are
279combined in a merge directory, result of the mount point.  This merge hierarchy
280may include files from the upper and lower layers, but modifications performed
281on the merge hierarchy only reflects on the upper layer.  From a Landlock
282policy point of view, each OverlayFS layers and merge hierarchies are
283standalone and contains their own set of files and directories, which is
284different from bind mounts.  A policy restricting an OverlayFS layer will not
285restrict the resulted merged hierarchy, and vice versa.  Landlock users should
286then only think about file hierarchies they want to allow access to, regardless
287of the underlying filesystem.
288
289Inheritance
290-----------
291
292Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain
293restrictions from its parent.  This is similar to the seccomp inheritance (cf.
294Documentation/userspace-api/seccomp_filter.rst) or any other LSM dealing with
295task's :manpage:`credentials(7)`.  For instance, one process's thread may apply
296Landlock rules to itself, but they will not be automatically applied to other
297sibling threads (unlike POSIX thread credential changes, cf.
298:manpage:`nptl(7)`).
299
300When a thread sandboxes itself, we have the guarantee that the related security
301policy will stay enforced on all this thread's descendants.  This allows
302creating standalone and modular security policies per application, which will
303automatically be composed between themselves according to their runtime parent
304policies.
305
306Ptrace restrictions
307-------------------
308
309A sandboxed process has less privileges than a non-sandboxed process and must
310then be subject to additional restrictions when manipulating another process.
311To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
312process, a sandboxed process should have a subset of the target process rules,
313which means the tracee must be in a sub-domain of the tracer.
314
315IPC scoping
316-----------
317
318Similar to the implicit `Ptrace restrictions`_, we may want to further restrict
319interactions between sandboxes. Each Landlock domain can be explicitly scoped
320for a set of actions by specifying it on a ruleset.  For example, if a
321sandboxed process should not be able to :manpage:`connect(2)` to a
322non-sandboxed process through abstract :manpage:`unix(7)` sockets, we can
323specify such restriction with ``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET``.
324
325A sandboxed process can connect to a non-sandboxed process when its domain is
326not scoped. If a process's domain is scoped, it can only connect to sockets
327created by processes in the same scope.
328
329A connected datagram socket behaves like a stream socket when its domain is
330scoped, meaning if the domain is scoped after the socket is connected , it can
331still :manpage:`send(2)` data just like a stream socket.  However, in the same
332scenario, a non-connected datagram socket cannot send data (with
333:manpage:`sendto(2)`) outside its scope.
334
335A process with a scoped domain can inherit a socket created by a non-scoped
336process. The process cannot connect to this socket since it has a scoped
337domain.
338
339IPC scoping does not support exceptions, so if a domain is scoped, no rules can
340be added to allow access to resources or processes outside of the scope.
341
342Truncating files
343----------------
344
345The operations covered by ``LANDLOCK_ACCESS_FS_WRITE_FILE`` and
346``LANDLOCK_ACCESS_FS_TRUNCATE`` both change the contents of a file and sometimes
347overlap in non-intuitive ways.  It is recommended to always specify both of
348these together.
349
350A particularly surprising example is :manpage:`creat(2)`.  The name suggests
351that this system call requires the rights to create and write files.  However,
352it also requires the truncate right if an existing file under the same name is
353already present.
354
355It should also be noted that truncating files does not require the
356``LANDLOCK_ACCESS_FS_WRITE_FILE`` right.  Apart from the :manpage:`truncate(2)`
357system call, this can also be done through :manpage:`open(2)` with the flags
358``O_RDONLY | O_TRUNC``.
359
360The truncate right is associated with the opened file (see below).
361
362Rights associated with file descriptors
363---------------------------------------
364
365When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE`` and
366``LANDLOCK_ACCESS_FS_IOCTL_DEV`` rights is associated with the newly created
367file descriptor and will be used for subsequent truncation and ioctl attempts
368using :manpage:`ftruncate(2)` and :manpage:`ioctl(2)`.  The behavior is similar
369to opening a file for reading or writing, where permissions are checked during
370:manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
371:manpage:`write(2)` calls.
372
373As a consequence, it is possible that a process has multiple open file
374descriptors referring to the same file, but Landlock enforces different things
375when operating with these file descriptors.  This can happen when a Landlock
376ruleset gets enforced and the process keeps file descriptors which were opened
377both before and after the enforcement.  It is also possible to pass such file
378descriptors between processes, keeping their Landlock properties, even when some
379of the involved processes do not have an enforced Landlock ruleset.
380
381Compatibility
382=============
383
384Backward and forward compatibility
385----------------------------------
386
387Landlock is designed to be compatible with past and future versions of the
388kernel.  This is achieved thanks to the system call attributes and the
389associated bitflags, particularly the ruleset's ``handled_access_fs``.  Making
390handled access right explicit enables the kernel and user space to have a clear
391contract with each other.  This is required to make sure sandboxing will not
392get stricter with a system update, which could break applications.
393
394Developers can subscribe to the `Landlock mailing list
395<https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
396test their applications with the latest available features.  In the interest of
397users, and because they may use different kernel versions, it is strongly
398encouraged to follow a best-effort security approach by checking the Landlock
399ABI version at runtime and only enforcing the supported features.
400
401.. _landlock_abi_versions:
402
403Landlock ABI versions
404---------------------
405
406The Landlock ABI version can be read with the sys_landlock_create_ruleset()
407system call:
408
409.. code-block:: c
410
411    int abi;
412
413    abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
414    if (abi < 0) {
415        switch (errno) {
416        case ENOSYS:
417            printf("Landlock is not supported by the current kernel.\n");
418            break;
419        case EOPNOTSUPP:
420            printf("Landlock is currently disabled.\n");
421            break;
422        }
423        return 0;
424    }
425    if (abi >= 2) {
426        printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
427    }
428
429The following kernel interfaces are implicitly supported by the first ABI
430version.  Features only supported from a specific version are explicitly marked
431as such.
432
433Kernel interface
434================
435
436Access rights
437-------------
438
439.. kernel-doc:: include/uapi/linux/landlock.h
440    :identifiers: fs_access net_access scope
441
442Creating a new ruleset
443----------------------
444
445.. kernel-doc:: security/landlock/syscalls.c
446    :identifiers: sys_landlock_create_ruleset
447
448.. kernel-doc:: include/uapi/linux/landlock.h
449    :identifiers: landlock_ruleset_attr
450
451Extending a ruleset
452-------------------
453
454.. kernel-doc:: security/landlock/syscalls.c
455    :identifiers: sys_landlock_add_rule
456
457.. kernel-doc:: include/uapi/linux/landlock.h
458    :identifiers: landlock_rule_type landlock_path_beneath_attr
459                  landlock_net_port_attr
460
461Enforcing a ruleset
462-------------------
463
464.. kernel-doc:: security/landlock/syscalls.c
465    :identifiers: sys_landlock_restrict_self
466
467Current limitations
468===================
469
470Filesystem topology modification
471--------------------------------
472
473Threads sandboxed with filesystem restrictions cannot modify filesystem
474topology, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`.
475However, :manpage:`chroot(2)` calls are not denied.
476
477Special filesystems
478-------------------
479
480Access to regular files and directories can be restricted by Landlock,
481according to the handled accesses of a ruleset.  However, files that do not
482come from a user-visible filesystem (e.g. pipe, socket), but can still be
483accessed through ``/proc/<pid>/fd/*``, cannot currently be explicitly
484restricted.  Likewise, some special kernel filesystems such as nsfs, which can
485be accessed through ``/proc/<pid>/ns/*``, cannot currently be explicitly
486restricted.  However, thanks to the `ptrace restrictions`_, access to such
487sensitive ``/proc`` files are automatically restricted according to domain
488hierarchies.  Future Landlock evolutions could still enable to explicitly
489restrict such paths with dedicated ruleset flags.
490
491Ruleset layers
492--------------
493
494There is a limit of 16 layers of stacked rulesets.  This can be an issue for a
495task willing to enforce a new ruleset in complement to its 16 inherited
496rulesets.  Once this limit is reached, sys_landlock_restrict_self() returns
497E2BIG.  It is then strongly suggested to carefully build rulesets once in the
498life of a thread, especially for applications able to launch other applications
499that may also want to sandbox themselves (e.g. shells, container managers,
500etc.).
501
502Memory usage
503------------
504
505Kernel memory allocated to create rulesets is accounted and can be restricted
506by the Documentation/admin-guide/cgroup-v1/memory.rst.
507
508IOCTL support
509-------------
510
511The ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right restricts the use of
512:manpage:`ioctl(2)`, but it only applies to *newly opened* device files.  This
513means specifically that pre-existing file descriptors like stdin, stdout and
514stderr are unaffected.
515
516Users should be aware that TTY devices have traditionally permitted to control
517other processes on the same TTY through the ``TIOCSTI`` and ``TIOCLINUX`` IOCTL
518commands.  Both of these require ``CAP_SYS_ADMIN`` on modern Linux systems, but
519the behavior is configurable for ``TIOCSTI``.
520
521On older systems, it is therefore recommended to close inherited TTY file
522descriptors, or to reopen them from ``/proc/self/fd/*`` without the
523``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right, if possible.
524
525Landlock's IOCTL support is coarse-grained at the moment, but may become more
526fine-grained in the future.  Until then, users are advised to establish the
527guarantees that they need through the file hierarchy, by only allowing the
528``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right on files where it is really required.
529
530Previous limitations
531====================
532
533File renaming and linking (ABI < 2)
534-----------------------------------
535
536Because Landlock targets unprivileged access controls, it needs to properly
537handle composition of rules.  Such property also implies rules nesting.
538Properly handling multiple layers of rulesets, each one of them able to
539restrict access to files, also implies inheritance of the ruleset restrictions
540from a parent to its hierarchy.  Because files are identified and restricted by
541their hierarchy, moving or linking a file from one directory to another implies
542propagation of the hierarchy constraints, or restriction of these actions
543according to the potentially lost constraints.  To protect against privilege
544escalations through renaming or linking, and for the sake of simplicity,
545Landlock previously limited linking and renaming to the same directory.
546Starting with the Landlock ABI version 2, it is now possible to securely
547control renaming and linking thanks to the new ``LANDLOCK_ACCESS_FS_REFER``
548access right.
549
550File truncation (ABI < 3)
551-------------------------
552
553File truncation could not be denied before the third Landlock ABI, so it is
554always allowed when using a kernel that only supports the first or second ABI.
555
556Starting with the Landlock ABI version 3, it is now possible to securely control
557truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right.
558
559Network support (ABI < 4)
560-------------------------
561
562Starting with the Landlock ABI version 4, it is now possible to restrict TCP
563bind and connect actions to only a set of allowed ports thanks to the new
564``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
565access rights.
566
567IOCTL (ABI < 5)
568---------------
569
570IOCTL operations could not be denied before the fifth Landlock ABI, so
571:manpage:`ioctl(2)` is always allowed when using a kernel that only supports an
572earlier ABI.
573
574Starting with the Landlock ABI version 5, it is possible to restrict the use of
575:manpage:`ioctl(2)` using the new ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
576
577Abstract UNIX socket scoping (ABI < 6)
578--------------------------------------
579
580Starting with the Landlock ABI version 6, it is possible to restrict
581connections to an abstract :manpage:`unix(7)` socket by setting
582``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`` to the ``scoped`` ruleset attribute.
583
584.. _kernel_support:
585
586Kernel support
587==============
588
589Build time configuration
590------------------------
591
592Landlock was first introduced in Linux 5.13 but it must be configured at build
593time with ``CONFIG_SECURITY_LANDLOCK=y``.  Landlock must also be enabled at boot
594time as the other security modules.  The list of security modules enabled by
595default is set with ``CONFIG_LSM``.  The kernel configuration should then
596contains ``CONFIG_LSM=landlock,[...]`` with ``[...]``  as the list of other
597potentially useful security modules for the running system (see the
598``CONFIG_LSM`` help).
599
600Boot time configuration
601-----------------------
602
603If the running kernel does not have ``landlock`` in ``CONFIG_LSM``, then we can
604enable Landlock by adding ``lsm=landlock,[...]`` to
605Documentation/admin-guide/kernel-parameters.rst in the boot loader
606configuration.
607
608For example, if the current built-in configuration is:
609
610.. code-block:: console
611
612    $ zgrep -h "^CONFIG_LSM=" "/boot/config-$(uname -r)" /proc/config.gz 2>/dev/null
613    CONFIG_LSM="lockdown,yama,integrity,apparmor"
614
615...and if the cmdline doesn't contain ``landlock`` either:
616
617.. code-block:: console
618
619    $ sed -n 's/.*\(\<lsm=\S\+\).*/\1/p' /proc/cmdline
620    lsm=lockdown,yama,integrity,apparmor
621
622...we should configure the boot loader to set a cmdline extending the ``lsm``
623list with the ``landlock,`` prefix::
624
625  lsm=landlock,lockdown,yama,integrity,apparmor
626
627After a reboot, we can check that Landlock is up and running by looking at
628kernel logs:
629
630.. code-block:: console
631
632    # dmesg | grep landlock || journalctl -kb -g landlock
633    [    0.000000] Command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
634    [    0.000000] Kernel command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
635    [    0.000000] LSM: initializing lsm=lockdown,capability,landlock,yama,integrity,apparmor
636    [    0.000000] landlock: Up and running.
637
638The kernel may be configured at build time to always load the ``lockdown`` and
639``capability`` LSMs.  In that case, these LSMs will appear at the beginning of
640the ``LSM: initializing`` log line as well, even if they are not configured in
641the boot loader.
642
643Network support
644---------------
645
646To be able to explicitly allow TCP operations (e.g., adding a network rule with
647``LANDLOCK_ACCESS_NET_BIND_TCP``), the kernel must support TCP
648(``CONFIG_INET=y``).  Otherwise, sys_landlock_add_rule() returns an
649``EAFNOSUPPORT`` error, which can safely be ignored because this kind of TCP
650operation is already not possible.
651
652Questions and answers
653=====================
654
655What about user space sandbox managers?
656---------------------------------------
657
658Using user space process to enforce restrictions on kernel resources can lead
659to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of
660the OS code and state
661<https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_).
662
663What about namespaces and containers?
664-------------------------------------
665
666Namespaces can help create sandboxes but they are not designed for
667access-control and then miss useful features for such use case (e.g. no
668fine-grained restrictions).  Moreover, their complexity can lead to security
669issues, especially when untrusted processes can manipulate them (cf.
670`Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_).
671
672Additional documentation
673========================
674
675* Documentation/security/landlock.rst
676* https://landlock.io
677
678.. Links
679.. _samples/landlock/sandboxer.c:
680   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.c
681