xref: /linux-6.15/include/uapi/linux/landlock.h (revision 4ea404fd)
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 <[email protected]>
6  * Copyright © 2018-2020 ANSSI
7  * Copyright © 2021-2025 Microsoft Corporation
8  */
9 
10 #ifndef _UAPI_LINUX_LANDLOCK_H
11 #define _UAPI_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  * This structure can grow in future Landlock versions.
36  */
37 struct landlock_ruleset_attr {
38 	/**
39 	 * @handled_access_fs: Bitmask of handled filesystem actions
40 	 * (cf. `Filesystem flags`_).
41 	 */
42 	__u64 handled_access_fs;
43 	/**
44 	 * @handled_access_net: Bitmask of handled network actions (cf. `Network
45 	 * flags`_).
46 	 */
47 	__u64 handled_access_net;
48 	/**
49 	 * @scoped: Bitmask of scopes (cf. `Scope flags`_)
50 	 * restricting a Landlock domain from accessing outside
51 	 * resources (e.g. IPCs).
52 	 */
53 	__u64 scoped;
54 };
55 
56 /*
57  * sys_landlock_create_ruleset() flags:
58  *
59  * - %LANDLOCK_CREATE_RULESET_VERSION: Get the highest supported Landlock ABI
60  *   version.
61  * - %LANDLOCK_CREATE_RULESET_ERRATA: Get a bitmask of fixed issues.
62  */
63 /* clang-format off */
64 #define LANDLOCK_CREATE_RULESET_VERSION			(1U << 0)
65 #define LANDLOCK_CREATE_RULESET_ERRATA			(1U << 1)
66 /* clang-format on */
67 
68 /*
69  * sys_landlock_restrict_self() flags:
70  *
71  * - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF: Do not create any log related to the
72  *   enforced restrictions.  This should only be set by tools launching unknown
73  *   or untrusted programs (e.g. a sandbox tool, container runtime, system
74  *   service manager).  Because programs sandboxing themselves should fix any
75  *   denied access, they should not set this flag to be aware of potential
76  *   issues reported by system's logs (i.e. audit).
77  * - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON: Explicitly ask to continue
78  *   logging denied access requests even after an :manpage:`execve(2)` call.
79  *   This flag should only be set if all the programs than can legitimately be
80  *   executed will not try to request a denied access (which could spam audit
81  *   logs).
82  * - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF: Do not create any log related
83  *   to the enforced restrictions coming from future nested domains created by
84  *   the caller or its descendants.  This should only be set according to a
85  *   runtime configuration (i.e. not hardcoded) by programs launching other
86  *   unknown or untrusted programs that may create their own Landlock domains
87  *   and spam logs.  The main use case is for container runtimes to enable users
88  *   to mute buggy sandboxed programs for a specific container image.  Other use
89  *   cases include sandboxer tools and init systems.  Unlike
90  *   %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF,
91  *   %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF does not impact the requested
92  *   restriction (if any) but only the future nested domains.
93  */
94 /* clang-format off */
95 #define LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF		(1U << 0)
96 #define LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON			(1U << 1)
97 #define LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF		(1U << 2)
98 /* clang-format on */
99 
100 /**
101  * enum landlock_rule_type - Landlock rule type
102  *
103  * Argument of sys_landlock_add_rule().
104  */
105 enum landlock_rule_type {
106 	/**
107 	 * @LANDLOCK_RULE_PATH_BENEATH: Type of a &struct
108 	 * landlock_path_beneath_attr .
109 	 */
110 	LANDLOCK_RULE_PATH_BENEATH = 1,
111 	/**
112 	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
113 	 * landlock_net_port_attr .
114 	 */
115 	LANDLOCK_RULE_NET_PORT,
116 };
117 
118 /**
119  * struct landlock_path_beneath_attr - Path hierarchy definition
120  *
121  * Argument of sys_landlock_add_rule().
122  */
123 struct landlock_path_beneath_attr {
124 	/**
125 	 * @allowed_access: Bitmask of allowed actions for this file hierarchy
126 	 * (cf. `Filesystem flags`_).
127 	 */
128 	__u64 allowed_access;
129 	/**
130 	 * @parent_fd: File descriptor, preferably opened with ``O_PATH``,
131 	 * which identifies the parent directory of a file hierarchy, or just a
132 	 * file.
133 	 */
134 	__s32 parent_fd;
135 	/*
136 	 * This struct is packed to avoid trailing reserved members.
137 	 * Cf. security/landlock/syscalls.c:build_check_abi()
138 	 */
139 } __attribute__((packed));
140 
141 /**
142  * struct landlock_net_port_attr - Network port definition
143  *
144  * Argument of sys_landlock_add_rule().
145  */
146 struct landlock_net_port_attr {
147 	/**
148 	 * @allowed_access: Bitmask of allowed network actions for a port
149 	 * (cf. `Network flags`_).
150 	 */
151 	__u64 allowed_access;
152 	/**
153 	 * @port: Network port in host endianness.
154 	 *
155 	 * It should be noted that port 0 passed to :manpage:`bind(2)` will bind
156 	 * to an available port from the ephemeral port range.  This can be
157 	 * configured with the ``/proc/sys/net/ipv4/ip_local_port_range`` sysctl
158 	 * (also used for IPv6).
159 	 *
160 	 * A Landlock rule with port 0 and the ``LANDLOCK_ACCESS_NET_BIND_TCP``
161 	 * right means that requesting to bind on port 0 is allowed and it will
162 	 * automatically translate to binding on the related port range.
163 	 */
164 	__u64 port;
165 };
166 
167 /**
168  * DOC: fs_access
169  *
170  * A set of actions on kernel objects may be defined by an attribute (e.g.
171  * &struct landlock_path_beneath_attr) including a bitmask of access.
172  *
173  * Filesystem flags
174  * ~~~~~~~~~~~~~~~~
175  *
176  * These flags enable to restrict a sandboxed process to a set of actions on
177  * files and directories.  Files or directories opened before the sandboxing
178  * are not subject to these restrictions.
179  *
180  * The following access rights apply only to files:
181  *
182  * - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file.
183  * - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access.  When
184  *   opening files for writing, you will often additionally need the
185  *   %LANDLOCK_ACCESS_FS_TRUNCATE right.  In many cases, these system calls
186  *   truncate existing files when overwriting them (e.g., :manpage:`creat(2)`).
187  * - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access.
188  * - %LANDLOCK_ACCESS_FS_TRUNCATE: Truncate a file with :manpage:`truncate(2)`,
189  *   :manpage:`ftruncate(2)`, :manpage:`creat(2)`, or :manpage:`open(2)` with
190  *   ``O_TRUNC``.  This access right is available since the third version of the
191  *   Landlock ABI.
192  *
193  * Whether an opened file can be truncated with :manpage:`ftruncate(2)` or used
194  * with `ioctl(2)` is determined during :manpage:`open(2)`, in the same way as
195  * read and write permissions are checked during :manpage:`open(2)` using
196  * %LANDLOCK_ACCESS_FS_READ_FILE and %LANDLOCK_ACCESS_FS_WRITE_FILE.
197  *
198  * A directory can receive access rights related to files or directories.  The
199  * following access right is applied to the directory itself, and the
200  * directories beneath it:
201  *
202  * - %LANDLOCK_ACCESS_FS_READ_DIR: Open a directory or list its content.
203  *
204  * However, the following access rights only apply to the content of a
205  * directory, not the directory itself:
206  *
207  * - %LANDLOCK_ACCESS_FS_REMOVE_DIR: Remove an empty directory or rename one.
208  * - %LANDLOCK_ACCESS_FS_REMOVE_FILE: Unlink (or rename) a file.
209  * - %LANDLOCK_ACCESS_FS_MAKE_CHAR: Create (or rename or link) a character
210  *   device.
211  * - %LANDLOCK_ACCESS_FS_MAKE_DIR: Create (or rename) a directory.
212  * - %LANDLOCK_ACCESS_FS_MAKE_REG: Create (or rename or link) a regular file.
213  * - %LANDLOCK_ACCESS_FS_MAKE_SOCK: Create (or rename or link) a UNIX domain
214  *   socket.
215  * - %LANDLOCK_ACCESS_FS_MAKE_FIFO: Create (or rename or link) a named pipe.
216  * - %LANDLOCK_ACCESS_FS_MAKE_BLOCK: Create (or rename or link) a block device.
217  * - %LANDLOCK_ACCESS_FS_MAKE_SYM: Create (or rename or link) a symbolic link.
218  * - %LANDLOCK_ACCESS_FS_REFER: Link or rename a file from or to a different
219  *   directory (i.e. reparent a file hierarchy).
220  *
221  *   This access right is available since the second version of the Landlock
222  *   ABI.
223  *
224  *   This is the only access right which is denied by default by any ruleset,
225  *   even if the right is not specified as handled at ruleset creation time.
226  *   The only way to make a ruleset grant this right is to explicitly allow it
227  *   for a specific directory by adding a matching rule to the ruleset.
228  *
229  *   In particular, when using the first Landlock ABI version, Landlock will
230  *   always deny attempts to reparent files between different directories.
231  *
232  *   In addition to the source and destination directories having the
233  *   %LANDLOCK_ACCESS_FS_REFER access right, the attempted link or rename
234  *   operation must meet the following constraints:
235  *
236  *   * The reparented file may not gain more access rights in the destination
237  *     directory than it previously had in the source directory.  If this is
238  *     attempted, the operation results in an ``EXDEV`` error.
239  *
240  *   * When linking or renaming, the ``LANDLOCK_ACCESS_FS_MAKE_*`` right for the
241  *     respective file type must be granted for the destination directory.
242  *     Otherwise, the operation results in an ``EACCES`` error.
243  *
244  *   * When renaming, the ``LANDLOCK_ACCESS_FS_REMOVE_*`` right for the
245  *     respective file type must be granted for the source directory.  Otherwise,
246  *     the operation results in an ``EACCES`` error.
247  *
248  *   If multiple requirements are not met, the ``EACCES`` error code takes
249  *   precedence over ``EXDEV``.
250  *
251  * The following access right applies both to files and directories:
252  *
253  * - %LANDLOCK_ACCESS_FS_IOCTL_DEV: Invoke :manpage:`ioctl(2)` commands on an opened
254  *   character or block device.
255  *
256  *   This access right applies to all `ioctl(2)` commands implemented by device
257  *   drivers.  However, the following common IOCTL commands continue to be
258  *   invokable independent of the %LANDLOCK_ACCESS_FS_IOCTL_DEV right:
259  *
260  *   * IOCTL commands targeting file descriptors (``FIOCLEX``, ``FIONCLEX``),
261  *   * IOCTL commands targeting file descriptions (``FIONBIO``, ``FIOASYNC``),
262  *   * IOCTL commands targeting file systems (``FIFREEZE``, ``FITHAW``,
263  *     ``FIGETBSZ``, ``FS_IOC_GETFSUUID``, ``FS_IOC_GETFSSYSFSPATH``)
264  *   * Some IOCTL commands which do not make sense when used with devices, but
265  *     whose implementations are safe and return the right error codes
266  *     (``FS_IOC_FIEMAP``, ``FICLONE``, ``FICLONERANGE``, ``FIDEDUPERANGE``)
267  *
268  *   This access right is available since the fifth version of the Landlock
269  *   ABI.
270  *
271  * .. warning::
272  *
273  *   It is currently not possible to restrict some file-related actions
274  *   accessible through these syscall families: :manpage:`chdir(2)`,
275  *   :manpage:`stat(2)`, :manpage:`flock(2)`, :manpage:`chmod(2)`,
276  *   :manpage:`chown(2)`, :manpage:`setxattr(2)`, :manpage:`utime(2)`,
277  *   :manpage:`fcntl(2)`, :manpage:`access(2)`.
278  *   Future Landlock evolutions will enable to restrict them.
279  */
280 /* clang-format off */
281 #define LANDLOCK_ACCESS_FS_EXECUTE			(1ULL << 0)
282 #define LANDLOCK_ACCESS_FS_WRITE_FILE			(1ULL << 1)
283 #define LANDLOCK_ACCESS_FS_READ_FILE			(1ULL << 2)
284 #define LANDLOCK_ACCESS_FS_READ_DIR			(1ULL << 3)
285 #define LANDLOCK_ACCESS_FS_REMOVE_DIR			(1ULL << 4)
286 #define LANDLOCK_ACCESS_FS_REMOVE_FILE			(1ULL << 5)
287 #define LANDLOCK_ACCESS_FS_MAKE_CHAR			(1ULL << 6)
288 #define LANDLOCK_ACCESS_FS_MAKE_DIR			(1ULL << 7)
289 #define LANDLOCK_ACCESS_FS_MAKE_REG			(1ULL << 8)
290 #define LANDLOCK_ACCESS_FS_MAKE_SOCK			(1ULL << 9)
291 #define LANDLOCK_ACCESS_FS_MAKE_FIFO			(1ULL << 10)
292 #define LANDLOCK_ACCESS_FS_MAKE_BLOCK			(1ULL << 11)
293 #define LANDLOCK_ACCESS_FS_MAKE_SYM			(1ULL << 12)
294 #define LANDLOCK_ACCESS_FS_REFER			(1ULL << 13)
295 #define LANDLOCK_ACCESS_FS_TRUNCATE			(1ULL << 14)
296 #define LANDLOCK_ACCESS_FS_IOCTL_DEV			(1ULL << 15)
297 /* clang-format on */
298 
299 /**
300  * DOC: net_access
301  *
302  * Network flags
303  * ~~~~~~~~~~~~~~~~
304  *
305  * These flags enable to restrict a sandboxed process to a set of network
306  * actions.
307  *
308  * This is supported since Landlock ABI version 4.
309  *
310  * The following access rights apply to TCP port numbers:
311  *
312  * - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port.
313  * - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to
314  *   a remote port.
315  */
316 /* clang-format off */
317 #define LANDLOCK_ACCESS_NET_BIND_TCP			(1ULL << 0)
318 #define LANDLOCK_ACCESS_NET_CONNECT_TCP			(1ULL << 1)
319 /* clang-format on */
320 
321 /**
322  * DOC: scope
323  *
324  * Scope flags
325  * ~~~~~~~~~~~
326  *
327  * These flags enable to isolate a sandboxed process from a set of IPC actions.
328  * Setting a flag for a ruleset will isolate the Landlock domain to forbid
329  * connections to resources outside the domain.
330  *
331  * This is supported since Landlock ABI version 6.
332  *
333  * Scopes:
334  *
335  * - %LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET: Restrict a sandboxed process from
336  *   connecting to an abstract UNIX socket created by a process outside the
337  *   related Landlock domain (e.g., a parent domain or a non-sandboxed process).
338  * - %LANDLOCK_SCOPE_SIGNAL: Restrict a sandboxed process from sending a signal
339  *   to another process outside the domain.
340  */
341 /* clang-format off */
342 #define LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET		(1ULL << 0)
343 #define LANDLOCK_SCOPE_SIGNAL		                (1ULL << 1)
344 /* clang-format on*/
345 
346 #endif /* _UAPI_LINUX_LANDLOCK_H */
347