Skip to content

Commit 51442e8

Browse files
BoardzMasterl0kod
authored andcommitted
landlock: Document network support
Describe network access rules for TCP sockets. Add network access example in the tutorial. Add kernel configuration support for network. Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com> Link: https://lore.kernel.org/r/20231026014751.414649-13-konstantin.meskhidze@huawei.com [mic: Update date, and do light cosmetic changes] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 5e990dc commit 51442e8

File tree

1 file changed

+71
-22
lines changed

1 file changed

+71
-22
lines changed

Documentation/userspace-api/landlock.rst

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ Landlock: unprivileged access control
88
=====================================
99

1010
:Author: Mickaël Salaün
11-
:Date: October 2022
11+
:Date: October 2023
1212

1313
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
1818
unexpected/malicious behaviors in user space applications. Landlock empowers
1919
any process, including unprivileged ones, to securely restrict themselves.
2020

@@ -28,20 +28,34 @@ appropriately <kernel_support>`.
2828
Landlock rules
2929
==============
3030

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
3433
the thread enforcing it, and its future children.
3534

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+
3646
Defining and enforcing a security policy
3747
----------------------------------------
3848

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.
4559

4660
.. code-block:: c
4761
@@ -62,6 +76,9 @@ the need to be explicit about the denied-by-default access rights.
6276
LANDLOCK_ACCESS_FS_MAKE_SYM |
6377
LANDLOCK_ACCESS_FS_REFER |
6478
LANDLOCK_ACCESS_FS_TRUNCATE,
79+
.handled_access_net =
80+
LANDLOCK_ACCESS_NET_BIND_TCP |
81+
LANDLOCK_ACCESS_NET_CONNECT_TCP,
6582
};
6683
6784
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
7087
using. To avoid binary enforcement (i.e. either all security features or
7188
none), we can leverage a dedicated Landlock command to get the current version
7289
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.
7691

7792
.. code-block:: c
7893
@@ -92,6 +107,12 @@ version of the ABI.
92107
case 2:
93108
/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
94109
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);
95116
}
96117
97118
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
143164
ABI version. In this example, this is not required because all of the requested
144165
``allowed_access`` rights are already available in ABI 1.
145166

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.
150184

151185
.. code-block:: c
152186
@@ -355,7 +389,7 @@ Access rights
355389
-------------
356390

357391
.. kernel-doc:: include/uapi/linux/landlock.h
358-
:identifiers: fs_access
392+
:identifiers: fs_access net_access
359393

360394
Creating a new ruleset
361395
----------------------
@@ -374,6 +408,7 @@ Extending a ruleset
374408

375409
.. kernel-doc:: include/uapi/linux/landlock.h
376410
:identifiers: landlock_rule_type landlock_path_beneath_attr
411+
landlock_net_port_attr
377412

378413
Enforcing a ruleset
379414
-------------------
@@ -451,6 +486,14 @@ always allowed when using a kernel that only supports the first or second ABI.
451486
Starting with the Landlock ABI version 3, it is now possible to securely control
452487
truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right.
453488

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+
454497
.. _kernel_support:
455498

456499
Kernel support
@@ -469,6 +512,12 @@ still enable it by adding ``lsm=landlock,[...]`` to
469512
Documentation/admin-guide/kernel-parameters.rst thanks to the bootloader
470513
configuration.
471514

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+
472521
Questions and answers
473522
=====================
474523

0 commit comments

Comments
 (0)