Skip to content

Commit 6100f29

Browse files
samasth-norwayl0kod
authored andcommitted
landlock: Add backwards compatibility for restrict flags
Add backwards compatibility handling for the restrict flags introduced in ABI version 7. This is shown as a separate code block (similar to the ruleset_attr handling in the switch statement) because restrict flags are passed to landlock_restrict_self() rather than being part of the ruleset attributes. Also fix misleading description of the /usr rule which incorrectly stated it "only allow[s] reading" when the code actually allows both reading and executing (LANDLOCK_ACCESS_FS_EXECUTE is included in allowed_access). Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com> Reviewed-by: Günther Noack <gnoack3000@gmail.com> Link: https://lore.kernel.org/r/20260128031814.2945394-2-samasth.norway.ananda@oracle.com [mic: Rebased and fixed conflict] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent d90ba69 commit 6100f29

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

Documentation/userspace-api/landlock.rst

Lines changed: 23 additions & 9 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: November 2025
11+
:Date: January 2026
1212

1313
The goal of Landlock is to enable restriction of ambient rights (e.g. global
1414
filesystem or network access) for a set of processes. Because Landlock
@@ -142,11 +142,11 @@ This enables the creation of an inclusive ruleset that will contain our rules.
142142
}
143143
144144
We can now add a new rule to this ruleset thanks to the returned file
145-
descriptor referring to this ruleset. The rule will only allow reading the
146-
file hierarchy ``/usr``. Without another rule, write actions would then be
147-
denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the
148-
``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with this file
149-
descriptor.
145+
descriptor referring to this ruleset. The rule will allow reading and
146+
executing the file hierarchy ``/usr``. Without another rule, write actions
147+
would then be denied by the ruleset. To add ``/usr`` to the ruleset, we open
148+
it with the ``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with
149+
this file descriptor.
150150

151151
.. code-block:: c
152152
@@ -191,10 +191,24 @@ number for a specific action: HTTPS connections.
191191
err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
192192
&net_port, 0);
193193
194+
When passing a non-zero ``flags`` argument to ``landlock_restrict_self()``, a
195+
similar backwards compatibility check is needed for the restrict flags
196+
(see sys_landlock_restrict_self() documentation for available flags):
197+
198+
.. code-block:: c
199+
200+
__u32 restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
201+
if (abi < 7) {
202+
/* Clear logging flags unsupported before ABI 7. */
203+
restrict_flags &= ~(LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF |
204+
LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
205+
LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
206+
}
207+
194208
The next step is to restrict the current thread from gaining more privileges
195209
(e.g. through a SUID binary). We now have a ruleset with the first rule
196-
allowing read access to ``/usr`` while denying all other handled accesses for
197-
the filesystem, and a second rule allowing HTTPS connections.
210+
allowing read and execute access to ``/usr`` while denying all other handled
211+
accesses for the filesystem, and a second rule allowing HTTPS connections.
198212

199213
.. code-block:: c
200214
@@ -208,7 +222,7 @@ The current thread is now ready to sandbox itself with the ruleset.
208222

209223
.. code-block:: c
210224
211-
if (landlock_restrict_self(ruleset_fd, 0)) {
225+
if (landlock_restrict_self(ruleset_fd, restrict_flags)) {
212226
perror("Failed to enforce ruleset");
213227
close(ruleset_fd);
214228
return 1;

0 commit comments

Comments
 (0)