xref: /linux/Documentation/userspace-api/landlock.rst (revision b8f82cb0d84d00c04cdbdce42f67df71b8507e8b)
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: March 2026
12
13The goal of Landlock is to enable restriction of ambient rights (e.g. global
14filesystem or network access) for a set of processes.  Because Landlock
15is a stackable LSM, it makes it possible to create safe security sandboxes as
16new security layers in addition to the existing system-wide access-controls.
17This kind 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            LANDLOCK_ACCESS_FS_RESOLVE_UNIX,
82        .handled_access_net =
83            LANDLOCK_ACCESS_NET_BIND_TCP |
84            LANDLOCK_ACCESS_NET_CONNECT_TCP,
85        .scoped =
86            LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
87            LANDLOCK_SCOPE_SIGNAL,
88    };
89
90Because we may not know which kernel version an application will be executed
91on, it is safer to follow a best-effort security approach.  Indeed, we
92should try to protect users as much as possible whatever the kernel they are
93using.
94
95To be compatible with older Linux versions, we detect the available Landlock ABI
96version, and only use the available subset of access rights:
97
98.. code-block:: c
99
100    int abi;
101
102    abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
103    if (abi < 0) {
104        /* Degrades gracefully if Landlock is not handled. */
105        perror("The running kernel does not enable to use Landlock");
106        return 0;
107    }
108    switch (abi) {
109    case 1:
110        /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */
111        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
112        __attribute__((fallthrough));
113    case 2:
114        /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
115        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
116        __attribute__((fallthrough));
117    case 3:
118        /* Removes network support for ABI < 4 */
119        ruleset_attr.handled_access_net &=
120            ~(LANDLOCK_ACCESS_NET_BIND_TCP |
121              LANDLOCK_ACCESS_NET_CONNECT_TCP);
122        __attribute__((fallthrough));
123    case 4:
124        /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
125        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
126        __attribute__((fallthrough));
127    case 5:
128        /* Removes LANDLOCK_SCOPE_* for ABI < 6 */
129        ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
130                                 LANDLOCK_SCOPE_SIGNAL);
131        __attribute__((fallthrough));
132    case 6 ... 8:
133        /* Removes LANDLOCK_ACCESS_FS_RESOLVE_UNIX for ABI < 9 */
134        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_RESOLVE_UNIX;
135    }
136
137This enables the creation of an inclusive ruleset that will contain our rules.
138
139.. code-block:: c
140
141    int ruleset_fd;
142
143    ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
144    if (ruleset_fd < 0) {
145        perror("Failed to create a ruleset");
146        return 1;
147    }
148
149We can now add a new rule to this ruleset thanks to the returned file
150descriptor referring to this ruleset.  The rule will allow reading and
151executing the file hierarchy ``/usr``.  Without another rule, write actions
152would then be denied by the ruleset.  To add ``/usr`` to the ruleset, we open
153it with the ``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with
154this file descriptor.
155
156.. code-block:: c
157
158    int err;
159    struct landlock_path_beneath_attr path_beneath = {
160        .allowed_access =
161            LANDLOCK_ACCESS_FS_EXECUTE |
162            LANDLOCK_ACCESS_FS_READ_FILE |
163            LANDLOCK_ACCESS_FS_READ_DIR,
164    };
165
166    path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
167    if (path_beneath.parent_fd < 0) {
168        perror("Failed to open file");
169        close(ruleset_fd);
170        return 1;
171    }
172    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
173                            &path_beneath, 0);
174    close(path_beneath.parent_fd);
175    if (err) {
176        perror("Failed to update ruleset");
177        close(ruleset_fd);
178        return 1;
179    }
180
181It may also be required to create rules following the same logic as explained
182for the ruleset creation, by filtering access rights according to the Landlock
183ABI version.  In this example, this is not required because all of the requested
184``allowed_access`` rights are already available in ABI 1.
185
186For network access-control, we can add a set of rules that allow to use a port
187number for a specific action: HTTPS connections.
188
189.. code-block:: c
190
191    struct landlock_net_port_attr net_port = {
192        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
193        .port = 443,
194    };
195
196    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
197                            &net_port, 0);
198
199When passing a non-zero ``flags`` argument to ``landlock_restrict_self()``, a
200similar backwards compatibility check is needed for the restrict flags
201(see sys_landlock_restrict_self() documentation for available flags):
202
203.. code-block:: c
204
205    __u32 restrict_flags =
206        LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
207        LANDLOCK_RESTRICT_SELF_TSYNC;
208    switch (abi) {
209    case 1 ... 6:
210        /* Removes logging flags for ABI < 7 */
211        restrict_flags &= ~(LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF |
212                            LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
213                            LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
214        __attribute__((fallthrough));
215    case 7:
216        /*
217         * Removes multithreaded enforcement flag for ABI < 8
218         *
219         * WARNING: Without this flag, calling landlock_restrict_self(2) is
220         * only equivalent if the calling process is single-threaded. Below
221         * ABI v8 (and as of ABI v8, when not using this flag), a Landlock
222         * policy would only be enforced for the calling thread and its
223         * children (and not for all threads, including parents and siblings).
224         */
225        restrict_flags &= ~LANDLOCK_RESTRICT_SELF_TSYNC;
226    }
227
228The next step is to restrict the current thread from gaining more privileges
229(e.g. through a SUID binary).  We now have a ruleset with the first rule
230allowing read and execute access to ``/usr`` while denying all other handled
231accesses for the filesystem, and a second rule allowing HTTPS connections.
232
233.. code-block:: c
234
235    if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
236        perror("Failed to restrict privileges");
237        close(ruleset_fd);
238        return 1;
239    }
240
241The current thread is now ready to sandbox itself with the ruleset.
242
243.. code-block:: c
244
245    if (landlock_restrict_self(ruleset_fd, restrict_flags)) {
246        perror("Failed to enforce ruleset");
247        close(ruleset_fd);
248        return 1;
249    }
250    close(ruleset_fd);
251
252If the ``landlock_restrict_self`` system call succeeds, the current thread is
253now restricted and this policy will be enforced on all its subsequently created
254children as well.  Once a thread is landlocked, there is no way to remove its
255security policy; only adding more restrictions is allowed.  These threads are
256now in a new Landlock domain, which is a merger of their parent one (if any)
257with the new ruleset.
258
259Full working code can be found in `samples/landlock/sandboxer.c`_.
260
261Good practices
262--------------
263
264It is recommended to set access rights to file hierarchy leaves as much as
265possible.  For instance, it is better to be able to have ``~/doc/`` as a
266read-only hierarchy and ``~/tmp/`` as a read-write hierarchy, compared to
267``~/`` as a read-only hierarchy and ``~/tmp/`` as a read-write hierarchy.
268Following this good practice leads to self-sufficient hierarchies that do not
269depend on their location (i.e. parent directories).  This is particularly
270relevant when we want to allow linking or renaming.  Indeed, having consistent
271access rights per directory enables changing the location of such directories
272without relying on the destination directory access rights (except those that
273are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
274documentation).
275
276Having self-sufficient hierarchies also helps to tighten the required access
277rights to the minimal set of data.  This also helps avoid sinkhole directories,
278i.e. directories where data can be linked to but not linked from.  However,
279this depends on data organization, which might not be controlled by developers.
280In this case, granting read-write access to ``~/tmp/``, instead of write-only
281access, would potentially allow moving ``~/tmp/`` to a non-readable directory
282and still keep the ability to list the content of ``~/tmp/``.
283
284Layers of file path access rights
285---------------------------------
286
287Each time a thread enforces a ruleset on itself, it updates its Landlock domain
288with a new layer of policy.  This complementary policy is stacked with any
289other rulesets potentially already restricting this thread.  A sandboxed thread
290can then safely add more constraints to itself with a new enforced ruleset.
291
292One policy layer grants access to a file path if at least one of its rules
293encountered on the path grants the access.  A sandboxed thread can only access
294a file path if all its enforced policy layers grant the access as well as all
295the other system access controls (e.g. filesystem DAC, other LSM policies,
296etc.).
297
298Bind mounts and OverlayFS
299-------------------------
300
301Landlock enables restricting access to file hierarchies, which means that these
302access rights can be propagated with bind mounts (cf.
303Documentation/filesystems/sharedsubtree.rst) but not with
304Documentation/filesystems/overlayfs.rst.
305
306A bind mount mirrors a source file hierarchy to a destination.  The destination
307hierarchy is then composed of the exact same files, on which Landlock rules can
308be tied, either via the source or the destination path.  These rules restrict
309access when they are encountered on a path, which means that they can restrict
310access to multiple file hierarchies at the same time, whether these hierarchies
311are the result of bind mounts or not.
312
313An OverlayFS mount point consists of upper and lower layers.  These layers are
314combined in a merge directory, and that merged directory becomes available at
315the mount point.  This merge hierarchy may include files from the upper and
316lower layers, but modifications performed on the merge hierarchy only reflect
317on the upper layer.  From a Landlock policy point of view, all OverlayFS layers
318and merge hierarchies are standalone and each contains their own set of files
319and directories, which is different from bind mounts.  A policy restricting an
320OverlayFS layer will not restrict the resulted merged hierarchy, and vice versa.
321Landlock users should then only think about file hierarchies they want to allow
322access to, regardless of the underlying filesystem.
323
324Inheritance
325-----------
326
327Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain
328restrictions from its parent.  This is similar to seccomp inheritance (cf.
329Documentation/userspace-api/seccomp_filter.rst) or any other LSM dealing with
330task's :manpage:`credentials(7)`.  For instance, one process's thread may apply
331Landlock rules to itself, but they will not be automatically applied to other
332sibling threads (unlike POSIX thread credential changes, cf.
333:manpage:`nptl(7)`).
334
335When a thread sandboxes itself, we have the guarantee that the related security
336policy will stay enforced on all this thread's descendants.  This allows
337creating standalone and modular security policies per application, which will
338automatically be composed between themselves according to their runtime parent
339policies.
340
341Ptrace restrictions
342-------------------
343
344A sandboxed process has less privileges than a non-sandboxed process and must
345then be subject to additional restrictions when manipulating another process.
346To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
347process, a sandboxed process should have a superset of the target process's
348access rights, which means the tracee must be in a sub-domain of the tracer.
349
350IPC scoping
351-----------
352
353Similar to the implicit `Ptrace restrictions`_, we may want to further restrict
354interactions between sandboxes.  Therefore, at ruleset creation time, each
355Landlock domain can restrict the scope for certain operations, so that these
356operations can only reach out to processes within the same Landlock domain or in
357a nested Landlock domain (the "scope").
358
359The operations which can be scoped are:
360
361``LANDLOCK_SCOPE_SIGNAL``
362    This limits the sending of signals to target processes which run within the
363    same or a nested Landlock domain.
364
365``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET``
366    This limits the set of abstract :manpage:`unix(7)` sockets to which we can
367    :manpage:`connect(2)` to socket addresses which were created by a process in
368    the same or a nested Landlock domain.
369
370    A :manpage:`sendto(2)` on a non-connected datagram socket is treated as if
371    it were doing an implicit :manpage:`connect(2)` and will be blocked if the
372    remote end does not stem from the same or a nested Landlock domain.
373
374    A :manpage:`sendto(2)` on a socket which was previously connected will not
375    be restricted.  This works for both datagram and stream sockets.
376
377IPC scoping does not support exceptions via :manpage:`landlock_add_rule(2)`.
378If an operation is scoped within a domain, no rules can be added to allow access
379to resources or processes outside of the scope.
380
381Truncating files
382----------------
383
384The operations covered by ``LANDLOCK_ACCESS_FS_WRITE_FILE`` and
385``LANDLOCK_ACCESS_FS_TRUNCATE`` both change the contents of a file and sometimes
386overlap in non-intuitive ways.  It is strongly recommended to always specify
387both of these together (either granting both, or granting none).
388
389A particularly surprising example is :manpage:`creat(2)`.  The name suggests
390that this system call requires the rights to create and write files.  However,
391it also requires the truncate right if an existing file under the same name is
392already present.
393
394It should also be noted that truncating files does not require the
395``LANDLOCK_ACCESS_FS_WRITE_FILE`` right.  Apart from the :manpage:`truncate(2)`
396system call, this can also be done through :manpage:`open(2)` with the flags
397``O_RDONLY | O_TRUNC``.
398
399At the same time, on some filesystems, :manpage:`fallocate(2)` offers a way to
400shorten file contents with ``FALLOC_FL_COLLAPSE_RANGE`` when the file is opened
401for writing, sidestepping the ``LANDLOCK_ACCESS_FS_TRUNCATE`` right.
402
403The truncate right is associated with the opened file (see below).
404
405Rights associated with file descriptors
406---------------------------------------
407
408When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE`` and
409``LANDLOCK_ACCESS_FS_IOCTL_DEV`` rights is associated with the newly created
410file descriptor and will be used for subsequent truncation and ioctl attempts
411using :manpage:`ftruncate(2)` and :manpage:`ioctl(2)`.  The behavior is similar
412to opening a file for reading or writing, where permissions are checked during
413:manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
414:manpage:`write(2)` calls.
415
416As a consequence, it is possible that a process has multiple open file
417descriptors referring to the same file, but Landlock enforces different things
418when operating with these file descriptors.  This can happen when a Landlock
419ruleset gets enforced and the process keeps file descriptors which were opened
420both before and after the enforcement.  It is also possible to pass such file
421descriptors between processes, keeping their Landlock properties, even when some
422of the involved processes do not have an enforced Landlock ruleset.
423
424Compatibility
425=============
426
427Backward and forward compatibility
428----------------------------------
429
430Landlock is designed to be compatible with past and future versions of the
431kernel.  This is achieved thanks to the system call attributes and the
432associated bitflags, particularly the ruleset's ``handled_access_fs``.  Making
433handled access rights explicit enables the kernel and user space to have a clear
434contract with each other.  This is required to make sure sandboxing will not
435get stricter with a system update, which could break applications.
436
437Developers can subscribe to the `Landlock mailing list
438<https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
439test their applications with the latest available features.  In the interest of
440users, and because they may use different kernel versions, it is strongly
441encouraged to follow a best-effort security approach by checking the Landlock
442ABI version at runtime and only enforcing the supported features.
443
444.. _landlock_abi_versions:
445
446Landlock ABI versions
447---------------------
448
449The Landlock ABI version can be read with the sys_landlock_create_ruleset()
450system call:
451
452.. code-block:: c
453
454    int abi;
455
456    abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
457    if (abi < 0) {
458        switch (errno) {
459        case ENOSYS:
460            printf("Landlock is not supported by the current kernel.\n");
461            break;
462        case EOPNOTSUPP:
463            printf("Landlock is currently disabled.\n");
464            break;
465        }
466        return 0;
467    }
468    if (abi >= 2) {
469        printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
470    }
471
472All Landlock kernel interfaces are supported by the first ABI version unless
473explicitly noted in their documentation.
474
475Landlock errata
476---------------
477
478In addition to ABI versions, Landlock provides an errata mechanism to track
479fixes for issues that may affect backwards compatibility or require userspace
480awareness.  The errata bitmask can be queried using:
481
482.. code-block:: c
483
484    int errata;
485
486    errata = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_ERRATA);
487    if (errata < 0) {
488        /* Landlock not available or disabled */
489        return 0;
490    }
491
492The returned value is a bitmask where each bit represents a specific erratum.
493If bit N is set (``errata & (1 << (N - 1))``), then erratum N has been fixed
494in the running kernel.
495
496.. warning::
497
498   **Most applications should NOT check errata.** In 99.9% of cases, checking
499   errata is unnecessary, increases code complexity, and can potentially
500   decrease protection if misused.  For example, disabling the sandbox when an
501   erratum is not fixed could leave the system less secure than using
502   Landlock's best-effort protection.  When in doubt, ignore errata.
503
504.. kernel-doc:: security/landlock/errata/abi-4.h
505    :doc: erratum_1
506
507.. kernel-doc:: security/landlock/errata/abi-6.h
508    :doc: erratum_2
509
510.. kernel-doc:: security/landlock/errata/abi-1.h
511    :doc: erratum_3
512
513How to check for errata
514~~~~~~~~~~~~~~~~~~~~~~~
515
516If you determine that your application needs to check for specific errata,
517use this pattern:
518
519.. code-block:: c
520
521    int errata = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_ERRATA);
522    if (errata >= 0) {
523        /* Check for specific erratum (1-indexed) */
524        if (errata & (1 << (erratum_number - 1))) {
525            /* Erratum N is fixed in this kernel */
526        } else {
527            /* Erratum N is NOT fixed - consider implications for your use case */
528        }
529    }
530
531**Important:** Only check errata if your application specifically relies on
532behavior that changed due to the fix.  The fixes generally make Landlock less
533restrictive or more correct, not more restrictive.
534
535Kernel interface
536================
537
538Access rights
539-------------
540
541.. kernel-doc:: include/uapi/linux/landlock.h
542    :identifiers: fs_access net_access scope
543
544Creating a new ruleset
545----------------------
546
547.. kernel-doc:: security/landlock/syscalls.c
548    :identifiers: sys_landlock_create_ruleset
549
550.. kernel-doc:: include/uapi/linux/landlock.h
551    :identifiers: landlock_ruleset_attr
552
553Extending a ruleset
554-------------------
555
556.. kernel-doc:: security/landlock/syscalls.c
557    :identifiers: sys_landlock_add_rule
558
559.. kernel-doc:: include/uapi/linux/landlock.h
560    :identifiers: landlock_rule_type landlock_path_beneath_attr
561                  landlock_net_port_attr
562
563Enforcing a ruleset
564-------------------
565
566.. kernel-doc:: security/landlock/syscalls.c
567    :identifiers: sys_landlock_restrict_self
568
569Current limitations
570===================
571
572Filesystem topology modification
573--------------------------------
574
575Threads sandboxed with filesystem restrictions cannot modify filesystem
576topology, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`.
577However, :manpage:`chroot(2)` calls are not denied.
578
579Special filesystems
580-------------------
581
582Access to regular files and directories can be restricted by Landlock,
583according to the handled accesses of a ruleset.  However, files that do not
584come from a user-visible filesystem (e.g. pipe, socket), but can still be
585accessed through ``/proc/<pid>/fd/*``, cannot currently be explicitly
586restricted.  Likewise, some special kernel filesystems such as nsfs, which can
587be accessed through ``/proc/<pid>/ns/*``, cannot currently be explicitly
588restricted.  However, thanks to the `ptrace restrictions`_, access to such
589sensitive ``/proc`` files are automatically restricted according to domain
590hierarchies.  Future Landlock evolutions could still enable to explicitly
591restrict such paths with dedicated ruleset flags.
592
593Ruleset layers
594--------------
595
596There is a limit of 16 layers of stacked rulesets.  This can be an issue for a
597task willing to enforce a new ruleset in complement to its 16 inherited
598rulesets.  Once this limit is reached, sys_landlock_restrict_self() returns
599E2BIG.  It is then strongly suggested to carefully build rulesets once in the
600life of a thread, especially for applications able to launch other applications
601that may also want to sandbox themselves (e.g. shells, container managers,
602etc.).
603
604Memory usage
605------------
606
607Kernel memory allocated to create rulesets is accounted and can be restricted
608by the Documentation/admin-guide/cgroup-v1/memory.rst.
609
610IOCTL support
611-------------
612
613The ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right restricts the use of
614:manpage:`ioctl(2)`, but it only applies to *newly opened* device files.  This
615means specifically that pre-existing file descriptors like stdin, stdout and
616stderr are unaffected.
617
618Users should be aware that TTY devices have traditionally permitted to control
619other processes on the same TTY through the ``TIOCSTI`` and ``TIOCLINUX`` IOCTL
620commands.  Both of these require ``CAP_SYS_ADMIN`` on modern Linux systems, but
621the behavior is configurable for ``TIOCSTI``.
622
623On older systems, it is therefore recommended to close inherited TTY file
624descriptors, or to reopen them from ``/proc/self/fd/*`` without the
625``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right, if possible.
626
627Landlock's IOCTL support is coarse-grained at the moment, but may become more
628fine-grained in the future.  Until then, users are advised to establish the
629guarantees that they need through the file hierarchy, by only allowing the
630``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right on files where it is really required.
631
632Previous limitations
633====================
634
635File renaming and linking (ABI < 2)
636-----------------------------------
637
638Because Landlock targets unprivileged access controls, it needs to properly
639handle composition of rules.  Such property also implies rules nesting.
640Properly handling multiple layers of rulesets, each one of them able to
641restrict access to files, also implies inheritance of the ruleset restrictions
642from a parent to its hierarchy.  Because files are identified and restricted by
643their hierarchy, moving or linking a file from one directory to another implies
644propagation of the hierarchy constraints, or restriction of these actions
645according to the potentially lost constraints.  To protect against privilege
646escalations through renaming or linking, and for the sake of simplicity,
647Landlock previously limited linking and renaming to the same directory.
648Starting with the Landlock ABI version 2, it is now possible to securely
649control renaming and linking thanks to the new ``LANDLOCK_ACCESS_FS_REFER``
650access right.
651
652File truncation (ABI < 3)
653-------------------------
654
655File truncation could not be denied before the third Landlock ABI, so it is
656always allowed when using a kernel that only supports the first or second ABI.
657
658Starting with the Landlock ABI version 3, it is now possible to securely control
659truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right.
660
661TCP bind and connect (ABI < 4)
662------------------------------
663
664Starting with the Landlock ABI version 4, it is now possible to restrict TCP
665bind and connect actions to only a set of allowed ports thanks to the new
666``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
667access rights.
668
669Device IOCTL (ABI < 5)
670----------------------
671
672IOCTL operations could not be denied before the fifth Landlock ABI, so
673:manpage:`ioctl(2)` is always allowed when using a kernel that only supports an
674earlier ABI.
675
676Starting with the Landlock ABI version 5, it is possible to restrict the use of
677:manpage:`ioctl(2)` on character and block devices using the new
678``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
679
680Abstract UNIX socket (ABI < 6)
681------------------------------
682
683Starting with the Landlock ABI version 6, it is possible to restrict
684connections to an abstract :manpage:`unix(7)` socket by setting
685``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`` to the ``scoped`` ruleset attribute.
686
687Signal (ABI < 6)
688----------------
689
690Starting with the Landlock ABI version 6, it is possible to restrict
691:manpage:`signal(7)` sending by setting ``LANDLOCK_SCOPE_SIGNAL`` to the
692``scoped`` ruleset attribute.
693
694Logging (ABI < 7)
695-----------------
696
697Starting with the Landlock ABI version 7, it is possible to control logging of
698Landlock audit events with the ``LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF``,
699``LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON``, and
700``LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF`` flags passed to
701sys_landlock_restrict_self().  See Documentation/admin-guide/LSM/landlock.rst
702for more details on audit.
703
704Thread synchronization (ABI < 8)
705--------------------------------
706
707Starting with the Landlock ABI version 8, it is now possible to
708enforce Landlock rulesets across all threads of the calling process
709using the ``LANDLOCK_RESTRICT_SELF_TSYNC`` flag passed to
710sys_landlock_restrict_self().
711
712Pathname UNIX sockets (ABI < 9)
713-------------------------------
714
715Starting with the Landlock ABI version 9, it is possible to restrict
716connections to pathname UNIX domain sockets (:manpage:`unix(7)`) using
717the new ``LANDLOCK_ACCESS_FS_RESOLVE_UNIX`` right.
718
719.. _kernel_support:
720
721Kernel support
722==============
723
724Build time configuration
725------------------------
726
727Landlock was first introduced in Linux 5.13 but it must be configured at build
728time with ``CONFIG_SECURITY_LANDLOCK=y``.  Landlock must also be enabled at boot
729time like other security modules.  The list of security modules enabled by
730default is set with ``CONFIG_LSM``.  The kernel configuration should then
731contain ``CONFIG_LSM=landlock,[...]`` with ``[...]``  as the list of other
732potentially useful security modules for the running system (see the
733``CONFIG_LSM`` help).
734
735Boot time configuration
736-----------------------
737
738If the running kernel does not have ``landlock`` in ``CONFIG_LSM``, then we can
739enable Landlock by adding ``lsm=landlock,[...]`` to
740Documentation/admin-guide/kernel-parameters.rst in the boot loader
741configuration.
742
743For example, if the current built-in configuration is:
744
745.. code-block:: console
746
747    $ zgrep -h "^CONFIG_LSM=" "/boot/config-$(uname -r)" /proc/config.gz 2>/dev/null
748    CONFIG_LSM="lockdown,yama,integrity,apparmor"
749
750...and if the cmdline doesn't contain ``landlock`` either:
751
752.. code-block:: console
753
754    $ sed -n 's/.*\(\<lsm=\S\+\).*/\1/p' /proc/cmdline
755    lsm=lockdown,yama,integrity,apparmor
756
757...we should configure the boot loader to set a cmdline extending the ``lsm``
758list with the ``landlock,`` prefix::
759
760  lsm=landlock,lockdown,yama,integrity,apparmor
761
762After a reboot, we can check that Landlock is up and running by looking at
763kernel logs:
764
765.. code-block:: console
766
767    # dmesg | grep landlock || journalctl -kb -g landlock
768    [    0.000000] Command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
769    [    0.000000] Kernel command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
770    [    0.000000] LSM: initializing lsm=lockdown,capability,landlock,yama,integrity,apparmor
771    [    0.000000] landlock: Up and running.
772
773The kernel may be configured at build time to always load the ``lockdown`` and
774``capability`` LSMs.  In that case, these LSMs will appear at the beginning of
775the ``LSM: initializing`` log line as well, even if they are not configured in
776the boot loader.
777
778Network support
779---------------
780
781To be able to explicitly allow TCP operations (e.g., adding a network rule with
782``LANDLOCK_ACCESS_NET_BIND_TCP``), the kernel must support TCP
783(``CONFIG_INET=y``).  Otherwise, sys_landlock_add_rule() returns an
784``EAFNOSUPPORT`` error, which can safely be ignored because this kind of TCP
785operation is already not possible.
786
787Questions and answers
788=====================
789
790What about user space sandbox managers?
791---------------------------------------
792
793Using user space processes to enforce restrictions on kernel resources can lead
794to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of
795the OS code and state
796<https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_).
797
798What about namespaces and containers?
799-------------------------------------
800
801Namespaces can help create sandboxes but they are not designed for
802access-control and then miss useful features for such use case (e.g. no
803fine-grained restrictions).  Moreover, their complexity can lead to security
804issues, especially when untrusted processes can manipulate them (cf.
805`Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_).
806
807How to disable Landlock audit records?
808--------------------------------------
809
810You might want to put in place filters as explained here:
811Documentation/admin-guide/LSM/landlock.rst
812
813Additional documentation
814========================
815
816* Documentation/admin-guide/LSM/landlock.rst
817* Documentation/security/landlock.rst
818* https://landlock.io
819
820.. Links
821.. _samples/landlock/sandboxer.c:
822   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.c
823