landlock.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. Copyright © 2017-2020 Mickaël Salaün <[email protected]>
  3. .. Copyright © 2019-2020 ANSSI
  4. .. Copyright © 2021-2022 Microsoft Corporation
  5. =====================================
  6. Landlock: unprivileged access control
  7. =====================================
  8. :Author: Mickaël Salaün
  9. :Date: September 2022
  10. The goal of Landlock is to enable to restrict ambient rights (e.g. global
  11. filesystem access) for a set of processes. Because Landlock is a stackable
  12. LSM, it makes possible to create safe security sandboxes as new security layers
  13. in addition to the existing system-wide access-controls. This kind of sandbox
  14. is expected to help mitigate the security impact of bugs or
  15. unexpected/malicious behaviors in user space applications. Landlock empowers
  16. any process, including unprivileged ones, to securely restrict themselves.
  17. We can quickly make sure that Landlock is enabled in the running system by
  18. looking for "landlock: Up and running" in kernel logs (as root): ``dmesg | grep
  19. landlock || journalctl -kg landlock`` . Developers can also easily check for
  20. Landlock support with a :ref:`related system call <landlock_abi_versions>`. If
  21. Landlock is not currently supported, we need to :ref:`configure the kernel
  22. appropriately <kernel_support>`.
  23. Landlock rules
  24. ==============
  25. A Landlock rule describes an action on an object. An object is currently a
  26. file hierarchy, and the related filesystem actions are defined with `access
  27. rights`_. A set of rules is aggregated in a ruleset, which can then restrict
  28. the thread enforcing it, and its future children.
  29. Defining and enforcing a security policy
  30. ----------------------------------------
  31. We first need to define the ruleset that will contain our rules. For this
  32. example, the ruleset will contain rules that only allow read actions, but write
  33. actions will be denied. The ruleset then needs to handle both of these kind of
  34. actions. This is required for backward and forward compatibility (i.e. the
  35. kernel and user space may not know each other's supported restrictions), hence
  36. the need to be explicit about the denied-by-default access rights.
  37. .. code-block:: c
  38. struct landlock_ruleset_attr ruleset_attr = {
  39. .handled_access_fs =
  40. LANDLOCK_ACCESS_FS_EXECUTE |
  41. LANDLOCK_ACCESS_FS_WRITE_FILE |
  42. LANDLOCK_ACCESS_FS_READ_FILE |
  43. LANDLOCK_ACCESS_FS_READ_DIR |
  44. LANDLOCK_ACCESS_FS_REMOVE_DIR |
  45. LANDLOCK_ACCESS_FS_REMOVE_FILE |
  46. LANDLOCK_ACCESS_FS_MAKE_CHAR |
  47. LANDLOCK_ACCESS_FS_MAKE_DIR |
  48. LANDLOCK_ACCESS_FS_MAKE_REG |
  49. LANDLOCK_ACCESS_FS_MAKE_SOCK |
  50. LANDLOCK_ACCESS_FS_MAKE_FIFO |
  51. LANDLOCK_ACCESS_FS_MAKE_BLOCK |
  52. LANDLOCK_ACCESS_FS_MAKE_SYM |
  53. LANDLOCK_ACCESS_FS_REFER,
  54. };
  55. Because we may not know on which kernel version an application will be
  56. executed, it is safer to follow a best-effort security approach. Indeed, we
  57. should try to protect users as much as possible whatever the kernel they are
  58. using. To avoid binary enforcement (i.e. either all security features or
  59. none), we can leverage a dedicated Landlock command to get the current version
  60. of the Landlock ABI and adapt the handled accesses. Let's check if we should
  61. remove the ``LANDLOCK_ACCESS_FS_REFER`` access right which is only supported
  62. starting with the second version of the ABI.
  63. .. code-block:: c
  64. int abi;
  65. abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
  66. if (abi < 2) {
  67. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
  68. }
  69. This enables to create an inclusive ruleset that will contain our rules.
  70. .. code-block:: c
  71. int ruleset_fd;
  72. ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  73. if (ruleset_fd < 0) {
  74. perror("Failed to create a ruleset");
  75. return 1;
  76. }
  77. We can now add a new rule to this ruleset thanks to the returned file
  78. descriptor referring to this ruleset. The rule will only allow reading the
  79. file hierarchy ``/usr``. Without another rule, write actions would then be
  80. denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the
  81. ``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with this file
  82. descriptor.
  83. .. code-block:: c
  84. int err;
  85. struct landlock_path_beneath_attr path_beneath = {
  86. .allowed_access =
  87. LANDLOCK_ACCESS_FS_EXECUTE |
  88. LANDLOCK_ACCESS_FS_READ_FILE |
  89. LANDLOCK_ACCESS_FS_READ_DIR,
  90. };
  91. path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
  92. if (path_beneath.parent_fd < 0) {
  93. perror("Failed to open file");
  94. close(ruleset_fd);
  95. return 1;
  96. }
  97. err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  98. &path_beneath, 0);
  99. close(path_beneath.parent_fd);
  100. if (err) {
  101. perror("Failed to update ruleset");
  102. close(ruleset_fd);
  103. return 1;
  104. }
  105. It may also be required to create rules following the same logic as explained
  106. for the ruleset creation, by filtering access rights according to the Landlock
  107. ABI version. In this example, this is not required because
  108. ``LANDLOCK_ACCESS_FS_REFER`` is not allowed by any rule.
  109. We now have a ruleset with one rule allowing read access to ``/usr`` while
  110. denying all other handled accesses for the filesystem. The next step is to
  111. restrict the current thread from gaining more privileges (e.g. thanks to a SUID
  112. binary).
  113. .. code-block:: c
  114. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  115. perror("Failed to restrict privileges");
  116. close(ruleset_fd);
  117. return 1;
  118. }
  119. The current thread is now ready to sandbox itself with the ruleset.
  120. .. code-block:: c
  121. if (landlock_restrict_self(ruleset_fd, 0)) {
  122. perror("Failed to enforce ruleset");
  123. close(ruleset_fd);
  124. return 1;
  125. }
  126. close(ruleset_fd);
  127. If the ``landlock_restrict_self`` system call succeeds, the current thread is
  128. now restricted and this policy will be enforced on all its subsequently created
  129. children as well. Once a thread is landlocked, there is no way to remove its
  130. security policy; only adding more restrictions is allowed. These threads are
  131. now in a new Landlock domain, merge of their parent one (if any) with the new
  132. ruleset.
  133. Full working code can be found in `samples/landlock/sandboxer.c`_.
  134. Good practices
  135. --------------
  136. It is recommended setting access rights to file hierarchy leaves as much as
  137. possible. For instance, it is better to be able to have ``~/doc/`` as a
  138. read-only hierarchy and ``~/tmp/`` as a read-write hierarchy, compared to
  139. ``~/`` as a read-only hierarchy and ``~/tmp/`` as a read-write hierarchy.
  140. Following this good practice leads to self-sufficient hierarchies that do not
  141. depend on their location (i.e. parent directories). This is particularly
  142. relevant when we want to allow linking or renaming. Indeed, having consistent
  143. access rights per directory enables to change the location of such directory
  144. without relying on the destination directory access rights (except those that
  145. are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
  146. documentation).
  147. Having self-sufficient hierarchies also helps to tighten the required access
  148. rights to the minimal set of data. This also helps avoid sinkhole directories,
  149. i.e. directories where data can be linked to but not linked from. However,
  150. this depends on data organization, which might not be controlled by developers.
  151. In this case, granting read-write access to ``~/tmp/``, instead of write-only
  152. access, would potentially allow to move ``~/tmp/`` to a non-readable directory
  153. and still keep the ability to list the content of ``~/tmp/``.
  154. Layers of file path access rights
  155. ---------------------------------
  156. Each time a thread enforces a ruleset on itself, it updates its Landlock domain
  157. with a new layer of policy. Indeed, this complementary policy is stacked with
  158. the potentially other rulesets already restricting this thread. A sandboxed
  159. thread can then safely add more constraints to itself with a new enforced
  160. ruleset.
  161. One policy layer grants access to a file path if at least one of its rules
  162. encountered on the path grants the access. A sandboxed thread can only access
  163. a file path if all its enforced policy layers grant the access as well as all
  164. the other system access controls (e.g. filesystem DAC, other LSM policies,
  165. etc.).
  166. Bind mounts and OverlayFS
  167. -------------------------
  168. Landlock enables to restrict access to file hierarchies, which means that these
  169. access rights can be propagated with bind mounts (cf.
  170. Documentation/filesystems/sharedsubtree.rst) but not with
  171. Documentation/filesystems/overlayfs.rst.
  172. A bind mount mirrors a source file hierarchy to a destination. The destination
  173. hierarchy is then composed of the exact same files, on which Landlock rules can
  174. be tied, either via the source or the destination path. These rules restrict
  175. access when they are encountered on a path, which means that they can restrict
  176. access to multiple file hierarchies at the same time, whether these hierarchies
  177. are the result of bind mounts or not.
  178. An OverlayFS mount point consists of upper and lower layers. These layers are
  179. combined in a merge directory, result of the mount point. This merge hierarchy
  180. may include files from the upper and lower layers, but modifications performed
  181. on the merge hierarchy only reflects on the upper layer. From a Landlock
  182. policy point of view, each OverlayFS layers and merge hierarchies are
  183. standalone and contains their own set of files and directories, which is
  184. different from bind mounts. A policy restricting an OverlayFS layer will not
  185. restrict the resulted merged hierarchy, and vice versa. Landlock users should
  186. then only think about file hierarchies they want to allow access to, regardless
  187. of the underlying filesystem.
  188. Inheritance
  189. -----------
  190. Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain
  191. restrictions from its parent. This is similar to the seccomp inheritance (cf.
  192. Documentation/userspace-api/seccomp_filter.rst) or any other LSM dealing with
  193. task's :manpage:`credentials(7)`. For instance, one process's thread may apply
  194. Landlock rules to itself, but they will not be automatically applied to other
  195. sibling threads (unlike POSIX thread credential changes, cf.
  196. :manpage:`nptl(7)`).
  197. When a thread sandboxes itself, we have the guarantee that the related security
  198. policy will stay enforced on all this thread's descendants. This allows
  199. creating standalone and modular security policies per application, which will
  200. automatically be composed between themselves according to their runtime parent
  201. policies.
  202. Ptrace restrictions
  203. -------------------
  204. A sandboxed process has less privileges than a non-sandboxed process and must
  205. then be subject to additional restrictions when manipulating another process.
  206. To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
  207. process, a sandboxed process should have a subset of the target process rules,
  208. which means the tracee must be in a sub-domain of the tracer.
  209. Compatibility
  210. =============
  211. Backward and forward compatibility
  212. ----------------------------------
  213. Landlock is designed to be compatible with past and future versions of the
  214. kernel. This is achieved thanks to the system call attributes and the
  215. associated bitflags, particularly the ruleset's ``handled_access_fs``. Making
  216. handled access right explicit enables the kernel and user space to have a clear
  217. contract with each other. This is required to make sure sandboxing will not
  218. get stricter with a system update, which could break applications.
  219. Developers can subscribe to the `Landlock mailing list
  220. <https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
  221. test their applications with the latest available features. In the interest of
  222. users, and because they may use different kernel versions, it is strongly
  223. encouraged to follow a best-effort security approach by checking the Landlock
  224. ABI version at runtime and only enforcing the supported features.
  225. .. _landlock_abi_versions:
  226. Landlock ABI versions
  227. ---------------------
  228. The Landlock ABI version can be read with the sys_landlock_create_ruleset()
  229. system call:
  230. .. code-block:: c
  231. int abi;
  232. abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
  233. if (abi < 0) {
  234. switch (errno) {
  235. case ENOSYS:
  236. printf("Landlock is not supported by the current kernel.\n");
  237. break;
  238. case EOPNOTSUPP:
  239. printf("Landlock is currently disabled.\n");
  240. break;
  241. }
  242. return 0;
  243. }
  244. if (abi >= 2) {
  245. printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
  246. }
  247. The following kernel interfaces are implicitly supported by the first ABI
  248. version. Features only supported from a specific version are explicitly marked
  249. as such.
  250. Kernel interface
  251. ================
  252. Access rights
  253. -------------
  254. .. kernel-doc:: include/uapi/linux/landlock.h
  255. :identifiers: fs_access
  256. Creating a new ruleset
  257. ----------------------
  258. .. kernel-doc:: security/landlock/syscalls.c
  259. :identifiers: sys_landlock_create_ruleset
  260. .. kernel-doc:: include/uapi/linux/landlock.h
  261. :identifiers: landlock_ruleset_attr
  262. Extending a ruleset
  263. -------------------
  264. .. kernel-doc:: security/landlock/syscalls.c
  265. :identifiers: sys_landlock_add_rule
  266. .. kernel-doc:: include/uapi/linux/landlock.h
  267. :identifiers: landlock_rule_type landlock_path_beneath_attr
  268. Enforcing a ruleset
  269. -------------------
  270. .. kernel-doc:: security/landlock/syscalls.c
  271. :identifiers: sys_landlock_restrict_self
  272. Current limitations
  273. ===================
  274. Filesystem topology modification
  275. --------------------------------
  276. As for file renaming and linking, a sandboxed thread cannot modify its
  277. filesystem topology, whether via :manpage:`mount(2)` or
  278. :manpage:`pivot_root(2)`. However, :manpage:`chroot(2)` calls are not denied.
  279. Special filesystems
  280. -------------------
  281. Access to regular files and directories can be restricted by Landlock,
  282. according to the handled accesses of a ruleset. However, files that do not
  283. come from a user-visible filesystem (e.g. pipe, socket), but can still be
  284. accessed through ``/proc/<pid>/fd/*``, cannot currently be explicitly
  285. restricted. Likewise, some special kernel filesystems such as nsfs, which can
  286. be accessed through ``/proc/<pid>/ns/*``, cannot currently be explicitly
  287. restricted. However, thanks to the `ptrace restrictions`_, access to such
  288. sensitive ``/proc`` files are automatically restricted according to domain
  289. hierarchies. Future Landlock evolutions could still enable to explicitly
  290. restrict such paths with dedicated ruleset flags.
  291. Ruleset layers
  292. --------------
  293. There is a limit of 16 layers of stacked rulesets. This can be an issue for a
  294. task willing to enforce a new ruleset in complement to its 16 inherited
  295. rulesets. Once this limit is reached, sys_landlock_restrict_self() returns
  296. E2BIG. It is then strongly suggested to carefully build rulesets once in the
  297. life of a thread, especially for applications able to launch other applications
  298. that may also want to sandbox themselves (e.g. shells, container managers,
  299. etc.).
  300. Memory usage
  301. ------------
  302. Kernel memory allocated to create rulesets is accounted and can be restricted
  303. by the Documentation/admin-guide/cgroup-v1/memory.rst.
  304. Previous limitations
  305. ====================
  306. File renaming and linking (ABI < 2)
  307. -----------------------------------
  308. Because Landlock targets unprivileged access controls, it needs to properly
  309. handle composition of rules. Such property also implies rules nesting.
  310. Properly handling multiple layers of rulesets, each one of them able to
  311. restrict access to files, also implies inheritance of the ruleset restrictions
  312. from a parent to its hierarchy. Because files are identified and restricted by
  313. their hierarchy, moving or linking a file from one directory to another implies
  314. propagation of the hierarchy constraints, or restriction of these actions
  315. according to the potentially lost constraints. To protect against privilege
  316. escalations through renaming or linking, and for the sake of simplicity,
  317. Landlock previously limited linking and renaming to the same directory.
  318. Starting with the Landlock ABI version 2, it is now possible to securely
  319. control renaming and linking thanks to the new ``LANDLOCK_ACCESS_FS_REFER``
  320. access right.
  321. .. _kernel_support:
  322. Kernel support
  323. ==============
  324. Landlock was first introduced in Linux 5.13 but it must be configured at build
  325. time with ``CONFIG_SECURITY_LANDLOCK=y``. Landlock must also be enabled at boot
  326. time as the other security modules. The list of security modules enabled by
  327. default is set with ``CONFIG_LSM``. The kernel configuration should then
  328. contains ``CONFIG_LSM=landlock,[...]`` with ``[...]`` as the list of other
  329. potentially useful security modules for the running system (see the
  330. ``CONFIG_LSM`` help).
  331. If the running kernel does not have ``landlock`` in ``CONFIG_LSM``, then we can
  332. still enable it by adding ``lsm=landlock,[...]`` to
  333. Documentation/admin-guide/kernel-parameters.rst thanks to the bootloader
  334. configuration.
  335. Questions and answers
  336. =====================
  337. What about user space sandbox managers?
  338. ---------------------------------------
  339. Using user space process to enforce restrictions on kernel resources can lead
  340. to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of
  341. the OS code and state
  342. <https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_).
  343. What about namespaces and containers?
  344. -------------------------------------
  345. Namespaces can help create sandboxes but they are not designed for
  346. access-control and then miss useful features for such use case (e.g. no
  347. fine-grained restrictions). Moreover, their complexity can lead to security
  348. issues, especially when untrusted processes can manipulate them (cf.
  349. `Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_).
  350. Additional documentation
  351. ========================
  352. * Documentation/security/landlock.rst
  353. * https://landlock.io
  354. .. Links
  355. .. _samples/landlock/sandboxer.c:
  356. https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.c