landlock.rst 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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: March 2026
  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 allow reading and
  121. executing the file hierarchy ``/usr``. Without another rule, write actions
  122. would then be denied by the ruleset. To add ``/usr`` to the ruleset, we open
  123. it with the ``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with
  124. this file 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. When passing a non-zero ``flags`` argument to ``landlock_restrict_self()``, a
  161. similar backwards compatibility check is needed for the restrict flags
  162. (see sys_landlock_restrict_self() documentation for available flags):
  163. .. code-block:: c
  164. __u32 restrict_flags =
  165. LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
  166. LANDLOCK_RESTRICT_SELF_TSYNC;
  167. switch (abi) {
  168. case 1 ... 6:
  169. /* Removes logging flags for ABI < 7 */
  170. restrict_flags &= ~(LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF |
  171. LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
  172. LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
  173. __attribute__((fallthrough));
  174. case 7:
  175. /*
  176. * Removes multithreaded enforcement flag for ABI < 8
  177. *
  178. * WARNING: Without this flag, calling landlock_restrict_self(2) is
  179. * only equivalent if the calling process is single-threaded. Below
  180. * ABI v8 (and as of ABI v8, when not using this flag), a Landlock
  181. * policy would only be enforced for the calling thread and its
  182. * children (and not for all threads, including parents and siblings).
  183. */
  184. restrict_flags &= ~LANDLOCK_RESTRICT_SELF_TSYNC;
  185. }
  186. The next step is to restrict the current thread from gaining more privileges
  187. (e.g. through a SUID binary). We now have a ruleset with the first rule
  188. allowing read and execute access to ``/usr`` while denying all other handled
  189. accesses for the filesystem, and a second rule allowing HTTPS connections.
  190. .. code-block:: c
  191. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  192. perror("Failed to restrict privileges");
  193. close(ruleset_fd);
  194. return 1;
  195. }
  196. The current thread is now ready to sandbox itself with the ruleset.
  197. .. code-block:: c
  198. if (landlock_restrict_self(ruleset_fd, restrict_flags)) {
  199. perror("Failed to enforce ruleset");
  200. close(ruleset_fd);
  201. return 1;
  202. }
  203. close(ruleset_fd);
  204. If the ``landlock_restrict_self`` system call succeeds, the current thread is
  205. now restricted and this policy will be enforced on all its subsequently created
  206. children as well. Once a thread is landlocked, there is no way to remove its
  207. security policy; only adding more restrictions is allowed. These threads are
  208. now in a new Landlock domain, which is a merger of their parent one (if any)
  209. with the new ruleset.
  210. Full working code can be found in `samples/landlock/sandboxer.c`_.
  211. Good practices
  212. --------------
  213. It is recommended to set access rights to file hierarchy leaves as much as
  214. possible. For instance, it is better to be able to have ``~/doc/`` as a
  215. read-only hierarchy and ``~/tmp/`` as a read-write hierarchy, compared to
  216. ``~/`` as a read-only hierarchy and ``~/tmp/`` as a read-write hierarchy.
  217. Following this good practice leads to self-sufficient hierarchies that do not
  218. depend on their location (i.e. parent directories). This is particularly
  219. relevant when we want to allow linking or renaming. Indeed, having consistent
  220. access rights per directory enables changing the location of such directories
  221. without relying on the destination directory access rights (except those that
  222. are required for this operation, see ``LANDLOCK_ACCESS_FS_REFER``
  223. documentation).
  224. Having self-sufficient hierarchies also helps to tighten the required access
  225. rights to the minimal set of data. This also helps avoid sinkhole directories,
  226. i.e. directories where data can be linked to but not linked from. However,
  227. this depends on data organization, which might not be controlled by developers.
  228. In this case, granting read-write access to ``~/tmp/``, instead of write-only
  229. access, would potentially allow moving ``~/tmp/`` to a non-readable directory
  230. and still keep the ability to list the content of ``~/tmp/``.
  231. Layers of file path access rights
  232. ---------------------------------
  233. Each time a thread enforces a ruleset on itself, it updates its Landlock domain
  234. with a new layer of policy. This complementary policy is stacked with any
  235. other rulesets potentially already restricting this thread. A sandboxed thread
  236. can then safely add more constraints to itself with a new enforced ruleset.
  237. One policy layer grants access to a file path if at least one of its rules
  238. encountered on the path grants the access. A sandboxed thread can only access
  239. a file path if all its enforced policy layers grant the access as well as all
  240. the other system access controls (e.g. filesystem DAC, other LSM policies,
  241. etc.).
  242. Bind mounts and OverlayFS
  243. -------------------------
  244. Landlock enables restricting access to file hierarchies, which means that these
  245. access rights can be propagated with bind mounts (cf.
  246. Documentation/filesystems/sharedsubtree.rst) but not with
  247. Documentation/filesystems/overlayfs.rst.
  248. A bind mount mirrors a source file hierarchy to a destination. The destination
  249. hierarchy is then composed of the exact same files, on which Landlock rules can
  250. be tied, either via the source or the destination path. These rules restrict
  251. access when they are encountered on a path, which means that they can restrict
  252. access to multiple file hierarchies at the same time, whether these hierarchies
  253. are the result of bind mounts or not.
  254. An OverlayFS mount point consists of upper and lower layers. These layers are
  255. combined in a merge directory, and that merged directory becomes available at
  256. the mount point. This merge hierarchy may include files from the upper and
  257. lower layers, but modifications performed on the merge hierarchy only reflect
  258. on the upper layer. From a Landlock policy point of view, all OverlayFS layers
  259. and merge hierarchies are standalone and each contains their own set of files
  260. and directories, which is different from bind mounts. A policy restricting an
  261. OverlayFS layer will not restrict the resulted merged hierarchy, and vice versa.
  262. Landlock users should then only think about file hierarchies they want to allow
  263. access to, regardless of the underlying filesystem.
  264. Inheritance
  265. -----------
  266. Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain
  267. restrictions from its parent. This is similar to seccomp inheritance (cf.
  268. Documentation/userspace-api/seccomp_filter.rst) or any other LSM dealing with
  269. task's :manpage:`credentials(7)`. For instance, one process's thread may apply
  270. Landlock rules to itself, but they will not be automatically applied to other
  271. sibling threads (unlike POSIX thread credential changes, cf.
  272. :manpage:`nptl(7)`).
  273. When a thread sandboxes itself, we have the guarantee that the related security
  274. policy will stay enforced on all this thread's descendants. This allows
  275. creating standalone and modular security policies per application, which will
  276. automatically be composed between themselves according to their runtime parent
  277. policies.
  278. Ptrace restrictions
  279. -------------------
  280. A sandboxed process has less privileges than a non-sandboxed process and must
  281. then be subject to additional restrictions when manipulating another process.
  282. To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
  283. process, a sandboxed process should have a superset of the target process's
  284. access rights, which means the tracee must be in a sub-domain of the tracer.
  285. IPC scoping
  286. -----------
  287. Similar to the implicit `Ptrace restrictions`_, we may want to further restrict
  288. interactions between sandboxes. Therefore, at ruleset creation time, each
  289. Landlock domain can restrict the scope for certain operations, so that these
  290. operations can only reach out to processes within the same Landlock domain or in
  291. a nested Landlock domain (the "scope").
  292. The operations which can be scoped are:
  293. ``LANDLOCK_SCOPE_SIGNAL``
  294. This limits the sending of signals to target processes which run within the
  295. same or a nested Landlock domain.
  296. ``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET``
  297. This limits the set of abstract :manpage:`unix(7)` sockets to which we can
  298. :manpage:`connect(2)` to socket addresses which were created by a process in
  299. the same or a nested Landlock domain.
  300. A :manpage:`sendto(2)` on a non-connected datagram socket is treated as if
  301. it were doing an implicit :manpage:`connect(2)` and will be blocked if the
  302. remote end does not stem from the same or a nested Landlock domain.
  303. A :manpage:`sendto(2)` on a socket which was previously connected will not
  304. be restricted. This works for both datagram and stream sockets.
  305. IPC scoping does not support exceptions via :manpage:`landlock_add_rule(2)`.
  306. If an operation is scoped within a domain, no rules can be added to allow access
  307. to resources or processes outside of the scope.
  308. Truncating files
  309. ----------------
  310. The operations covered by ``LANDLOCK_ACCESS_FS_WRITE_FILE`` and
  311. ``LANDLOCK_ACCESS_FS_TRUNCATE`` both change the contents of a file and sometimes
  312. overlap in non-intuitive ways. It is recommended to always specify both of
  313. these together.
  314. A particularly surprising example is :manpage:`creat(2)`. The name suggests
  315. that this system call requires the rights to create and write files. However,
  316. it also requires the truncate right if an existing file under the same name is
  317. already present.
  318. It should also be noted that truncating files does not require the
  319. ``LANDLOCK_ACCESS_FS_WRITE_FILE`` right. Apart from the :manpage:`truncate(2)`
  320. system call, this can also be done through :manpage:`open(2)` with the flags
  321. ``O_RDONLY | O_TRUNC``.
  322. The truncate right is associated with the opened file (see below).
  323. Rights associated with file descriptors
  324. ---------------------------------------
  325. When opening a file, the availability of the ``LANDLOCK_ACCESS_FS_TRUNCATE`` and
  326. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` rights is associated with the newly created
  327. file descriptor and will be used for subsequent truncation and ioctl attempts
  328. using :manpage:`ftruncate(2)` and :manpage:`ioctl(2)`. The behavior is similar
  329. to opening a file for reading or writing, where permissions are checked during
  330. :manpage:`open(2)`, but not during the subsequent :manpage:`read(2)` and
  331. :manpage:`write(2)` calls.
  332. As a consequence, it is possible that a process has multiple open file
  333. descriptors referring to the same file, but Landlock enforces different things
  334. when operating with these file descriptors. This can happen when a Landlock
  335. ruleset gets enforced and the process keeps file descriptors which were opened
  336. both before and after the enforcement. It is also possible to pass such file
  337. descriptors between processes, keeping their Landlock properties, even when some
  338. of the involved processes do not have an enforced Landlock ruleset.
  339. Compatibility
  340. =============
  341. Backward and forward compatibility
  342. ----------------------------------
  343. Landlock is designed to be compatible with past and future versions of the
  344. kernel. This is achieved thanks to the system call attributes and the
  345. associated bitflags, particularly the ruleset's ``handled_access_fs``. Making
  346. handled access rights explicit enables the kernel and user space to have a clear
  347. contract with each other. This is required to make sure sandboxing will not
  348. get stricter with a system update, which could break applications.
  349. Developers can subscribe to the `Landlock mailing list
  350. <https://subspace.kernel.org/lists.linux.dev.html>`_ to knowingly update and
  351. test their applications with the latest available features. In the interest of
  352. users, and because they may use different kernel versions, it is strongly
  353. encouraged to follow a best-effort security approach by checking the Landlock
  354. ABI version at runtime and only enforcing the supported features.
  355. .. _landlock_abi_versions:
  356. Landlock ABI versions
  357. ---------------------
  358. The Landlock ABI version can be read with the sys_landlock_create_ruleset()
  359. system call:
  360. .. code-block:: c
  361. int abi;
  362. abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
  363. if (abi < 0) {
  364. switch (errno) {
  365. case ENOSYS:
  366. printf("Landlock is not supported by the current kernel.\n");
  367. break;
  368. case EOPNOTSUPP:
  369. printf("Landlock is currently disabled.\n");
  370. break;
  371. }
  372. return 0;
  373. }
  374. if (abi >= 2) {
  375. printf("Landlock supports LANDLOCK_ACCESS_FS_REFER.\n");
  376. }
  377. All Landlock kernel interfaces are supported by the first ABI version unless
  378. explicitly noted in their documentation.
  379. Landlock errata
  380. ---------------
  381. In addition to ABI versions, Landlock provides an errata mechanism to track
  382. fixes for issues that may affect backwards compatibility or require userspace
  383. awareness. The errata bitmask can be queried using:
  384. .. code-block:: c
  385. int errata;
  386. errata = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_ERRATA);
  387. if (errata < 0) {
  388. /* Landlock not available or disabled */
  389. return 0;
  390. }
  391. The returned value is a bitmask where each bit represents a specific erratum.
  392. If bit N is set (``errata & (1 << (N - 1))``), then erratum N has been fixed
  393. in the running kernel.
  394. .. warning::
  395. **Most applications should NOT check errata.** In 99.9% of cases, checking
  396. errata is unnecessary, increases code complexity, and can potentially
  397. decrease protection if misused. For example, disabling the sandbox when an
  398. erratum is not fixed could leave the system less secure than using
  399. Landlock's best-effort protection. When in doubt, ignore errata.
  400. .. kernel-doc:: security/landlock/errata/abi-4.h
  401. :doc: erratum_1
  402. .. kernel-doc:: security/landlock/errata/abi-6.h
  403. :doc: erratum_2
  404. .. kernel-doc:: security/landlock/errata/abi-1.h
  405. :doc: erratum_3
  406. How to check for errata
  407. ~~~~~~~~~~~~~~~~~~~~~~~
  408. If you determine that your application needs to check for specific errata,
  409. use this pattern:
  410. .. code-block:: c
  411. int errata = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_ERRATA);
  412. if (errata >= 0) {
  413. /* Check for specific erratum (1-indexed) */
  414. if (errata & (1 << (erratum_number - 1))) {
  415. /* Erratum N is fixed in this kernel */
  416. } else {
  417. /* Erratum N is NOT fixed - consider implications for your use case */
  418. }
  419. }
  420. **Important:** Only check errata if your application specifically relies on
  421. behavior that changed due to the fix. The fixes generally make Landlock less
  422. restrictive or more correct, not more restrictive.
  423. Kernel interface
  424. ================
  425. Access rights
  426. -------------
  427. .. kernel-doc:: include/uapi/linux/landlock.h
  428. :identifiers: fs_access net_access scope
  429. Creating a new ruleset
  430. ----------------------
  431. .. kernel-doc:: security/landlock/syscalls.c
  432. :identifiers: sys_landlock_create_ruleset
  433. .. kernel-doc:: include/uapi/linux/landlock.h
  434. :identifiers: landlock_ruleset_attr
  435. Extending a ruleset
  436. -------------------
  437. .. kernel-doc:: security/landlock/syscalls.c
  438. :identifiers: sys_landlock_add_rule
  439. .. kernel-doc:: include/uapi/linux/landlock.h
  440. :identifiers: landlock_rule_type landlock_path_beneath_attr
  441. landlock_net_port_attr
  442. Enforcing a ruleset
  443. -------------------
  444. .. kernel-doc:: security/landlock/syscalls.c
  445. :identifiers: sys_landlock_restrict_self
  446. Current limitations
  447. ===================
  448. Filesystem topology modification
  449. --------------------------------
  450. Threads sandboxed with filesystem restrictions cannot modify filesystem
  451. topology, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`.
  452. However, :manpage:`chroot(2)` calls are not denied.
  453. Special filesystems
  454. -------------------
  455. Access to regular files and directories can be restricted by Landlock,
  456. according to the handled accesses of a ruleset. However, files that do not
  457. come from a user-visible filesystem (e.g. pipe, socket), but can still be
  458. accessed through ``/proc/<pid>/fd/*``, cannot currently be explicitly
  459. restricted. Likewise, some special kernel filesystems such as nsfs, which can
  460. be accessed through ``/proc/<pid>/ns/*``, cannot currently be explicitly
  461. restricted. However, thanks to the `ptrace restrictions`_, access to such
  462. sensitive ``/proc`` files are automatically restricted according to domain
  463. hierarchies. Future Landlock evolutions could still enable to explicitly
  464. restrict such paths with dedicated ruleset flags.
  465. Ruleset layers
  466. --------------
  467. There is a limit of 16 layers of stacked rulesets. This can be an issue for a
  468. task willing to enforce a new ruleset in complement to its 16 inherited
  469. rulesets. Once this limit is reached, sys_landlock_restrict_self() returns
  470. E2BIG. It is then strongly suggested to carefully build rulesets once in the
  471. life of a thread, especially for applications able to launch other applications
  472. that may also want to sandbox themselves (e.g. shells, container managers,
  473. etc.).
  474. Memory usage
  475. ------------
  476. Kernel memory allocated to create rulesets is accounted and can be restricted
  477. by the Documentation/admin-guide/cgroup-v1/memory.rst.
  478. IOCTL support
  479. -------------
  480. The ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right restricts the use of
  481. :manpage:`ioctl(2)`, but it only applies to *newly opened* device files. This
  482. means specifically that pre-existing file descriptors like stdin, stdout and
  483. stderr are unaffected.
  484. Users should be aware that TTY devices have traditionally permitted to control
  485. other processes on the same TTY through the ``TIOCSTI`` and ``TIOCLINUX`` IOCTL
  486. commands. Both of these require ``CAP_SYS_ADMIN`` on modern Linux systems, but
  487. the behavior is configurable for ``TIOCSTI``.
  488. On older systems, it is therefore recommended to close inherited TTY file
  489. descriptors, or to reopen them from ``/proc/self/fd/*`` without the
  490. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right, if possible.
  491. Landlock's IOCTL support is coarse-grained at the moment, but may become more
  492. fine-grained in the future. Until then, users are advised to establish the
  493. guarantees that they need through the file hierarchy, by only allowing the
  494. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right on files where it is really required.
  495. Previous limitations
  496. ====================
  497. File renaming and linking (ABI < 2)
  498. -----------------------------------
  499. Because Landlock targets unprivileged access controls, it needs to properly
  500. handle composition of rules. Such property also implies rules nesting.
  501. Properly handling multiple layers of rulesets, each one of them able to
  502. restrict access to files, also implies inheritance of the ruleset restrictions
  503. from a parent to its hierarchy. Because files are identified and restricted by
  504. their hierarchy, moving or linking a file from one directory to another implies
  505. propagation of the hierarchy constraints, or restriction of these actions
  506. according to the potentially lost constraints. To protect against privilege
  507. escalations through renaming or linking, and for the sake of simplicity,
  508. Landlock previously limited linking and renaming to the same directory.
  509. Starting with the Landlock ABI version 2, it is now possible to securely
  510. control renaming and linking thanks to the new ``LANDLOCK_ACCESS_FS_REFER``
  511. access right.
  512. File truncation (ABI < 3)
  513. -------------------------
  514. File truncation could not be denied before the third Landlock ABI, so it is
  515. always allowed when using a kernel that only supports the first or second ABI.
  516. Starting with the Landlock ABI version 3, it is now possible to securely control
  517. truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right.
  518. TCP bind and connect (ABI < 4)
  519. ------------------------------
  520. Starting with the Landlock ABI version 4, it is now possible to restrict TCP
  521. bind and connect actions to only a set of allowed ports thanks to the new
  522. ``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
  523. access rights.
  524. Device IOCTL (ABI < 5)
  525. ----------------------
  526. IOCTL operations could not be denied before the fifth Landlock ABI, so
  527. :manpage:`ioctl(2)` is always allowed when using a kernel that only supports an
  528. earlier ABI.
  529. Starting with the Landlock ABI version 5, it is possible to restrict the use of
  530. :manpage:`ioctl(2)` on character and block devices using the new
  531. ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
  532. Abstract UNIX socket (ABI < 6)
  533. ------------------------------
  534. Starting with the Landlock ABI version 6, it is possible to restrict
  535. connections to an abstract :manpage:`unix(7)` socket by setting
  536. ``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`` to the ``scoped`` ruleset attribute.
  537. Signal (ABI < 6)
  538. ----------------
  539. Starting with the Landlock ABI version 6, it is possible to restrict
  540. :manpage:`signal(7)` sending by setting ``LANDLOCK_SCOPE_SIGNAL`` to the
  541. ``scoped`` ruleset attribute.
  542. Logging (ABI < 7)
  543. -----------------
  544. Starting with the Landlock ABI version 7, it is possible to control logging of
  545. Landlock audit events with the ``LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF``,
  546. ``LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON``, and
  547. ``LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF`` flags passed to
  548. sys_landlock_restrict_self(). See Documentation/admin-guide/LSM/landlock.rst
  549. for more details on audit.
  550. Thread synchronization (ABI < 8)
  551. --------------------------------
  552. Starting with the Landlock ABI version 8, it is now possible to
  553. enforce Landlock rulesets across all threads of the calling process
  554. using the ``LANDLOCK_RESTRICT_SELF_TSYNC`` flag passed to
  555. sys_landlock_restrict_self().
  556. .. _kernel_support:
  557. Kernel support
  558. ==============
  559. Build time configuration
  560. ------------------------
  561. Landlock was first introduced in Linux 5.13 but it must be configured at build
  562. time with ``CONFIG_SECURITY_LANDLOCK=y``. Landlock must also be enabled at boot
  563. time like other security modules. The list of security modules enabled by
  564. default is set with ``CONFIG_LSM``. The kernel configuration should then
  565. contain ``CONFIG_LSM=landlock,[...]`` with ``[...]`` as the list of other
  566. potentially useful security modules for the running system (see the
  567. ``CONFIG_LSM`` help).
  568. Boot time configuration
  569. -----------------------
  570. If the running kernel does not have ``landlock`` in ``CONFIG_LSM``, then we can
  571. enable Landlock by adding ``lsm=landlock,[...]`` to
  572. Documentation/admin-guide/kernel-parameters.rst in the boot loader
  573. configuration.
  574. For example, if the current built-in configuration is:
  575. .. code-block:: console
  576. $ zgrep -h "^CONFIG_LSM=" "/boot/config-$(uname -r)" /proc/config.gz 2>/dev/null
  577. CONFIG_LSM="lockdown,yama,integrity,apparmor"
  578. ...and if the cmdline doesn't contain ``landlock`` either:
  579. .. code-block:: console
  580. $ sed -n 's/.*\(\<lsm=\S\+\).*/\1/p' /proc/cmdline
  581. lsm=lockdown,yama,integrity,apparmor
  582. ...we should configure the boot loader to set a cmdline extending the ``lsm``
  583. list with the ``landlock,`` prefix::
  584. lsm=landlock,lockdown,yama,integrity,apparmor
  585. After a reboot, we can check that Landlock is up and running by looking at
  586. kernel logs:
  587. .. code-block:: console
  588. # dmesg | grep landlock || journalctl -kb -g landlock
  589. [ 0.000000] Command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
  590. [ 0.000000] Kernel command line: [...] lsm=landlock,lockdown,yama,integrity,apparmor
  591. [ 0.000000] LSM: initializing lsm=lockdown,capability,landlock,yama,integrity,apparmor
  592. [ 0.000000] landlock: Up and running.
  593. The kernel may be configured at build time to always load the ``lockdown`` and
  594. ``capability`` LSMs. In that case, these LSMs will appear at the beginning of
  595. the ``LSM: initializing`` log line as well, even if they are not configured in
  596. the boot loader.
  597. Network support
  598. ---------------
  599. To be able to explicitly allow TCP operations (e.g., adding a network rule with
  600. ``LANDLOCK_ACCESS_NET_BIND_TCP``), the kernel must support TCP
  601. (``CONFIG_INET=y``). Otherwise, sys_landlock_add_rule() returns an
  602. ``EAFNOSUPPORT`` error, which can safely be ignored because this kind of TCP
  603. operation is already not possible.
  604. Questions and answers
  605. =====================
  606. What about user space sandbox managers?
  607. ---------------------------------------
  608. Using user space processes to enforce restrictions on kernel resources can lead
  609. to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of
  610. the OS code and state
  611. <https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_).
  612. What about namespaces and containers?
  613. -------------------------------------
  614. Namespaces can help create sandboxes but they are not designed for
  615. access-control and then miss useful features for such use case (e.g. no
  616. fine-grained restrictions). Moreover, their complexity can lead to security
  617. issues, especially when untrusted processes can manipulate them (cf.
  618. `Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_).
  619. How to disable Landlock audit records?
  620. --------------------------------------
  621. You might want to put in place filters as explained here:
  622. Documentation/admin-guide/LSM/landlock.rst
  623. Additional documentation
  624. ========================
  625. * Documentation/admin-guide/LSM/landlock.rst
  626. * Documentation/security/landlock.rst
  627. * https://landlock.io
  628. .. Links
  629. .. _samples/landlock/sandboxer.c:
  630. https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.c