Skip to content

Commit 6f59abf

Browse files
committed
landlock: Document LANDLOCK_ACCESS_FS_REFER and ABI versioning
Add LANDLOCK_ACCESS_FS_REFER in the example and properly check to only use it if the current kernel support it thanks to the Landlock ABI version. Move the file renaming and linking limitation to a new "Previous limitations" section. Improve documentation about the backward and forward compatibility, including the rational for ruleset's handled_access_fs. Update the document date. Reviewed-by: Paul Moore <paul@paul-moore.com> Signed-off-by: Mickaël Salaün <mic@digikod.net> Link: https://lore.kernel.org/r/20220506161102.525323-11-mic@digikod.net
1 parent 76b902f commit 6f59abf

1 file changed

Lines changed: 106 additions & 20 deletions

File tree

Documentation/userspace-api/landlock.rst

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Landlock: unprivileged access control
88
=====================================
99

1010
:Author: Mickaël Salaün
11-
:Date: March 2021
11+
:Date: May 2022
1212

1313
The goal of Landlock is to enable to restrict ambient rights (e.g. global
1414
filesystem access) for a set of processes. Because Landlock is a stackable
@@ -29,14 +29,15 @@ the thread enforcing it, and its future children.
2929
Defining and enforcing a security policy
3030
----------------------------------------
3131

