logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

landlock.h (11708B)


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