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