Debian Codegraph @

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