xref: /linux/Documentation/userspace-api/landlock.rst (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
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: October 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 the actions a process is allowed to perform on a
33specific resource.  A set of rules is aggregated in a ruleset, which can then
34restrict the thread enforcing it, and its future children.
35
36The existing types of rules are:
37
38Filesystem rules
39    The rule key is a file hierarchy, and the actions it allows are
40    defined with `filesystem access rights`.
41
42Network rules (since ABI v4 for TCP and v10 for UDP)
43    For these rules, the object is a TCP or UDP port,
44    and the related actions are defined with `network access rights`.
45
46Capability rules (since ABI v12)
47    The rule body lists which Linux capabilities (see
48    :manpage:`capabilities(7)`) the process may exercise; the action is
49    defined with `permission flags`.
50
51Namespace rules (since ABI v12)
52    The rule body lists which namespace types (see
53    :manpage:`namespaces(7)`) the process may use; the action is defined
54    with `permission flags`.
55
56Defining and enforcing a security policy
57----------------------------------------
58
59We first need to define the ruleset that will contain our rules.
60
61For this example, the ruleset will contain rules that only allow some
62filesystem read actions and some specific UDP and TCP actions. Filesystem
63write actions and other TCP/UDP actions will be denied.
64
65The ruleset then needs to handle all these kinds of actions.  This is
66required for backward and forward compatibility (i.e. the kernel and user
67space may not know each other's supported restrictions), hence the need
68to be explicit about the denied-by-default access rights.
69
70.. code-block:: c
71
72    struct landlock_ruleset_attr ruleset_attr = {
73        .handled_access_fs =
74            LANDLOCK_ACCESS_FS_EXECUTE |
75            LANDLOCK_ACCESS_FS_WRITE_FILE |
76            LANDLOCK_ACCESS_FS_READ_FILE |
77            LANDLOCK_ACCESS_FS_READ_DIR |
78            LANDLOCK_ACCESS_FS_REMOVE_DIR |
79            LANDLOCK_ACCESS_FS_REMOVE_FILE |
80            LANDLOCK_ACCESS_FS_MAKE_CHAR |
81            LANDLOCK_ACCESS_FS_MAKE_DIR |
82            LANDLOCK_ACCESS_FS_MAKE_REG |
83            LANDLOCK_ACCESS_FS_MAKE_SOCK |
84            LANDLOCK_ACCESS_FS_MAKE_FIFO |
85            LANDLOCK_ACCESS_FS_MAKE_BLOCK |
86            LANDLOCK_ACCESS_FS_MAKE_SYM |
87            LANDLOCK_ACCESS_FS_REFER |
88            LANDLOCK_ACCESS_FS_TRUNCATE |
89            LANDLOCK_ACCESS_FS_IOCTL_DEV |
90            LANDLOCK_ACCESS_FS_RESOLVE_UNIX,
91        .handled_access_net =
92            LANDLOCK_ACCESS_NET_BIND_TCP |
93            LANDLOCK_ACCESS_NET_CONNECT_TCP |
94            LANDLOCK_ACCESS_NET_BIND_UDP |
95            LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
96        .scoped =
97            LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
98            LANDLOCK_SCOPE_SIGNAL,
99        .handled_permissions =
100            LANDLOCK_PERMISSION_NAMESPACE_USE |
101            LANDLOCK_PERMISSION_CAPABILITY_USE,
102    };
103
104Because we may not know which kernel version an application will be executed
105on, it is safer to follow a best-effort security approach.  Indeed, we
106should try to protect users as much as possible whatever the kernel they are
107using.
108
109To be compatible with older Linux versions, we detect the available Landlock ABI
110version, and only use the available subset of access rights:
111
112.. code-block:: c
113
114    int abi;
115
116    abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
117    if (abi < 0) {
118        /* Degrades gracefully if Landlock is not handled. */
119        perror("The running kernel does not enable to use Landlock");
120        return 0;
121    }
122    switch (abi) {
123    case 1:
124        /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */
125        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
126        __attribute__((fallthrough));
127    case 2:
128        /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
129        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
130        __attribute__((fallthrough));
131    case 3:
132        /* Removes network support for ABI < 4 */
133        ruleset_attr.handled_access_net &=
134            ~(LANDLOCK_ACCESS_NET_BIND_TCP |
135              LANDLOCK_ACCESS_NET_CONNECT_TCP);
136        __attribute__((fallthrough));
137    case 4:
138        /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
139        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
140        __attribute__((fallthrough));
141    case 5:
142        /* Removes LANDLOCK_SCOPE_* for ABI < 6 */
143        ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
144                                 LANDLOCK_SCOPE_SIGNAL);
145        __attribute__((fallthrough));
146    case 6 ... 8:
147        /* Removes LANDLOCK_ACCESS_FS_RESOLVE_UNIX for ABI < 9 */
148        ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_RESOLVE_UNIX;
149        __attribute__((fallthrough));
150    case 9:
151        /* Removes LANDLOCK_ACCESS_NET_*_UDP for ABI < 10 */
152        ruleset_attr.handled_access_net &=
153            ~(LANDLOCK_ACCESS_NET_BIND_UDP |
154              LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
155        __attribute__((fallthrough));
156    case 10:
157    case 11:
158        /* Removes LANDLOCK_PERMISSION_* for ABI < 12 */
159        ruleset_attr.handled_permissions &=
160            ~(LANDLOCK_PERMISSION_NAMESPACE_USE |
161              LANDLOCK_PERMISSION_CAPABILITY_USE);
162    }
163
164This enables the creation of an inclusive ruleset that will contain our rules.
165
166.. code-block:: c
167
168    int ruleset_fd;
169
170    ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
171    if (ruleset_fd < 0) {
172        perror("Failed to create a ruleset");
173        return 1;
174    }
175
176We can now add a new rule to this ruleset thanks to the returned file
177descriptor referring to this ruleset.  The rule will allow reading and
178executing the file hierarchy ``/usr``.  Without another rule, write actions
179would then be denied by the ruleset.  To add ``/usr`` to the ruleset, we open
180it with the ``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with
181this file descriptor.
182
183.. code-block:: c
184
185    int err = 0;
186    struct landlock_path_beneath_attr path_beneath = {
187        .allowed_access =
188            LANDLOCK_ACCESS_FS_EXECUTE |
189            LANDLOCK_ACCESS_FS_READ_FILE |
190            LANDLOCK_ACCESS_FS_READ_DIR,
191    };
192
193    path_beneath.allowed_access &= ruleset_attr.handled_access_fs;
194    if (path_beneath.allowed_access) {
195        path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
196        if (path_beneath.parent_fd < 0) {
197            perror("Failed to open file");
198            close(ruleset_fd);
199            return 1;
200        }
201        err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
202                                &path_beneath, 0);
203        close(path_beneath.parent_fd);
204        if (err) {
205            perror("Failed to update ruleset");
206            close(ruleset_fd);
207            return 1;
208        }
209    }
210
211As shown above, masking the rule's ``allowed_access`` against the ruleset's
212``handled_access_*`` is the recommended best-effort pattern: rights the running
213kernel does not support are dropped (the compatibility switch above already
214cleared them in ``handled_access_*``), and the rule is skipped if no supported
215right remains.
216
217For network access-control, we will add a set of rules to allow DNS
218queries, which requires both UDP and TCP. For TCP, we need to allow
219outbound connections to port 53, which can be handled and granted starting
220with ABI 4:
221
222.. code-block:: c
223
224    struct landlock_net_port_attr tcp_conn = {
225        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
226        .port = 53,
227    };
228
229    tcp_conn.allowed_access &= ruleset_attr.handled_access_net;
230    if (tcp_conn.allowed_access)
231        err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
232                                &tcp_conn, 0);
233
234We also need to be able to send UDP datagrams to port 53, which requires
235granting ``LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP``. Since our DNS client will
236emit datagrams without explicitly binding to a specific source port, its UDP
237socket will automatically bind an ephemeral port. To allow this behaviour,
238we also need to grant ``LANDLOCK_ACCESS_NET_BIND_UDP`` on port 0, as if
239the program explicitly called :manpage:`bind(2)` on port 0.
240
241.. code-block:: c
242
243    struct landlock_net_port_attr udp_send = {
244        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
245        .port = 53,
246    };
247
248    udp_send.allowed_access &= ruleset_attr.handled_access_net;
249    if (udp_send.allowed_access)
250        err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
251                                &udp_send, 0);
252    [...]
253
254    struct landlock_net_port_attr udp_bind = {
255        .allowed_access = LANDLOCK_ACCESS_NET_BIND_UDP,
256        .port = 0,
257    };
258
259    udp_bind.allowed_access &= ruleset_attr.handled_access_net;
260    if (udp_bind.allowed_access)
261        err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
262                                &udp_bind, 0);
263
264Capability and namespace rules use a different attribute layout:
265``permissions`` identifies the permission category (a single
266``LANDLOCK_PERMISSION_*`` flag) and a type-specific value field carries the
267bitmask to allow within it.  See `Capability and namespace restrictions`_ for
268the model.
269
270For capability access-control, we can add rules that allow specific
271capabilities (see :manpage:`capabilities(7)` for the list of ``CAP_*``
272values).  For instance, to allow ``CAP_SYS_CHROOT`` (so the sandboxed
273process can call :manpage:`chroot(2)` inside a user namespace):
274
275.. code-block:: c
276
277    struct landlock_capability_attr cap_attr = {
278        .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE,
279        .allowed_capabilities = (1ULL << CAP_SYS_CHROOT),
280    };
281
282    cap_attr.permissions &= ruleset_attr.handled_permissions;
283    if (cap_attr.permissions)
284        err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
285                                &cap_attr, 0);
286
287For namespace access-control, we can add rules that allow using specific
288namespace types (see :manpage:`namespaces(7)` for the list of ``CLONE_NEW*``
289values): creating them via :manpage:`unshare(2)` / :manpage:`clone(2)` /
290:manpage:`clone3(2)`, joining them via :manpage:`setns(2)`, or acquiring an fd
291reference via :manpage:`open_tree(2)` / :manpage:`fsmount(2)`.  For instance,
292to allow creating user namespaces (which grants all capabilities inside the new
293namespace):
294
295.. code-block:: c
296
297    struct landlock_namespace_attr ns_attr = {
298        .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE,
299        .allowed_namespace_types = CLONE_NEWUSER,
300    };
301
302    ns_attr.permissions &= ruleset_attr.handled_permissions;
303    if (ns_attr.permissions)
304        err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
305                                &ns_attr, 0);
306
307Together, these two rules allow an unprivileged process to create a user
308namespace and call :manpage:`chroot(2)` inside it, while denying all other
309capabilities and namespace types.  User namespace creation is the one operation
310that does not require ``CAP_SYS_ADMIN``, so no capability rule is needed for it.
311See `Capability and namespace restrictions`_ for details on capability
312requirements.
313
314When passing a non-zero ``flags`` argument to ``landlock_restrict_self()``, a
315similar backwards compatibility check is needed for the restrict flags
316(see sys_landlock_restrict_self() documentation for available flags):
317
318.. code-block:: c
319
320    __u32 restrict_flags =
321        LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
322        LANDLOCK_RESTRICT_SELF_TSYNC |
323        LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
324    switch (abi) {
325    case 1 ... 6:
326        /* Removes logging flags for ABI < 7 */
327        restrict_flags &= ~(LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF |
328                            LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
329                            LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
330        __attribute__((fallthrough));
331    case 7:
332        /*
333         * Removes multithreaded enforcement flag for ABI < 8
334         *
335         * WARNING: Without this flag, calling landlock_restrict_self(2) is
336         * only equivalent if the calling process is single-threaded. Below
337         * ABI v8 (and as of ABI v8, when not using this flag), a Landlock
338         * policy would only be enforced for the calling thread and its
339         * children (and not for all threads, including parents and siblings).
340         */
341        restrict_flags &= ~LANDLOCK_RESTRICT_SELF_TSYNC;
342        __attribute__((fallthrough));
343    case 8 ... 10:
344        /* Removes no new privs flag for ABI < 11 */
345        restrict_flags &= ~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
346    }
347
348The next step is to restrict the current thread from gaining more privileges
349(e.g. through a SUID binary).  For unprivileged processes, setting the
350no_new_privs attribute is required by Landlock.
351
352Processes with ``CAP_SYS_ADMIN`` in their namespace can enforce a ruleset
353without setting no_new_privs, but leaving no_new_privs unset is risky even
354when Landlock does not require this attribute: sandboxed processes could
355still execute set-user-ID, set-group-ID or file-capability binaries, which
356would then run with elevated privileges while being restricted by a Landlock
357domain they may not expect, making them potential confused deputies.
358no_new_privs should only be left unset if such a privilege transition is
359expected.
360
361We now have a ruleset with the first rule allowing read and execute access to
362``/usr`` while denying all other handled accesses for the filesystem, and two
363more rules allowing DNS queries.
364
365.. code-block:: c
366
367    /*
368     * If the ABI > 10, we can tie setting no_new_privs with successful ruleset
369     * enforcement and skip the manual prctl(PR_SET_NO_NEW_PRIVS, ...) call.
370     */
371    if (!(restrict_flags & LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS) &&
372        prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
373        perror("Failed to restrict privileges");
374        close(ruleset_fd);
375        return 1;
376    }
377
378The current thread is now ready to sandbox itself with the ruleset.
379
380.. code-block:: c
381
382    if (landlock_restrict_self(ruleset_fd, restrict_flags)) {
383        perror("Failed to enforce ruleset");
384        close(ruleset_fd);
385        return 1;
386    }
387    close(ruleset_fd);
388
389If the ``landlock_restrict_self`` system call succeeds, the current thread is
390now restricted and this policy will be enforced on all its subsequently created
391children as well.  Once a thread is landlocked, there is no way to remove its
392security policy; only adding more restrictions is allowed.  These threads are
393now in a new Landlock domain, which is a merger of their parent one (if any)
394with the new ruleset.
395
396Full working code can be found in `samples/landlock/sandboxer.c`_.
397
398Good practices
399--------------
400
401It is recommended to set access rights to file hierarchy leaves as much as
402possible.  For instance, it is better to be able to have ``~/doc/`` as a
403read-only hierarchy and ``~/tmp/`` as a read-write hierarchy, compared to
404``~/`` as a read-only hierarchy and ``~/tmp/`` as a read-write hierarchy.
405Following this good practice leads to self-sufficient hierarchies that do not
406depend on their location (i.e. parent directories).  This is particularly
407relevant when we want to allow linking or renaming.  Indeed, having consistent
408access rights per directory enables changing the location of such directories
409without relying on the destination directory access rights (except those that
410are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
411documentation).
412
413Having self-sufficient hierarchies also helps to tighten the required access
414rights to the minimal set of data.  This also helps avoid sinkhole directories,
415i.e. directories where data can be linked to but not linked from.  However,
416this depends on data organization, which might not be controlled by developers.
417In this case, granting read-write access to ``~/tmp/``, instead of write-only
418access, would potentially allow moving ``~/tmp/`` to a non-readable directory
419and still keep the ability to list the content of ``~/tmp/``.
420
421Layers of file path access rights
422---------------------------------
423
424Each time a thread enforces a ruleset on itself, it updates its Landlock domain
425with a new layer of policy.  This complementary policy is stacked with any
426other rulesets potentially already restricting this thread.  A sandboxed thread
427can then safely add more constraints to itself with a new enforced ruleset.
428
429One policy layer grants access to a file path if at least one of its rules
430encountered on the path grants the access.  A sandboxed thread can only access
431a file path if all its enforced policy layers grant the access as well as all
432the other system access controls (e.g. filesystem DAC, other LSM policies,
433etc.).
434
435Bind mounts and OverlayFS
436-------------------------
437
438Landlock enables restricting access to file hierarchies, which means that these
439access rights can be propagated with bind mounts (cf.
440Documentation/filesystems/sharedsubtree.rst) but not with
441Documentation/filesystems/overlayfs.rst.
442
443A bind mount mirrors a source file hierarchy to a destination.  The destination
444hierarchy is then composed of the exact same files, on which Landlock rules can
445be tied, either via the source or the destination path.  These rules restrict
446access when they are encountered on a path, which means that they can restrict
447access to multiple file hierarchies at the same time, whether these hierarchies
448are the result of bind mounts or not.
449
450An OverlayFS mount point consists of upper and lower layers.  These layers are
451combined in a merge directory, and that merged directory becomes available at
452the mount point.  This merge hierarchy may include files from the upper and
453lower layers, but modifications performed on the merge hierarchy only reflect
454on the upper layer.  From a Landlock policy point of view, all OverlayFS layers
455and merge hierarchies are standalone and each contains their own set of files
456and directories, which is different from bind mounts.  A policy restricting an
457OverlayFS layer will not restrict the resulted merged hierarchy, and vice versa.
458Landlock users should then only think about file hierarchies they want to allow
459access to, regardless of the underlying filesystem.
460
461Inheritance
462-----------
463
464Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain
465restrictions from its parent.  This is similar to seccomp inheritance (cf.
466Documentation/userspace-api/seccomp_filter.rst) or any other LSM dealing with
467task's :manpage:`credentials(7)`.  For instance, one process's thread may apply
468Landlock rules to itself, but they will not be automatically applied to other
469sibling threads (unlike POSIX thread credential changes, cf.
470:manpage:`nptl(7)`).
471
472When a thread sandboxes itself, we have the guarantee that the related security
473policy will stay enforced on all this thread's descendants.  This allows
474creating standalone and modular security policies per application, which will
475automatically be composed between themselves according to their runtime parent
476policies.
477
478Ptrace restrictions
479-------------------
480
481A sandboxed process has less privileges than a non-sandboxed process and must
482then be subject to additional restrictions when manipulating another process.
483To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
484process, a sandboxed process should have a superset of the target process's
485access rights, which means the tracee must be in a sub-domain of the tracer.
486
487IPC scoping
488-----------
489
490Similar to the implicit `Ptrace restrictions`_, we may want to further restrict
491interactions between sandboxes.  Therefore, at ruleset creation time, each
492Landlock domain can restrict the scope for certain operations, so that these
493operations can only reach out to processes within the same Landlock domain or in
494a nested Landlock domain (the "scope").
495
496The operations which can be scoped are:
497
498``LANDLOCK_SCOPE_SIGNAL``
499    This limits the sending of signals to target processes which run within the
500    same or a nested Landlock domain.
501
502``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET``
503    This limits the set of abstract :manpage:`unix(7)` sockets to which we can
504    :manpage:`connect(2)` to socket addresses which were created by a process in
505    the same or a nested Landlock domain.
506
507    A :manpage:`sendto(2)` on a non-connected datagram socket is treated as if
508    it were doing an implicit :manpage:`connect(2)` and will be blocked if the
509    remote end does not stem from the same or a nested Landlock domain.
510
511    A :manpage:`sendto(2)` on a socket which was previously connected will not
512    be restricted.  This works for both datagram and stream sockets.
513
514Scoping does not support exceptions via :manpage:`landlock_add_rule(2)`.  If an
515operation is scoped within a domain, no rules can be added to allow access to
516resources or processes outside of the scope.
517
518Capability and namespace restrictions
519-------------------------------------
520
521``handled_permissions`` declares per-category permissions: each permission
522selects which members of a kernel-defined category (CAP_* capabilities,
523CLONE_NEW* namespace types) the process may use.  Unlike per-object access
524rights (``handled_access_*``) or cross-domain scopes (``scoped``), per-category
525permissions constrain the sandboxed process's own use of these enums; members
526not allowed by a rule are denied by default.
527
528``LANDLOCK_PERMISSION_NAMESPACE_USE`` gates *acquisition of access* to
529namespaces: creation via :manpage:`unshare(2)` / :manpage:`clone(2)`
530/ :manpage:`clone3(2)`, entry via :manpage:`setns(2)`, and fd-reference
531acquisition via :manpage:`open_tree(2)` / :manpage:`fsmount(2)`.  Namespaces
532the process is already a member of when the domain is enforced are implicitly
533allowed (the process could not continue running otherwise); rules describe
534which new namespace types the process may acquire.
535``LANDLOCK_PERMISSION_CAPABILITY_USE`` gates every exercise of a capability
536after the domain is enforced, regardless
537of how the capability was obtained (inherited credentials, ``CLONE_NEWUSER``
538grant, ``setuid``/file-cap-bearing :manpage:`execve(2)`, etc.).  Configuring
539both together restricts what privileges are available *and* the namespaces in
540which they take effect, which matters because user namespace creation has no
541capability check and grants all capabilities within the new namespace: gating
542only one of the two leaves a kernel attack-surface widening path open.  When a
543domain handles both permissions, an operation is allowed only if each
544independently allows it: :manpage:`unshare(2)` with ``CLONE_NEWNET`` then
545requires both a rule allowing ``CLONE_NEWNET`` and a rule allowing
546``CAP_SYS_ADMIN``.
547
548``LANDLOCK_PERMISSION_CAPABILITY_USE`` complements :manpage:`prctl(2)`
549``PR_SET_NO_NEW_PRIVS`` but does not replace it.  ``PR_SET_NO_NEW_PRIVS``
550prevents privilege *acquisition* via :manpage:`execve(2)` (setuid, file
551capability xattrs, privilege-elevating LSM transitions) and is a prerequisite
552for unprivileged Landlock self-sandboxing.
553``LANDLOCK_PERMISSION_CAPABILITY_USE`` restricts *exercise* of capabilities the
554process already holds, including those
555gained via ``CLONE_NEWUSER`` which ``PR_SET_NO_NEW_PRIVS`` does not block.
556Sandboxes typically set both.
557
558Rules are added with ``LANDLOCK_RULE_CAPABILITY`` and &struct
559landlock_capability_attr (each rule lists ``CAP_*`` values to allow), and with
560``LANDLOCK_RULE_NAMESPACE`` and &struct landlock_namespace_attr (each rule
561lists ``CLONE_NEW*`` flags to allow).  Landlock is purely restrictive: it can
562only deny what the traditional check would have allowed, never grant additional
563privileges.
564
565Denials of specific capabilities or namespace types can be excluded from audit
566logging per rule, by listing them in the ``quiet_capabilities`` or
567``quiet_namespace_types`` field of the rule (independently of the allowed
568members).  Quieting requires the permission to be handled, is member-granular,
569and only suppresses the audit record of a denial attributed to the rule's own
570layer; it never changes the denial itself or suppresses its trace event, which
571reports ``logged=0``.  Sandboxes should quiet only specific members known to be
572requested but expected to be denied (for example for compatibility with older
573callers), never a blanket set: a blanket set follows the running kernel's known
574members and would hide surprising or future denials from audit.
575
576Rule bodies silently accept values unknown to the current kernel (capabilities
577above ``CAP_LAST_CAP``, unrecognised ``CLONE_NEW*`` bits): they have no runtime
578effect, so a rule compiled against future kernel headers loads without error on
579older kernels.  Future kernels gain new members denied by default until a rule
580explicitly allows them.
581
582The single ``LANDLOCK_PERMISSION_NAMESPACE_USE`` bit gates every kernel path
583that grants the calling process access to a namespace of the controlled types,
584whether by becoming a member of the namespace or by holding a file descriptor
585that references it.  The covered syscall paths are:
586
587* :manpage:`unshare(2)` with ``CLONE_NEW*``: the caller becomes a member of a
588  newly-created namespace.
589* :manpage:`clone(2)` (or :manpage:`clone3(2)`) with ``CLONE_NEW*``: the
590  child becomes a member of a newly-created namespace.
591* :manpage:`setns(2)`: the caller becomes a member of an existing namespace
592  referenced by file descriptor.
593* :manpage:`open_tree(2)` with ``OPEN_TREE_NAMESPACE``: the caller obtains a
594  file descriptor referring to a newly-created mount namespace.
595* :manpage:`open_tree(2)` with ``OPEN_TREE_CLONE``: the caller obtains a file
596  descriptor referring to a newly-created anonymous mount namespace.
597* :manpage:`fsmount(2)` with ``FSMOUNT_NAMESPACE``: the caller obtains a file
598  descriptor referring to a newly-created mount namespace.
599* :manpage:`fsmount(2)` (default): the caller obtains a file descriptor
600  referring to a newly-created anonymous mount namespace.
601
602Anonymous mount namespaces (created by ``open_tree(OPEN_TREE_CLONE)`` and the
603default :manpage:`fsmount(2)`) are intentionally covered by the bit even though
604the calling process does not become a member of them.  Without this coverage, a
605sandboxed process could combine ``open_tree(OPEN_TREE_CLONE)`` with
606:manpage:`move_mount(2)` to graft mounts from a freshly-allocated mount
607namespace into its current namespace, bypassing a ``CLONE_NEWNS`` restriction
608(these mount operations require ``CAP_SYS_ADMIN``, which a sandboxed process
609typically obtains by first creating a user namespace).
610
611In practice, unprivileged processes first create a user namespace (which
612requires no capability and grants all capabilities within it), then use those
613capabilities to create other namespace types.  All non-user namespace types
614require ``CAP_SYS_ADMIN`` for both creation and :manpage:`setns(2)` entry; mount
615namespace entry additionally requires ``CAP_SYS_CHROOT``.  For
616:manpage:`setns(2)`, capabilities are checked relative to the target namespace,
617so a process in an ancestor user namespace naturally satisfies them; this
618includes joining user namespaces, which requires ``CAP_SYS_ADMIN``.  When
619``LANDLOCK_PERMISSION_CAPABILITY_USE`` is also handled, each of these
620capabilities must be explicitly allowed by a rule.
621
622Creating a non-user namespace always requires ``CAP_SYS_ADMIN``, and
623``LANDLOCK_PERMISSION_CAPABILITY_USE`` (when handled) gates that check
624regardless of which user namespace it targets.  This is true whether the
625namespace is created
626on its own, combined with ``CLONE_NEWUSER`` in a single :manpage:`unshare(2)`
627call, or in a separate :manpage:`unshare(2)` call after a user namespace: in
628all cases the kernel checks ``CAP_SYS_ADMIN`` against the owning user
629namespace, and Landlock denies it unless a rule allows
630``CAP_SYS_ADMIN``.  Combining ``CLONE_NEWUSER`` with another ``CLONE_NEW*``
631flag therefore does not avoid the capability check; only user namespace
632creation itself needs no capability rule.
633
634When creating child user namespaces, it is recommended to also create a
635dedicated Landlock domain with restrictions relevant to each namespace context.
636
637Note that ``LANDLOCK_PERMISSION_CAPABILITY_USE`` restricts the *use* of
638capabilities, not their presence in the process's credential.  Capability sets
639can change
640after a domain is enforced through user namespace entry or :manpage:`capset(2)`;
641privileged sandboxes that did not set ``PR_SET_NO_NEW_PRIVS`` may also gain
642capabilities through :manpage:`execve(2)` of binaries with file capabilities.
643In all cases, :manpage:`capget(2)` will report the credential's capability sets,
644but any denied capability will fail with ``EPERM`` when exercised.  Do not rely
645on :manpage:`capget(2)` to determine whether the policy permits a given
646capability; only the actual operation will return ``EPERM`` upon denial.
647
648Truncating files
649----------------
650
651The operations covered by ``LANDLOCK_ACCESS_FS_WRITE_FILE`` and
652``LANDLOCK_ACCESS_FS_TRUNCATE`` both change the contents of a file and sometimes
653overlap in non-intuitive ways.  It is strongly recommended to always specify
654both of these together (either granting both, or granting none).
655
656A particularly surprising example is :manpage:`creat(2)`.  The name suggests
657that this system call requires the rights to create and write files.  However,
658it also requires the truncate right if an existing file under the same name is
659already present.
660
661It should also be noted that truncating files does not require the
662``LANDLOCK_ACCESS_FS_WRITE_FILE`` right.  Apart from the :manpage:`truncate(2)`
663system call, this can also be done through :manpage:`open(2)` with the flags
664``O_RDONLY | O_TRUNC``.
665
666At the same time, on some filesystems, :manpage:`fallocate(2)` offers a way to
667shorten file contents with ``FALLOC_FL_COLLAPSE_RANGE`` when the file is opened
668for writing, sidestepping the ``LANDLOCK_ACCESS_FS_TRUNCATE`` right.
669
670The truncate right is associated with the opened file (see below).
671
672Rights associated with file descriptors
673---------------------------------------
674
675When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE`` and
676``LANDLOCK_ACCESS_FS_IOCTL_DEV`` rights is associated with the newly created
677file descriptor and will be used for subsequent truncation and ioctl attempts
678using :manpage:`ftruncate(2)` and :manpage:`ioctl(2)`.  The behavior is similar
679to opening a file for reading or writing, where permissions are checked during
680:manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
681:manpage:`write(2)` calls.
682
683As a consequence, it is possible that a process has multiple open file
684descriptors referring to the same file, but Landlock enforces different things
685when operating with these file descriptors.  This can happen when a Landlock
686ruleset gets enforced and the process keeps file descriptors which were opened
687both before and after the enforcement.  It is also possible to pass such file
688descriptors between processes, keeping their Landlock properties, even when some
689of the involved processes do not have an enforced Landlock ruleset.
690
691Compatibility
692=============
693
694Backward and forward compatibility
695----------------------------------
696
697Landlock is designed to be compatible with past and future versions of the
698kernel.  This is achieved thanks to the system call attributes and the
699associated bitflags, particularly the ruleset's ``handled_access_fs``.  Making
700handled access rights explicit enables the kernel and user space to have a clear
701contract with each other.  This is required to make sure sandboxing will not
702get stricter with a system update, which could break applications.
703
704Developers can subscribe to the `Landlock mailing list
705<https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
706test their applications with the latest available features.  In the interest of
707users, and because they may use different kernel versions, it is strongly
708encouraged to follow a best-effort security approach by checking the Landlock
709ABI version at runtime and only enforcing the supported features.
710
711.. _landlock_abi_versions:
712
713Landlock ABI versions
714---------------------
715
716The Landlock ABI version can be read with the sys_landlock_create_ruleset()
717system call:
718
719.. code-block:: c
720
721    int abi;
722
723    abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
724    if (abi < 0) {
725        switch (errno) {
726        case ENOSYS:
727            printf("Landlock is not supported by the current kernel.\n");
728            break;
729        case EOPNOTSUPP:
730            printf("Landlock is currently disabled.\n");
731            break;
732        }
733        return 0;
734    }
735    if (abi >= 2) {
736        printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
737    }
738
739All Landlock kernel interfaces are supported by the first ABI version unless
740explicitly noted in their documentation.
741
742Landlock errata
743---------------
744
745In addition to ABI versions, Landlock provides an errata mechanism to track
746fixes for issues that may affect backwards compatibility or require userspace
747awareness.  The errata bitmask can be queried using:
748
749.. code-block:: c
750
751    int errata;
752
753    errata = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_ERRATA);
754    if (errata < 0) {
755        /* Landlock not available or disabled */
756        return 0;
757    }
758
759The returned value is a bitmask where each bit represents a specific erratum.
760If bit N is set (``errata & (1 << (N - 1))``), then erratum N has been fixed
761in the running kernel.
762
763.. warning::
764
765   **Most applications should NOT check errata.** In 99.9% of cases, checking
766   errata is unnecessary, increases code complexity, and can potentially
767   decrease protection if misused.  For example, disabling the sandbox when an
768   erratum is not fixed could leave the system less secure than using
769   Landlock's best-effort protection.  When in doubt, ignore errata.
770
771.. kernel-doc:: security/landlock/errata/abi-4.h
772    :doc: erratum_1
773
774.. kernel-doc:: security/landlock/errata/abi-6.h
775    :doc: erratum_2
776
777.. kernel-doc:: security/landlock/errata/abi-1.h
778    :doc: erratum_3
779
780.. kernel-doc:: security/landlock/errata/abi-1.h
781    :doc: erratum_4
782
783How to check for errata
784~~~~~~~~~~~~~~~~~~~~~~~
785
786If you determine that your application needs to check for specific errata,
787use this pattern:
788
789.. code-block:: c
790
791    int errata = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_ERRATA);
792    if (errata >= 0) {
793        /* Check for specific erratum (1-indexed) */
794        if (errata & (1 << (erratum_number - 1))) {
795            /* Erratum N is fixed in this kernel */
796        } else {
797            /* Erratum N is NOT fixed - consider implications for your use case */
798        }
799    }
800
801**Important:** Only check errata if your application specifically relies on
802behavior that changed due to the fix.  The fixes generally make Landlock less
803restrictive or more correct, not more restrictive.
804
805Kernel interface
806================
807
808Access rights
809-------------
810
811.. kernel-doc:: include/uapi/linux/landlock.h
812    :identifiers: fs_access net_access scope permission
813
814Creating a new ruleset
815----------------------
816
817.. kernel-doc:: security/landlock/syscalls.c
818    :identifiers: sys_landlock_create_ruleset
819
820.. kernel-doc:: include/uapi/linux/landlock.h
821    :identifiers: landlock_ruleset_attr
822
823Extending a ruleset
824-------------------
825
826.. kernel-doc:: security/landlock/syscalls.c
827    :identifiers: sys_landlock_add_rule
828
829.. kernel-doc:: include/uapi/linux/landlock.h
830    :identifiers: landlock_rule_type landlock_path_beneath_attr
831                  landlock_net_port_attr landlock_capability_attr
832                  landlock_namespace_attr
833
834Enforcing a ruleset
835-------------------
836
837.. kernel-doc:: security/landlock/syscalls.c
838    :identifiers: sys_landlock_restrict_self
839
840Current limitations
841===================
842
843Filesystem topology modification
844--------------------------------
845
846Threads sandboxed with filesystem restrictions cannot modify filesystem
847topology, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`.
848However, :manpage:`chroot(2)` calls are not denied.
849
850Special filesystems
851-------------------
852
853Access to regular files and directories can be restricted by Landlock,
854according to the handled accesses of a ruleset.  However, files that do not
855come from a user-visible filesystem (e.g. pipe, socket), but can still be
856accessed through ``/proc/<pid>/fd/*``, cannot currently be explicitly
857restricted.  Likewise, some special kernel filesystems such as nsfs, which can
858be accessed through ``/proc/<pid>/ns/*``, cannot currently be explicitly
859restricted.  However, thanks to the `ptrace restrictions`_, access to such
860sensitive ``/proc`` files are automatically restricted according to domain
861hierarchies.  Future Landlock evolutions could still enable to explicitly
862restrict such paths with dedicated ruleset flags.
863
864Ruleset layers
865--------------
866
867There is a limit of 16 layers of stacked rulesets.  This can be an issue for a
868task willing to enforce a new ruleset in complement to its 16 inherited
869rulesets.  Once this limit is reached, sys_landlock_restrict_self() returns
870E2BIG.  It is then strongly suggested to carefully build rulesets once in the
871life of a thread, especially for applications able to launch other applications
872that may also want to sandbox themselves (e.g. shells, container managers,
873etc.).
874
875Memory usage
876------------
877
878Kernel memory allocated to create rulesets is accounted and can be restricted
879by the Documentation/admin-guide/cgroup-v1/memory.rst.
880
881IOCTL support
882-------------
883
884The ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right restricts the use of
885:manpage:`ioctl(2)`, but it only applies to *newly opened* device files.  This
886means specifically that pre-existing file descriptors like stdin, stdout and
887stderr are unaffected.
888
889Users should be aware that TTY devices have traditionally permitted to control
890other processes on the same TTY through the ``TIOCSTI`` and ``TIOCLINUX`` IOCTL
891commands.  Both of these require ``CAP_SYS_ADMIN`` on modern Linux systems, but
892the behavior is configurable for ``TIOCSTI``.
893
894On older systems, it is therefore recommended to close inherited TTY file
895descriptors, or to reopen them from ``/proc/self/fd/*`` without the
896``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right, if possible.
897
898Landlock's IOCTL support is coarse-grained at the moment, but may become more
899fine-grained in the future.  Until then, users are advised to establish the
900guarantees that they need through the file hierarchy, by only allowing the
901``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right on files where it is really required.
902
903Previous limitations
904====================
905
906File renaming and linking (ABI < 2)
907-----------------------------------
908
909Because Landlock targets unprivileged access controls, it needs to properly
910handle composition of rules.  Such property also implies rules nesting.
911Properly handling multiple layers of rulesets, each one of them able to
912restrict access to files, also implies inheritance of the ruleset restrictions
913from a parent to its hierarchy.  Because files are identified and restricted by
914their hierarchy, moving or linking a file from one directory to another implies
915propagation of the hierarchy constraints, or restriction of these actions
916according to the potentially lost constraints.  To protect against privilege
917escalations through renaming or linking, and for the sake of simplicity,
918Landlock previously limited linking and renaming to the same directory.
919Starting with the Landlock ABI version 2, it is now possible to securely
920control renaming and linking thanks to the new ``LANDLOCK_ACCESS_FS_REFER``
921access right.
922
923File truncation (ABI < 3)
924-------------------------
925
926File truncation could not be denied before the third Landlock ABI, so it is
927always allowed when using a kernel that only supports the first or second ABI.
928
929Starting with the Landlock ABI version 3, it is now possible to securely control
930truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right.
931
932TCP bind and connect (ABI < 4)
933------------------------------
934
935Starting with the Landlock ABI version 4, it is now possible to restrict TCP
936bind and connect actions to only a set of allowed ports thanks to the new
937``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
938access rights.
939
940Device IOCTL (ABI < 5)
941----------------------
942
943IOCTL operations could not be denied before the fifth Landlock ABI, so
944:manpage:`ioctl(2)` is always allowed when using a kernel that only supports an
945earlier ABI.
946
947Starting with the Landlock ABI version 5, it is possible to restrict the use of
948:manpage:`ioctl(2)` on character and block devices using the new
949``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
950
951Abstract UNIX socket (ABI < 6)
952------------------------------
953
954Starting with the Landlock ABI version 6, it is possible to restrict
955connections to an abstract :manpage:`unix(7)` socket by setting
956``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`` to the ``scoped`` ruleset attribute.
957
958Signal (ABI < 6)
959----------------
960
961Starting with the Landlock ABI version 6, it is possible to restrict
962:manpage:`signal(7)` sending by setting ``LANDLOCK_SCOPE_SIGNAL`` to the
963``scoped`` ruleset attribute.
964
965.. _landlock_log_flags:
966
967Logging (ABI < 7)
968-----------------
969
970Starting with the Landlock ABI version 7, it is possible to control logging of
971Landlock audit events with the ``LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF``,
972``LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON``, and
973``LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF`` flags passed to
974sys_landlock_restrict_self().  These flags control audit record generation.
975Landlock tracepoints are not affected by these flags and always fire when
976enabled, providing an alternative observability channel for debugging and
977monitoring.  See Documentation/admin-guide/LSM/landlock.rst for more
978details on audit and tracepoints, and
979Documentation/trace/events-landlock.rst for the complete trace event
980reference.
981
982Thread synchronization (ABI < 8)
983--------------------------------
984
985Starting with the Landlock ABI version 8, it is now possible to
986enforce Landlock rulesets across all threads of the calling process
987using the ``LANDLOCK_RESTRICT_SELF_TSYNC`` flag passed to
988sys_landlock_restrict_self().
989
990Pathname UNIX sockets (ABI < 9)
991-------------------------------
992
993Starting with the Landlock ABI version 9, it is possible to restrict
994connections to pathname UNIX domain sockets (:manpage:`unix(7)`) using
995the new ``LANDLOCK_ACCESS_FS_RESOLVE_UNIX`` right.
996
997UDP bind, connect and send* (ABI < 10)
998--------------------------------------
999
1000Starting with the Landlock ABI version 10, it is possible to restrict
1001setting the local port of UDP sockets with the
1002``LANDLOCK_ACCESS_NET_BIND_UDP`` right. This includes restricting the
1003ability to trigger autobind of an ephemeral port by the kernel by e.g.
1004sending a first datagram or setting the remote peer of a socket.
1005The ``LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP`` right controls setting the
1006remote port of UDP sockets (via :manpage:`connect(2)`), and sending
1007datagrams to an explicit remote port (ignoring any destination set on
1008UDP sockets, via e.g. :manpage:`sendto(2)`).
1009
1010Quiet rule flag (ABI < 10)
1011--------------------------
1012
1013Starting with the Landlock ABI version 10, it is possible to selectively
1014suppress logs for specific denied accesses on a per-object basis with
1015the ``LANDLOCK_ADD_RULE_QUIET`` flag of sys_landlock_add_rule(), in
1016combination with the ``quiet_access_fs`` and ``quiet_access_net`` fields
1017of struct landlock_ruleset_attr.  It is also now possible to suppress
1018logs for scope accesses via the ``quiet_scoped`` field of struct
1019landlock_ruleset_attr.  The object is marked as quiet within a ruleset
1020when at least one sys_landlock_add_rule() call is made for it with the
1021``LANDLOCK_ADD_RULE_QUIET`` flag, additional add-rule calls for the same
1022object without this flag do not clear it.
1023
1024no_new_privs flag (ABI < 11)
1025----------------------------
1026
1027Starting with the Landlock ABI version 11, sys_landlock_restrict_self()
1028accepts the ``LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS`` flag, which sets the
1029no_new_privs attribute of the calling thread only once the enforcement of
1030the ruleset succeeded: no_new_privs is set if and only if the call
1031succeeds.  This removes the need for a prior :manpage:`prctl(2)`
1032``PR_SET_NO_NEW_PRIVS`` call (or ``CAP_SYS_ADMIN`` use).  When combined
1033with ``LANDLOCK_RESTRICT_SELF_TSYNC``, no_new_privs is set on all threads
1034of the process.  As explained in the tutorial above, leaving no_new_privs
1035unset is risky even when Landlock does not require it.
1036
1037Capability (ABI < 12)
1038---------------------
1039
1040Starting with the Landlock ABI version 12, it is possible to restrict
1041:manpage:`capabilities(7)` with the new ``LANDLOCK_PERMISSION_CAPABILITY_USE``
1042permission flag and ``LANDLOCK_RULE_CAPABILITY`` rule type.
1043
1044Namespace (ABI < 12)
1045--------------------
1046
1047Starting with the Landlock ABI version 12, it is possible to restrict namespace
1048use (see :manpage:`namespaces(7)`) across creation (:manpage:`unshare(2)`,
1049:manpage:`clone(2)`, :manpage:`clone3(2)`), entry (:manpage:`setns(2)`), and
1050fd-reference acquisition (:manpage:`open_tree(2)`, :manpage:`fsmount(2)`) with
1051the new ``LANDLOCK_PERMISSION_NAMESPACE_USE`` permission flag and
1052``LANDLOCK_RULE_NAMESPACE`` rule type.
1053
1054.. _kernel_support:
1055
1056Kernel support
1057==============
1058
1059Build time configuration
1060------------------------
1061
1062Landlock was first introduced in Linux 5.13 but it must be configured at build
1063time with ``CONFIG_SECURITY_LANDLOCK=y``.  Landlock must also be enabled at boot
1064time like other security modules.  The list of security modules enabled by
1065default is set with ``CONFIG_LSM``.  The kernel configuration should then
1066contain ``CONFIG_LSM=landlock,[...]`` with ``[...]``  as the list of other
1067potentially useful security modules for the running system (see the
1068``CONFIG_LSM`` help).
1069
1070Boot time configuration
1071-----------------------
1072
1073If the running kernel does not have ``landlock`` in ``CONFIG_LSM``, then we can
1074enable Landlock by adding ``lsm=landlock,[...]`` to
1075Documentation/admin-guide/kernel-parameters.rst in the boot loader
1076configuration.
1077
1078For example, if the current built-in configuration is:
1079
1080.. code-block:: console
1081
1082    $ zgrep -h "^CONFIG_LSM=" "/boot/config-$(uname -r)" /proc/config.gz 2>/dev/null
1083    CONFIG_LSM="lockdown,yama,integrity,apparmor"
1084
1085...and if the cmdline doesn't contain ``landlock`` either:
1086
1087.. code-block:: console
1088
1089    $ sed -n 's/.*\(\<lsm=\S\+\).*/\1/p' /proc/cmdline
1090    lsm=lockdown,yama,integrity,apparmor
1091
1092...we should configure the boot loader to set a cmdline extending the ``lsm``
1093list with the ``landlock,`` prefix::
1094
1095  lsm=landlock,lockdown,yama,integrity,apparmor
1096
1097After a reboot, we can check that Landlock is up and running by looking at
1098kernel logs:
1099
1100.. code-block:: console
1101
1102    # dmesg | grep landlock || journalctl -kb -g landlock
1103    [    0.000000] Command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
1104    [    0.000000] Kernel command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
1105    [    0.000000] LSM: initializing lsm=lockdown,capability,landlock,yama,integrity,apparmor
1106    [    0.000000] landlock: Up and running.
1107
1108The kernel may be configured at build time to always load the ``lockdown`` and
1109``capability`` LSMs.  In that case, these LSMs will appear at the beginning of
1110the ``LSM: initializing`` log line as well, even if they are not configured in
1111the boot loader.
1112
1113Network support
1114---------------
1115
1116To be able to explicitly allow TCP or UDP operations (e.g., adding a network rule with
1117``LANDLOCK_ACCESS_NET_BIND_TCP``), the kernel must support the TCP/IP protocol suite
1118(``CONFIG_INET=y``).  Otherwise, sys_landlock_add_rule() returns an
1119``EAFNOSUPPORT`` error, which can safely be ignored because this kind of TCP or UDP
1120operation is already not possible.
1121
1122Questions and answers
1123=====================
1124
1125What about user space sandbox managers?
1126---------------------------------------
1127
1128Using user space processes to enforce restrictions on kernel resources can lead
1129to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of
1130the OS code and state
1131<https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_).
1132
1133What about namespaces and containers?
1134-------------------------------------
1135
1136Namespaces can help create sandboxes but they are not designed for
1137access-control and then miss useful features for such use case (e.g. no
1138fine-grained restrictions).  Moreover, their complexity can lead to security
1139issues, especially when untrusted processes can manipulate them (cf.
1140`Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_).
1141
1142How to disable Landlock audit records?
1143--------------------------------------
1144
1145You might want to put in place filters as explained here:
1146Documentation/admin-guide/LSM/landlock.rst
1147
1148Additional documentation
1149========================
1150
1151* Documentation/admin-guide/LSM/landlock.rst
1152* Documentation/trace/events-landlock.rst
1153* Documentation/security/landlock.rst
1154* https://landlock.io
1155
1156.. Links
1157.. _samples/landlock/sandboxer.c:
1158   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.c
1159