| 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 2024-05-02 "Linux man-pages 6.9.1" |
| 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 |
An object is currently a file hierarchy, |
| 44 |
and the related filesystem actions are defined with access rights (see |
| 45 |
.BR landlock_add_rule (2)). |
| 46 |
A set of rules is aggregated in a ruleset, |
| 47 |
which can then restrict the thread enforcing it, |
| 48 |
and its future children. |
| 49 |
.\" |
| 50 |
.SS Filesystem actions |
| 51 |
These flags enable to restrict a sandboxed process to a |
| 52 |
set of actions on files and directories. |
| 53 |
Files or directories opened before the sandboxing |
| 54 |
are not subject to these restrictions. |
| 55 |
See |
| 56 |
.BR landlock_add_rule (2) |
| 57 |
and |
| 58 |
.BR landlock_create_ruleset (2) |
| 59 |
for more context. |
| 60 |
.P |
| 61 |
A file can only receive these access rights: |
| 62 |
.TP |
| 63 |
.B LANDLOCK_ACCESS_FS_EXECUTE |
| 64 |
Execute a file. |
| 65 |
.TP |
| 66 |
.B LANDLOCK_ACCESS_FS_WRITE_FILE |
| 67 |
Open a file with write access. |
| 68 |
.IP |
| 69 |
When opening files for writing, |
| 70 |
you will often additionally need the |
| 71 |
.B LANDLOCK_ACCESS_FS_TRUNCATE |
| 72 |
right. |
| 73 |
In many cases, |
| 74 |
these system calls truncate existing files when overwriting them |
| 75 |
(e.g., |
| 76 |
.BR creat (2)). |
| 77 |
.TP |
| 78 |
.B LANDLOCK_ACCESS_FS_READ_FILE |
| 79 |
Open a file with read access. |
| 80 |
.TP |
| 81 |
.B LANDLOCK_ACCESS_FS_TRUNCATE |
| 82 |
Truncate a file with |
| 83 |
.BR truncate (2), |
| 84 |
.BR ftruncate (2), |
| 85 |
.BR creat (2), |
| 86 |
or |
| 87 |
.BR open (2) |
| 88 |
with |
| 89 |
.BR O_TRUNC . |
| 90 |
Whether an opened file can be truncated with |
| 91 |
.BR ftruncate (2) |
| 92 |
is determined during |
| 93 |
.BR open (2), |
| 94 |
in the same way as read and write permissions are checked during |
| 95 |
.BR open (2) |
| 96 |
using |
| 97 |
.B LANDLOCK_ACCESS_FS_READ_FILE |
| 98 |
and |
| 99 |
.BR LANDLOCK_ACCESS_FS_WRITE_FILE . |
| 100 |
This access right is available since the third version of the Landlock ABI. |
| 101 |
.P |
| 102 |
A directory can receive access rights related to files or directories. |
| 103 |
The following access right is applied to the directory itself, |
| 104 |
and the directories beneath it: |
| 105 |
.TP |
| 106 |
.B LANDLOCK_ACCESS_FS_READ_DIR |
| 107 |
Open a directory or list its content. |
| 108 |
.P |
| 109 |
However, |
| 110 |
the following access rights only apply to the content of a directory, |
| 111 |
not the directory itself: |
| 112 |
.TP |
| 113 |
.B LANDLOCK_ACCESS_FS_REMOVE_DIR |
| 114 |
Remove an empty directory or rename one. |
| 115 |
.TP |
| 116 |
.B LANDLOCK_ACCESS_FS_REMOVE_FILE |
| 117 |
Unlink (or rename) a file. |
| 118 |
.TP |
| 119 |
.B LANDLOCK_ACCESS_FS_MAKE_CHAR |
| 120 |
Create (or rename or link) a character device. |
| 121 |
.TP |
| 122 |
.B LANDLOCK_ACCESS_FS_MAKE_DIR |
| 123 |
Create (or rename) a directory. |
| 124 |
.TP |
| 125 |
.B LANDLOCK_ACCESS_FS_MAKE_REG |
| 126 |
Create (or rename or link) a regular file. |
| 127 |
.TP |
| 128 |
.B LANDLOCK_ACCESS_FS_MAKE_SOCK |
| 129 |
Create (or rename or link) a UNIX domain socket. |
| 130 |
.TP |
| 131 |
.B LANDLOCK_ACCESS_FS_MAKE_FIFO |
| 132 |
Create (or rename or link) a named pipe. |
| 133 |
.TP |
| 134 |
.B LANDLOCK_ACCESS_FS_MAKE_BLOCK |
| 135 |
Create (or rename or link) a block device. |
| 136 |
.TP |
| 137 |
.B LANDLOCK_ACCESS_FS_MAKE_SYM |
| 138 |
Create (or rename or link) a symbolic link. |
| 139 |
.TP |
| 140 |
.B LANDLOCK_ACCESS_FS_REFER |
| 141 |
Link or rename a file from or to a different directory |
| 142 |
(i.e., reparent a file hierarchy). |
| 143 |
.IP |
| 144 |
This access right is available since the second version of the Landlock ABI. |
| 145 |
.IP |
| 146 |
This is the only access right which is denied by default by any ruleset, |
| 147 |
even if the right is not specified as handled at ruleset creation time. |
| 148 |
The only way to make a ruleset grant this right |
| 149 |
is to explicitly allow it for a specific directory |
| 150 |
by adding a matching rule to the ruleset. |
| 151 |
.IP |
| 152 |
In particular, when using the first Landlock ABI version, |
| 153 |
Landlock will always deny attempts to reparent files |
| 154 |
between different directories. |
| 155 |
.IP |
| 156 |
In addition to the source and destination directories having the |
| 157 |
.B LANDLOCK_ACCESS_FS_REFER |
| 158 |
access right, |
| 159 |
the attempted link or rename operation must meet the following constraints: |
| 160 |
.RS |
| 161 |
.IP \[bu] 3 |
| 162 |
The reparented file may not gain more access rights in the destination directory |
| 163 |
than it previously had in the source directory. |
| 164 |
If this is attempted, the operation results in an |
| 165 |
.B EXDEV |
| 166 |
error. |
| 167 |
.IP \[bu] |
| 168 |
When linking or renaming, the |
| 169 |
.BI LANDLOCK_ACCESS_FS_MAKE_ * |
| 170 |
right for the respective file type must be granted |
| 171 |
for the destination directory. |
| 172 |
Otherwise, the operation results in an |
| 173 |
.B EACCES |
| 174 |
error. |
| 175 |
.IP \[bu] |
| 176 |
When renaming, the |
| 177 |
.BI LANDLOCK_ACCESS_FS_REMOVE_ * |
| 178 |
right for the respective file type must be granted |
| 179 |
for the source directory. |
| 180 |
Otherwise, the operation results in an |
| 181 |
.B EACCES |
| 182 |
error. |
| 183 |
.RE |
| 184 |
.IP |
| 185 |
If multiple requirements are not met, the |
| 186 |
.B EACCES |
| 187 |
error code takes precedence over |
| 188 |
.BR EXDEV . |
| 189 |
.\" |
| 190 |
.SS Layers of file path access rights |
| 191 |
Each time a thread enforces a ruleset on itself, |
| 192 |
it updates its Landlock domain with a new layer of policy. |
| 193 |
Indeed, this complementary policy is composed with the |
| 194 |
potentially other rulesets already restricting this thread. |
| 195 |
A sandboxed thread can then safely add more constraints to itself with a |
| 196 |
new enforced ruleset. |
| 197 |
.P |
| 198 |
One policy layer grants access to a file path |
| 199 |
if at least one of its rules encountered on the path grants the access. |
| 200 |
A sandboxed thread can only access a file path |
| 201 |
if all its enforced policy layers grant the access |
| 202 |
as well as all the other system access controls |
| 203 |
(e.g., filesystem DAC, other LSM policies, etc.). |
| 204 |
.\" |
| 205 |
.SS Bind mounts and OverlayFS |
| 206 |
Landlock enables restricting access to file hierarchies, |
| 207 |
which means that these access rights can be propagated with bind mounts |
| 208 |
(cf. |
| 209 |
.BR mount_namespaces (7)) |
| 210 |
but not with OverlayFS. |
| 211 |
.P |
| 212 |
A bind mount mirrors a source file hierarchy to a destination. |
| 213 |
The destination hierarchy is then composed of the exact same files, |
| 214 |
on which Landlock rules can be tied, |
| 215 |
either via the source or the destination path. |
| 216 |
These rules restrict access when they are encountered on a path, |
| 217 |
which means that they can restrict access to |
| 218 |
multiple file hierarchies at the same time, |
| 219 |
whether these hierarchies are the result of bind mounts or not. |
| 220 |
.P |
| 221 |
An OverlayFS mount point consists of upper and lower layers. |
| 222 |
These layers are combined in a merge directory, result of the mount point. |
| 223 |
This merge hierarchy may include files from the upper and lower layers, |
| 224 |
but modifications performed on the merge hierarchy |
| 225 |
only reflect on the upper layer. |
| 226 |
From a Landlock policy point of view, |
| 227 |
each of the OverlayFS layers and merge hierarchies is standalone and |
| 228 |
contains its own set of files and directories, |
| 229 |
which is different from a bind mount. |
| 230 |
A policy restricting an OverlayFS layer will not restrict |
| 231 |
the resulted merged hierarchy, and vice versa. |
| 232 |
Landlock users should then only think about file hierarchies they want to |
| 233 |
allow access to, regardless of the underlying filesystem. |
| 234 |
.\" |
| 235 |
.SS Inheritance |
| 236 |
Every new thread resulting from a |
| 237 |
.BR clone (2) |
| 238 |
inherits Landlock domain restrictions from its parent. |
| 239 |
This is similar to the |
| 240 |
.BR seccomp (2) |
| 241 |
inheritance or any other LSM dealing with tasks' |
| 242 |
.BR credentials (7). |
| 243 |
For instance, one process's thread may apply Landlock rules to itself, |
| 244 |
but they will not be automatically applied to other sibling threads |
| 245 |
(unlike POSIX thread credential changes, cf. |
| 246 |
.BR nptl (7)). |
| 247 |
.P |
| 248 |
When a thread sandboxes itself, |
| 249 |
we have the guarantee that the related security policy |
| 250 |
will stay enforced on all this thread's descendants. |
| 251 |
This allows creating standalone and modular security policies |
| 252 |
per application, |
| 253 |
which will automatically be composed between themselves |
| 254 |
according to their run-time parent policies. |
| 255 |
.\" |
| 256 |
.SS Ptrace restrictions |
| 257 |
A sandboxed process has less privileges than a non-sandboxed process and |
| 258 |
must then be subject to additional restrictions |
| 259 |
when manipulating another process. |
| 260 |
To be allowed to use |
| 261 |
.BR ptrace (2) |
| 262 |
and related syscalls on a target process, |
| 263 |
a sandboxed process should have a subset of the target process rules, |
| 264 |
which means the tracee must be in a sub-domain of the tracer. |
| 265 |
.\" |
| 266 |
.SS Truncating files |
| 267 |
The operations covered by |
| 268 |
.B LANDLOCK_ACCESS_FS_WRITE_FILE |
| 269 |
and |
| 270 |
.B LANDLOCK_ACCESS_FS_TRUNCATE |
| 271 |
both change the contents of a file and sometimes overlap in |
| 272 |
non-intuitive ways. |
| 273 |
It is recommended to always specify both of these together. |
| 274 |
.P |
| 275 |
A particularly surprising example is |
| 276 |
.BR creat (2). |
| 277 |
The name suggests that this system call requires |
| 278 |
the rights to create and write files. |
| 279 |
However, it also requires the truncate right |
| 280 |
if an existing file under the same name is already present. |
| 281 |
.P |
| 282 |
It should also be noted that truncating files does not require the |
| 283 |
.B LANDLOCK_ACCESS_FS_WRITE_FILE |
| 284 |
right. |
| 285 |
Apart from the |
| 286 |
.BR truncate (2) |
| 287 |
system call, this can also be done through |
| 288 |
.BR open (2) |
| 289 |
with the flags |
| 290 |
.IR "O_RDONLY\ |\ O_TRUNC" . |
| 291 |
.P |
| 292 |
When opening a file, the availability of the |
| 293 |
.B LANDLOCK_ACCESS_FS_TRUNCATE |
| 294 |
right is associated with the newly created file descriptor |
| 295 |
and will be used for subsequent truncation attempts using |
| 296 |
.BR ftruncate (2). |
| 297 |
The behavior is similar to opening a file for reading or writing, |
| 298 |
where permissions are checked during |
| 299 |
.BR open (2), |
| 300 |
but not during the subsequent |
| 301 |
.BR read (2) |
| 302 |
and |
| 303 |
.BR write (2) |
| 304 |
calls. |
| 305 |
.P |
| 306 |
As a consequence, |
| 307 |
it is possible to have multiple open file descriptors for the same file, |
| 308 |
where one grants the right to truncate the file and the other does not. |
| 309 |
It is also possible to pass such file descriptors between processes, |
| 310 |
keeping their Landlock properties, |
| 311 |
even when these processes do not have an enforced Landlock ruleset. |
| 312 |
.SH VERSIONS |
| 313 |
Landlock was introduced in Linux 5.13. |
| 314 |
.P |
| 315 |
To determine which Landlock features are available, |
| 316 |
users should query the Landlock ABI version: |
| 317 |
.TS |
| 318 |
box; |
| 319 |
ntb| ntb| lbx |
| 320 |
nt| nt| lbx. |
| 321 |
ABI Kernel Newly introduced access rights |
| 322 |
_ _ _ |
| 323 |
1 5.13 LANDLOCK_ACCESS_FS_EXECUTE |
| 324 |
\^ \^ LANDLOCK_ACCESS_FS_WRITE_FILE |
| 325 |
\^ \^ LANDLOCK_ACCESS_FS_READ_FILE |
| 326 |
\^ \^ LANDLOCK_ACCESS_FS_READ_DIR |
| 327 |
\^ \^ LANDLOCK_ACCESS_FS_REMOVE_DIR |
| 328 |
\^ \^ LANDLOCK_ACCESS_FS_REMOVE_FILE |
| 329 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_CHAR |
| 330 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_DIR |
| 331 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_REG |
| 332 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_SOCK |
| 333 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_FIFO |
| 334 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_BLOCK |
| 335 |
\^ \^ LANDLOCK_ACCESS_FS_MAKE_SYM |
| 336 |
_ _ _ |
| 337 |
2 5.19 LANDLOCK_ACCESS_FS_REFER |
| 338 |
_ _ _ |
| 339 |
3 6.2 LANDLOCK_ACCESS_FS_TRUNCATE |
| 340 |
.TE |
| 341 |
.P |
| 342 |
Users should use the Landlock ABI version rather than the kernel version |
| 343 |
to determine which features are available. |
| 344 |
The mainline kernel versions listed here are only included for orientation. |
| 345 |
Kernels from other sources may contain backported features, |
| 346 |
and their version numbers may not match. |
| 347 |
.P |
| 348 |
To query the running kernel's Landlock ABI version, |
| 349 |
programs may pass the |
| 350 |
.B LANDLOCK_CREATE_RULESET_VERSION |
| 351 |
flag to |
| 352 |
.BR landlock_create_ruleset (2). |
| 353 |
.P |
| 354 |
When building fallback mechanisms for compatibility with older kernels, |
| 355 |
users are advised to consider the special semantics of the |
| 356 |
.B LANDLOCK_ACCESS_FS_REFER |
| 357 |
access right: |
| 358 |
In ABI v1, |
| 359 |
linking and moving of files between different directories is always forbidden, |
| 360 |
so programs relying on such operations are only compatible |
| 361 |
with Landlock ABI v2 and higher. |
| 362 |
.SH NOTES |
| 363 |
Landlock is enabled by |
| 364 |
.BR CONFIG_SECURITY_LANDLOCK . |
| 365 |
The |
| 366 |
.I lsm=lsm1,...,lsmN |
| 367 |
command line parameter controls the sequence of the initialization of |
| 368 |
Linux Security Modules. |
| 369 |
It must contain the string |
| 370 |
.I landlock |
| 371 |
to enable Landlock. |
| 372 |
If the command line parameter is not specified, |
| 373 |
the initialization falls back to the value of the deprecated |
| 374 |
.I security= |
| 375 |
command line parameter and further to the value of |
| 376 |
.BR CONFIG_LSM . |
| 377 |
We can check that Landlock is enabled by looking for |
| 378 |
.I landlock: Up and running. |
| 379 |
in kernel logs. |
| 380 |
.SH CAVEATS |
| 381 |
It is currently not possible to restrict some file-related actions |
| 382 |
accessible through these system call families: |
| 383 |
.BR chdir (2), |
| 384 |
.BR stat (2), |
| 385 |
.BR flock (2), |
| 386 |
.BR chmod (2), |
| 387 |
.BR chown (2), |
| 388 |
.BR setxattr (2), |
| 389 |
.BR utime (2), |
| 390 |
.BR ioctl (2), |
| 391 |
.BR fcntl (2), |
| 392 |
.BR access (2). |
| 393 |
Future Landlock evolutions will enable to restrict them. |
| 394 |
.SH EXAMPLES |
| 395 |
We first need to create the ruleset that will contain our rules. |
| 396 |
.P |
| 397 |
For this example, |
| 398 |
the ruleset will contain rules that only allow read actions, |
| 399 |
but write actions will be denied. |
| 400 |
The ruleset then needs to handle both of these kinds of actions. |
| 401 |
See the |
| 402 |
.B DESCRIPTION |
| 403 |
section for the description of filesystem actions. |
| 404 |
.P |
| 405 |
.in +4n |
| 406 |
.EX |
| 407 |
struct landlock_ruleset_attr attr = {0}; |
| 408 |
int ruleset_fd; |
| 409 |
\& |
| 410 |
attr.handled_access_fs = |
| 411 |
LANDLOCK_ACCESS_FS_EXECUTE | |
| 412 |
LANDLOCK_ACCESS_FS_WRITE_FILE | |
| 413 |
LANDLOCK_ACCESS_FS_READ_FILE | |
| 414 |
LANDLOCK_ACCESS_FS_READ_DIR | |
| 415 |
LANDLOCK_ACCESS_FS_REMOVE_DIR | |
| 416 |
LANDLOCK_ACCESS_FS_REMOVE_FILE | |
| 417 |
LANDLOCK_ACCESS_FS_MAKE_CHAR | |
| 418 |
LANDLOCK_ACCESS_FS_MAKE_DIR | |
| 419 |
LANDLOCK_ACCESS_FS_MAKE_REG | |
| 420 |
LANDLOCK_ACCESS_FS_MAKE_SOCK | |
| 421 |
LANDLOCK_ACCESS_FS_MAKE_FIFO | |
| 422 |
LANDLOCK_ACCESS_FS_MAKE_BLOCK | |
| 423 |
LANDLOCK_ACCESS_FS_MAKE_SYM | |
| 424 |
LANDLOCK_ACCESS_FS_REFER | |
| 425 |
LANDLOCK_ACCESS_FS_TRUNCATE; |
| 426 |
.EE |
| 427 |
.in |
| 428 |
.P |
| 429 |
To be compatible with older Linux versions, |
| 430 |
we detect the available Landlock ABI version, |
| 431 |
and only use the available subset of access rights: |
| 432 |
.P |
| 433 |
.in +4n |
| 434 |
.EX |
| 435 |
/* |
| 436 |
* Table of available file system access rights by ABI version, |
| 437 |
* numbers hardcoded to keep the example short. |
| 438 |
*/ |
| 439 |
__u64 landlock_fs_access_rights[] = { |
| 440 |
(LANDLOCK_ACCESS_FS_MAKE_SYM << 1) \- 1, /* v1 */ |
| 441 |
(LANDLOCK_ACCESS_FS_REFER << 1) \- 1, /* v2: add "refer" */ |
| 442 |
(LANDLOCK_ACCESS_FS_TRUNCATE << 1) \- 1, /* v3: add "truncate" */ |
| 443 |
}; |
| 444 |
\& |
| 445 |
int abi = landlock_create_ruleset(NULL, 0, |
| 446 |
LANDLOCK_CREATE_RULESET_VERSION); |
| 447 |
if (abi == \-1) { |
| 448 |
/* |
| 449 |
* Kernel too old, not compiled with Landlock, |
| 450 |
* or Landlock was not enabled at boot time. |
| 451 |
*/ |
| 452 |
perror("Unable to use Landlock"); |
| 453 |
return; /* Graceful fallback: Do nothing. */ |
| 454 |
} |
| 455 |
abi = MIN(abi, 3); |
| 456 |
\& |
| 457 |
/* Only use the available rights in the ruleset. */ |
| 458 |
attr.handled_access_fs &= landlock_fs_access_rights[abi \- 1]; |
| 459 |
.EE |
| 460 |
.in |
| 461 |
.P |
| 462 |
The available access rights for each ABI version are listed in the |
| 463 |
.B VERSIONS |
| 464 |
section. |
| 465 |
.P |
| 466 |
If our program needed to create hard links |
| 467 |
or rename files between different directories |
| 468 |
.RB ( LANDLOCK_ACCESS_FS_REFER ), |
| 469 |
we would require the following change to the backwards compatibility logic: |
| 470 |
Directory reparenting is not possible |
| 471 |
in a process restricted with Landlock ABI version 1. |
| 472 |
Therefore, |
| 473 |
if the program needed to do file reparenting, |
| 474 |
and if only Landlock ABI version 1 was available, |
| 475 |
we could not restrict the process. |
| 476 |
.P |
| 477 |
Now that the ruleset attributes are determined, |
| 478 |
we create the Landlock ruleset |
| 479 |
and acquire a file descriptor as a handle to it, |
| 480 |
using |
| 481 |
.BR landlock_create_ruleset (2): |
| 482 |
.P |
| 483 |
.in +4n |
| 484 |
.EX |
| 485 |
ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0); |
| 486 |
if (ruleset_fd == \-1) { |
| 487 |
perror("Failed to create a ruleset"); |
| 488 |
exit(EXIT_FAILURE); |
| 489 |
} |
| 490 |
.EE |
| 491 |
.in |
| 492 |
.P |
| 493 |
We can now add a new rule to the ruleset through the ruleset's file descriptor. |
| 494 |
The requested access rights must be a subset of the access rights |
| 495 |
which were specified in |
| 496 |
.I attr.handled_access_fs |
| 497 |
at ruleset creation time. |
| 498 |
.P |
| 499 |
In this example, the rule will only allow reading the file hierarchy |
| 500 |
.IR /usr . |
| 501 |
Without another rule, write actions would then be denied by the ruleset. |
| 502 |
To add |
| 503 |
.I /usr |
| 504 |
to the ruleset, we open it with the |
| 505 |
.I O_PATH |
| 506 |
flag and fill the |
| 507 |
.I struct landlock_path_beneath_attr |
| 508 |
with this file descriptor. |
| 509 |
.P |
| 510 |
.in +4n |
| 511 |
.EX |
| 512 |
struct landlock_path_beneath_attr path_beneath = {0}; |
| 513 |
int err; |
| 514 |
\& |
| 515 |
path_beneath.allowed_access = |
| 516 |
LANDLOCK_ACCESS_FS_EXECUTE | |
| 517 |
LANDLOCK_ACCESS_FS_READ_FILE | |
| 518 |
LANDLOCK_ACCESS_FS_READ_DIR; |
| 519 |
\& |
| 520 |
path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC); |
| 521 |
if (path_beneath.parent_fd == \-1) { |
| 522 |
perror("Failed to open file"); |
| 523 |
close(ruleset_fd); |
| 524 |
exit(EXIT_FAILURE); |
| 525 |
} |
| 526 |
err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, |
| 527 |
&path_beneath, 0); |
| 528 |
close(path_beneath.parent_fd); |
| 529 |
if (err) { |
| 530 |
perror("Failed to update ruleset"); |
| 531 |
close(ruleset_fd); |
| 532 |
exit(EXIT_FAILURE); |
| 533 |
} |
| 534 |
.EE |
| 535 |
.in |
| 536 |
.P |
| 537 |
We now have a ruleset with one rule allowing read access to |
| 538 |
.I /usr |
| 539 |
while denying all other handled accesses for the filesystem. |
| 540 |
The next step is to restrict the current thread from gaining more |
| 541 |
privileges |
| 542 |
(e.g., thanks to a set-user-ID binary). |
| 543 |
.P |
| 544 |
.in +4n |
| 545 |
.EX |
| 546 |
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 547 |
perror("Failed to restrict privileges"); |
| 548 |
close(ruleset_fd); |
| 549 |
exit(EXIT_FAILURE); |
| 550 |
} |
| 551 |
.EE |
| 552 |
.in |
| 553 |
.P |
| 554 |
The current thread is now ready to sandbox itself with the ruleset. |
| 555 |
.P |
| 556 |
.in +4n |
| 557 |
.EX |
| 558 |
if (landlock_restrict_self(ruleset_fd, 0)) { |
| 559 |
perror("Failed to enforce ruleset"); |
| 560 |
close(ruleset_fd); |
| 561 |
exit(EXIT_FAILURE); |
| 562 |
} |
| 563 |
close(ruleset_fd); |
| 564 |
.EE |
| 565 |
.in |
| 566 |
.P |
| 567 |
If the |
| 568 |
.BR landlock_restrict_self (2) |
| 569 |
system call succeeds, the current thread is now restricted and |
| 570 |
this policy will be enforced on all its subsequently created children as well. |
| 571 |
Once a thread is landlocked, there is no way to remove its security policy; |
| 572 |
only adding more restrictions is allowed. |
| 573 |
These threads are now in a new Landlock domain, |
| 574 |
merge of their parent one (if any) with the new ruleset. |
| 575 |
.P |
| 576 |
Full working code can be found in |
| 577 |
.UR https://git.kernel.org/\:pub/\:scm/\:linux/\:kernel/\:git/\:stable/\:linux.git/\:tree/\:samples/\:landlock/\:sandboxer.c |
| 578 |
.UE |
| 579 |
.SH SEE ALSO |
| 580 |
.BR landlock_create_ruleset (2), |
| 581 |
.BR landlock_add_rule (2), |
| 582 |
.BR landlock_restrict_self (2) |
| 583 |
.P |
| 584 |
.UR https://landlock.io/ |
| 585 |
.UE |