32-
We first need to create the ruleset that will contain our rules. For this
32+
We first need to define the ruleset that will contain our rules. For this
3333
example, the ruleset will contain rules that only allow read actions, but write
3434
actions will be denied. The ruleset then needs to handle both of these kind of
35-
actions.
35+
actions. This is required for backward and forward compatibility (i.e. the
36+
kernel and user space may not know each other's supported restrictions), hence
37+
the need to be explicit about the denied-by-default access rights.
3638

3739
.. code-block:: c
3840
39-
int ruleset_fd;
4041
struct landlock_ruleset_attr ruleset_attr = {
4142
.handled_access_fs =
4243
LANDLOCK_ACCESS_FS_EXECUTE |
@@ -51,9 +52,34 @@ actions.
5152
LANDLOCK_ACCESS_FS_MAKE_SOCK |
5253
LANDLOCK_ACCESS_FS_MAKE_FIFO |
5354
LANDLOCK_ACCESS_FS_MAKE_BLOCK |
54-
LANDLOCK_ACCESS_FS_MAKE_SYM,
55+
LANDLOCK_ACCESS_FS_MAKE_SYM |
56+
LANDLOCK_ACCESS_FS_REFER,
5557
};
5658
59+
Because we may not know on which kernel version an application will be
60+
executed, it is safer to follow a best-effort security approach. Indeed, we
61+
should try to protect users as much as possible whatever the kernel they are
62+
using. To avoid binary enforcement (i.e. either all security features or
63+
none), we can leverage a dedicated Landlock command to get the current version
64+
of the Landlock ABI and adapt the handled accesses. Let's check if we should
65+
remove the `LANDLOCK_ACCESS_FS_REFER` access right which is only supported
66+
starting with the second version of the ABI.
67+
68+
.. code-block:: c
69+
70+
int abi;
71+
72+
abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
73+
if (abi < 2) {
74+
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
75+
}
76+
77+
This enables to create an inclusive ruleset that will contain our rules.
78+
79+
.. code-block:: c
80+
81+
int ruleset_fd;
82+
5783
ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
5884
if (ruleset_fd < 0) {
5985
perror("Failed to create a ruleset");
@@ -92,6 +118,11 @@ descriptor.
92118
return 1;
93119
}
94120
121+
It may also be required to create rules following the same logic as explained
122+
for the ruleset creation, by filtering access rights according to the Landlock
123+
ABI version. In this example, this is not required because
124+
`LANDLOCK_ACCESS_FS_REFER` is not allowed by any rule.
125+
95126
We now have a ruleset with one rule allowing read access to ``/usr`` while
96127
denying all other handled accesses for the filesystem. The next step is to
97128
restrict the current thread from gaining more privileges (e.g. thanks to a SUID
@@ -192,6 +223,56 @@ To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
192223
process, a sandboxed process should have a subset of the target process rules,
193224
which means the tracee must be in a sub-domain of the tracer.
194225

226+
Compatibility
227+
=============
228+
229+
Backward and forward compatibility
230+
----------------------------------
231+
232+
Landlock is designed to be compatible with past and future versions of the
233+
kernel. This is achieved thanks to the system call attributes and the
234+
associated bitflags, particularly the ruleset's `handled_access_fs`. Making
235+
handled access right explicit enables the kernel and user space to have a clear
236+
contract with each other. This is required to make sure sandboxing will not
237+
get stricter with a system update, which could break applications.
238+
239+
Developers can subscribe to the `Landlock mailing list
240+
<https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
241+
test their applications with the latest available features. In the interest of
242+
users, and because they may use different kernel versions, it is strongly
243+
encouraged to follow a best-effort security approach by checking the Landlock
244+
ABI version at runtime and only enforcing the supported features.
245+
246+
Landlock ABI versions
247+
---------------------
248+
249+
The Landlock ABI version can be read with the sys_landlock_create_ruleset()
250+
system call:
251+
252+
.. code-block:: c
253+
254+
int abi;
255+
256+
abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
257+
if (abi < 0) {
258+
switch (errno) {
259+
case ENOSYS:
260+
printf("Landlock is not supported by the current kernel.\n");
261+
break;
262+
case EOPNOTSUPP:
263+
printf("Landlock is currently disabled.\n");
264+
break;
265+
}
266+
return 0;
267+
}
268+
if (abi >= 2) {
269+
printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
270+
}
271+
272+
The following kernel interfaces are implicitly supported by the first ABI
273+
version. Features only supported from a specific version are explicitly marked
274+
as such.
275+
195276
Kernel interface
196277
================
197278

@@ -228,21 +309,6 @@ Enforcing a ruleset
228309
Current limitations
229310
===================
230311

231-
File renaming and linking
232-
-------------------------
233-
234-
Because Landlock targets unprivileged access controls, it is needed to properly
235-
handle composition of rules. Such property also implies rules nesting.
236-
Properly handling multiple layers of ruleset, each one of them able to restrict
237-
access to files, also implies to inherit the ruleset restrictions from a parent
238-
to its hierarchy. Because files are identified and restricted by their
239-
hierarchy, moving or linking a file from one directory to another implies to
240-
propagate the hierarchy constraints. To protect against privilege escalations
241-
through renaming or linking, and for the sake of simplicity, Landlock currently
242-
limits linking and renaming to the same directory. Future Landlock evolutions
243-
will enable more flexibility for renaming and linking, with dedicated ruleset
244-
flags.
245-
246312
Filesystem topology modification
247313
--------------------------------
248314

@@ -281,6 +347,26 @@ Memory usage
281347
Kernel memory allocated to create rulesets is accounted and can be restricted
282348
by the Documentation/admin-guide/cgroup-v1/memory.rst.
283349

350+
Previous limitations
351+
====================
352+
353+
File renaming and linking (ABI 1)
354+
---------------------------------
355+
356+
Because Landlock targets unprivileged access controls, it needs to properly
357+
handle composition of rules. Such property also implies rules nesting.
358+
Properly handling multiple layers of rulesets, each one of them able to
359+
restrict access to files, also implies inheritance of the ruleset restrictions
360+
from a parent to its hierarchy. Because files are identified and restricted by
361+
their hierarchy, moving or linking a file from one directory to another implies
362+
propagation of the hierarchy constraints, or restriction of these actions
363+
according to the potentially lost constraints. To protect against privilege
364+
escalations through renaming or linking, and for the sake of simplicity,
365+
Landlock previously limited linking and renaming to the same directory.
366+
Starting with the Landlock ABI version 2, it is now possible to securely
367+
control renaming and linking thanks to the new `LANDLOCK_ACCESS_FS_REFER`
368+
access right.
369+
284370
Questions and answers
285371
=====================
286372

0 commit comments

Comments
 (0)