@@ -8,13 +8,13 @@ Landlock: unprivileged access control
8
8
=====================================
9
9
10
10
:Author: Mickaël Salaün
11
- :Date: October 2022
11
+ :Date: October 2023
12
12
13
13
The goal of Landlock is to enable to restrict ambient rights (e.g. global
14
- filesystem access) for a set of processes. Because Landlock is a stackable
15
- LSM, it makes possible to create safe security sandboxes as new security layers
16
- in addition to the existing system-wide access-controls. This kind of sandbox
17
- is expected to help mitigate the security impact of bugs or
14
+ filesystem or network access) for a set of processes. Because Landlock
15
+ is a stackable LSM, it makes possible to create safe security sandboxes as new
16
+ security layers in addition to the existing system-wide access-controls. This
17
+ kind of sandbox is expected to help mitigate the security impact of bugs or
18
18
unexpected/malicious behaviors in user space applications. Landlock empowers
19
19
any process, including unprivileged ones, to securely restrict themselves.
20
20
@@ -28,20 +28,34 @@ appropriately <kernel_support>`.
28
28
Landlock rules
29
29
==============
30
30
31
- A Landlock rule describes an action on an object. An object is currently a
32
- file hierarchy, and the related filesystem actions are defined with `access
33
- rights `_. A set of rules is aggregated in a ruleset, which can then restrict
31
+ A Landlock rule describes an action on an object which the process intends to
32
+ perform. A set of rules is aggregated in a ruleset, which can then restrict
34
33
the thread enforcing it, and its future children.
35
34
35
+ The two existing types of rules are:
36
+
37
+ Filesystem rules
38
+ For these rules, the object is a file hierarchy,
39
+ and the related filesystem actions are defined with
40
+ `filesystem access rights `.
41
+
42
+ Network rules (since ABI v4)
43
+ For these rules, the object is a TCP port,
44
+ and the related actions are defined with `network access rights `.
45
+
36
46
Defining and enforcing a security policy
37
47
----------------------------------------
38
48
39
- We first need to define the ruleset that will contain our rules. For this
40
- example, the ruleset will contain rules that only allow read actions, but write
41
- actions will be denied. The ruleset then needs to handle both of these kind of
42
- actions. This is required for backward and forward compatibility (i.e. the
43
- kernel and user space may not know each other's supported restrictions), hence
44
- the need to be explicit about the denied-by-default access rights.
49
+ We first need to define the ruleset that will contain our rules.
50
+
51
+ For this example, the ruleset will contain rules that only allow filesystem
52
+ read actions and establish a specific TCP connection. Filesystem write
53
+ actions and other TCP actions will be denied.
54
+
55
+ The ruleset then needs to handle both these kinds of actions. This is
56
+ required for backward and forward compatibility (i.e. the kernel and user
57
+ space may not know each other's supported restrictions), hence the need
58
+ to be explicit about the denied-by-default access rights.
45
59
46
60
.. code-block :: c
47
61
@@ -62,6 +76,9 @@ the need to be explicit about the denied-by-default access rights.
62
76
LANDLOCK_ACCESS_FS_MAKE_SYM |
63
77
LANDLOCK_ACCESS_FS_REFER |
64
78
LANDLOCK_ACCESS_FS_TRUNCATE,
79
+ .handled_access_net =
80
+ LANDLOCK_ACCESS_NET_BIND_TCP |
81
+ LANDLOCK_ACCESS_NET_CONNECT_TCP,
65
82
};
66
83
67
84
Because we may not know on which kernel version an application will be
@@ -70,9 +87,7 @@ should try to protect users as much as possible whatever the kernel they are
70
87
using. To avoid binary enforcement (i.e. either all security features or
71
88
none), we can leverage a dedicated Landlock command to get the current version
72
89
of the Landlock ABI and adapt the handled accesses. Let's check if we should
73
- remove the ``LANDLOCK_ACCESS_FS_REFER `` or ``LANDLOCK_ACCESS_FS_TRUNCATE ``
74
- access rights, which are only supported starting with the second and third
75
- version of the ABI.
90
+ remove access rights which are only supported in higher versions of the ABI.
76
91
77
92
.. code-block :: c
78
93
@@ -92,6 +107,12 @@ version of the ABI.
92
107
case 2:
93
108
/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
94
109
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
110
+ __attribute__((fallthrough));
111
+ case 3:
112
+ /* Removes network support for ABI < 4 */
113
+ ruleset_attr.handled_access_net &=
114
+ ~(LANDLOCK_ACCESS_NET_BIND_TCP |
115
+ LANDLOCK_ACCESS_NET_CONNECT_TCP);
95
116
}
96
117
97
118
This enables to create an inclusive ruleset that will contain our rules.
@@ -143,10 +164,23 @@ for the ruleset creation, by filtering access rights according to the Landlock
143
164
ABI version. In this example, this is not required because all of the requested
144
165
``allowed_access `` rights are already available in ABI 1.
145
166
146
- We now have a ruleset with one rule allowing read access to ``/usr `` while
147
- denying all other handled accesses for the filesystem. The next step is to
148
- restrict the current thread from gaining more privileges (e.g. thanks to a SUID
149
- binary).
167
+ For network access-control, we can add a set of rules that allow to use a port
168
+ number for a specific action: HTTPS connections.
169
+
170
+ .. code-block :: c
171
+
172
+ struct landlock_net_port_attr net_port = {
173
+ .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
174
+ .port = 443,
175
+ };
176
+
177
+ err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
178
+ &net_port, 0);
179
+
180
+ The next step is to restrict the current thread from gaining more privileges
181
+ (e.g. through a SUID binary). We now have a ruleset with the first rule
182
+ allowing read access to ``/usr `` while denying all other handled accesses for
183
+ the filesystem, and a second rule allowing HTTPS connections.
150
184
151
185
.. code-block :: c
152
186
@@ -355,7 +389,7 @@ Access rights
355
389
-------------
356
390
357
391
.. kernel-doc :: include/uapi/linux/landlock.h
358
- :identifiers: fs_access
392
+ :identifiers: fs_access net_access
359
393
360
394
Creating a new ruleset
361
395
----------------------
@@ -374,6 +408,7 @@ Extending a ruleset
374
408
375
409
.. kernel-doc :: include/uapi/linux/landlock.h
376
410
:identifiers: landlock_rule_type landlock_path_beneath_attr
411
+ landlock_net_port_attr
377
412
378
413
Enforcing a ruleset
379
414
-------------------
@@ -451,6 +486,14 @@ always allowed when using a kernel that only supports the first or second ABI.
451
486
Starting with the Landlock ABI version 3, it is now possible to securely control
452
487
truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE `` access right.
453
488
489
+ Network support (ABI < 4)
490
+ -------------------------
491
+
492
+ Starting with the Landlock ABI version 4, it is now possible to restrict TCP
493
+ bind and connect actions to only a set of allowed ports thanks to the new
494
+ ``LANDLOCK_ACCESS_NET_BIND_TCP `` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP ``
495
+ access rights.
496
+
454
497
.. _kernel_support :
455
498
456
499
Kernel support
@@ -469,6 +512,12 @@ still enable it by adding ``lsm=landlock,[...]`` to
469
512
Documentation/admin-guide/kernel-parameters.rst thanks to the bootloader
470
513
configuration.
471
514
515
+ To be able to explicitly allow TCP operations (e.g., adding a network rule with
516
+ ``LANDLOCK_ACCESS_NET_BIND_TCP ``), the kernel must support TCP
517
+ (``CONFIG_INET=y ``). Otherwise, sys_landlock_add_rule() returns an
518
+ ``EAFNOSUPPORT `` error, which can safely be ignored because this kind of TCP
519
+ operation is already not possible.
520
+
472
521
Questions and answers
473
522
=====================
474
523
0 commit comments