1/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2/*
3 * Landlock - User space API
4 *
5 * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2018-2020 ANSSI
7 * Copyright © 2021-2025 Microsoft Corporation
8 */
9
10#ifndef _LINUX_LANDLOCK_H
11#define _LINUX_LANDLOCK_H
12
13#include <linux/types.h>
14
15/**
16 * struct landlock_ruleset_attr - Ruleset definition.
17 *
18 * Argument of sys_landlock_create_ruleset().
19 *
20 * This structure defines a set of *handled access rights*, a set of actions on
21 * different object types, which should be denied by default when the ruleset is
22 * enacted. Vice versa, access rights that are not specifically listed here are
23 * not going to be denied by this ruleset when it is enacted.
24 *
25 * For historical reasons, the %LANDLOCK_ACCESS_FS_REFER right is always denied
26 * by default, even when its bit is not set in @handled_access_fs. In order to
27 * add new rules with this access right, the bit must still be set explicitly
28 * (cf. `Filesystem flags`_).
29 *
30 * The explicit listing of *handled access rights* is required for backwards
31 * compatibility reasons. In most use cases, processes that use Landlock will
32 * *handle* a wide range or all access rights that they know about at build time
33 * (and that they have tested with a kernel that supported them all).
34 *
35 * @quiet_access_fs and @quiet_access_net are bitmasks of actions for which a
36 * denial by this layer will not trigger a log if the corresponding object (or
37 * its children, for filesystem rules) is marked with the "quiet" bit via
38 * %LANDLOCK_ADD_RULE_QUIET, even if logging would normally take place per
39 * landlock_restrict_self() flags. @quiet_scoped is similar, except that it
40 * does not require marking any objects as quiet - if the ruleset is created
41 * with any bits set in @quiet_scoped, then denial of such scoped resources will
42 * not trigger any log. These 3 fields are available since Landlock ABI version
43 * 10.
44 *
45 * @quiet_access_fs, @quiet_access_net and @quiet_scoped must be a subset of
46 * @handled_access_fs, @handled_access_net and @scoped respectively.
47 *
48 * This structure can grow in future Landlock versions.
49 */
50struct landlock_ruleset_attr {
51 /**
52 * @handled_access_fs: Bitmask of handled filesystem actions
53 * (cf. `Filesystem flags`_).
54 */
55 __u64 handled_access_fs;
56 /**
57 * @handled_access_net: Bitmask of handled network actions (cf. `Network
58 * flags`_).
59 */
60 __u64 handled_access_net;
61 /**
62 * @scoped: Bitmask of scopes (cf. `Scope flags`_)
63 * restricting a Landlock domain from accessing outside
64 * resources (e.g. IPCs).
65 */
66 __u64 scoped;
67 /**
68 * @quiet_access_fs: Bitmask of filesystem actions which should not be
69 * logged if per-object quiet flag is set.
70 */
71 __u64 quiet_access_fs;
72 /**
73 * @quiet_access_net: Bitmask of network actions which should not be
74 * logged if per-object quiet flag is set.
75 */
76 __u64 quiet_access_net;
77 /**
78 * @quiet_scoped: Bitmask of scoped actions which should not be logged.
79 */
80 __u64 quiet_scoped;
81};
82
83/**
84 * DOC: landlock_create_ruleset_flags
85 *
86 * **Flags**
87 *
88 * %LANDLOCK_CREATE_RULESET_VERSION
89 * Get the highest supported Landlock ABI version (starting at 1).
90 *
91 * %LANDLOCK_CREATE_RULESET_ERRATA
92 * Get a bitmask of fixed issues for the current Landlock ABI version.
93 */
94/* clang-format off */
95#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
96#define LANDLOCK_CREATE_RULESET_ERRATA (1U << 1)
97/* clang-format on */
98
99/**
100 * DOC: landlock_add_rule_flags
101 *
102 * **Flags**
103 *
104 * %LANDLOCK_ADD_RULE_QUIET
105 * Together with the quiet_* fields in struct landlock_ruleset_attr,
106 * this flag controls whether Landlock will log audit messages when
107 * access to the objects covered by this rule is denied by this layer.
108 *
109 * If logging is enabled, when Landlock denies an access, it will
110 * suppress the log if all of the following are true:
111 *
112 * - this layer is the innermost layer that denied the access;
113 * - all accesses denied by this layer are part of the quiet_* fields
114 * in the related struct landlock_ruleset_attr;
115 * - the object (or one of its parents, for filesystem rules) is
116 * marked as "quiet" via %LANDLOCK_ADD_RULE_QUIET.
117 *
118 * Because logging is only suppressed by a layer if the layer denies
119 * access, a sandboxed program cannot use this flag to "hide" access
120 * denials, without denying itself the access in the first place.
121 *
122 * The effect of this flag does not depend on the value of
123 * allowed_access in the passed in rule_attr. When this flag is
124 * present, the caller is also allowed to pass in an empty
125 * allowed_access.
126 */
127
128/* clang-format off */
129#define LANDLOCK_ADD_RULE_QUIET (1U << 0)
130/* clang-format on */
131
132/**
133 * DOC: landlock_restrict_self_flags
134 *
135 * **Flags**
136 *
137 * By default, denied accesses originating from programs that sandbox themselves
138 * are logged via the audit subsystem. Such events typically indicate unexpected
139 * behavior, such as bugs or exploitation attempts. However, to avoid excessive
140 * logging, access requests denied by a domain not created by the originating
141 * program are not logged by default. The rationale is that programs should know
142 * their own behavior, but not necessarily the behavior of other programs. This
143 * default configuration is suitable for most programs that sandbox themselves.
144 * For specific use cases, the following flags allow programs to modify this
145 * default logging behavior.
146 *
147 * The %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF and
148 * %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON flags apply to the newly created
149 * Landlock domain.
150 *
151 * %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF
152 * Disables logging of denied accesses originating from the thread creating
153 * the Landlock domain, as well as its children, as long as they continue
154 * running the same executable code (i.e., without an intervening
155 * :manpage:`execve(2)` call). This is intended for programs that execute
156 * unknown code without invoking :manpage:`execve(2)`, such as script
157 * interpreters. Programs that only sandbox themselves should not set this
158 * flag, so users can be notified of unauthorized access attempts via system
159 * logs.
160 *
161 * %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON
162 * Enables logging of denied accesses after an :manpage:`execve(2)` call,
163 * providing visibility into unauthorized access attempts by newly executed
164 * programs within the created Landlock domain. This flag is recommended
165 * only when all potential executables in the domain are expected to comply
166 * with the access restrictions, as excessive audit log entries could make
167 * it more difficult to identify critical events.
168 *
169 * %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
170 * Disables logging of denied accesses originating from nested Landlock
171 * domains created by the caller or its descendants. This flag should be set
172 * according to runtime configuration, not hardcoded, to avoid suppressing
173 * important security events. It is useful for container runtimes or
174 * sandboxing tools that may launch programs which themselves create
175 * Landlock domains and could otherwise generate excessive logs. Unlike
176 * ``LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF``, this flag only affects
177 * future nested domains, not the one being created. It can also be used
178 * with a @ruleset_fd value of -1 to mute subdomain logs without creating a
179 * domain. When combined with %LANDLOCK_RESTRICT_SELF_TSYNC and a
180 * @ruleset_fd value of -1, this configuration is propagated to all threads
181 * of the current process.
182 *
183 * The following flag supports policy enforcement in multithreaded processes:
184 *
185 * %LANDLOCK_RESTRICT_SELF_TSYNC
186 * Applies the new Landlock configuration atomically to all threads of the
187 * current process, including the Landlock domain and logging
188 * configuration. This overrides the Landlock configuration of sibling
189 * threads, irrespective of previously established Landlock domains and
190 * logging configurations on these threads.
191 *
192 * If the calling thread is running with no_new_privs, this operation
193 * enables no_new_privs on the sibling threads as well.
194 */
195/* clang-format off */
196#define LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF (1U << 0)
197#define LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON (1U << 1)
198#define LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF (1U << 2)
199#define LANDLOCK_RESTRICT_SELF_TSYNC (1U << 3)
200/* clang-format on */
201
202/**
203 * enum landlock_rule_type - Landlock rule type
204 *
205 * Argument of sys_landlock_add_rule().
206 */
207enum landlock_rule_type {
208 /**
209 * @LANDLOCK_RULE_PATH_BENEATH: Type of a &struct
210 * landlock_path_beneath_attr .
211 */
212 LANDLOCK_RULE_PATH_BENEATH = 1,
213 /**
214 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
215 * landlock_net_port_attr .
216 */
217 LANDLOCK_RULE_NET_PORT,
218};
219
220/**
221 * struct landlock_path_beneath_attr - Path hierarchy definition
222 *
223 * Argument of sys_landlock_add_rule().
224 */
225struct landlock_path_beneath_attr {
226 /**
227 * @allowed_access: Bitmask of allowed actions for this file hierarchy
228 * (cf. `Filesystem flags`_).
229 */
230 __u64 allowed_access;
231 /**
232 * @parent_fd: File descriptor, preferably opened with ``O_PATH``,
233 * which identifies the parent directory of a file hierarchy, or just a
234 * file.
235 */
236 __s32 parent_fd;
237 /*
238 * This struct is packed to avoid trailing reserved members.
239 * Cf. security/landlock/syscalls.c:build_check_abi()
240 */
241} __attribute__((packed));
242
243/**
244 * struct landlock_net_port_attr - Network port definition
245 *
246 * Argument of sys_landlock_add_rule().
247 */
248struct landlock_net_port_attr {
249 /**
250 * @allowed_access: Bitmask of allowed network actions for a port
251 * (cf. `Network flags`_).
252 */
253 __u64 allowed_access;
254 /**
255 * @port: Network port in host endianness.
256 *
257 * It should be noted that port 0 passed to :manpage:`bind(2)` will bind
258 * to an available port from the ephemeral port range. This can be
259 * configured with the ``/proc/sys/net/ipv4/ip_local_port_range`` sysctl
260 * (also used for IPv6), and within that range, on a per-socket basis
261 * with ``setsockopt(IP_LOCAL_PORT_RANGE)``.
262 *
263 * A Landlock rule with port 0 and the %LANDLOCK_ACCESS_NET_BIND_TCP or
264 * %LANDLOCK_ACCESS_NET_BIND_UDP right means that requesting to bind on
265 * port 0 is allowed and it will automatically translate to binding on a
266 * kernel-assigned ephemeral port.
267 */
268 __u64 port;
269};
270
271/**
272 * DOC: fs_access
273 *
274 * A set of actions on kernel objects may be defined by an attribute (e.g.
275 * &struct landlock_path_beneath_attr) including a bitmask of access.
276 *
277 * Filesystem flags
278 * ~~~~~~~~~~~~~~~~
279 *
280 * These flags enable to restrict a sandboxed process to a set of actions on
281 * files and directories. Files or directories opened before the sandboxing
282 * are not subject to these restrictions.
283 *
284 * The following access rights apply only to files:
285 *
286 * - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file.
287 * - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. When
288 * opening files for writing, you will often additionally need the
289 * %LANDLOCK_ACCESS_FS_TRUNCATE right. In many cases, these system calls
290 * truncate existing files when overwriting them (e.g., :manpage:`creat(2)`).
291 * - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access.
292 * - %LANDLOCK_ACCESS_FS_TRUNCATE: Truncate a file with :manpage:`truncate(2)`,
293 * :manpage:`ftruncate(2)`, :manpage:`creat(2)`, or :manpage:`open(2)` with
294 * ``O_TRUNC``. This access right is available since the third version of the
295 * Landlock ABI.
296 * - %LANDLOCK_ACCESS_FS_IOCTL_DEV: Invoke :manpage:`ioctl(2)` commands on an opened
297 * character or block device.
298 *
299 * This access right applies to all `ioctl(2)` commands implemented by device
300 * drivers. However, the following common IOCTL commands continue to be
301 * invokable independent of the %LANDLOCK_ACCESS_FS_IOCTL_DEV right:
302 *
303 * * IOCTL commands targeting file descriptors (``FIOCLEX``, ``FIONCLEX``),
304 * * IOCTL commands targeting file descriptions (``FIONBIO``, ``FIOASYNC``),
305 * * IOCTL commands targeting file systems (``FIFREEZE``, ``FITHAW``,
306 * ``FIGETBSZ``, ``FS_IOC_GETFSUUID``, ``FS_IOC_GETFSSYSFSPATH``)
307 * * Some IOCTL commands which do not make sense when used with devices, but
308 * whose implementations are safe and return the right error codes
309 * (``FS_IOC_FIEMAP``, ``FICLONE``, ``FICLONERANGE``, ``FIDEDUPERANGE``)
310 *
311 * This access right is available since the fifth version of the Landlock
312 * ABI.
313 * - %LANDLOCK_ACCESS_FS_RESOLVE_UNIX: Look up pathname UNIX domain sockets
314 * (:manpage:`unix(7)`). On UNIX domain sockets, this restricts both calls to
315 * :manpage:`connect(2)` as well as calls to :manpage:`sendmsg(2)` with an
316 * explicit recipient address.
317 *
318 * This access right only applies to connections to UNIX server sockets which
319 * were created outside of the newly created Landlock domain (e.g. from within
320 * a parent domain or from an unrestricted process). Newly created UNIX
321 * servers within the same Landlock domain continue to be accessible. In this
322 * regard, %LANDLOCK_ACCESS_FS_RESOLVE_UNIX has the same semantics as the
323 * ``LANDLOCK_SCOPE_*`` flags.
324 *
325 * If a resolve attempt is denied, the operation returns an ``EACCES`` error,
326 * in line with other filesystem access rights (but different to denials for
327 * abstract UNIX domain sockets).
328 *
329 * This access right is available since the ninth version of the Landlock ABI.
330 *
331 * The rationale for this design is described in
332 * :ref:`Documentation/security/landlock.rst <scoped-flags-interaction>`.
333 *
334 * Whether an opened file can be truncated with :manpage:`ftruncate(2)` or used
335 * with `ioctl(2)` is determined during :manpage:`open(2)`, in the same way as
336 * read and write permissions are checked during :manpage:`open(2)` using
337 * %LANDLOCK_ACCESS_FS_READ_FILE and %LANDLOCK_ACCESS_FS_WRITE_FILE.
338 *
339 * A directory can receive access rights related to files or directories. The
340 * following access right is applied to the directory itself, and the
341 * directories beneath it:
342 *
343 * - %LANDLOCK_ACCESS_FS_READ_DIR: Open a directory or list its content.
344 *
345 * However, the following access rights only apply to the content of a
346 * directory, not the directory itself:
347 *
348 * - %LANDLOCK_ACCESS_FS_REMOVE_DIR: Remove an empty directory or rename one.
349 * - %LANDLOCK_ACCESS_FS_REMOVE_FILE: Unlink (or rename) a file.
350 * - %LANDLOCK_ACCESS_FS_MAKE_CHAR: Create (or rename or link) a character
351 * device.
352 * - %LANDLOCK_ACCESS_FS_MAKE_DIR: Create (or rename) a directory.
353 * - %LANDLOCK_ACCESS_FS_MAKE_REG: Create (or rename or link) a regular file.
354 * - %LANDLOCK_ACCESS_FS_MAKE_SOCK: Create (or rename or link) a UNIX domain
355 * socket.
356 * - %LANDLOCK_ACCESS_FS_MAKE_FIFO: Create (or rename or link) a named pipe.
357 * - %LANDLOCK_ACCESS_FS_MAKE_BLOCK: Create (or rename or link) a block device.
358 * - %LANDLOCK_ACCESS_FS_MAKE_SYM: Create (or rename or link) a symbolic link.
359 * - %LANDLOCK_ACCESS_FS_REFER: Link or rename a file from or to a different
360 * directory (i.e. reparent a file hierarchy).
361 *
362 * This access right is available since the second version of the Landlock
363 * ABI.
364 *
365 * This is the only access right which is denied by default by any ruleset,
366 * even if the right is not specified as handled at ruleset creation time.
367 * The only way to make a ruleset grant this right is to explicitly allow it
368 * for a specific directory by adding a matching rule to the ruleset.
369 *
370 * In particular, when using the first Landlock ABI version, Landlock will
371 * always deny attempts to reparent files between different directories.
372 *
373 * In addition to the source and destination directories having the
374 * %LANDLOCK_ACCESS_FS_REFER access right, the attempted link or rename
375 * operation must meet the following constraints:
376 *
377 * * The reparented file may not gain more access rights in the destination
378 * directory than it previously had in the source directory. If this is
379 * attempted, the operation results in an ``EXDEV`` error.
380 *
381 * * When linking or renaming, the ``LANDLOCK_ACCESS_FS_MAKE_*`` right for the
382 * respective file type must be granted for the destination directory.
383 * Otherwise, the operation results in an ``EACCES`` error.
384 *
385 * * When renaming, the ``LANDLOCK_ACCESS_FS_REMOVE_*`` right for the
386 * respective file type must be granted for the source directory. Otherwise,
387 * the operation results in an ``EACCES`` error.
388 *
389 * If multiple requirements are not met, the ``EACCES`` error code takes
390 * precedence over ``EXDEV``.
391 *
392 * .. warning::
393 *
394 * It is currently not possible to restrict some file-related actions
395 * accessible through these syscall families: :manpage:`chdir(2)`,
396 * :manpage:`stat(2)`, :manpage:`flock(2)`, :manpage:`chmod(2)`,
397 * :manpage:`chown(2)`, :manpage:`setxattr(2)`, :manpage:`utime(2)`,
398 * :manpage:`fcntl(2)`, :manpage:`access(2)`.
399 * Future Landlock evolutions will enable to restrict them.
400 */
401/* clang-format off */
402#define LANDLOCK_ACCESS_FS_EXECUTE (1ULL << 0)
403#define LANDLOCK_ACCESS_FS_WRITE_FILE (1ULL << 1)
404#define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2)
405#define LANDLOCK_ACCESS_FS_READ_DIR (1ULL << 3)
406#define LANDLOCK_ACCESS_FS_REMOVE_DIR (1ULL << 4)
407#define LANDLOCK_ACCESS_FS_REMOVE_FILE (1ULL << 5)
408#define LANDLOCK_ACCESS_FS_MAKE_CHAR (1ULL << 6)
409#define LANDLOCK_ACCESS_FS_MAKE_DIR (1ULL << 7)
410#define LANDLOCK_ACCESS_FS_MAKE_REG (1ULL << 8)
411#define LANDLOCK_ACCESS_FS_MAKE_SOCK (1ULL << 9)
412#define LANDLOCK_ACCESS_FS_MAKE_FIFO (1ULL << 10)
413#define LANDLOCK_ACCESS_FS_MAKE_BLOCK (1ULL << 11)
414#define LANDLOCK_ACCESS_FS_MAKE_SYM (1ULL << 12)
415#define LANDLOCK_ACCESS_FS_REFER (1ULL << 13)
416#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)
417#define LANDLOCK_ACCESS_FS_IOCTL_DEV (1ULL << 15)
418#define LANDLOCK_ACCESS_FS_RESOLVE_UNIX (1ULL << 16)
419/* clang-format on */
420
421/**
422 * DOC: net_access
423 *
424 * Network flags
425 * ~~~~~~~~~~~~~~~~
426 *
427 * These flags enable to restrict a sandboxed process to a set of network
428 * actions.
429 *
430 * The following access rights apply to TCP port numbers:
431 *
432 * - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind TCP sockets to the given local
433 * port. Support added in Landlock ABI version 4.
434 * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect TCP sockets to the given
435 * remote port. Support added in Landlock ABI version 4.
436 *
437 * And similarly for UDP port numbers:
438 *
439 * - %LANDLOCK_ACCESS_NET_BIND_UDP: Bind UDP sockets to the given local
440 * port. Support added in Landlock ABI version 10.
441 * - %LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP: Set the remote port of UDP
442 * sockets to the given port, or send datagrams to the given remote port
443 * ignoring any destination pre-set on a socket. Support added in
444 * Landlock ABI version 10.
445 *
446 * .. note:: Setting a remote address or sending a first datagram
447 * auto-binds UDP sockets to an ephemeral local source port if not
448 * already bound. To allow this if both %LANDLOCK_ACCESS_NET_BIND_UDP
449 * and %LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP are handled, you need to
450 * either:
451 *
452 * - use a socket already bound to a port before the ruleset started
453 * being enforced;
454 * - or grant %LANDLOCK_ACCESS_NET_BIND_UDP on port 0, meaning "any
455 * port in the ephemeral port range";
456 * - or grant %LANDLOCK_ACCESS_NET_BIND_UDP on a specific port, and
457 * call :manpage:`bind(2)` on that port before trying to
458 * :manpage:`connect(2)` or send datagrams.
459 *
460 * .. note:: Sending datagrams to an ``AF_UNSPEC`` destination address
461 * family is not supported for IPv6 UDP sockets: you will need to use a
462 * ``NULL`` address instead.
463 */
464/* clang-format off */
465#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0)
466#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
467#define LANDLOCK_ACCESS_NET_BIND_UDP (1ULL << 2)
468#define LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP (1ULL << 3)
469/* clang-format on */
470
471/**
472 * DOC: scope
473 *
474 * Scope flags
475 * ~~~~~~~~~~~
476 *
477 * These flags enable to isolate a sandboxed process from a set of IPC actions.
478 * Setting a flag for a ruleset will isolate the Landlock domain to forbid
479 * connections to resources outside the domain.
480 *
481 * This is supported since Landlock ABI version 6.
482 *
483 * Scopes:
484 *
485 * - %LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET: Restrict a sandboxed process from
486 * connecting to an abstract UNIX socket created by a process outside the
487 * related Landlock domain (e.g., a parent domain or a non-sandboxed process).
488 * - %LANDLOCK_SCOPE_SIGNAL: Restrict a sandboxed process from sending a signal
489 * to another process outside the domain.
490 */
491/* clang-format off */
492#define LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET (1ULL << 0)
493#define LANDLOCK_SCOPE_SIGNAL (1ULL << 1)
494/* clang-format on*/
495
496#endif /* _LINUX_LANDLOCK_H */