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