xref: /openbmc/linux/include/uapi/linux/landlock.h (revision d699090510c3223641a23834b4710e2d4309a6ad)
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  */
20 struct 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 
36 /*
37  * sys_landlock_create_ruleset() flags:
38  *
39  * - %LANDLOCK_CREATE_RULESET_VERSION: Get the highest supported Landlock ABI
40  *   version.
41  * - %LANDLOCK_CREATE_RULESET_ERRATA: Get a bitmask of fixed issues.
42  */
43 /* clang-format off */
44 #define LANDLOCK_CREATE_RULESET_VERSION			(1U << 0)
45 #define LANDLOCK_CREATE_RULESET_ERRATA			(1U << 1)
46 /* clang-format on */
47 
48 /**
49  * enum landlock_rule_type - Landlock rule type
50  *
51  * Argument of sys_landlock_add_rule().
52  */
53 enum landlock_rule_type {
54 	/**
55 	 * @LANDLOCK_RULE_PATH_BENEATH: Type of a &struct
56 	 * landlock_path_beneath_attr .
57 	 */
58 	LANDLOCK_RULE_PATH_BENEATH = 1,
59 };
60 
61 /**
62  * struct landlock_path_beneath_attr - Path hierarchy definition
63  *
64  * Argument of sys_landlock_add_rule().
65  */
66 struct landlock_path_beneath_attr {
67 	/**
68 	 * @allowed_access: Bitmask of allowed actions for this file hierarchy
69 	 * (cf. `Filesystem flags`_).
70 	 */
71 	__u64 allowed_access;
72 	/**
73 	 * @parent_fd: File descriptor, preferably opened with ``O_PATH``,
74 	 * which identifies the parent directory of a file hierarchy, or just a
75 	 * file.
76 	 */
77 	__s32 parent_fd;
78 	/*
79 	 * This struct is packed to avoid trailing reserved members.
80 	 * Cf. security/landlock/syscalls.c:build_check_abi()
81 	 */
82 } __attribute__((packed));
83 
84 /**
85  * DOC: fs_access
86  *
87  * A set of actions on kernel objects may be defined by an attribute (e.g.
88  * &struct landlock_path_beneath_attr) including a bitmask of access.
89  *
90  * Filesystem flags
91  * ~~~~~~~~~~~~~~~~
92  *
93  * These flags enable to restrict a sandboxed process to a set of actions on
94  * files and directories.  Files or directories opened before the sandboxing
95  * are not subject to these restrictions.
96  *
97  * A file can only receive these access rights:
98  *
99  * - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file.
100  * - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. Note that
101  *   you might additionally need the %LANDLOCK_ACCESS_FS_TRUNCATE right in order
102  *   to overwrite files with :manpage:`open(2)` using ``O_TRUNC`` or
103  *   :manpage:`creat(2)`.
104  * - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access.
105  * - %LANDLOCK_ACCESS_FS_TRUNCATE: Truncate a file with :manpage:`truncate(2)`,
106  *   :manpage:`ftruncate(2)`, :manpage:`creat(2)`, or :manpage:`open(2)` with
107  *   ``O_TRUNC``. Whether an opened file can be truncated with
108  *   :manpage:`ftruncate(2)` is determined during :manpage:`open(2)`, in the
109  *   same way as read and write permissions are checked during
110  *   :manpage:`open(2)` using %LANDLOCK_ACCESS_FS_READ_FILE and
111  *   %LANDLOCK_ACCESS_FS_WRITE_FILE. This access right is available since the
112  *   third version of the Landlock ABI.
113  *
114  * A directory can receive access rights related to files or directories.  The
115  * following access right is applied to the directory itself, and the
116  * directories beneath it:
117  *
118  * - %LANDLOCK_ACCESS_FS_READ_DIR: Open a directory or list its content.
119  *
120  * However, the following access rights only apply to the content of a
121  * directory, not the directory itself:
122  *
123  * - %LANDLOCK_ACCESS_FS_REMOVE_DIR: Remove an empty directory or rename one.
124  * - %LANDLOCK_ACCESS_FS_REMOVE_FILE: Unlink (or rename) a file.
125  * - %LANDLOCK_ACCESS_FS_MAKE_CHAR: Create (or rename or link) a character
126  *   device.
127  * - %LANDLOCK_ACCESS_FS_MAKE_DIR: Create (or rename) a directory.
128  * - %LANDLOCK_ACCESS_FS_MAKE_REG: Create (or rename or link) a regular file.
129  * - %LANDLOCK_ACCESS_FS_MAKE_SOCK: Create (or rename or link) a UNIX domain
130  *   socket.
131  * - %LANDLOCK_ACCESS_FS_MAKE_FIFO: Create (or rename or link) a named pipe.
132  * - %LANDLOCK_ACCESS_FS_MAKE_BLOCK: Create (or rename or link) a block device.
133  * - %LANDLOCK_ACCESS_FS_MAKE_SYM: Create (or rename or link) a symbolic link.
134  * - %LANDLOCK_ACCESS_FS_REFER: Link or rename a file from or to a different
135  *   directory (i.e. reparent a file hierarchy).
136  *
137  *   This access right is available since the second version of the Landlock
138  *   ABI.
139  *
140  *   This is the only access right which is denied by default by any ruleset,
141  *   even if the right is not specified as handled at ruleset creation time.
142  *   The only way to make a ruleset grant this right is to explicitly allow it
143  *   for a specific directory by adding a matching rule to the ruleset.
144  *
145  *   In particular, when using the first Landlock ABI version, Landlock will
146  *   always deny attempts to reparent files between different directories.
147  *
148  *   In addition to the source and destination directories having the
149  *   %LANDLOCK_ACCESS_FS_REFER access right, the attempted link or rename
150  *   operation must meet the following constraints:
151  *
152  *   * The reparented file may not gain more access rights in the destination
153  *     directory than it previously had in the source directory.  If this is
154  *     attempted, the operation results in an ``EXDEV`` error.
155  *
156  *   * When linking or renaming, the ``LANDLOCK_ACCESS_FS_MAKE_*`` right for the
157  *     respective file type must be granted for the destination directory.
158  *     Otherwise, the operation results in an ``EACCES`` error.
159  *
160  *   * When renaming, the ``LANDLOCK_ACCESS_FS_REMOVE_*`` right for the
161  *     respective file type must be granted for the source directory.  Otherwise,
162  *     the operation results in an ``EACCES`` error.
163  *
164  *   If multiple requirements are not met, the ``EACCES`` error code takes
165  *   precedence over ``EXDEV``.
166  *
167  * .. warning::
168  *
169  *   It is currently not possible to restrict some file-related actions
170  *   accessible through these syscall families: :manpage:`chdir(2)`,
171  *   :manpage:`stat(2)`, :manpage:`flock(2)`, :manpage:`chmod(2)`,
172  *   :manpage:`chown(2)`, :manpage:`setxattr(2)`, :manpage:`utime(2)`,
173  *   :manpage:`ioctl(2)`, :manpage:`fcntl(2)`, :manpage:`access(2)`.
174  *   Future Landlock evolutions will enable to restrict them.
175  */
176 /* clang-format off */
177 #define LANDLOCK_ACCESS_FS_EXECUTE			(1ULL << 0)
178 #define LANDLOCK_ACCESS_FS_WRITE_FILE			(1ULL << 1)
179 #define LANDLOCK_ACCESS_FS_READ_FILE			(1ULL << 2)
180 #define LANDLOCK_ACCESS_FS_READ_DIR			(1ULL << 3)
181 #define LANDLOCK_ACCESS_FS_REMOVE_DIR			(1ULL << 4)
182 #define LANDLOCK_ACCESS_FS_REMOVE_FILE			(1ULL << 5)
183 #define LANDLOCK_ACCESS_FS_MAKE_CHAR			(1ULL << 6)
184 #define LANDLOCK_ACCESS_FS_MAKE_DIR			(1ULL << 7)
185 #define LANDLOCK_ACCESS_FS_MAKE_REG			(1ULL << 8)
186 #define LANDLOCK_ACCESS_FS_MAKE_SOCK			(1ULL << 9)
187 #define LANDLOCK_ACCESS_FS_MAKE_FIFO			(1ULL << 10)
188 #define LANDLOCK_ACCESS_FS_MAKE_BLOCK			(1ULL << 11)
189 #define LANDLOCK_ACCESS_FS_MAKE_SYM			(1ULL << 12)
190 #define LANDLOCK_ACCESS_FS_REFER			(1ULL << 13)
191 #define LANDLOCK_ACCESS_FS_TRUNCATE			(1ULL << 14)
192 /* clang-format on */
193 
194 #endif /* _UAPI_LINUX_LANDLOCK_H */
195