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