1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Landlock - System call implementations and user space interfaces 4 * 5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> 6 * Copyright © 2018-2020 ANSSI 7 * Copyright © 2021-2025 Microsoft Corporation 8 */ 9 10 #include <asm/current.h> 11 #include <linux/anon_inodes.h> 12 #include <linux/bitops.h> 13 #include <linux/build_bug.h> 14 #include <linux/capability.h> 15 #include <linux/cleanup.h> 16 #include <linux/compiler_types.h> 17 #include <linux/dcache.h> 18 #include <linux/err.h> 19 #include <linux/errno.h> 20 #include <linux/fs.h> 21 #include <linux/limits.h> 22 #include <linux/mount.h> 23 #include <linux/path.h> 24 #include <linux/sched.h> 25 #include <linux/security.h> 26 #include <linux/stddef.h> 27 #include <linux/syscalls.h> 28 #include <linux/types.h> 29 #include <linux/uaccess.h> 30 #include <uapi/linux/landlock.h> 31 32 #include "cred.h" 33 #include "domain.h" 34 #include "fs.h" 35 #include "limits.h" 36 #include "net.h" 37 #include "ruleset.h" 38 #include "setup.h" 39 #include "tsync.h" 40 41 static bool is_initialized(void) 42 { 43 if (likely(landlock_initialized)) 44 return true; 45 46 pr_warn_once( 47 "Disabled but requested by user space. " 48 "You should enable Landlock at boot time: " 49 "https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n"); 50 return false; 51 } 52 53 /** 54 * copy_min_struct_from_user - Safe future-proof argument copying 55 * 56 * Extend copy_struct_from_user() to check for consistent user buffer. 57 * 58 * @dst: Kernel space pointer or NULL. 59 * @ksize: Actual size of the data pointed to by @dst. 60 * @ksize_min: Minimal required size to be copied. 61 * @src: User space pointer or NULL. 62 * @usize: (Alleged) size of the data pointed to by @src. 63 * 64 * Return: 0 on success, -errno on failure. 65 */ 66 static __always_inline int 67 copy_min_struct_from_user(void *const dst, const size_t ksize, 68 const size_t ksize_min, const void __user *const src, 69 const size_t usize) 70 { 71 /* Checks buffer inconsistencies. */ 72 BUILD_BUG_ON(!dst); 73 if (!src) 74 return -EFAULT; 75 76 /* Checks size ranges. */ 77 BUILD_BUG_ON(ksize <= 0); 78 BUILD_BUG_ON(ksize < ksize_min); 79 if (usize < ksize_min) 80 return -EINVAL; 81 if (usize > PAGE_SIZE) 82 return -E2BIG; 83 84 /* Copies user buffer and fills with zeros. */ 85 return copy_struct_from_user(dst, ksize, src, usize); 86 } 87 88 /* 89 * This function only contains arithmetic operations with constants, leading to 90 * BUILD_BUG_ON(). The related code is evaluated and checked at build time, 91 * but it is then ignored thanks to compiler optimizations. 92 */ 93 static void build_check_abi(void) 94 { 95 struct landlock_ruleset_attr ruleset_attr; 96 struct landlock_path_beneath_attr path_beneath_attr; 97 struct landlock_net_port_attr net_port_attr; 98 size_t ruleset_size, path_beneath_size, net_port_size; 99 100 /* 101 * For each user space ABI structures, first checks that there is no 102 * hole in them, then checks that all architectures have the same 103 * struct size. 104 */ 105 ruleset_size = sizeof(ruleset_attr.handled_access_fs); 106 ruleset_size += sizeof(ruleset_attr.handled_access_net); 107 ruleset_size += sizeof(ruleset_attr.scoped); 108 BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size); 109 BUILD_BUG_ON(sizeof(ruleset_attr) != 24); 110 111 path_beneath_size = sizeof(path_beneath_attr.allowed_access); 112 path_beneath_size += sizeof(path_beneath_attr.parent_fd); 113 BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size); 114 BUILD_BUG_ON(sizeof(path_beneath_attr) != 12); 115 116 net_port_size = sizeof(net_port_attr.allowed_access); 117 net_port_size += sizeof(net_port_attr.port); 118 BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size); 119 BUILD_BUG_ON(sizeof(net_port_attr) != 16); 120 } 121 122 /* Ruleset handling */ 123 124 static int fop_ruleset_release(struct inode *const inode, 125 struct file *const filp) 126 { 127 struct landlock_ruleset *ruleset = filp->private_data; 128 129 landlock_put_ruleset(ruleset); 130 return 0; 131 } 132 133 static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf, 134 const size_t size, loff_t *const ppos) 135 { 136 /* Dummy handler to enable FMODE_CAN_READ. */ 137 return -EINVAL; 138 } 139 140 static ssize_t fop_dummy_write(struct file *const filp, 141 const char __user *const buf, const size_t size, 142 loff_t *const ppos) 143 { 144 /* Dummy handler to enable FMODE_CAN_WRITE. */ 145 return -EINVAL; 146 } 147 148 /* 149 * A ruleset file descriptor enables to build a ruleset by adding (i.e. 150 * writing) rule after rule, without relying on the task's context. This 151 * reentrant design is also used in a read way to enforce the ruleset on the 152 * current task. 153 */ 154 static const struct file_operations ruleset_fops = { 155 .release = fop_ruleset_release, 156 .read = fop_dummy_read, 157 .write = fop_dummy_write, 158 }; 159 160 /* 161 * The Landlock ABI version should be incremented for each new Landlock-related 162 * user space visible change (e.g. Landlock syscalls). This version should 163 * only be incremented once per Linux release. When incrementing, the date in 164 * Documentation/userspace-api/landlock.rst should be updated to reflect the 165 * UAPI change. 166 * If the change involves a fix that requires userspace awareness, also update 167 * the errata documentation in Documentation/userspace-api/landlock.rst . 168 */ 169 const int landlock_abi_version = 9; 170 171 /** 172 * sys_landlock_create_ruleset - Create a new ruleset 173 * 174 * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of 175 * the new ruleset. 176 * @size: Size of the pointed &struct landlock_ruleset_attr (needed for 177 * backward and forward compatibility). 178 * @flags: Supported values: 179 * 180 * - %LANDLOCK_CREATE_RULESET_VERSION 181 * - %LANDLOCK_CREATE_RULESET_ERRATA 182 * 183 * This system call enables to create a new Landlock ruleset. 184 * 185 * If %LANDLOCK_CREATE_RULESET_VERSION or %LANDLOCK_CREATE_RULESET_ERRATA is 186 * set, then @attr must be NULL and @size must be 0. 187 * 188 * Return: The ruleset file descriptor on success, the Landlock ABI version if 189 * %LANDLOCK_CREATE_RULESET_VERSION is set, the errata value if 190 * %LANDLOCK_CREATE_RULESET_ERRATA is set, or -errno on failure. Possible 191 * returned errors are: 192 * 193 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; 194 * - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small 195 * @size; 196 * - %E2BIG: @attr or @size inconsistencies; 197 * - %EFAULT: @attr or @size inconsistencies; 198 * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs. 199 * 200 * .. kernel-doc:: include/uapi/linux/landlock.h 201 * :identifiers: landlock_create_ruleset_flags 202 */ 203 SYSCALL_DEFINE3(landlock_create_ruleset, 204 const struct landlock_ruleset_attr __user *const, attr, 205 const size_t, size, const __u32, flags) 206 { 207 struct landlock_ruleset_attr ruleset_attr; 208 struct landlock_ruleset *ruleset; 209 int err, ruleset_fd; 210 211 /* Build-time checks. */ 212 build_check_abi(); 213 214 if (!is_initialized()) 215 return -EOPNOTSUPP; 216 217 if (flags) { 218 if (attr || size) 219 return -EINVAL; 220 221 if (flags == LANDLOCK_CREATE_RULESET_VERSION) 222 return landlock_abi_version; 223 224 if (flags == LANDLOCK_CREATE_RULESET_ERRATA) 225 return landlock_errata; 226 227 return -EINVAL; 228 } 229 230 /* Copies raw user space buffer. */ 231 err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr), 232 offsetofend(typeof(ruleset_attr), 233 handled_access_fs), 234 attr, size); 235 if (err) 236 return err; 237 238 /* Checks content (and 32-bits cast). */ 239 if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) != 240 LANDLOCK_MASK_ACCESS_FS) 241 return -EINVAL; 242 243 /* Checks network content (and 32-bits cast). */ 244 if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) != 245 LANDLOCK_MASK_ACCESS_NET) 246 return -EINVAL; 247 248 /* Checks IPC scoping content (and 32-bits cast). */ 249 if ((ruleset_attr.scoped | LANDLOCK_MASK_SCOPE) != LANDLOCK_MASK_SCOPE) 250 return -EINVAL; 251 252 /* Checks arguments and transforms to kernel struct. */ 253 ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs, 254 ruleset_attr.handled_access_net, 255 ruleset_attr.scoped); 256 if (IS_ERR(ruleset)) 257 return PTR_ERR(ruleset); 258 259 /* Creates anonymous FD referring to the ruleset. */ 260 ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops, 261 ruleset, O_RDWR | O_CLOEXEC); 262 if (ruleset_fd < 0) 263 landlock_put_ruleset(ruleset); 264 return ruleset_fd; 265 } 266 267 /* 268 * Returns an owned ruleset from a FD. It is thus needed to call 269 * landlock_put_ruleset() on the return value. 270 */ 271 static struct landlock_ruleset *get_ruleset_from_fd(const int fd, 272 const fmode_t mode) 273 { 274 CLASS(fd, ruleset_f)(fd); 275 struct landlock_ruleset *ruleset; 276 277 if (fd_empty(ruleset_f)) 278 return ERR_PTR(-EBADF); 279 280 /* Checks FD type and access right. */ 281 if (fd_file(ruleset_f)->f_op != &ruleset_fops) 282 return ERR_PTR(-EBADFD); 283 if (!(fd_file(ruleset_f)->f_mode & mode)) 284 return ERR_PTR(-EPERM); 285 ruleset = fd_file(ruleset_f)->private_data; 286 if (WARN_ON_ONCE(ruleset->num_layers != 1)) 287 return ERR_PTR(-EINVAL); 288 landlock_get_ruleset(ruleset); 289 return ruleset; 290 } 291 292 /* Path handling */ 293 294 /* 295 * @path: Must call put_path(@path) after the call if it succeeded. 296 */ 297 static int get_path_from_fd(const s32 fd, struct path *const path) 298 { 299 CLASS(fd_raw, f)(fd); 300 301 BUILD_BUG_ON(!__same_type( 302 fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd)); 303 304 if (fd_empty(f)) 305 return -EBADF; 306 /* 307 * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including 308 * pseudo filesystems that will never be mountable (e.g. sockfs, 309 * pipefs). 310 */ 311 if ((fd_file(f)->f_op == &ruleset_fops) || 312 (fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) || 313 (fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) || 314 IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry))) 315 return -EBADFD; 316 317 *path = fd_file(f)->f_path; 318 path_get(path); 319 return 0; 320 } 321 322 static int add_rule_path_beneath(struct landlock_ruleset *const ruleset, 323 const void __user *const rule_attr) 324 { 325 struct landlock_path_beneath_attr path_beneath_attr; 326 struct path path; 327 int res, err; 328 access_mask_t mask; 329 330 /* Copies raw user space buffer. */ 331 res = copy_from_user(&path_beneath_attr, rule_attr, 332 sizeof(path_beneath_attr)); 333 if (res) 334 return -EFAULT; 335 336 /* 337 * Informs about useless rule: empty allowed_access (i.e. deny rules) 338 * are ignored in path walks. 339 */ 340 if (!path_beneath_attr.allowed_access) 341 return -ENOMSG; 342 343 /* Checks that allowed_access matches the @ruleset constraints. */ 344 mask = ruleset->access_masks[0].fs; 345 if ((path_beneath_attr.allowed_access | mask) != mask) 346 return -EINVAL; 347 348 /* Gets and checks the new rule. */ 349 err = get_path_from_fd(path_beneath_attr.parent_fd, &path); 350 if (err) 351 return err; 352 353 /* Imports the new rule. */ 354 err = landlock_append_fs_rule(ruleset, &path, 355 path_beneath_attr.allowed_access); 356 path_put(&path); 357 return err; 358 } 359 360 static int add_rule_net_port(struct landlock_ruleset *ruleset, 361 const void __user *const rule_attr) 362 { 363 struct landlock_net_port_attr net_port_attr; 364 int res; 365 access_mask_t mask; 366 367 /* Copies raw user space buffer. */ 368 res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr)); 369 if (res) 370 return -EFAULT; 371 372 /* 373 * Informs about useless rule: empty allowed_access (i.e. deny rules) 374 * are ignored by network actions. 375 */ 376 if (!net_port_attr.allowed_access) 377 return -ENOMSG; 378 379 /* Checks that allowed_access matches the @ruleset constraints. */ 380 mask = landlock_get_net_access_mask(ruleset, 0); 381 if ((net_port_attr.allowed_access | mask) != mask) 382 return -EINVAL; 383 384 /* Denies inserting a rule with port greater than 65535. */ 385 if (net_port_attr.port > U16_MAX) 386 return -EINVAL; 387 388 /* Imports the new rule. */ 389 return landlock_append_net_rule(ruleset, net_port_attr.port, 390 net_port_attr.allowed_access); 391 } 392 393 /** 394 * sys_landlock_add_rule - Add a new rule to a ruleset 395 * 396 * @ruleset_fd: File descriptor tied to the ruleset that should be extended 397 * with the new rule. 398 * @rule_type: Identify the structure type pointed to by @rule_attr: 399 * %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT. 400 * @rule_attr: Pointer to a rule (matching the @rule_type). 401 * @flags: Must be 0. 402 * 403 * This system call enables to define a new rule and add it to an existing 404 * ruleset. 405 * 406 * Return: 0 on success, or -errno on failure. Possible returned errors are: 407 * 408 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; 409 * - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not 410 * supported by the running kernel; 411 * - %EINVAL: @flags is not 0; 412 * - %EINVAL: The rule accesses are inconsistent (i.e. 413 * &landlock_path_beneath_attr.allowed_access or 414 * &landlock_net_port_attr.allowed_access is not a subset of the ruleset 415 * handled accesses) 416 * - %EINVAL: &landlock_net_port_attr.port is greater than 65535; 417 * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is 418 * 0); 419 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a 420 * member of @rule_attr is not a file descriptor as expected; 421 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of 422 * @rule_attr is not the expected file descriptor type; 423 * - %EPERM: @ruleset_fd has no write access to the underlying ruleset; 424 * - %EFAULT: @rule_attr was not a valid address. 425 */ 426 SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd, 427 const enum landlock_rule_type, rule_type, 428 const void __user *const, rule_attr, const __u32, flags) 429 { 430 struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL; 431 432 if (!is_initialized()) 433 return -EOPNOTSUPP; 434 435 /* No flag for now. */ 436 if (flags) 437 return -EINVAL; 438 439 /* Gets and checks the ruleset. */ 440 ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE); 441 if (IS_ERR(ruleset)) 442 return PTR_ERR(ruleset); 443 444 switch (rule_type) { 445 case LANDLOCK_RULE_PATH_BENEATH: 446 return add_rule_path_beneath(ruleset, rule_attr); 447 case LANDLOCK_RULE_NET_PORT: 448 return add_rule_net_port(ruleset, rule_attr); 449 default: 450 return -EINVAL; 451 } 452 } 453 454 /* Enforcement */ 455 456 /** 457 * sys_landlock_restrict_self - Enforce a ruleset on the calling thread 458 * 459 * @ruleset_fd: File descriptor tied to the ruleset to merge with the target. 460 * @flags: Supported values: 461 * 462 * - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF 463 * - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON 464 * - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF 465 * - %LANDLOCK_RESTRICT_SELF_TSYNC 466 * 467 * This system call enforces a Landlock ruleset on the current thread. 468 * Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its 469 * namespace or is running with no_new_privs. This avoids scenarios where 470 * unprivileged tasks can affect the behavior of privileged children. 471 * 472 * Return: 0 on success, or -errno on failure. Possible returned errors are: 473 * 474 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; 475 * - %EINVAL: @flags contains an unknown bit. 476 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread; 477 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor; 478 * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the 479 * current thread is not running with no_new_privs, or it doesn't have 480 * %CAP_SYS_ADMIN in its namespace. 481 * - %E2BIG: The maximum number of stacked rulesets is reached for the current 482 * thread. 483 * 484 * .. kernel-doc:: include/uapi/linux/landlock.h 485 * :identifiers: landlock_restrict_self_flags 486 */ 487 SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, 488 flags) 489 { 490 struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL; 491 struct cred *new_cred; 492 struct landlock_cred_security *new_llcred; 493 bool __maybe_unused log_same_exec, log_new_exec, log_subdomains, 494 prev_log_subdomains; 495 496 if (!is_initialized()) 497 return -EOPNOTSUPP; 498 499 /* 500 * Similar checks as for seccomp(2), except that an -EPERM may be 501 * returned. 502 */ 503 if (!task_no_new_privs(current) && 504 !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN)) 505 return -EPERM; 506 507 if ((flags | LANDLOCK_MASK_RESTRICT_SELF) != 508 LANDLOCK_MASK_RESTRICT_SELF) 509 return -EINVAL; 510 511 /* Translates "off" flag to boolean. */ 512 log_same_exec = !(flags & LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF); 513 /* Translates "on" flag to boolean. */ 514 log_new_exec = !!(flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON); 515 /* Translates "off" flag to boolean. */ 516 log_subdomains = !(flags & LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF); 517 518 /* 519 * It is allowed to set LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF with 520 * -1 as ruleset_fd, optionally combined with 521 * LANDLOCK_RESTRICT_SELF_TSYNC to propagate this configuration to all 522 * threads. No other flag must be set. 523 */ 524 if (!(ruleset_fd == -1 && 525 (flags & ~LANDLOCK_RESTRICT_SELF_TSYNC) == 526 LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF)) { 527 /* Gets and checks the ruleset. */ 528 ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ); 529 if (IS_ERR(ruleset)) 530 return PTR_ERR(ruleset); 531 } 532 533 /* Prepares new credentials. */ 534 new_cred = prepare_creds(); 535 if (!new_cred) 536 return -ENOMEM; 537 538 new_llcred = landlock_cred(new_cred); 539 540 #ifdef CONFIG_AUDIT 541 prev_log_subdomains = !new_llcred->log_subdomains_off; 542 new_llcred->log_subdomains_off = !prev_log_subdomains || 543 !log_subdomains; 544 #endif /* CONFIG_AUDIT */ 545 546 /* 547 * The only case when a ruleset may not be set is if 548 * LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF is set (optionally with 549 * LANDLOCK_RESTRICT_SELF_TSYNC) and ruleset_fd is -1. We could 550 * optimize this case by not calling commit_creds() if this flag was 551 * already set, but it is not worth the complexity. 552 */ 553 if (ruleset) { 554 /* 555 * There is no possible race condition while copying and 556 * manipulating the current credentials because they are 557 * dedicated per thread. 558 */ 559 struct landlock_ruleset *const new_dom = 560 landlock_merge_ruleset(new_llcred->domain, ruleset); 561 if (IS_ERR(new_dom)) { 562 abort_creds(new_cred); 563 return PTR_ERR(new_dom); 564 } 565 566 #ifdef CONFIG_AUDIT 567 new_dom->hierarchy->log_same_exec = log_same_exec; 568 new_dom->hierarchy->log_new_exec = log_new_exec; 569 if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains) 570 new_dom->hierarchy->log_status = LANDLOCK_LOG_DISABLED; 571 #endif /* CONFIG_AUDIT */ 572 573 /* Replaces the old (prepared) domain. */ 574 landlock_put_ruleset(new_llcred->domain); 575 new_llcred->domain = new_dom; 576 577 #ifdef CONFIG_AUDIT 578 new_llcred->domain_exec |= BIT(new_dom->num_layers - 1); 579 #endif /* CONFIG_AUDIT */ 580 } 581 582 if (flags & LANDLOCK_RESTRICT_SELF_TSYNC) { 583 const int err = landlock_restrict_sibling_threads( 584 current_cred(), new_cred); 585 if (err) { 586 abort_creds(new_cred); 587 return err; 588 } 589 } 590 591 return commit_creds(new_cred); 592 } 593