Debian Codegraph @

upstream/debian-unstable/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-04-21 "Linux man-pages 6.18"
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 constants
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 _ _ _
476 7 6.15 LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF
477 \^ \^ LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON
478 \^ \^ LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
479 _ _ _
480 8 7.0 LANDLOCK_RESTRICT_SELF_TSYNC
481 .TE
482 .P
483 Users should use the Landlock ABI version rather than the kernel version
484 to determine which features are available.
485 The mainline kernel versions listed here are only included for orientation.
486 Kernels from other sources may contain backported features,
487 and their version numbers may not match.
488 .P
489 To query the running kernel's Landlock ABI version,
490 programs may pass the
491 .B LANDLOCK_CREATE_RULESET_VERSION
492 flag to
493 .BR landlock_create_ruleset (2).
494 .P
495 When building fallback mechanisms for compatibility with older kernels,
496 users are advised to consider the special semantics of the
497 .B LANDLOCK_ACCESS_FS_REFER
498 access right:
499 In ABI v1,
500 linking and moving of files between different directories is always forbidden,
501 so programs relying on such operations are only compatible
502 with Landlock ABI v2 and higher.
503 .SH NOTES
504 Landlock is enabled by
505 .BR CONFIG_SECURITY_LANDLOCK .
506 The
507 .I lsm=lsm1,...,lsmN
508 command line parameter controls the sequence of the initialization of
509 Linux Security Modules.
510 It must contain the string
511 .I landlock
512 to enable Landlock.
513 If the command line parameter is not specified,
514 the initialization falls back to the value of the deprecated
515 .I security=
516 command line parameter and further to the value of
517 .BR CONFIG_LSM .
518 We can check that Landlock is enabled by looking for
519 .I landlock: Up and running.
520 in kernel logs.
521 .SH CAVEATS
522 It is currently not possible to restrict some file-related actions
523 accessible through these system call families:
524 .BR chdir (2),
525 .BR stat (2),
526 .BR flock (2),
527 .BR chmod (2),
528 .BR chown (2),
529 .BR setxattr (2),
530 .BR utime (2),
531 .BR fcntl (2),
532 .BR access (2).
533 Future Landlock evolutions will enable to restrict them.
534 .SH EXAMPLES
535 We first need to create the ruleset that will contain our rules.
536 .P
537 For this example,
538 the ruleset will contain rules that only allow read actions,
539 but write actions will be denied.
540 The ruleset then needs to handle both of these kinds of actions.
541 See the
542 .B DESCRIPTION
543 section for the description of filesystem actions.
544 .P
545 .in +4n
546 .EX
547 struct landlock_ruleset_attr attr = {0};
548 int ruleset_fd;
549 \&
550 attr.handled_access_fs =
551 LANDLOCK_ACCESS_FS_EXECUTE |
552 LANDLOCK_ACCESS_FS_WRITE_FILE |
553 LANDLOCK_ACCESS_FS_READ_FILE |
554 LANDLOCK_ACCESS_FS_READ_DIR |
555 LANDLOCK_ACCESS_FS_REMOVE_DIR |
556 LANDLOCK_ACCESS_FS_REMOVE_FILE |
557 LANDLOCK_ACCESS_FS_MAKE_CHAR |
558 LANDLOCK_ACCESS_FS_MAKE_DIR |
559 LANDLOCK_ACCESS_FS_MAKE_REG |
560 LANDLOCK_ACCESS_FS_MAKE_SOCK |
561 LANDLOCK_ACCESS_FS_MAKE_FIFO |
562 LANDLOCK_ACCESS_FS_MAKE_BLOCK |
563 LANDLOCK_ACCESS_FS_MAKE_SYM |
564 LANDLOCK_ACCESS_FS_REFER |
565 LANDLOCK_ACCESS_FS_TRUNCATE |
566 LANDLOCK_ACCESS_FS_IOCTL_DEV;
567 .EE
568 .in
569 .P
570 To be compatible with older Linux versions,
571 we detect the available Landlock ABI version,
572 and only use the available subset of access rights:
573 .P
574 .in +4n
575 .EX
576 /*
577 * Table of available file system access rights by ABI version,
578 * numbers hardcoded to keep the example short.
579 */
580 __u64 landlock_fs_access_rights[] = {
581 (LANDLOCK_ACCESS_FS_MAKE_SYM << 1) \- 1, /* v1 */
582 (LANDLOCK_ACCESS_FS_REFER << 1) \- 1, /* v2: add "refer" */
583 (LANDLOCK_ACCESS_FS_TRUNCATE << 1) \- 1, /* v3: add "truncate" */
584 (LANDLOCK_ACCESS_FS_TRUNCATE << 1) \- 1, /* v4: TCP support */
585 (LANDLOCK_ACCESS_FS_IOCTL_DEV << 1) \- 1, /* v5: add "ioctl_dev" */
586 };
587 \&
588 int abi = landlock_create_ruleset(NULL, 0,
589 LANDLOCK_CREATE_RULESET_VERSION);
590 if (abi == \-1) {
591 /*
592 * Kernel too old, not compiled with Landlock,
593 * or Landlock was not enabled at boot time.
594 */
595 perror("Unable to use Landlock");
596 return; /* Graceful fallback: Do nothing. */
597 }
598 abi = MIN(abi, 3);
599 \&
600 /* Only use the available rights in the ruleset. */
601 attr.handled_access_fs &= landlock_fs_access_rights[abi \- 1];
602 .EE
603 .in
604 .P
605 The available access rights for each ABI version are listed in the
606 .B VERSIONS
607 section.
608 .P
609 If our program needed to create hard links
610 or rename files between different directories
611 .RB ( LANDLOCK_ACCESS_FS_REFER ),
612 we would require the following change to the backwards compatibility logic:
613 Directory reparenting is not possible
614 in a process restricted with Landlock ABI version 1.
615 Therefore,
616 if the program needed to do file reparenting,
617 and if only Landlock ABI version 1 was available,
618 we could not restrict the process.
619 .P
620 Now that the ruleset attributes are determined,
621 we create the Landlock ruleset
622 and acquire a file descriptor as a handle to it,
623 using
624 .BR landlock_create_ruleset (2):
625 .P
626 .in +4n
627 .EX
628 ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
629 if (ruleset_fd == \-1) {
630 perror("Failed to create a ruleset");
631 exit(EXIT_FAILURE);
632 }
633 .EE
634 .in
635 .P
636 We can now add a new rule to the ruleset through the ruleset's file descriptor.
637 The requested access rights must be a subset of the access rights
638 which were specified in
639 .I attr.handled_access_fs
640 at ruleset creation time.
641 .P
642 In this example, the rule will only allow reading the file hierarchy
643 .IR /usr .
644 Without another rule, write actions would then be denied by the ruleset.
645 To add
646 .I /usr
647 to the ruleset, we open it with the
648 .I O_PATH
649 flag and fill the
650 .I struct landlock_path_beneath_attr
651 with this file descriptor.
652 .P
653 .in +4n
654 .EX
655 struct landlock_path_beneath_attr path_beneath = {0};
656 int err;
657 \&
658 path_beneath.allowed_access =
659 LANDLOCK_ACCESS_FS_EXECUTE |
660 LANDLOCK_ACCESS_FS_READ_FILE |
661 LANDLOCK_ACCESS_FS_READ_DIR;
662 \&
663 path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC);
664 if (path_beneath.parent_fd == \-1) {
665 perror("Failed to open file");
666 close(ruleset_fd);
667 exit(EXIT_FAILURE);
668 }
669 err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
670 &path_beneath, 0);
671 close(path_beneath.parent_fd);
672 if (err) {
673 perror("Failed to update ruleset");
674 close(ruleset_fd);
675 exit(EXIT_FAILURE);
676 }
677 .EE
678 .in
679 .P
680 We now have a ruleset with one rule allowing read access to
681 .I /usr
682 while denying all other handled accesses for the filesystem.
683 The next step is to restrict the current thread from gaining more
684 privileges
685 (e.g., thanks to a set-user-ID binary).
686 .P
687 .in +4n
688 .EX
689 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
690 perror("Failed to restrict privileges");
691 close(ruleset_fd);
692 exit(EXIT_FAILURE);
693 }
694 .EE
695 .in
696 .P
697 The current thread is now ready to sandbox itself with the ruleset.
698 .P
699 .in +4n
700 .EX
701 if (landlock_restrict_self(ruleset_fd, 0)) {
702 perror("Failed to enforce ruleset");
703 close(ruleset_fd);
704 exit(EXIT_FAILURE);
705 }
706 close(ruleset_fd);
707 .EE
708 .in
709 .P
710 If the
711 .BR landlock_restrict_self (2)
712 system call succeeds, the current thread is now restricted and
713 this policy will be enforced on all its subsequently created children as well.
714 Once a thread is landlocked, there is no way to remove its security policy;
715 only adding more restrictions is allowed.
716 These threads are now in a new Landlock domain,
717 merge of their parent one (if any) with the new ruleset.
718 .P
719 Full working code can be found in
720 .UR https:\://git.kernel.org/\:pub/\:scm/\:linux/\:kernel/\:git/\:stable/\:linux.git/\:tree/\:samples/\:landlock/\:sandboxer.c
721 .UE
722 .SH SEE ALSO
723 .BR landlock_create_ruleset (2),
724 .BR landlock_add_rule (2),
725 .BR landlock_restrict_self (2)
726 .P
727 .UR https:\://landlock.io/
728 .UE