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