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