Debian Codegraph @

upstream/mageia-cauldron/man7/landlock.7

raw
1 '\" t
2 .\" Copyright 2017-2020, Mickaël Salaün <[email protected]>
3 .\" Copyright 2019-2020, ANSSI
4 .\" Copyright 2021, Microsoft Corp.
5 .\" Copyright, the authors of the Linux man-pages project
6 .\"
7 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\"
9 .TH Landlock 7 2026-02-08 "Linux man-pages 6.17"
10 .SH NAME
11 Landlock \- unprivileged access-control
12 .SH DESCRIPTION
13 Landlock is an access-control system that enables any processes to
14 securely restrict themselves and their future children.
15 Because Landlock is a stackable Linux Security Module (LSM),
16 it makes it possible to create safe security sandboxes
17 as new security layers in addition to
18 the existing system-wide access-controls.
19 This kind of sandbox is expected to help mitigate
20 the security impact of bugs,
21 and unexpected or malicious behaviors in applications.
22 .P
23 A Landlock security policy is a set of access rights
24 (e.g., open a file in read-only, make a directory, etc.)
25 tied to a file hierarchy.
26 Such policy can be configured and enforced by processes for themselves
27 using three system calls:
28 .IP \[bu] 3
29 .BR landlock_create_ruleset (2)
30 creates a new ruleset;
31 .IP \[bu]
32 .BR landlock_add_rule (2)
33 adds a new rule to a ruleset;
34 .IP \[bu]
35 .BR landlock_restrict_self (2)
36 enforces a ruleset on the calling thread.
37 .P
38 To be able to use these system calls,
39 the running kernel must support Landlock and
40 it must be enabled at boot time.
41 .\"
42 .SS Landlock rules
43 A Landlock rule describes an action on an object
44 which the process intends to perform.
45 A set of rules is aggregated in a ruleset,
46 which can then restrict the thread enforcing it,
47 and its future children.
48 .P
49 The two existing types of rules are:
50 .TP
51 .B Filesystem rules
52 For these rules, the object is a file hierarchy,
53 and the related filesystem actions are defined with
54 .IR "filesystem access rights" .
55 .TP
56 .BR "Network rules" " (since ABI v4)"
57 For these rules, the object is a TCP port,
58 and the related actions are defined with
59 .IR "network access rights" .
60 .\"
61 .SS Filesystem actions
62 These flags enable to restrict a sandboxed process to a
63 set of actions on files and directories.
64 Files or directories opened before the sandboxing
65 are not subject to these restrictions.
66 See
67 .BR landlock_add_rule (2)
68 and
69 .BR landlock_create_ruleset (2)
70 for more context.
71 .P
72 The following access rights apply only to files:
73 .TP
74 .B LANDLOCK_ACCESS_FS_EXECUTE
75 Execute a file.
76 .TP
77 .B LANDLOCK_ACCESS_FS_WRITE_FILE
78 Open a file with write access.
79 .IP
80 When opening files for writing,
81 you will often additionally need the
82 .B LANDLOCK_ACCESS_FS_TRUNCATE
83 right.
84 In many cases,
85 these system calls truncate existing files when overwriting them
86 (e.g.,
87 .BR creat (2)).
88 .TP
89 .B LANDLOCK_ACCESS_FS_READ_FILE
90 Open a file with read access.
91 .TP
92 .B LANDLOCK_ACCESS_FS_TRUNCATE
93 Truncate a file with
94 .BR truncate (2),
95 .BR ftruncate (2),
96 .BR creat (2),
97 or
98 .BR open (2)
99 with
100 .BR O_TRUNC .
101 .IP
102 This access right is available since the third version of the Landlock ABI.
103 .TP
104 .B LANDLOCK_ACCESS_FS_IOCTL_DEV
105 Invoke
106 .BR ioctl (2)
107 commands on an opened character or block device.
108 .IP
109 This access right applies to all
110 .BR ioctl (2)
111 commands implemented by device drivers.
112 However, the following common IOCTL commands continue to be invokable
113 independent of the
114 .B LANDLOCK_ACCESS_FS_IOCTL_DEV
115 right:
116 .RS
117 .IP \[bu] 3
118 IOCTL commands targeting file descriptors
119 .RB ( FIOCLEX ,
120 .BR FIONCLEX ),
121 .IP \[bu]
122 IOCTL commands targeting file descriptions
123 .RB ( FIONBIO ,
124 .BR FIOASYNC ),
125 .IP \[bu]
126 IOCTL commands targeting file systems
127 .RB ( FIFREEZE ,
128 .BR FITHAW ,
129 .BR FIGETBSZ ,
130 .BR FS_IOC_GETFSUUID ,
131 .BR FS_IOC_GETFSSYSFSPATH )
132 .IP \[bu]
133 Some IOCTL commands which do not make sense when used with devices, but
134 whose implementations are safe and return the right error codes
135 .RB ( FS_IOC_FIEMAP ,
136 .BR FICLONE ,
137 .BR FICLONERANGE ,
138 .BR FIDEDUPERANGE )
139 .RE
140 .IP
141 This access right is available since the fifth version of the Landlock ABI.
142 .P
143 Whether an opened file can be truncated with
144 .BR ftruncate (2)
145 or used with
146 .BR ioctl (2)
147 is determined during
148 .BR open (2),
149 in the same way as read and write permissions are checked during
150 .BR open (2)
151 using
152 .B LANDLOCK_ACCESS_FS_READ_FILE
153 and
154 .BR LANDLOCK_ACCESS_FS_WRITE_FILE .
155 .P
156 A directory can receive access rights related to files or directories.
157 The following access right is applied to the directory itself,
158 and the directories beneath it:
159 .TP
160 .B LANDLOCK_ACCESS_FS_READ_DIR
161 Open a directory or list its content.
162 .P
163 However,
164 the following access rights only apply to the content of a directory,
165 not the directory itself:
166 .TP
167 .B LANDLOCK_ACCESS_FS_REMOVE_DIR
168 Remove an empty directory or rename one.
169 .TP
170 .B LANDLOCK_ACCESS_FS_REMOVE_FILE
171 Unlink (or rename) a file.
172 .TP
173 .B LANDLOCK_ACCESS_FS_MAKE_CHAR
174 Create (or rename or link) a character device.
175 .TP
176 .B LANDLOCK_ACCESS_FS_MAKE_DIR
177 Create (or rename) a directory.
178 .TP
179 .B LANDLOCK_ACCESS_FS_MAKE_REG
180 Create (or rename or link) a regular file.
181 .TP
182 .B LANDLOCK_ACCESS_FS_MAKE_SOCK
183 Create (or rename or link) a UNIX domain socket.
184 .TP
185 .B LANDLOCK_ACCESS_FS_MAKE_FIFO
186 Create (or rename or link) a named pipe.
187 .TP
188 .B LANDLOCK_ACCESS_FS_MAKE_BLOCK
189 Create (or rename or link) a block device.
190 .TP
191 .B LANDLOCK_ACCESS_FS_MAKE_SYM
192 Create (or rename or link) a symbolic link.
193 .TP
194 .B LANDLOCK_ACCESS_FS_REFER
195 Link or rename a file from or to a different directory
196 (i.e., reparent a file hierarchy).
197 .IP
198 This access right is available since the second version of the Landlock ABI.
199 .IP
200 This is the only access right which is denied by default by any ruleset,
201 even if the right is not specified as handled at ruleset creation time.
202 The only way to make a ruleset grant this right
203 is to explicitly allow it for a specific directory
204 by adding a matching rule to the ruleset.
205 .IP
206 In particular, when using the first Landlock ABI version,
207 Landlock will always deny attempts to reparent files
208 between different directories.
209 .IP
210 In addition to the source and destination directories having the
211 .B LANDLOCK_ACCESS_FS_REFER
212 access right,
213 the attempted link or rename operation must meet the following constraints:
214 .RS
215 .IP \[bu] 3
216 The reparented file may not gain more access rights in the destination directory
217 than it previously had in the source directory.
218 If this is attempted, the operation results in an
219 .B EXDEV
220 error.
221 .IP \[bu]
222 When linking or renaming, the
223 .BI LANDLOCK_ACCESS_FS_MAKE_ *
224 right for the respective file type must be granted
225 for the destination directory.
226 Otherwise, the operation results in an
227 .B EACCES
228 error.
229 .IP \[bu]
230 When renaming, the
231 .BI LANDLOCK_ACCESS_FS_REMOVE_ *
232 right for the respective file type must be granted
233 for the source directory.
234 Otherwise, the operation results in an
235 .B EACCES
236 error.
237 .RE
238 .IP
239 If multiple requirements are not met, the
240 .B EACCES
241 error code takes precedence over
242 .BR EXDEV .
243 .\"
244 .SS Network flags
245 These flags enable to restrict a sandboxed process
246 to a set of network actions.
247 .P
248 This is supported since Landlock ABI version 4.
249 .P
250 The following access rights apply to TCP port numbers:
251 .TP
252 .B LANDLOCK_ACCESS_NET_BIND_TCP
253 Bind a TCP socket to a local port.
254 .TP
255 .B LANDLOCK_ACCESS_NET_CONNECT_TCP
256 Connect an active TCP socket to a remote port.
257 .\"
258 .SS Scope flags
259 These flags enable isolating a sandboxed process from a set of IPC actions.
260 Setting a flag for a ruleset will isolate the Landlock domain
261 to forbid connections to resources outside the domain.
262 .P
263 This is supported since Landlock ABI version 6.
264 .P
265 The following scopes exist:
266 .TP
267 .B LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
268 Restrict a sandboxed process from connecting to an abstract UNIX socket
269 created by a process outside the related Landlock domain
270 (e.g., a parent domain or a non-sandboxed process).
271 .TP
272 .B LANDLOCK_SCOPE_SIGNAL
273 Restrict a sandboxed process from sending a signal
274 to another process outside the domain.
275 .\"
276 .SS Layers of file path access rights
277 Each time a thread enforces a ruleset on itself,
278 it updates its Landlock domain with a new layer of policy.
279 Indeed, this complementary policy is composed with the
280 potentially other rulesets already restricting this thread.
281 A sandboxed thread can then safely add more constraints to itself with a
282 new enforced ruleset.
283 .P
284 One policy layer grants access to a file path
285 if at least one of its rules encountered on the path grants the access.
286 A sandboxed thread can only access a file path
287 if all its enforced policy layers grant the access
288 as well as all the other system access controls
289 (e.g., filesystem DAC, other LSM policies, etc.).
290 .\"
291 .SS Bind mounts and OverlayFS
292 Landlock enables restricting access to file hierarchies,
293 which means that these access rights can be propagated with bind mounts
294 (cf.
295 .BR mount_namespaces (7))
296 but not with OverlayFS.
297 .P
298 A bind mount mirrors a source file hierarchy to a destination.
299 The destination hierarchy is then composed of the exact same files,
300 on which Landlock rules can be tied,
301 either via the source or the destination path.
302 These rules restrict access when they are encountered on a path,
303 which means that they can restrict access to
304 multiple file hierarchies at the same time,
305 whether these hierarchies are the result of bind mounts or not.
306 .P
307 An OverlayFS mount point consists of upper and lower layers.
308 These layers are combined in a merge directory, result of the mount point.
309 This merge hierarchy may include files from the upper and lower layers,
310 but modifications performed on the merge hierarchy
311 only reflect on the upper layer.
312 From a Landlock policy point of view,
313 each of the OverlayFS layers and merge hierarchies is standalone and
314 contains its own set of files and directories,
315 which is different from a bind mount.
316 A policy restricting an OverlayFS layer will not restrict
317 the resulted merged hierarchy, and vice versa.
318 Landlock users should then only think about file hierarchies they want to
319 allow access to, regardless of the underlying filesystem.
320 .\"
321 .SS Inheritance
322 Every new thread resulting from a
323 .BR clone (2)
324 inherits Landlock domain restrictions from its parent.
325 This is similar to the
326 .BR seccomp (2)
327 inheritance or any other LSM dealing with tasks'
328 .BR credentials (7).
329 For instance, one process's thread may apply Landlock rules to itself,
330 but they will not be automatically applied to other sibling threads
331 (unlike POSIX thread credential changes, cf.
332 .BR nptl (7)).
333 .P
334 When a thread sandboxes itself,
335 we have the guarantee that the related security policy
336 will stay enforced on all this thread's descendants.
337 This allows creating standalone and modular security policies
338 per application,
339 which will automatically be composed between themselves
340 according to their run-time parent policies.
341 .\"
342 .SS Ptrace restrictions
343 A sandboxed process has less privileges than a non-sandboxed process and
344 must then be subject to additional restrictions
345 when manipulating another process.
346 To be allowed to use
347 .BR ptrace (2)
348 and related syscalls on a target process,
349 a sandboxed process should have a subset of the target process rules,
350 which means the tracee must be in a sub-domain of the tracer.
351 .\"
352 .SS IPC scoping
353 Similar to the implicit
354 .BR "Ptrace restrictions" ,
355 we may want to further restrict interactions between sandboxes.
356 Therefore, at ruleset creation time,
357 each Landlock domain can restrict the scope for certain operations,
358 so that these operations can only reach out to processes
359 within the same Landlock domain or in a nested Landlock domain (the "scope").
360 .P
361 The operations which can be scoped are:
362 .TP
363 .B LANDLOCK_SCOPE_SIGNAL
364 This limits the sending of signals to target processes
365 which run within the same or a nested Landlock domain.
366 .TP
367 .B LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
368 This limits the set of abstract
369 .BR unix (7)
370 sockets to which we can
371 .BR connect (2)
372 to socket addresses which were created
373 by a process in the same or a nested Landlock domain.
374 .IP
375 A
376 .BR sendto (2)
377 on a non-connected datagram socket is treated as if it were doing an implicit
378 .BR connect (2)
379 and will be blocked if the remote end does not stem
380 from the same or a nested Landlock domain.
381 .IP
382 A
383 .BR sendto (2)
384 on a socket which was previously connected will not be restricted.
385 This works for both datagram and stream sockets.
386 .P
387 IPC scoping does not support exceptions via
388 .BR landlock_add_rule (2).
389 If an operation is scoped within a domain,
390 no rules can be added to allow access to
391 resources or processes outside of the scope.
392 .\"
393 .SS Truncating files
394 The operations covered by
395 .B LANDLOCK_ACCESS_FS_WRITE_FILE
396 and
397 .B LANDLOCK_ACCESS_FS_TRUNCATE
398 both change the contents of a file and sometimes overlap in
399 non-intuitive ways.
400 It is recommended to always specify both of these together.
401 .P
402 A particularly surprising example is
403 .BR creat (2).
404 The name suggests that this system call requires
405 the rights to create and write files.
406 However, it also requires the truncate right
407 if an existing file under the same name is already present.
408 .P
409 It should also be noted that truncating files does not require the
410 .B LANDLOCK_ACCESS_FS_WRITE_FILE
411 right.
412 Apart from the
413 .BR truncate (2)
414 system call, this can also be done through
415 .BR open (2)
416 with the flags
417 .IR "O_RDONLY\ |\ O_TRUNC" .
418 .P
419 When opening a file, the availability of the
420 .B LANDLOCK_ACCESS_FS_TRUNCATE
421 right is associated with the newly created file descriptor
422 and will be used for subsequent truncation attempts using
423 .BR ftruncate (2).
424 The behavior is similar to opening a file for reading or writing,
425 where permissions are checked during
426 .BR open (2),
427 but not during the subsequent
428 .BR read (2)
429 and
430 .BR write (2)
431 calls.
432 .P
433 As a consequence,
434 it is possible to have multiple open file descriptors for the same file,
435 where one grants the right to truncate the file and the other does not.
436 It is also possible to pass such file descriptors between processes,
437 keeping their Landlock properties,
438 even when these processes do not have an enforced Landlock ruleset.
439 .SH VERSIONS
440 Landlock was introduced in Linux 5.13.
441 .P
442 To determine which Landlock features are available,
443 users should query the Landlock ABI version:
444 .TS
445 box;
446 ntb| ntb| lbx
447 nt| nt| lbx.
448 ABI Kernel Newly introduced access rights
449 _ _ _
450 1 5.13 LANDLOCK_ACCESS_FS_EXECUTE
451 \^ \^ LANDLOCK_ACCESS_FS_WRITE_FILE
452 \^ \^ LANDLOCK_ACCESS_FS_READ_FILE
453 \^ \^ LANDLOCK_ACCESS_FS_READ_DIR
454 \^ \^ LANDLOCK_ACCESS_FS_REMOVE_DIR
455 \^ \^ LANDLOCK_ACCESS_FS_REMOVE_FILE
456 \^ \^ LANDLOCK_ACCESS_FS_MAKE_CHAR
457 \^ \^ LANDLOCK_ACCESS_FS_MAKE_DIR
458 \^ \^ LANDLOCK_ACCESS_FS_MAKE_REG
459 \^ \^ LANDLOCK_ACCESS_FS_MAKE_SOCK
460 \^ \^ LANDLOCK_ACCESS_FS_MAKE_FIFO
461 \^ \^ LANDLOCK_ACCESS_FS_MAKE_BLOCK
462 \^ \^ LANDLOCK_ACCESS_FS_MAKE_SYM
463 _ _ _
464 2 5.19 LANDLOCK_ACCESS_FS_REFER
465 _ _ _
466 3 6.2 LANDLOCK_ACCESS_FS_TRUNCATE
467 _ _ _
468 4 6.7 LANDLOCK_ACCESS_NET_BIND_TCP
469 \^ \^ LANDLOCK_ACCESS_NET_CONNECT_TCP
470 _ _ _
471 5 6.10 LANDLOCK_ACCESS_FS_IOCTL_DEV
472 _ _ _
473 6 6.12 LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
474 \^ \^ LANDLOCK_SCOPE_SIGNAL
475 .TE
476 .P
477 Users should use the Landlock ABI version rather than the kernel version
478 to determine which features are available.
479 The mainline kernel versions listed here are only included for orientation.
480 Kernels from other sources may contain backported features,
481 and their version numbers may not match.
482 .P
483 To query the running kernel's Landlock ABI version,
484 programs may pass the
485 .B LANDLOCK_CREATE_RULESET_VERSION
486 flag to
487 .BR landlock_create_ruleset (2).
488 .P
489 When building fallback mechanisms for compatibility with older kernels,
490 users are advised to consider the special semantics of the
491 .B LANDLOCK_ACCESS_FS_REFER
492 access right:
493 In ABI v1,
494 linking and moving of files between different directories is always forbidden,
495 so programs relying on such operations are only compatible
496 with Landlock ABI v2 and higher.
497 .SH NOTES
498 Landlock is enabled by
499 .BR CONFIG_SECURITY_LANDLOCK .
500 The
501 .I lsm=lsm1,...,lsmN
502 command line parameter controls the sequence of the initialization of
503 Linux Security Modules.
504 It must contain the string
505 .I landlock
506 to enable Landlock.
507 If the command line parameter is not specified,
508 the initialization falls back to the value of the deprecated
509 .I security=
510 command line parameter and further to the value of
511 .BR CONFIG_LSM .
512 We can check that Landlock is enabled by looking for
513 .I landlock: Up and running.
514 in kernel logs.
515 .SH CAVEATS
516 It is currently not possible to restrict some file-related actions
517 accessible through these system call families:
518 .BR chdir (2),
519 .BR stat (2),
520 .BR flock (2),
521 .BR chmod (2),
522 .BR chown (2),
523 .BR setxattr (2),
524 .BR utime (2),
525 .BR fcntl (2),
526 .BR access (2).
527 Future Landlock evolutions will enable to restrict them.
528 .SH EXAMPLES
529 We first need to create the ruleset that will contain our rules.
530 .P
531 For this example,
532 the ruleset will contain rules that only allow read actions,
533 but write actions will be denied.
534 The ruleset then needs to handle both of these kinds of actions.
535 See the
536 .B DESCRIPTION
537 section for the description of filesystem actions.
538 .P
539 .in +4n
540 .EX
541 struct landlock_ruleset_attr attr = {0};
542 int ruleset_fd;
543 \&
544 attr.handled_access_fs =
545 LANDLOCK_ACCESS_FS_EXECUTE |
546 LANDLOCK_ACCESS_FS_WRITE_FILE |
547 LANDLOCK_ACCESS_FS_READ_FILE |
548 LANDLOCK_ACCESS_FS_READ_DIR |
549 LANDLOCK_ACCESS_FS_REMOVE_DIR |
550 LANDLOCK_ACCESS_FS_REMOVE_FILE |
551 LANDLOCK_ACCESS_FS_MAKE_CHAR |
552 LANDLOCK_ACCESS_FS_MAKE_DIR |
553 LANDLOCK_ACCESS_FS_MAKE_REG |
554 LANDLOCK_ACCESS_FS_MAKE_SOCK |
555 LANDLOCK_ACCESS_FS_MAKE_FIFO |
556 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
557 LANDLOCK_ACCESS_FS_MAKE_SYM |
558 LANDLOCK_ACCESS_FS_REFER |
559 LANDLOCK_ACCESS_FS_TRUNCATE |
560 LANDLOCK_ACCESS_FS_IOCTL_DEV;
561 .EE
562 .in
563 .P
564 To be compatible with older Linux versions,
565 we detect the available Landlock ABI version,
566 and only use the available subset of access rights:
567 .P
568 .in +4n
569 .EX
570 /*
571 * Table of available file system access rights by ABI version,
572 * numbers hardcoded to keep the example short.
573 */
574 __u64 landlock_fs_access_rights[] = {
575 (LANDLOCK_ACCESS_FS_MAKE_SYM << 1) \- 1, /* v1 */
576 (LANDLOCK_ACCESS_FS_REFER << 1) \- 1, /* v2: add "refer" */
577 (LANDLOCK_ACCESS_FS_TRUNCATE << 1) \- 1, /* v3: add "truncate" */
578 (LANDLOCK_ACCESS_FS_TRUNCATE << 1) \- 1, /* v4: TCP support */
579 (LANDLOCK_ACCESS_FS_IOCTL_DEV << 1) \- 1, /* v5: add "ioctl_dev" */
580 };
581 \&
582 int abi = landlock_create_ruleset(NULL, 0,
583 LANDLOCK_CREATE_RULESET_VERSION);
584 if (abi == \-1) {
585 /*
586 * Kernel too old, not compiled with Landlock,
587 * or Landlock was not enabled at boot time.
588 */
589 perror("Unable to use Landlock");
590 return; /* Graceful fallback: Do nothing. */
591 }
592 abi = MIN(abi, 3);
593 \&
594 /* Only use the available rights in the ruleset. */
595 attr.handled_access_fs &= landlock_fs_access_rights[abi \- 1];
596 .EE
597 .in
598 .P
599 The available access rights for each ABI version are listed in the
600 .B VERSIONS
601 section.
602 .P
603 If our program needed to create hard links
604 or rename files between different directories
605 .RB ( LANDLOCK_ACCESS_FS_REFER ),
606 we would require the following change to the backwards compatibility logic:
607 Directory reparenting is not possible
608 in a process restricted with Landlock ABI version 1.
609 Therefore,
610 if the program needed to do file reparenting,
611 and if only Landlock ABI version 1 was available,
612 we could not restrict the process.
613 .P
614 Now that the ruleset attributes are determined,
615 we create the Landlock ruleset
616 and acquire a file descriptor as a handle to it,
617 using
618 .BR landlock_create_ruleset (2):
619 .P
620 .in +4n
621 .EX
622 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
623 if (ruleset_fd == \-1) {
624 perror("Failed to create a ruleset");
625 exit(EXIT_FAILURE);
626 }
627 .EE
628 .in
629 .P
630 We can now add a new rule to the ruleset through the ruleset's file descriptor.
631 The requested access rights must be a subset of the access rights
632 which were specified in
633 .I attr.handled_access_fs
634 at ruleset creation time.
635 .P
636 In this example, the rule will only allow reading the file hierarchy
637 .IR /usr .
638 Without another rule, write actions would then be denied by the ruleset.
639 To add
640 .I /usr
641 to the ruleset, we open it with the
642 .I O_PATH
643 flag and fill the
644 .I struct landlock_path_beneath_attr
645 with this file descriptor.
646 .P
647 .in +4n
648 .EX
649 struct landlock_path_beneath_attr path_beneath = {0};
650 int err;
651 \&
652 path_beneath.allowed_access =
653 LANDLOCK_ACCESS_FS_EXECUTE |
654 LANDLOCK_ACCESS_FS_READ_FILE |
655 LANDLOCK_ACCESS_FS_READ_DIR;
656 \&
657 path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
658 if (path_beneath.parent_fd == \-1) {
659 perror("Failed to open file");
660 close(ruleset_fd);
661 exit(EXIT_FAILURE);
662 }
663 err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
664 &path_beneath, 0);
665 close(path_beneath.parent_fd);
666 if (err) {
667 perror("Failed to update ruleset");
668 close(ruleset_fd);
669 exit(EXIT_FAILURE);
670 }
671 .EE
672 .in
673 .P
674 We now have a ruleset with one rule allowing read access to
675 .I /usr
676 while denying all other handled accesses for the filesystem.
677 The next step is to restrict the current thread from gaining more
678 privileges
679 (e.g., thanks to a set-user-ID binary).
680 .P
681 .in +4n
682 .EX
683 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
684 perror("Failed to restrict privileges");
685 close(ruleset_fd);
686 exit(EXIT_FAILURE);
687 }
688 .EE
689 .in
690 .P
691 The current thread is now ready to sandbox itself with the ruleset.
692 .P
693 .in +4n
694 .EX
695 if (landlock_restrict_self(ruleset_fd, 0)) {
696 perror("Failed to enforce ruleset");
697 close(ruleset_fd);
698 exit(EXIT_FAILURE);
699 }
700 close(ruleset_fd);
701 .EE
702 .in
703 .P
704 If the
705 .BR landlock_restrict_self (2)
706 system call succeeds, the current thread is now restricted and
707 this policy will be enforced on all its subsequently created children as well.
708 Once a thread is landlocked, there is no way to remove its security policy;
709 only adding more restrictions is allowed.
710 These threads are now in a new Landlock domain,
711 merge of their parent one (if any) with the new ruleset.
712 .P
713 Full working code can be found in
714 .UR https:\://git.kernel.org/\:pub/\:scm/\:linux/\:kernel/\:git/\:stable/\:linux.git/\:tree/\:samples/\:landlock/\:sandboxer.c
715 .UE
716 .SH SEE ALSO
717 .BR landlock_create_ruleset (2),
718 .BR landlock_add_rule (2),
719 .BR landlock_restrict_self (2)
720 .P
721 .UR https:\://landlock.io/
722 .UE