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