landlock.rst 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  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: October 2024
  10. The goal of Landlock is to enable restriction of ambient rights (e.g. global
  11. filesystem or network access) for a set of processes. Because Landlock
  12. is a stackable LSM, it makes it possible to create safe security sandboxes as
  13. new security layers in addition to the existing system-wide access-controls.
  14. This kind of sandbox 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):
  19. ``dmesg | grep landlock || journalctl -kb -g landlock`` .
  20. Developers can also easily check for Landlock support with a
  21. :ref:`related system call <landlock_abi_versions>`.
  22. If Landlock is not currently supported, we need to
  23. :ref:`configure the kernel appropriately <kernel_support>`.
  24. Landlock rules
  25. ==============
  26. A Landlock rule describes an action on an object which the process intends to
  27. perform. A set of rules is aggregated in a ruleset, which can then restrict
  28. the thread enforcing it, and its future children.
  29. The two existing types of rules are:
  30. Filesystem rules
  31. For these rules, the object is a file hierarchy,
  32. and the related filesystem actions are defined with
  33. `filesystem access rights`.
  34. Network rules (since ABI v4)
  35. For these rules, the object is a TCP port,
  36. and the related actions are defined with `network access rights`.
  37. Defining and enforcing a security policy
  38. ----------------------------------------
  39. We first need to define the ruleset that will contain our rules.
  40. For this example, the ruleset will contain rules that only allow filesystem
  41. read actions and establish a specific TCP connection. Filesystem write
  42. actions and other TCP actions will be denied.
  43. The ruleset then needs to handle both these kinds of actions. This is
  44. required for backward and forward compatibility (i.e. the kernel and user
  45. space may not know each other's supported restrictions), hence the need
  46. to be explicit about the denied-by-default access rights.
  47. .. code-block:: c
  48. struct landlock_ruleset_attr ruleset_attr = {
  49. .handled_access_fs =
  50. LANDLOCK_ACCESS_FS_EXECUTE |
  51. LANDLOCK_ACCESS_FS_WRITE_FILE |
  52. LANDLOCK_ACCESS_FS_READ_FILE |
  53. LANDLOCK_ACCESS_FS_READ_DIR |
  54. LANDLOCK_ACCESS_FS_REMOVE_DIR |
  55. LANDLOCK_ACCESS_FS_REMOVE_FILE |
  56. LANDLOCK_ACCESS_FS_MAKE_CHAR |
  57. LANDLOCK_ACCESS_FS_MAKE_DIR |
  58. LANDLOCK_ACCESS_FS_MAKE_REG |
  59. LANDLOCK_ACCESS_FS_MAKE_SOCK |
  60. LANDLOCK_ACCESS_FS_MAKE_FIFO |
  61. LANDLOCK_ACCESS_FS_MAKE_BLOCK |
  62. LANDLOCK_ACCESS_FS_MAKE_SYM |
  63. LANDLOCK_ACCESS_FS_REFER |
  64. LANDLOCK_ACCESS_FS_TRUNCATE |
  65. LANDLOCK_ACCESS_FS_IOCTL_DEV,
  66. .handled_access_net =
  67. LANDLOCK_ACCESS_NET_BIND_TCP |
  68. LANDLOCK_ACCESS_NET_CONNECT_TCP,
  69. .scoped =
  70. LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
  71. LANDLOCK_SCOPE_SIGNAL,
  72. };
  73. Because we may not know which kernel version an application will be executed
  74. on, it is safer to follow a best-effort security approach. Indeed, we
  75. should try to protect users as much as possible whatever the kernel they are
  76. using.
  77. To be compatible with older Linux versions, we detect the available Landlock ABI
  78. version, and only use the available subset of access rights:
  79. .. code-block:: c
  80. int abi;
  81. abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
  82. if (abi < 0) {
  83. /* Degrades gracefully if Landlock is not handled. */
  84. perror("The running kernel does not enable to use Landlock");
  85. return 0;
  86. }
  87. switch (abi) {
  88. case 1:
  89. /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */
  90. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
  91. __attribute__((fallthrough));
  92. case 2:
  93. /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
  94. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
  95. __attribute__((fallthrough));
  96. case 3:
  97. /* Removes network support for ABI < 4 */
  98. ruleset_attr.handled_access_net &=
  99. ~(LANDLOCK_ACCESS_NET_BIND_TCP |
  100. LANDLOCK_ACCESS_NET_CONNECT_TCP);
  101. __attribute__((fallthrough));
  102. case 4:
  103. /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
  104. ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
  105. __attribute__((fallthrough));
  106. case 5:
  107. /* Removes LANDLOCK_SCOPE_* for ABI < 6 */
  108. ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
  109. LANDLOCK_SCOPE_SIGNAL);
  110. }
  111. This enables the creation of an inclusive ruleset that will contain our rules.
  112. .. code-block:: c
  113. int ruleset_fd;
  114. ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  115. if (ruleset_fd < 0) {
  116. perror("Failed to create a ruleset");
  117. return 1;
  118. }
  119. We can now add a new rule to this ruleset thanks to the returned file
  120. descriptor referring to this ruleset. The rule will only allow reading the
  121. file hierarchy ``/usr``. Without another rule, write actions would then be
  122. denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the
  123. ``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with this file
  124. descriptor.
  125. .. code-block:: c
  126. int err;
  127. struct landlock_path_beneath_attr path_beneath = {
  128. .allowed_access =
  129. LANDLOCK_ACCESS_FS_EXECUTE |
  130. LANDLOCK_ACCESS_FS_READ_FILE |
  131. LANDLOCK_ACCESS_FS_READ_DIR,
  132. };
  133. path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
  134. if (path_beneath.parent_fd < 0) {
  135. perror("Failed to open file");
  136. close(ruleset_fd);
  137. return 1;
  138. }
  139. err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  140. &path_beneath, 0);
  141. close(path_beneath.parent_fd);
  142. if (err) {
  143. perror("Failed to update ruleset");
  144. close(ruleset_fd);
  145. return 1;
  146. }
  147. It may also be required to create rules following the same logic as explained
  148. for the ruleset creation, by filtering access rights according to the Landlock
  149. ABI version. In this example, this is not required because all of the requested
  150. ``allowed_access`` rights are already available in ABI 1.
  151. For network access-control, we can add a set of rules that allow to use a port
  152. number for a specific action: HTTPS connections.
  153. .. code-block:: c
  154. struct landlock_net_port_attr net_port = {
  155. .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
  156. .port = 443,
  157. };
  158. err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
  159. &net_port, 0);
  160. The next step is to restrict the current thread from gaining more privileges
  161. (e.g. through a SUID binary). We now have a ruleset with the first rule
  162. allowing read access to ``/usr`` while denying all other handled accesses for
  163. the filesystem, and a second rule allowing HTTPS connections.
  164. .. code-block:: c
  165. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  166. perror("Failed to restrict privileges");
  167. close(ruleset_fd);
  168. return 1;
  169. }
  170. The current thread is now ready to sandbox itself with the ruleset.
  171. .. code-block:: c
  172. if (landlock_restrict_self(ruleset_fd, 0)) {
  173. perror("Failed to enforce ruleset");
  174. close(ruleset_fd);
  175. return 1;
  176. }
  177. close(ruleset_fd);
  178. If the ``landlock_restrict_self`` system call succeeds, the current thread is
  179. now restricted and this policy will be enforced on all its subsequently created
  180. children as well. Once a thread is landlocked, there is no way to remove its
  181. security policy; only adding more restrictions is allowed. These threads are
  182. now in a new Landlock domain, which is a merger of their parent one (if any)
  183. with the new ruleset.
  184. Full working code can be found in `samples/landlock/sandboxer.c`_.
  185. Good practices
  186. --------------
  187. It is recommended to set access rights to file hierarchy leaves as much as
  188. possible. For instance, it is better to be able to have ``~/doc/`` as a
  189. read-only hierarchy and ``~/tmp/`` as a read-write hierarchy, compared to
  190. ``~/`` as a read-only hierarchy and ``~/tmp/`` as a read-write hierarchy.
  191. Following this good practice leads to self-sufficient hierarchies that do not
  192. depend on their location (i.e. parent directories). This is particularly
  193. relevant when we want to allow linking or renaming. Indeed, having consistent
  194. access rights per directory enables changing the location of such directories
  195. without relying on the destination directory access rights (except those that
  196. are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
  197. documentation).
  198. Having self-sufficient hierarchies also helps to tighten the required access
  199. rights to the minimal set of data. This also helps avoid sinkhole directories,
  200. i.e. directories where data can be linked to but not linked from. However,
  201. this depends on data organization, which might not be controlled by developers.
  202. In this case, granting read-write access to ``~/tmp/``, instead of write-only
  203. access, would potentially allow moving ``~/tmp/`` to a non-readable directory
  204. and still keep the ability to list the content of ``~/tmp/``.
  205. Layers of file path access rights
  206. ---------------------------------
  207. Each time a thread enforces a ruleset on itself, it updates its Landlock domain
  208. with a new layer of policy. This complementary policy is stacked with any
  209. other rulesets potentially already restricting this thread. A sandboxed thread
  210. can then safely add more constraints to itself with a new enforced ruleset.
  211. One policy layer grants access to a file path if at least one of its rules
  212. encountered on the path grants the access. A sandboxed thread can only access
  213. a file path if all its enforced policy layers grant the access as well as all
  214. the other system access controls (e.g. filesystem DAC, other LSM policies,
  215. etc.).
  216. Bind mounts and OverlayFS
  217. -------------------------
  218. Landlock enables restricting access to file hierarchies, which means that these
  219. access rights can be propagated with bind mounts (cf.
  220. Documentation/filesystems/sharedsubtree.rst) but not with
  221. Documentation/filesystems/overlayfs.rst.
  222. A bind mount mirrors a source file hierarchy to a destination. The destination
  223. hierarchy is then composed of the exact same files, on which Landlock rules can
  224. be tied, either via the source or the destination path. These rules restrict
  225. access when they are encountered on a path, which means that they can restrict
  226. access to multiple file hierarchies at the same time, whether these hierarchies
  227. are the result of bind mounts or not.
  228. An OverlayFS mount point consists of upper and lower layers. These layers are
  229. combined in a merge directory, and that merged directory becomes available at
  230. the mount point. This merge hierarchy may include files from the upper and
  231. lower layers, but modifications performed on the merge hierarchy only reflect
  232. on the upper layer. From a Landlock policy point of view, all OverlayFS layers
  233. and merge hierarchies are standalone and each contains their own set of files
  234. and directories, which is different from bind mounts. A policy restricting an
  235. OverlayFS layer will not restrict the resulted merged hierarchy, and vice versa.
  236. Landlock users should then only think about file hierarchies they want to allow
  237. access to, regardless of the underlying filesystem.
  238. Inheritance
  239. -----------
  240. Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain
  241. restrictions from its parent. This is similar to seccomp inheritance (cf.
  242. Documentation/userspace-api/seccomp_filter.rst) or any other LSM dealing with
  243. task's :manpage:`credentials(7)`. For instance, one process's thread may apply
  244. Landlock rules to itself, but they will not be automatically applied to other
  245. sibling threads (unlike POSIX thread credential changes, cf.
  246. :manpage:`nptl(7)`).
  247. When a thread sandboxes itself, we have the guarantee that the related security
  248. policy will stay enforced on all this thread's descendants. This allows
  249. creating standalone and modular security policies per application, which will
  250. automatically be composed between themselves according to their runtime parent
  251. policies.
  252. Ptrace restrictions
  253. -------------------
  254. A sandboxed process has less privileges than a non-sandboxed process and must
  255. then be subject to additional restrictions when manipulating another process.
  256. To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
  257. process, a sandboxed process should have a superset of the target process's
  258. access rights, which means the tracee must be in a sub-domain of the tracer.
  259. IPC scoping
  260. -----------
  261. Similar to the implicit `Ptrace restrictions`_, we may want to further restrict
  262. interactions between sandboxes. Each Landlock domain can be explicitly scoped
  263. for a set of actions by specifying it on a ruleset. For example, if a
  264. sandboxed process should not be able to :manpage:`connect(2)` to a
  265. non-sandboxed process through abstract :manpage:`unix(7)` sockets, we can
  266. specify such a restriction with ``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET``.
  267. Moreover, if a sandboxed process should not be able to send a signal to a
  268. non-sandboxed process, we can specify this restriction with
  269. ``LANDLOCK_SCOPE_SIGNAL``.
  270. A sandboxed process can connect to a non-sandboxed process when its domain is
  271. not scoped. If a process's domain is scoped, it can only connect to sockets
  272. created by processes in the same scope.
  273. Moreover, If a process is scoped to send signal to a non-scoped process, it can
  274. only send signals to processes in the same scope.
  275. A connected datagram socket behaves like a stream socket when its domain is
  276. scoped, meaning if the domain is scoped after the socket is connected , it can
  277. still :manpage:`send(2)` data just like a stream socket. However, in the same
  278. scenario, a non-connected datagram socket cannot send data (with
  279. :manpage:`sendto(2)`) outside its scope.
  280. A process with a scoped domain can inherit a socket created by a non-scoped
  281. process. The process cannot connect to this socket since it has a scoped
  282. domain.
  283. IPC scoping does not support exceptions, so if a domain is scoped, no rules can
  284. be added to allow access to resources or processes outside of the scope.
  285. Truncating files
  286. ----------------
  287. The operations covered by ``LANDLOCK_ACCESS_FS_WRITE_FILE`` and
  288. ``LANDLOCK_ACCESS_FS_TRUNCATE`` both change the contents of a file and sometimes
  289. overlap in non-intuitive ways. It is recommended to always specify both of
  290. these together.
  291. A particularly surprising example is :manpage:`creat(2)`. The name suggests
  292. that this system call requires the rights to create and write files. However,
  293. it also requires the truncate right if an existing file under the same name is
  294. already present.
  295. It should also be noted that truncating files does not require the
  296. ``LANDLOCK_ACCESS_FS_WRITE_FILE`` right. Apart from the :manpage:`truncate(2)`
  297. system call, this can also be done through :manpage:`open(2)` with the flags
  298. ``O_RDONLY | O_TRUNC``.
  299. The truncate right is associated with the opened file (see below).
  300. Rights associated with file descriptors
  301. ---------------------------------------
  302. When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE`` and
  303. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` rights is associated with the newly created
  304. file descriptor and will be used for subsequent truncation and ioctl attempts
  305. using :manpage:`ftruncate(2)` and :manpage:`ioctl(2)`. The behavior is similar
  306. to opening a file for reading or writing, where permissions are checked during
  307. :manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
  308. :manpage:`write(2)` calls.
  309. As a consequence, it is possible that a process has multiple open file
  310. descriptors referring to the same file, but Landlock enforces different things
  311. when operating with these file descriptors. This can happen when a Landlock
  312. ruleset gets enforced and the process keeps file descriptors which were opened
  313. both before and after the enforcement. It is also possible to pass such file
  314. descriptors between processes, keeping their Landlock properties, even when some
  315. of the involved processes do not have an enforced Landlock ruleset.
  316. Compatibility
  317. =============
  318. Backward and forward compatibility
  319. ----------------------------------
  320. Landlock is designed to be compatible with past and future versions of the
  321. kernel. This is achieved thanks to the system call attributes and the
  322. associated bitflags, particularly the ruleset's ``handled_access_fs``. Making
  323. handled access rights explicit enables the kernel and user space to have a clear
  324. contract with each other. This is required to make sure sandboxing will not
  325. get stricter with a system update, which could break applications.
  326. Developers can subscribe to the `Landlock mailing list
  327. <https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
  328. test their applications with the latest available features. In the interest of
  329. users, and because they may use different kernel versions, it is strongly
  330. encouraged to follow a best-effort security approach by checking the Landlock
  331. ABI version at runtime and only enforcing the supported features.
  332. .. _landlock_abi_versions:
  333. Landlock ABI versions
  334. ---------------------
  335. The Landlock ABI version can be read with the sys_landlock_create_ruleset()
  336. system call:
  337. .. code-block:: c
  338. int abi;
  339. abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
  340. if (abi < 0) {
  341. switch (errno) {
  342. case ENOSYS:
  343. printf("Landlock is not supported by the current kernel.\n");
  344. break;
  345. case EOPNOTSUPP:
  346. printf("Landlock is currently disabled.\n");
  347. break;
  348. }
  349. return 0;
  350. }
  351. if (abi >= 2) {
  352. printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
  353. }
  354. The following kernel interfaces are implicitly supported by the first ABI
  355. version. Features only supported from a specific version are explicitly marked
  356. as such.
  357. Kernel interface
  358. ================
  359. Access rights
  360. -------------
  361. .. kernel-doc:: include/uapi/linux/landlock.h
  362. :identifiers: fs_access net_access scope
  363. Creating a new ruleset
  364. ----------------------
  365. .. kernel-doc:: security/landlock/syscalls.c
  366. :identifiers: sys_landlock_create_ruleset
  367. .. kernel-doc:: include/uapi/linux/landlock.h
  368. :identifiers: landlock_ruleset_attr
  369. Extending a ruleset
  370. -------------------
  371. .. kernel-doc:: security/landlock/syscalls.c
  372. :identifiers: sys_landlock_add_rule
  373. .. kernel-doc:: include/uapi/linux/landlock.h
  374. :identifiers: landlock_rule_type landlock_path_beneath_attr
  375. landlock_net_port_attr
  376. Enforcing a ruleset
  377. -------------------
  378. .. kernel-doc:: security/landlock/syscalls.c
  379. :identifiers: sys_landlock_restrict_self
  380. Current limitations
  381. ===================
  382. Filesystem topology modification
  383. --------------------------------
  384. Threads sandboxed with filesystem restrictions cannot modify filesystem
  385. topology, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`.
  386. However, :manpage:`chroot(2)` calls are not denied.
  387. Special filesystems
  388. -------------------
  389. Access to regular files and directories can be restricted by Landlock,
  390. according to the handled accesses of a ruleset. However, files that do not
  391. come from a user-visible filesystem (e.g. pipe, socket), but can still be
  392. accessed through ``/proc/<pid>/fd/*``, cannot currently be explicitly
  393. restricted. Likewise, some special kernel filesystems such as nsfs, which can
  394. be accessed through ``/proc/<pid>/ns/*``, cannot currently be explicitly
  395. restricted. However, thanks to the `ptrace restrictions`_, access to such
  396. sensitive ``/proc`` files are automatically restricted according to domain
  397. hierarchies. Future Landlock evolutions could still enable to explicitly
  398. restrict such paths with dedicated ruleset flags.
  399. Ruleset layers
  400. --------------
  401. There is a limit of 16 layers of stacked rulesets. This can be an issue for a
  402. task willing to enforce a new ruleset in complement to its 16 inherited
  403. rulesets. Once this limit is reached, sys_landlock_restrict_self() returns
  404. E2BIG. It is then strongly suggested to carefully build rulesets once in the
  405. life of a thread, especially for applications able to launch other applications
  406. that may also want to sandbox themselves (e.g. shells, container managers,
  407. etc.).
  408. Memory usage
  409. ------------
  410. Kernel memory allocated to create rulesets is accounted and can be restricted
  411. by the Documentation/admin-guide/cgroup-v1/memory.rst.
  412. IOCTL support
  413. -------------
  414. The ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right restricts the use of
  415. :manpage:`ioctl(2)`, but it only applies to *newly opened* device files. This
  416. means specifically that pre-existing file descriptors like stdin, stdout and
  417. stderr are unaffected.
  418. Users should be aware that TTY devices have traditionally permitted to control
  419. other processes on the same TTY through the ``TIOCSTI`` and ``TIOCLINUX`` IOCTL
  420. commands. Both of these require ``CAP_SYS_ADMIN`` on modern Linux systems, but
  421. the behavior is configurable for ``TIOCSTI``.
  422. On older systems, it is therefore recommended to close inherited TTY file
  423. descriptors, or to reopen them from ``/proc/self/fd/*`` without the
  424. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right, if possible.
  425. Landlock's IOCTL support is coarse-grained at the moment, but may become more
  426. fine-grained in the future. Until then, users are advised to establish the
  427. guarantees that they need through the file hierarchy, by only allowing the
  428. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right on files where it is really required.
  429. Previous limitations
  430. ====================
  431. File renaming and linking (ABI < 2)
  432. -----------------------------------
  433. Because Landlock targets unprivileged access controls, it needs to properly
  434. handle composition of rules. Such property also implies rules nesting.
  435. Properly handling multiple layers of rulesets, each one of them able to
  436. restrict access to files, also implies inheritance of the ruleset restrictions
  437. from a parent to its hierarchy. Because files are identified and restricted by
  438. their hierarchy, moving or linking a file from one directory to another implies
  439. propagation of the hierarchy constraints, or restriction of these actions
  440. according to the potentially lost constraints. To protect against privilege
  441. escalations through renaming or linking, and for the sake of simplicity,
  442. Landlock previously limited linking and renaming to the same directory.
  443. Starting with the Landlock ABI version 2, it is now possible to securely
  444. control renaming and linking thanks to the new ``LANDLOCK_ACCESS_FS_REFER``
  445. access right.
  446. File truncation (ABI < 3)
  447. -------------------------
  448. File truncation could not be denied before the third Landlock ABI, so it is
  449. always allowed when using a kernel that only supports the first or second ABI.
  450. Starting with the Landlock ABI version 3, it is now possible to securely control
  451. truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right.
  452. TCP bind and connect (ABI < 4)
  453. ------------------------------
  454. Starting with the Landlock ABI version 4, it is now possible to restrict TCP
  455. bind and connect actions to only a set of allowed ports thanks to the new
  456. ``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
  457. access rights.
  458. Device IOCTL (ABI < 5)
  459. ----------------------
  460. IOCTL operations could not be denied before the fifth Landlock ABI, so
  461. :manpage:`ioctl(2)` is always allowed when using a kernel that only supports an
  462. earlier ABI.
  463. Starting with the Landlock ABI version 5, it is possible to restrict the use of
  464. :manpage:`ioctl(2)` on character and block devices using the new
  465. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
  466. Abstract UNIX socket (ABI < 6)
  467. ------------------------------
  468. Starting with the Landlock ABI version 6, it is possible to restrict
  469. connections to an abstract :manpage:`unix(7)` socket by setting
  470. ``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`` to the ``scoped`` ruleset attribute.
  471. Signal (ABI < 6)
  472. ----------------
  473. Starting with the Landlock ABI version 6, it is possible to restrict
  474. :manpage:`signal(7)` sending by setting ``LANDLOCK_SCOPE_SIGNAL`` to the
  475. ``scoped`` ruleset attribute.
  476. .. _kernel_support:
  477. Kernel support
  478. ==============
  479. Build time configuration
  480. ------------------------
  481. Landlock was first introduced in Linux 5.13 but it must be configured at build
  482. time with ``CONFIG_SECURITY_LANDLOCK=y``. Landlock must also be enabled at boot
  483. time like other security modules. The list of security modules enabled by
  484. default is set with ``CONFIG_LSM``. The kernel configuration should then
  485. contain ``CONFIG_LSM=landlock,[...]`` with ``[...]`` as the list of other
  486. potentially useful security modules for the running system (see the
  487. ``CONFIG_LSM`` help).
  488. Boot time configuration
  489. -----------------------
  490. If the running kernel does not have ``landlock`` in ``CONFIG_LSM``, then we can
  491. enable Landlock by adding ``lsm=landlock,[...]`` to
  492. Documentation/admin-guide/kernel-parameters.rst in the boot loader
  493. configuration.
  494. For example, if the current built-in configuration is:
  495. .. code-block:: console
  496. $ zgrep -h "^CONFIG_LSM=" "/boot/config-$(uname -r)" /proc/config.gz 2>/dev/null
  497. CONFIG_LSM="lockdown,yama,integrity,apparmor"
  498. ...and if the cmdline doesn't contain ``landlock`` either:
  499. .. code-block:: console
  500. $ sed -n 's/.*\(\<lsm=\S\+\).*/\1/p' /proc/cmdline
  501. lsm=lockdown,yama,integrity,apparmor
  502. ...we should configure the boot loader to set a cmdline extending the ``lsm``
  503. list with the ``landlock,`` prefix::
  504. lsm=landlock,lockdown,yama,integrity,apparmor
  505. After a reboot, we can check that Landlock is up and running by looking at
  506. kernel logs:
  507. .. code-block:: console
  508. # dmesg | grep landlock || journalctl -kb -g landlock
  509. [ 0.000000] Command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
  510. [ 0.000000] Kernel command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
  511. [ 0.000000] LSM: initializing lsm=lockdown,capability,landlock,yama,integrity,apparmor
  512. [ 0.000000] landlock: Up and running.
  513. The kernel may be configured at build time to always load the ``lockdown`` and
  514. ``capability`` LSMs. In that case, these LSMs will appear at the beginning of
  515. the ``LSM: initializing`` log line as well, even if they are not configured in
  516. the boot loader.
  517. Network support
  518. ---------------
  519. To be able to explicitly allow TCP operations (e.g., adding a network rule with
  520. ``LANDLOCK_ACCESS_NET_BIND_TCP``), the kernel must support TCP
  521. (``CONFIG_INET=y``). Otherwise, sys_landlock_add_rule() returns an
  522. ``EAFNOSUPPORT`` error, which can safely be ignored because this kind of TCP
  523. operation is already not possible.
  524. Questions and answers
  525. =====================
  526. What about user space sandbox managers?
  527. ---------------------------------------
  528. Using user space processes to enforce restrictions on kernel resources can lead
  529. to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of
  530. the OS code and state
  531. <https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_).
  532. What about namespaces and containers?
  533. -------------------------------------
  534. Namespaces can help create sandboxes but they are not designed for
  535. access-control and then miss useful features for such use case (e.g. no
  536. fine-grained restrictions). Moreover, their complexity can lead to security
  537. issues, especially when untrusted processes can manipulate them (cf.
  538. `Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_).
  539. Additional documentation
  540. ========================
  541. * Documentation/security/landlock.rst
  542. * https://landlock.io
  543. .. Links
  544. .. _samples/landlock/sandboxer.c:
  545. https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.c