xref: /linux/Documentation/security/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 © 2026 Cloudflare, Inc.
5
6==================================
7Landlock LSM: kernel documentation
8==================================
9
10:Author: Mickaël Salaün
11:Date: October 2026
12
13Landlock's goal is to create scoped access-control (i.e. sandboxing).  To
14harden a whole system, this feature should be available to any process,
15including unprivileged ones.  Because such a process may be compromised or
16backdoored (i.e. untrusted), Landlock's features must be safe to use from the
17kernel and other processes point of view.  Landlock's interface must therefore
18expose a minimal attack surface.
19
20Landlock is designed to be usable by unprivileged processes while following the
21system security policy enforced by other access control mechanisms (e.g. DAC,
22LSM).  A Landlock rule shall not interfere with other access-controls enforced
23on the system, only add more restrictions.
24
25Any user can enforce Landlock rulesets on their processes.  They are merged and
26evaluated against inherited rulesets in a way that ensures that only more
27constraints can be added.
28
29User space documentation can be found here:
30Documentation/userspace-api/landlock.rst.
31
32Guiding principles for safe access controls
33===========================================
34
35* A Landlock rule shall be focused on access control on kernel objects instead
36  of syscall filtering (i.e. syscall arguments), which is the purpose of
37  seccomp-bpf.
38* To avoid multiple kinds of side-channel attacks (e.g. leak of security
39  policies, CPU-based attacks), Landlock rules shall not be able to
40  programmatically communicate with user space.
41* Kernel access check shall not slow down access request from unsandboxed
42  processes.
43* Computation related to Landlock operations (e.g. enforcing a ruleset) shall
44  only impact the processes requesting them.
45* Resources (e.g. file descriptors) directly obtained from the kernel by a
46  sandboxed process shall retain their scoped accesses (at the time of resource
47  acquisition) whatever process uses them.
48  Cf. `File descriptor access rights`_.
49* Access denials shall be logged according to system and Landlock domain
50  configurations.  Log entries must contain information about the cause of the
51  denial and the owner of the related security policy.  Such log generation
52  should have a negligible performance and memory impact on allowed requests.
53
54Design choices
55==============
56
57Inode access rights
58-------------------
59
60All access rights are tied to an inode and what can be accessed through it.
61Reading the content of a directory does not imply to be allowed to read the
62content of a listed inode.  Indeed, a file name is local to its parent
63directory, and an inode can be referenced by multiple file names thanks to
64(hard) links.  Being able to unlink a file only has a direct impact on the
65directory, not the unlinked inode.  This is the reason why
66``LANDLOCK_ACCESS_FS_REMOVE_FILE`` or ``LANDLOCK_ACCESS_FS_REFER`` are not
67allowed to be tied to files but only to directories.
68
69File descriptor access rights
70-----------------------------
71
72Access rights are checked and tied to file descriptors at open time.  The
73underlying principle is that equivalent sequences of operations should lead to
74the same results, when they are executed under the same Landlock domain.
75
76Taking the ``LANDLOCK_ACCESS_FS_TRUNCATE`` right as an example, it may be
77allowed to open a file for writing without being allowed to
78:manpage:`ftruncate` the resulting file descriptor if the related file
79hierarchy doesn't grant that access right.  The following sequences of
80operations have the same semantic and should then have the same result:
81
82* ``truncate(path);``
83* ``int fd = open(path, O_WRONLY); ftruncate(fd); close(fd);``
84
85Similarly to file access modes (e.g. ``O_RDWR``), Landlock access rights
86attached to file descriptors are retained even if they are passed between
87processes (e.g. through a Unix domain socket).  Such access rights will then be
88enforced even if the receiving process is not sandboxed by Landlock.  Indeed,
89this is required to keep access controls consistent over the whole system, and
90this avoids unattended bypasses through file descriptor passing (i.e. confused
91deputy attack).
92
93.. _scoped-flags-interaction:
94
95Interaction between scoped flags and other access rights
96--------------------------------------------------------
97
98The ``scoped`` flags in &struct landlock_ruleset_attr restrict the
99use of *outgoing* IPC from the created Landlock domain, while they
100permit reaching out to IPC endpoints *within* the created Landlock
101domain.
102
103In the future, scoped flags *may* interact with other access rights,
104e.g. so that abstract UNIX sockets can be allow-listed by name, or so
105that signals can be allow-listed by signal number or target process.
106
107When introducing ``LANDLOCK_ACCESS_FS_RESOLVE_UNIX``, we defined it to
108implicitly have the same scoping semantics as a
109``LANDLOCK_SCOPE_PATHNAME_UNIX_SOCKET`` flag would have: connecting to
110UNIX sockets within the same domain (where
111``LANDLOCK_ACCESS_FS_RESOLVE_UNIX`` is used) is unconditionally
112allowed.
113
114The reasoning is:
115
116* Like other IPC mechanisms, connecting to named UNIX sockets in the
117  same domain should be expected and harmless.  (If needed, users can
118  further refine their Landlock policies with nested domains or by
119  restricting ``LANDLOCK_ACCESS_FS_MAKE_SOCK``.)
120* We reserve the option to still introduce
121  ``LANDLOCK_SCOPE_PATHNAME_UNIX_SOCKET`` in the future.  (This would
122  be useful if we wanted to have a Landlock rule to permit IPC access
123  to other Landlock domains.)
124* But we can postpone the point in time when users have to deal with
125  two interacting flags visible in the userspace API.  (In particular,
126  it is possible that it won't be needed in practice, in which case we
127  can avoid the second flag altogether.)
128* If we *do* introduce ``LANDLOCK_SCOPE_PATHNAME_UNIX_SOCKET`` in the
129  future, setting this scoped flag in a ruleset does *not reduce* the
130  restrictions, because access within the same scope is already
131  allowed based on ``LANDLOCK_ACCESS_FS_RESOLVE_UNIX``.
132
133Composability with user namespaces
134----------------------------------
135
136Landlock domain-based scoping and the kernel's user-namespace-based capability
137scoping enforce isolation over independent hierarchies.  Landlock checks domain
138ancestry; the kernel's ``ns_capable()`` checks user namespace ancestry.  These
139hierarchies are orthogonal: Landlock enforcement is deterministic with respect
140to its own configuration, regardless of namespace or capability state, and vice
141versa.  This orthogonality is a design invariant that must hold for all Landlock
142access controls.
143
144Design philosophy
145-----------------
146
147Landlock's goal is to restrict a sandboxed process's access to three kinds of
148resources: data (files, sockets, pipes), other processes (signals, ptrace), and
149kernel-internal resources whose use widens the kernel attack surface
150(capabilities, namespace types).  Each access right or permission gates one or
151more operations that grant such access; restricting the operations is how
152Landlock restricts the underlying access.
153
154When designing a new access control, identify the protected resource kind
155first (data, processes, or kernel-internal resources).  The operations to
156restrict follow from the protected resource, by identifying which kernel code
157paths grant access to the resource and at which place in the code the access to
158the resource can be gated.  Do not design a permission around
159"restrict the unshare(2) syscall" or similar mechanism-centric framings; design
160it around "restrict the process from acquiring access to namespace types" (the
161protected resource), letting the operation set follow.
162
163Ruleset restriction models
164--------------------------
165
166Landlock provides three restriction models that differ in how rules identify the
167resource being restricted.
168
169In general, the ``struct landlock_ruleset_attr`` specifies the operations to be
170denied by default under the enforced policy.  The *rules* added to the ruleset
171define the exceptions to these restrictions, allow-listing specific conditions
172under which these operations are still permitted.
173
174Per-object access rights (``handled_access_*``)
175~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176
177Per-object access rights control operations on a specific resource instance,
178identified in the rule key by a value drawn from an open-ended space: a file
179hierarchy referenced by ``parent_fd``, or a network port identified by its
18016-bit number.
181
182Each ``handled_access_*`` field declares a set of access rights, operations
183which are to be denied by default once the ruleset is enforced.
184
185The rule body declares which of the multiple distinct operations on that object
186instance are allowed (open, read, write, truncate; bind, connect).
187
188Operations are grouped by object type in the respective ``handled_access_*``
189field: a new operation on an existing type extends that field, and a new object
190type gets its own ``handled_access_*`` field.
191
192Per-category permissions (``handled_permissions``)
193~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194
195Per-category permissions control the process's exercise of category members,
196where the category is a small kernel-defined enumeration (a Linux capability
197number ``CAP_*``, a namespace type ``CLONE_NEW*``).  Unlike per-object access
198rights, which restrict specific operations on a single resource instance,
199per-category permissions gate the prerequisite operation itself (exercising a
200capability, acquiring access to a namespace), so gating it transitively covers a
201broad set of downstream operations (e.g. denying ``CAP_SYS_ADMIN`` blocks all
202admin operations such as mounts).
203
204These category members are the LSM-level access-control objects (the entities
205the process is authorized against) even though they are enum values rather than
206externally-instantiated kernel data structures.  Per-category permissions apply
207where the controlled operation collapses to "may the process use this category
208member at all" (use a capability; acquire access to a namespace), so the rule
209body lists which category members the process may exercise.
210
211Each ``LANDLOCK_PERMISSION_*`` flag maps to its own rule type and covers every
212kernel path that exercises a member.  When a ruleset handles a permission, all
213uses of category members are denied unless explicitly allowed by a rule.
214
215Logging of a denied member can be suppressed at the same granularity as the
216restriction itself: per rule and per member.  A rule names the members whose
217denial should not be audited; suppression only affects the audit record, never
218the denial or its trace event, which reports ``logged=0``.  Suppression only
219applies when the layer owning the rule is the one that denied the member.
220
221See Documentation/userspace-api/landlock.rst for the concrete syscall paths
222covered by each permission.
223
224The category enum is owned by the corresponding kernel subsystem (capabilities,
225namespaces, etc.).  Userspace policy authors query category member availability
226via the relevant non-Landlock interfaces:
227
228* For capabilities: ``<linux/capability.h>``,
229  ``/proc/sys/kernel/cap_last_cap``, ``prctl(PR_CAPBSET_READ)``.
230* For namespaces: ``<linux/sched.h>``, ``/proc/$$/ns/*``,
231  :manpage:`unshare(2)` runtime probe.
232
233The Landlock ABI version does not encode this availability; ABI versioning
234describes which Landlock features (rule types, access rights, scopes,
235permissions) the kernel implements, not which category members the kernel knows
236about.
237
238Forward compatibility for new category members follows a simple rule set:
239
240* New members in future kernels are automatically denied: rules whitelist
241  specific values, and a member not in any rule is denied.
242* Kernel-side compatibility for split categories is handled by the owning
243  subsystem (e.g., when ``CAP_BPF`` was split from ``CAP_SYS_ADMIN``, either
244  capability became sufficient for the affected operations, so a rule allowing
245  ``CAP_SYS_ADMIN`` continues to allow operations now gated by
246  ``CAP_SYS_ADMIN || CAP_BPF``).
247* Unknown values in the rule body are silently accepted rather than rejected.
248  Rejecting them would tie Landlock policy semantics to the running kernel's
249  category-member set: a rule built against future headers would fail to load
250  on older kernels, forcing policy authors to know each kernel's enumeration.
251  Acceptance is fail-safe in both directions: a rule referring to a value the
252  running kernel does not yet know has no effect (deny-by-default still applies
253  to that operation), and a rule written against future headers loads
254  identically across kernels so the same policy keeps the same restrictions.
255  When a value becomes real on a future kernel, the policy activates as written
256  by the author.
257* In contrast, unknown ``LANDLOCK_PERMISSION_*`` flags in
258  ``handled_permissions`` are rejected (``-EINVAL``), since Landlock owns that
259  bit space.
260
261Cross-domain scopes (``scoped``)
262~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263
264Scopes restrict **cross-domain interactions** categorically, without rules.
265Setting a scope flag (e.g.  ``LANDLOCK_SCOPE_SIGNAL``) denies the operation to
266targets outside the Landlock domain or its children.  Like per-category
267permissions, scopes provide complete coverage of the controlled operation.
268
269Choosing a model for a new feature
270~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
271
272* If the new feature controls operations on resource objects supplied by the
273  sandbox author, extend or add a per-object access right
274  (``handled_access_*``).
275* If the new feature controls a per-category operation gated by an enum (a
276  Linux capability, a namespace type, a socket family, etc.), use a
277  per-category permission (``handled_permissions``).  When several such enums
278  could classify the operation, prefer the enum the originating subsystem
279  already uses for capability/access checks (e.g. ``CAP_*`` for ``capable()``
280  hooks, ``CLONE_NEW*`` for namespace hooks).
281* When an operation is gated by multiple kernel-defined enums (a classic
282  example being ``CAP_SYS_ADMIN`` plus a ``CLONE_NEW*`` flag for non-user
283  namespace creation), define one per-category permission per enum dimension.
284  Sandbox authors handle each dimension's permission in ``handled_permissions``
285  and add rules for each; the kernel enforces each dimension at its own LSM
286  hook.  ``LANDLOCK_PERMISSION_NAMESPACE_USE`` and
287  ``LANDLOCK_PERMISSION_CAPABILITY_USE`` follow this pattern.
288* If the new feature restricts a categorical cross-domain interaction with no
289  per-target granularity, use a cross-domain scope (``scoped``).
290* For all three models, confirm a single LSM hook (or small set of related
291  hooks) covers every kernel path that exercises the operation.
292
293Tests
294=====
295
296Userspace tests for backward compatibility, ptrace restrictions and filesystem
297support can be found here: `tools/testing/selftests/landlock/`_.
298
299Kernel structures
300=================
301
302Object
303------
304
305.. kernel-doc:: security/landlock/object.h
306    :identifiers:
307
308Filesystem
309----------
310
311.. kernel-doc:: security/landlock/fs.h
312    :identifiers:
313
314Namespace
315---------
316
317.. kernel-doc:: security/landlock/ns.h
318    :identifiers:
319
320Capability
321----------
322
323.. kernel-doc:: security/landlock/cap.h
324    :identifiers:
325
326Process credential
327------------------
328
329.. kernel-doc:: security/landlock/cred.h
330    :identifiers:
331
332Ruleset and domain
333------------------
334
335A domain is a read-only ruleset tied to a set of subjects (i.e. tasks'
336credentials).  Each time a ruleset is enforced on a task, the current domain is
337duplicated and the ruleset is imported as a new layer of rules in the new
338domain.  Indeed, once in a domain, each rule is tied to a layer level.  To
339grant access to an object, at least one rule of each layer must allow the
340requested action on the object.  A task can then only transit to a new domain
341that is the intersection of the constraints from the current domain and those
342of a ruleset provided by the task.
343
344The definition of a subject is implicit for a task sandboxing itself, which
345makes the reasoning much easier and helps avoid pitfalls.
346
347.. kernel-doc:: security/landlock/ruleset.h
348    :identifiers:
349
350.. kernel-doc:: security/landlock/domain.h
351    :identifiers:
352
353Denial logging
354==============
355
356Access denials are logged through two independent channels: audit
357records and tracepoints.  Both are managed by the common denial
358framework in ``log.c``, compiled under ``CONFIG_SECURITY_LANDLOCK_LOG``
359(automatically selected by ``CONFIG_AUDIT`` or ``CONFIG_TRACEPOINTS``).
360
361Audit records respect audit configuration, the domain's Landlock log
362flags, and ``LANDLOCK_LOG_DISABLED``.  Tracepoints fire unconditionally,
363independent of these settings.  The denial counter (``num_denials``) is
364always incremented regardless of logging configuration.
365
366Each denial tracepoint carries a ``logged`` field reporting the
367audit-logging verdict: whether the denial would be written to the audit
368log if audit were configured and active.  This verdict is the same
369whether or not the kernel is built with audit support, so a
370tracepoints-only build reports the selection audit would make.  A quiet
371rule (``LANDLOCK_ADD_RULE_QUIET`` with the access in the ``quiet_*``
372fields of ``struct landlock_ruleset_attr``) suppresses logging by
373setting ``logged=0`` the same way, as do the per-member ``quiet_*``
374masks of capability and namespace rules.
375
376See Documentation/admin-guide/LSM/landlock.rst for audit record format,
377tracepoint usage, and filtering examples.
378
379.. kernel-doc:: security/landlock/log.h
380    :identifiers:
381
382Trace events
383------------
384
385See Documentation/trace/events-landlock.rst for trace event usage and format
386details; the full event reference lives there and is not duplicated here.
387
388Additional documentation
389========================
390
391* Documentation/userspace-api/landlock.rst
392* Documentation/admin-guide/LSM/landlock.rst
393* Documentation/trace/events-landlock.rst
394* https://landlock.io
395
396.. Links
397.. _tools/testing/selftests/landlock/:
398   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/landlock/
399