Skip to content

Commit a3746da

Browse files
gnoackl0kod
authored andcommitted
landlock: Document IOCTL support
In the paragraph above the fallback logic, use the shorter phrasing from the landlock(7) man page. Signed-off-by: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20240419161122.2023765-10-gnoack@google.com [mic: Update date, and fix redundant "access"] Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent cd13738 commit a3746da

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

Documentation/userspace-api/landlock.rst

Lines changed: 62 additions & 16 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: October 2023
11+
:Date: April 2024
1212

1313
The goal of Landlock is to enable to restrict ambient rights (e.g. global
1414
filesystem or network access) for a set of processes. Because Landlock
@@ -76,7 +76,8 @@ to be explicit about the denied-by-default access rights.
7676
LANDLOCK_ACCESS_FS_MAKE_BLOCK |
7777
LANDLOCK_ACCESS_FS_MAKE_SYM |
7878
LANDLOCK_ACCESS_FS_REFER |
79-
LANDLOCK_ACCESS_FS_TRUNCATE,
79+
LANDLOCK_ACCESS_FS_TRUNCATE |
80+
LANDLOCK_ACCESS_FS_IOCTL_DEV,
8081
.handled_access_net =
8182
LANDLOCK_ACCESS_NET_BIND_TCP |
8283
LANDLOCK_ACCESS_NET_CONNECT_TCP,
@@ -85,10 +86,10 @@ to be explicit about the denied-by-default access rights.
8586
Because we may not know on which kernel version an application will be
8687
executed, it is safer to follow a best-effort security approach. Indeed, we
8788
should try to protect users as much as possible whatever the kernel they are
88-
using. To avoid binary enforcement (i.e. either all security features or
89-
none), we can leverage a dedicated Landlock command to get the current version
90-
of the Landlock ABI and adapt the handled accesses. Let's check if we should
91-
remove access rights which are only supported in higher versions of the ABI.
89+
using.
90+
91+
To be compatible with older Linux versions, we detect the available Landlock ABI
92+
version, and only use the available subset of access rights:
9293

9394
.. code-block:: c
9495
@@ -114,6 +115,10 @@ remove access rights which are only supported in higher versions of the ABI.
114115
ruleset_attr.handled_access_net &=
115116
~(LANDLOCK_ACCESS_NET_BIND_TCP |
116117
LANDLOCK_ACCESS_NET_CONNECT_TCP);
118+
__attribute__((fallthrough));
119+
case 4:
120+
/* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
121+
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
117122
}
118123
119124
This enables to create an inclusive ruleset that will contain our rules.
@@ -225,6 +230,7 @@ access rights per directory enables to change the location of such directory
225230
without relying on the destination directory access rights (except those that
226231
are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
227232
documentation).
233+
228234
Having self-sufficient hierarchies also helps to tighten the required access
229235
rights to the minimal set of data. This also helps avoid sinkhole directories,
230236
i.e. directories where data can be linked to but not linked from. However,
@@ -318,18 +324,26 @@ It should also be noted that truncating files does not require the
318324
system call, this can also be done through :manpage:`open(2)` with the flags
319325
``O_RDONLY | O_TRUNC``.
320326

321-
When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE``
322-
right is associated with the newly created file descriptor and will be used for
323-
subsequent truncation attempts using :manpage:`ftruncate(2)`. The behavior is
324-
similar to opening a file for reading or writing, where permissions are checked
325-
during :manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
327+
The truncate right is associated with the opened file (see below).
328+
329+
Rights associated with file descriptors
330+
---------------------------------------
331+
332+
When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE`` and
333+
``LANDLOCK_ACCESS_FS_IOCTL_DEV`` rights is associated with the newly created
334+
file descriptor and will be used for subsequent truncation and ioctl attempts
335+
using :manpage:`ftruncate(2)` and :manpage:`ioctl(2)`. The behavior is similar
336+
to opening a file for reading or writing, where permissions are checked during
337+
:manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
326338
:manpage:`write(2)` calls.
327339

328-
As a consequence, it is possible to have multiple open file descriptors for the
329-
same file, where one grants the right to truncate the file and the other does
330-
not. It is also possible to pass such file descriptors between processes,
331-
keeping their Landlock properties, even when these processes do not have an
332-
enforced Landlock ruleset.
340+
As a consequence, it is possible that a process has multiple open file
341+
descriptors referring to the same file, but Landlock enforces different things
342+
when operating with these file descriptors. This can happen when a Landlock
343+
ruleset gets enforced and the process keeps file descriptors which were opened
344+
both before and after the enforcement. It is also possible to pass such file
345+
descriptors between processes, keeping their Landlock properties, even when some
346+
of the involved processes do not have an enforced Landlock ruleset.
333347

334348
Compatibility
335349
=============
@@ -458,6 +472,28 @@ Memory usage
458472
Kernel memory allocated to create rulesets is accounted and can be restricted
459473
by the Documentation/admin-guide/cgroup-v1/memory.rst.
460474

475+
IOCTL support
476+
-------------
477+
478+
The ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right restricts the use of
479+
:manpage:`ioctl(2)`, but it only applies to *newly opened* device files. This
480+
means specifically that pre-existing file descriptors like stdin, stdout and
481+
stderr are unaffected.
482+
483+
Users should be aware that TTY devices have traditionally permitted to control
484+
other processes on the same TTY through the ``TIOCSTI`` and ``TIOCLINUX`` IOCTL
485+
commands. Both of these require ``CAP_SYS_ADMIN`` on modern Linux systems, but
486+
the behavior is configurable for ``TIOCSTI``.
487+
488+
On older systems, it is therefore recommended to close inherited TTY file
489+
descriptors, or to reopen them from ``/proc/self/fd/*`` without the
490+
``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right, if possible.
491+
492+
Landlock's IOCTL support is coarse-grained at the moment, but may become more
493+
fine-grained in the future. Until then, users are advised to establish the
494+
guarantees that they need through the file hierarchy, by only allowing the
495+
``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right on files where it is really required.
496+
461497
Previous limitations
462498
====================
463499

@@ -495,6 +531,16 @@ bind and connect actions to only a set of allowed ports thanks to the new
495531
``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
496532
access rights.
497533

534+
IOCTL (ABI < 5)
535+
---------------
536+
537+
IOCTL operations could not be denied before the fifth Landlock ABI, so
538+
:manpage:`ioctl(2)` is always allowed when using a kernel that only supports an
539+
earlier ABI.
540+
541+
Starting with the Landlock ABI version 5, it is possible to restrict the use of
542+
:manpage:`ioctl(2)` using the new ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
543+
498544
.. _kernel_support:
499545

500546
Kernel support

0 commit comments

Comments
 (0)