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