1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Security-Enhanced Linux (SELinux) security module 4 * 5 * This file contains the SELinux hook function implementations. 6 * 7 * Authors: Stephen Smalley, <stephen.smalley.work@gmail.com> 8 * Chris Vance, <cvance@nai.com> 9 * Wayne Salamon, <wsalamon@nai.com> 10 * James Morris <jmorris@redhat.com> 11 * 12 * Copyright (C) 2001,2002 Networks Associates Technology, Inc. 13 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> 14 * Eric Paris <eparis@redhat.com> 15 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 16 * <dgoeddel@trustedcs.com> 17 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. 18 * Paul Moore <paul@paul-moore.com> 19 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 20 * Yuichi Nakamura <ynakam@hitachisoft.jp> 21 * Copyright (C) 2016 Mellanox Technologies 22 */ 23 24 #include <linux/init.h> 25 #include <linux/kd.h> 26 #include <linux/kernel.h> 27 #include <linux/kernel_read_file.h> 28 #include <linux/errno.h> 29 #include <linux/sched/signal.h> 30 #include <linux/sched/task.h> 31 #include <linux/lsm_hooks.h> 32 #include <linux/xattr.h> 33 #include <linux/capability.h> 34 #include <linux/unistd.h> 35 #include <linux/mm.h> 36 #include <linux/mman.h> 37 #include <linux/slab.h> 38 #include <linux/pagemap.h> 39 #include <linux/proc_fs.h> 40 #include <linux/swap.h> 41 #include <linux/spinlock.h> 42 #include <linux/syscalls.h> 43 #include <linux/dcache.h> 44 #include <linux/file.h> 45 #include <linux/fdtable.h> 46 #include <linux/namei.h> 47 #include <linux/mount.h> 48 #include <linux/fs_context.h> 49 #include <linux/fs_parser.h> 50 #include <linux/netfilter_ipv4.h> 51 #include <linux/netfilter_ipv6.h> 52 #include <linux/tty.h> 53 #include <net/icmp.h> 54 #include <net/ip.h> /* for local_port_range[] */ 55 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 56 #include <net/inet_connection_sock.h> 57 #include <net/net_namespace.h> 58 #include <net/netlabel.h> 59 #include <linux/uaccess.h> 60 #include <asm/ioctls.h> 61 #include <linux/atomic.h> 62 #include <linux/bitops.h> 63 #include <linux/interrupt.h> 64 #include <linux/netdevice.h> /* for network interface checks */ 65 #include <net/netlink.h> 66 #include <linux/tcp.h> 67 #include <linux/udp.h> 68 #include <linux/sctp.h> 69 #include <net/sctp/structs.h> 70 #include <linux/quota.h> 71 #include <linux/un.h> /* for Unix socket types */ 72 #include <net/af_unix.h> /* for Unix socket types */ 73 #include <linux/parser.h> 74 #include <linux/nfs_mount.h> 75 #include <net/ipv6.h> 76 #include <linux/hugetlb.h> 77 #include <linux/personality.h> 78 #include <linux/audit.h> 79 #include <linux/string.h> 80 #include <linux/mutex.h> 81 #include <linux/posix-timers.h> 82 #include <linux/syslog.h> 83 #include <linux/user_namespace.h> 84 #include <linux/export.h> 85 #include <linux/msg.h> 86 #include <linux/shm.h> 87 #include <uapi/linux/shm.h> 88 #include <linux/bpf.h> 89 #include <linux/kernfs.h> 90 #include <linux/stringhash.h> /* for hashlen_string() */ 91 #include <uapi/linux/mount.h> 92 #include <linux/fsnotify.h> 93 #include <linux/fanotify.h> 94 #include <linux/io_uring/cmd.h> 95 #include <uapi/linux/lsm.h> 96 #include <linux/memfd.h> 97 #include <uapi/linux/inet_diag.h> 98 99 #include "initcalls.h" 100 #include "avc.h" 101 #include "objsec.h" 102 #include "netif.h" 103 #include "netnode.h" 104 #include "netport.h" 105 #include "ibpkey.h" 106 #include "xfrm.h" 107 #include "netlabel.h" 108 #include "audit.h" 109 #include "avc_ss.h" 110 #include "ima.h" 111 112 #define SELINUX_INODE_INIT_XATTRS 1 113 114 struct selinux_state selinux_state; 115 116 /* SECMARK reference count */ 117 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 118 119 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 120 static int selinux_enforcing_boot __initdata; 121 122 static int __init enforcing_setup(char *str) 123 { 124 unsigned long enforcing; 125 if (!kstrtoul(str, 0, &enforcing)) 126 selinux_enforcing_boot = enforcing ? 1 : 0; 127 return 1; 128 } 129 __setup("enforcing=", enforcing_setup); 130 #else 131 #define selinux_enforcing_boot 1 132 #endif 133 134 int selinux_enabled_boot __initdata = 1; 135 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 136 static int __init selinux_enabled_setup(char *str) 137 { 138 unsigned long enabled; 139 if (!kstrtoul(str, 0, &enabled)) 140 selinux_enabled_boot = enabled ? 1 : 0; 141 return 1; 142 } 143 __setup("selinux=", selinux_enabled_setup); 144 #endif 145 146 static int __init checkreqprot_setup(char *str) 147 { 148 unsigned long checkreqprot; 149 150 if (!kstrtoul(str, 0, &checkreqprot)) { 151 if (checkreqprot) 152 pr_err("SELinux: checkreqprot set to 1 via kernel parameter. This is no longer supported.\n"); 153 } 154 return 1; 155 } 156 __setup("checkreqprot=", checkreqprot_setup); 157 158 /** 159 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 160 * 161 * Description: 162 * This function checks the SECMARK reference counter to see if any SECMARK 163 * targets are currently configured, if the reference counter is greater than 164 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 165 * enabled, false (0) if SECMARK is disabled. If the always_check_network 166 * policy capability is enabled, SECMARK is always considered enabled. 167 * 168 */ 169 static int selinux_secmark_enabled(void) 170 { 171 return (selinux_policycap_alwaysnetwork() || 172 atomic_read(&selinux_secmark_refcount)); 173 } 174 175 /** 176 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 177 * 178 * Description: 179 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 180 * (1) if any are enabled or false (0) if neither are enabled. If the 181 * always_check_network policy capability is enabled, peer labeling 182 * is always considered enabled. 183 * 184 */ 185 static int selinux_peerlbl_enabled(void) 186 { 187 return (selinux_policycap_alwaysnetwork() || 188 netlbl_enabled() || selinux_xfrm_enabled()); 189 } 190 191 static int selinux_netcache_avc_callback(u32 event) 192 { 193 if (event == AVC_CALLBACK_RESET) { 194 sel_netif_flush(); 195 sel_netnode_flush(); 196 sel_netport_flush(); 197 synchronize_net(); 198 } 199 return 0; 200 } 201 202 static int selinux_lsm_notifier_avc_callback(u32 event) 203 { 204 if (event == AVC_CALLBACK_RESET) { 205 sel_ib_pkey_flush(); 206 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 207 } 208 209 return 0; 210 } 211 212 /* 213 * initialise the security for the init task 214 */ 215 static void cred_init_security(void) 216 { 217 struct cred_security_struct *crsec; 218 219 /* NOTE: the lsm framework zeros out the buffer on allocation */ 220 221 crsec = selinux_cred(unrcu_pointer(current->real_cred)); 222 crsec->osid = crsec->sid = SECINITSID_KERNEL; 223 } 224 225 /* 226 * get the security ID of a set of credentials 227 */ 228 static inline u32 cred_sid(const struct cred *cred) 229 { 230 const struct cred_security_struct *crsec; 231 232 crsec = selinux_cred(cred); 233 return crsec->sid; 234 } 235 236 static void __ad_net_init(struct common_audit_data *ad, 237 struct lsm_network_audit *net, 238 int ifindex, struct sock *sk, u16 family) 239 { 240 ad->type = LSM_AUDIT_DATA_NET; 241 ad->u.net = net; 242 net->netif = ifindex; 243 net->sk = sk; 244 net->family = family; 245 } 246 247 static void ad_net_init_from_sk(struct common_audit_data *ad, 248 struct lsm_network_audit *net, 249 struct sock *sk) 250 { 251 __ad_net_init(ad, net, 0, sk, 0); 252 } 253 254 static void ad_net_init_from_iif(struct common_audit_data *ad, 255 struct lsm_network_audit *net, 256 int ifindex, u16 family) 257 { 258 __ad_net_init(ad, net, ifindex, NULL, family); 259 } 260 261 /* 262 * get the objective security ID of a task 263 */ 264 static inline u32 task_sid_obj(const struct task_struct *task) 265 { 266 u32 sid; 267 268 rcu_read_lock(); 269 sid = cred_sid(__task_cred(task)); 270 rcu_read_unlock(); 271 return sid; 272 } 273 274 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 275 276 /* 277 * Try reloading inode security labels that have been marked as invalid. The 278 * @may_sleep parameter indicates when sleeping and thus reloading labels is 279 * allowed; when set to false, returns -ECHILD when the label is 280 * invalid. The @dentry parameter should be set to a dentry of the inode. 281 */ 282 static int __inode_security_revalidate(struct inode *inode, 283 struct dentry *dentry, 284 bool may_sleep) 285 { 286 if (!selinux_initialized()) 287 return 0; 288 289 if (may_sleep) 290 might_sleep(); 291 else 292 return -ECHILD; 293 294 /* 295 * Check to ensure that an inode's SELinux state is valid and try 296 * reloading the inode security label if necessary. This will fail if 297 * @dentry is NULL and no dentry for this inode can be found; in that 298 * case, continue using the old label. 299 */ 300 inode_doinit_with_dentry(inode, dentry); 301 return 0; 302 } 303 304 static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 305 { 306 return selinux_inode(inode); 307 } 308 309 static inline struct inode_security_struct *inode_security_rcu(struct inode *inode, 310 bool rcu) 311 { 312 int rc; 313 struct inode_security_struct *isec = selinux_inode(inode); 314 315 /* check below is racy, but revalidate will recheck with lock held */ 316 if (data_race(likely(isec->initialized == LABEL_INITIALIZED))) 317 return isec; 318 rc = __inode_security_revalidate(inode, NULL, !rcu); 319 if (rc) 320 return ERR_PTR(rc); 321 return isec; 322 } 323 324 /* 325 * Get the security label of an inode. 326 */ 327 static inline struct inode_security_struct *inode_security(struct inode *inode) 328 { 329 struct inode_security_struct *isec = selinux_inode(inode); 330 331 /* check below is racy, but revalidate will recheck with lock held */ 332 if (data_race(likely(isec->initialized == LABEL_INITIALIZED))) 333 return isec; 334 __inode_security_revalidate(inode, NULL, true); 335 return isec; 336 } 337 338 static inline struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 339 { 340 return selinux_inode(d_backing_inode(dentry)); 341 } 342 343 /* 344 * Get the security label of a dentry's backing inode. 345 */ 346 static inline struct inode_security_struct *backing_inode_security(struct dentry *dentry) 347 { 348 struct inode *inode = d_backing_inode(dentry); 349 struct inode_security_struct *isec = selinux_inode(inode); 350 351 /* check below is racy, but revalidate will recheck with lock held */ 352 if (data_race(likely(isec->initialized == LABEL_INITIALIZED))) 353 return isec; 354 __inode_security_revalidate(inode, dentry, true); 355 return isec; 356 } 357 358 static void inode_free_security(struct inode *inode) 359 { 360 struct inode_security_struct *isec = selinux_inode(inode); 361 struct superblock_security_struct *sbsec; 362 363 if (!isec) 364 return; 365 sbsec = selinux_superblock(inode->i_sb); 366 /* 367 * As not all inode security structures are in a list, we check for 368 * empty list outside of the lock to make sure that we won't waste 369 * time taking a lock doing nothing. 370 * 371 * The list_del_init() function can be safely called more than once. 372 * It should not be possible for this function to be called with 373 * concurrent list_add(), but for better safety against future changes 374 * in the code, we use list_empty_careful() here. 375 */ 376 if (!list_empty_careful(&isec->list)) { 377 spin_lock(&sbsec->isec_lock); 378 list_del_init(&isec->list); 379 spin_unlock(&sbsec->isec_lock); 380 } 381 } 382 383 struct selinux_mnt_opts { 384 u32 fscontext_sid; 385 u32 context_sid; 386 u32 rootcontext_sid; 387 u32 defcontext_sid; 388 }; 389 390 static void selinux_free_mnt_opts(void *mnt_opts) 391 { 392 kfree(mnt_opts); 393 } 394 395 enum { 396 Opt_error = -1, 397 Opt_context = 0, 398 Opt_defcontext = 1, 399 Opt_fscontext = 2, 400 Opt_rootcontext = 3, 401 Opt_seclabel = 4, 402 }; 403 404 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 405 static const struct { 406 const char *name; 407 int len; 408 int opt; 409 bool has_arg; 410 } tokens[] = { 411 A(context, true), 412 A(fscontext, true), 413 A(defcontext, true), 414 A(rootcontext, true), 415 A(seclabel, false), 416 }; 417 #undef A 418 419 static int match_opt_prefix(char *s, int l, char **arg) 420 { 421 unsigned int i; 422 423 for (i = 0; i < ARRAY_SIZE(tokens); i++) { 424 size_t len = tokens[i].len; 425 if (len > l || memcmp(s, tokens[i].name, len)) 426 continue; 427 if (tokens[i].has_arg) { 428 if (len == l || s[len] != '=') 429 continue; 430 *arg = s + len + 1; 431 } else if (len != l) 432 continue; 433 return tokens[i].opt; 434 } 435 return Opt_error; 436 } 437 438 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 439 440 static int may_context_mount_sb_relabel(u32 sid, 441 struct superblock_security_struct *sbsec, 442 const struct cred *cred) 443 { 444 const struct cred_security_struct *crsec = selinux_cred(cred); 445 int rc; 446 447 rc = avc_has_perm(crsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 448 FILESYSTEM__RELABELFROM, NULL); 449 if (rc) 450 return rc; 451 452 rc = avc_has_perm(crsec->sid, sid, SECCLASS_FILESYSTEM, 453 FILESYSTEM__RELABELTO, NULL); 454 return rc; 455 } 456 457 static int may_context_mount_inode_relabel(u32 sid, 458 struct superblock_security_struct *sbsec, 459 const struct cred *cred) 460 { 461 const struct cred_security_struct *crsec = selinux_cred(cred); 462 int rc; 463 rc = avc_has_perm(crsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 464 FILESYSTEM__RELABELFROM, NULL); 465 if (rc) 466 return rc; 467 468 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 469 FILESYSTEM__ASSOCIATE, NULL); 470 return rc; 471 } 472 473 static int selinux_is_genfs_special_handling(struct super_block *sb) 474 { 475 /* Special handling. Genfs but also in-core setxattr handler */ 476 return !strcmp(sb->s_type->name, "sysfs") || 477 !strcmp(sb->s_type->name, "pstore") || 478 !strcmp(sb->s_type->name, "debugfs") || 479 !strcmp(sb->s_type->name, "tracefs") || 480 !strcmp(sb->s_type->name, "rootfs") || 481 (selinux_policycap_cgroupseclabel() && 482 (!strcmp(sb->s_type->name, "cgroup") || 483 !strcmp(sb->s_type->name, "cgroup2"))) || 484 (selinux_policycap_functionfs_seclabel() && 485 !strcmp(sb->s_type->name, "functionfs")); 486 } 487 488 static int selinux_is_sblabel_mnt(struct super_block *sb) 489 { 490 struct superblock_security_struct *sbsec = selinux_superblock(sb); 491 492 /* 493 * IMPORTANT: Double-check logic in this function when adding a new 494 * SECURITY_FS_USE_* definition! 495 */ 496 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7); 497 498 switch (sbsec->behavior) { 499 case SECURITY_FS_USE_XATTR: 500 case SECURITY_FS_USE_TRANS: 501 case SECURITY_FS_USE_TASK: 502 case SECURITY_FS_USE_NATIVE: 503 return 1; 504 505 case SECURITY_FS_USE_GENFS: 506 return selinux_is_genfs_special_handling(sb); 507 508 /* Never allow relabeling on context mounts */ 509 case SECURITY_FS_USE_MNTPOINT: 510 case SECURITY_FS_USE_NONE: 511 default: 512 return 0; 513 } 514 } 515 516 static int sb_check_xattr_support(struct super_block *sb) 517 { 518 struct superblock_security_struct *sbsec = selinux_superblock(sb); 519 struct dentry *root = sb->s_root; 520 struct inode *root_inode = d_backing_inode(root); 521 u32 sid; 522 int rc; 523 524 /* 525 * Make sure that the xattr handler exists and that no 526 * error other than -ENODATA is returned by getxattr on 527 * the root directory. -ENODATA is ok, as this may be 528 * the first boot of the SELinux kernel before we have 529 * assigned xattr values to the filesystem. 530 */ 531 if (!(root_inode->i_opflags & IOP_XATTR)) { 532 pr_warn("SELinux: (dev %s, type %s) has no xattr support\n", 533 sb->s_id, sb->s_type->name); 534 goto fallback; 535 } 536 537 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 538 if (rc < 0 && rc != -ENODATA) { 539 if (rc == -EOPNOTSUPP) { 540 pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n", 541 sb->s_id, sb->s_type->name); 542 goto fallback; 543 } else { 544 pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n", 545 sb->s_id, sb->s_type->name, -rc); 546 return rc; 547 } 548 } 549 return 0; 550 551 fallback: 552 /* No xattr support - try to fallback to genfs if possible. */ 553 rc = security_genfs_sid(sb->s_type->name, "/", 554 SECCLASS_DIR, &sid); 555 if (rc) 556 return -EOPNOTSUPP; 557 558 pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n", 559 sb->s_id, sb->s_type->name); 560 sbsec->behavior = SECURITY_FS_USE_GENFS; 561 sbsec->sid = sid; 562 return 0; 563 } 564 565 static int sb_finish_set_opts(struct super_block *sb) 566 { 567 struct superblock_security_struct *sbsec = selinux_superblock(sb); 568 struct dentry *root = sb->s_root; 569 struct inode *root_inode = d_backing_inode(root); 570 int rc = 0; 571 572 if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 573 rc = sb_check_xattr_support(sb); 574 if (rc) 575 return rc; 576 } 577 578 sbsec->flags |= SE_SBINITIALIZED; 579 580 /* 581 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 582 * leave the flag untouched because sb_clone_mnt_opts might be handing 583 * us a superblock that needs the flag to be cleared. 584 */ 585 if (selinux_is_sblabel_mnt(sb)) 586 sbsec->flags |= SBLABEL_MNT; 587 else 588 sbsec->flags &= ~SBLABEL_MNT; 589 590 /* Initialize the root inode. */ 591 rc = inode_doinit_with_dentry(root_inode, root); 592 593 /* Initialize any other inodes associated with the superblock, e.g. 594 inodes created prior to initial policy load or inodes created 595 during get_sb by a pseudo filesystem that directly 596 populates itself. */ 597 spin_lock(&sbsec->isec_lock); 598 while (!list_empty(&sbsec->isec_head)) { 599 struct inode_security_struct *isec = 600 list_first_entry(&sbsec->isec_head, 601 struct inode_security_struct, list); 602 struct inode *inode = isec->inode; 603 list_del_init(&isec->list); 604 spin_unlock(&sbsec->isec_lock); 605 inode = igrab(inode); 606 if (inode) { 607 if (!IS_PRIVATE(inode)) 608 inode_doinit_with_dentry(inode, NULL); 609 iput(inode); 610 } 611 spin_lock(&sbsec->isec_lock); 612 } 613 spin_unlock(&sbsec->isec_lock); 614 return rc; 615 } 616 617 static int bad_option(struct superblock_security_struct *sbsec, char flag, 618 u32 old_sid, u32 new_sid) 619 { 620 char mnt_flags = sbsec->flags & SE_MNTMASK; 621 622 /* check if the old mount command had the same options */ 623 if (sbsec->flags & SE_SBINITIALIZED) 624 if (!(sbsec->flags & flag) || 625 (old_sid != new_sid)) 626 return 1; 627 628 /* check if we were passed the same options twice, 629 * aka someone passed context=a,context=b 630 */ 631 if (!(sbsec->flags & SE_SBINITIALIZED)) 632 if (mnt_flags & flag) 633 return 1; 634 return 0; 635 } 636 637 /* 638 * Allow filesystems with binary mount data to explicitly set mount point 639 * labeling information. 640 */ 641 static int selinux_set_mnt_opts(struct super_block *sb, 642 void *mnt_opts, 643 unsigned long kern_flags, 644 unsigned long *set_kern_flags) 645 { 646 const struct cred *cred = current_cred(); 647 struct superblock_security_struct *sbsec = selinux_superblock(sb); 648 struct dentry *root = sb->s_root; 649 struct selinux_mnt_opts *opts = mnt_opts; 650 struct inode_security_struct *root_isec; 651 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 652 u32 defcontext_sid = 0; 653 int rc = 0; 654 655 /* 656 * Specifying internal flags without providing a place to 657 * place the results is not allowed 658 */ 659 if (kern_flags && !set_kern_flags) 660 return -EINVAL; 661 662 mutex_lock(&sbsec->lock); 663 664 if (!selinux_initialized()) { 665 if (!opts) { 666 /* Defer initialization until selinux_complete_init, 667 after the initial policy is loaded and the security 668 server is ready to handle calls. */ 669 if (kern_flags & SECURITY_LSM_NATIVE_LABELS) { 670 sbsec->flags |= SE_SBNATIVE; 671 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 672 } 673 goto out; 674 } 675 rc = -EINVAL; 676 pr_warn("SELinux: Unable to set superblock options " 677 "before the security server is initialized\n"); 678 goto out; 679 } 680 681 /* 682 * Binary mount data FS will come through this function twice. Once 683 * from an explicit call and once from the generic calls from the vfs. 684 * Since the generic VFS calls will not contain any security mount data 685 * we need to skip the double mount verification. 686 * 687 * This does open a hole in which we will not notice if the first 688 * mount using this sb set explicit options and a second mount using 689 * this sb does not set any security options. (The first options 690 * will be used for both mounts) 691 */ 692 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 693 && !opts) { 694 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && 695 sbsec->behavior == SECURITY_FS_USE_NATIVE) 696 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 697 goto out; 698 } 699 700 root_isec = backing_inode_security_novalidate(root); 701 702 /* 703 * parse the mount options, check if they are valid sids. 704 * also check if someone is trying to mount the same sb more 705 * than once with different security options. 706 */ 707 if (opts) { 708 if (opts->fscontext_sid) { 709 fscontext_sid = opts->fscontext_sid; 710 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 711 fscontext_sid)) 712 goto out_double_mount; 713 sbsec->flags |= FSCONTEXT_MNT; 714 } 715 if (opts->context_sid) { 716 context_sid = opts->context_sid; 717 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 718 context_sid)) 719 goto out_double_mount; 720 sbsec->flags |= CONTEXT_MNT; 721 } 722 if (opts->rootcontext_sid) { 723 rootcontext_sid = opts->rootcontext_sid; 724 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 725 rootcontext_sid)) 726 goto out_double_mount; 727 sbsec->flags |= ROOTCONTEXT_MNT; 728 } 729 if (opts->defcontext_sid) { 730 defcontext_sid = opts->defcontext_sid; 731 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 732 defcontext_sid)) 733 goto out_double_mount; 734 sbsec->flags |= DEFCONTEXT_MNT; 735 } 736 } 737 738 if (sbsec->flags & SE_SBINITIALIZED) { 739 /* previously mounted with options, but not on this attempt? */ 740 if ((sbsec->flags & SE_MNTMASK) && !opts) 741 goto out_double_mount; 742 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && 743 sbsec->behavior == SECURITY_FS_USE_NATIVE) 744 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 745 rc = 0; 746 goto out; 747 } 748 749 sbsec->creator_sid = current_sid(); 750 751 if (strcmp(sb->s_type->name, "proc") == 0) 752 sbsec->flags |= SE_SBPROC | SE_SBGENFS; 753 754 if (!strcmp(sb->s_type->name, "debugfs") || 755 !strcmp(sb->s_type->name, "tracefs") || 756 !strcmp(sb->s_type->name, "binder") || 757 !strcmp(sb->s_type->name, "bpf") || 758 !strcmp(sb->s_type->name, "pstore") || 759 !strcmp(sb->s_type->name, "securityfs") || 760 (selinux_policycap_functionfs_seclabel() && 761 !strcmp(sb->s_type->name, "functionfs"))) 762 sbsec->flags |= SE_SBGENFS; 763 764 if (!strcmp(sb->s_type->name, "sysfs") || 765 !strcmp(sb->s_type->name, "cgroup") || 766 !strcmp(sb->s_type->name, "cgroup2")) 767 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR; 768 769 if (!sbsec->behavior) { 770 /* 771 * Determine the labeling behavior to use for this 772 * filesystem type. 773 */ 774 rc = security_fs_use(sb); 775 if (rc) { 776 pr_warn("%s: security_fs_use(%s) returned %d\n", 777 __func__, sb->s_type->name, rc); 778 goto out; 779 } 780 } 781 782 /* 783 * If this is a user namespace mount and the filesystem type is not 784 * explicitly whitelisted, then no contexts are allowed on the command 785 * line and security labels must be ignored. 786 */ 787 if (sb->s_user_ns != &init_user_ns && 788 strcmp(sb->s_type->name, "tmpfs") && 789 strcmp(sb->s_type->name, "ramfs") && 790 strcmp(sb->s_type->name, "devpts") && 791 strcmp(sb->s_type->name, "overlay")) { 792 if (context_sid || fscontext_sid || rootcontext_sid || 793 defcontext_sid) { 794 rc = -EACCES; 795 goto out; 796 } 797 if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 798 sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 799 rc = security_transition_sid(current_sid(), 800 current_sid(), 801 SECCLASS_FILE, NULL, 802 &sbsec->mntpoint_sid); 803 if (rc) 804 goto out; 805 } 806 goto out_set_opts; 807 } 808 809 /* sets the context of the superblock for the fs being mounted. */ 810 if (fscontext_sid) { 811 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 812 if (rc) 813 goto out; 814 815 sbsec->sid = fscontext_sid; 816 } 817 818 /* 819 * Switch to using mount point labeling behavior. 820 * sets the label used on all file below the mountpoint, and will set 821 * the superblock context if not already set. 822 */ 823 if (sbsec->flags & SE_SBNATIVE) { 824 /* 825 * This means we are initializing a superblock that has been 826 * mounted before the SELinux was initialized and the 827 * filesystem requested native labeling. We had already 828 * returned SECURITY_LSM_NATIVE_LABELS in *set_kern_flags 829 * in the original mount attempt, so now we just need to set 830 * the SECURITY_FS_USE_NATIVE behavior. 831 */ 832 sbsec->behavior = SECURITY_FS_USE_NATIVE; 833 } else if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 834 sbsec->behavior = SECURITY_FS_USE_NATIVE; 835 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 836 } 837 838 if (context_sid) { 839 if (!fscontext_sid) { 840 rc = may_context_mount_sb_relabel(context_sid, sbsec, 841 cred); 842 if (rc) 843 goto out; 844 sbsec->sid = context_sid; 845 } else { 846 rc = may_context_mount_inode_relabel(context_sid, sbsec, 847 cred); 848 if (rc) 849 goto out; 850 } 851 if (!rootcontext_sid) 852 rootcontext_sid = context_sid; 853 854 sbsec->mntpoint_sid = context_sid; 855 sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 856 } 857 858 if (rootcontext_sid) { 859 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 860 cred); 861 if (rc) 862 goto out; 863 864 root_isec->sid = rootcontext_sid; 865 root_isec->initialized = LABEL_INITIALIZED; 866 } 867 868 if (defcontext_sid) { 869 if (sbsec->behavior != SECURITY_FS_USE_XATTR && 870 sbsec->behavior != SECURITY_FS_USE_NATIVE) { 871 rc = -EINVAL; 872 pr_warn("SELinux: defcontext option is " 873 "invalid for this filesystem type\n"); 874 goto out; 875 } 876 877 if (defcontext_sid != sbsec->def_sid) { 878 rc = may_context_mount_inode_relabel(defcontext_sid, 879 sbsec, cred); 880 if (rc) 881 goto out; 882 } 883 884 sbsec->def_sid = defcontext_sid; 885 } 886 887 out_set_opts: 888 rc = sb_finish_set_opts(sb); 889 out: 890 mutex_unlock(&sbsec->lock); 891 return rc; 892 out_double_mount: 893 rc = -EINVAL; 894 pr_warn("SELinux: mount invalid. Same superblock, different " 895 "security settings for (dev %s, type %s)\n", sb->s_id, 896 sb->s_type->name); 897 goto out; 898 } 899 900 static int selinux_cmp_sb_context(const struct super_block *oldsb, 901 const struct super_block *newsb) 902 { 903 struct superblock_security_struct *old = selinux_superblock(oldsb); 904 struct superblock_security_struct *new = selinux_superblock(newsb); 905 char oldflags = old->flags & SE_MNTMASK; 906 char newflags = new->flags & SE_MNTMASK; 907 908 if (oldflags != newflags) 909 goto mismatch; 910 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 911 goto mismatch; 912 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 913 goto mismatch; 914 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 915 goto mismatch; 916 if (oldflags & ROOTCONTEXT_MNT) { 917 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 918 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 919 if (oldroot->sid != newroot->sid) 920 goto mismatch; 921 } 922 if (old->creator_sid != new->creator_sid) 923 goto mismatch; 924 return 0; 925 mismatch: 926 pr_warn("SELinux: mount invalid. Same superblock, " 927 "different security settings for (dev %s, " 928 "type %s)\n", newsb->s_id, newsb->s_type->name); 929 return -EBUSY; 930 } 931 932 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 933 struct super_block *newsb, 934 unsigned long kern_flags, 935 unsigned long *set_kern_flags) 936 { 937 int rc = 0; 938 const struct superblock_security_struct *oldsbsec = 939 selinux_superblock(oldsb); 940 struct superblock_security_struct *newsbsec = selinux_superblock(newsb); 941 942 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 943 int set_context = (oldsbsec->flags & CONTEXT_MNT); 944 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 945 946 /* 947 * Specifying internal flags without providing a place to 948 * place the results is not allowed. 949 */ 950 if (kern_flags && !set_kern_flags) 951 return -EINVAL; 952 953 mutex_lock(&newsbsec->lock); 954 955 /* 956 * if the parent was able to be mounted it clearly had no special lsm 957 * mount options. thus we can safely deal with this superblock later 958 */ 959 if (!selinux_initialized()) { 960 if (kern_flags & SECURITY_LSM_NATIVE_LABELS) { 961 newsbsec->flags |= SE_SBNATIVE; 962 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 963 } 964 goto out; 965 } 966 967 /* how can we clone if the old one wasn't set up?? */ 968 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 969 970 /* if fs is reusing a sb, make sure that the contexts match */ 971 if (newsbsec->flags & SE_SBINITIALIZED) { 972 mutex_unlock(&newsbsec->lock); 973 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) 974 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 975 return selinux_cmp_sb_context(oldsb, newsb); 976 } 977 978 newsbsec->flags = oldsbsec->flags; 979 980 newsbsec->sid = oldsbsec->sid; 981 newsbsec->def_sid = oldsbsec->def_sid; 982 newsbsec->behavior = oldsbsec->behavior; 983 newsbsec->creator_sid = oldsbsec->creator_sid; 984 985 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 986 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 987 rc = security_fs_use(newsb); 988 if (rc) 989 goto out; 990 } 991 992 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 993 newsbsec->behavior = SECURITY_FS_USE_NATIVE; 994 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 995 } 996 997 if (set_context) { 998 u32 sid = oldsbsec->mntpoint_sid; 999 1000 if (!set_fscontext) 1001 newsbsec->sid = sid; 1002 if (!set_rootcontext) { 1003 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1004 newisec->sid = sid; 1005 } 1006 newsbsec->mntpoint_sid = sid; 1007 } 1008 if (set_rootcontext) { 1009 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 1010 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1011 1012 newisec->sid = oldisec->sid; 1013 } 1014 1015 sb_finish_set_opts(newsb); 1016 out: 1017 mutex_unlock(&newsbsec->lock); 1018 return rc; 1019 } 1020 1021 /* 1022 * NOTE: the caller is responsible for freeing the memory even if on error. 1023 */ 1024 static int selinux_add_opt(int token, const char *s, void **mnt_opts) 1025 { 1026 struct selinux_mnt_opts *opts = *mnt_opts; 1027 u32 *dst_sid; 1028 int rc; 1029 1030 if (token == Opt_seclabel) 1031 /* eaten and completely ignored */ 1032 return 0; 1033 if (!s) 1034 return -EINVAL; 1035 1036 if (!selinux_initialized()) { 1037 pr_warn("SELinux: Unable to set superblock options before the security server is initialized\n"); 1038 return -EINVAL; 1039 } 1040 1041 if (!opts) { 1042 opts = kzalloc_obj(*opts); 1043 if (!opts) 1044 return -ENOMEM; 1045 *mnt_opts = opts; 1046 } 1047 1048 switch (token) { 1049 case Opt_context: 1050 if (opts->context_sid || opts->defcontext_sid) 1051 goto err; 1052 dst_sid = &opts->context_sid; 1053 break; 1054 case Opt_fscontext: 1055 if (opts->fscontext_sid) 1056 goto err; 1057 dst_sid = &opts->fscontext_sid; 1058 break; 1059 case Opt_rootcontext: 1060 if (opts->rootcontext_sid) 1061 goto err; 1062 dst_sid = &opts->rootcontext_sid; 1063 break; 1064 case Opt_defcontext: 1065 if (opts->context_sid || opts->defcontext_sid) 1066 goto err; 1067 dst_sid = &opts->defcontext_sid; 1068 break; 1069 default: 1070 WARN_ON(1); 1071 return -EINVAL; 1072 } 1073 rc = security_context_str_to_sid(s, dst_sid, GFP_KERNEL); 1074 if (rc) 1075 pr_warn("SELinux: security_context_str_to_sid (%s) failed with errno=%d\n", 1076 s, rc); 1077 return rc; 1078 1079 err: 1080 pr_warn(SEL_MOUNT_FAIL_MSG); 1081 return -EINVAL; 1082 } 1083 1084 static int show_sid(struct seq_file *m, u32 sid) 1085 { 1086 char *context = NULL; 1087 u32 len; 1088 int rc; 1089 1090 rc = security_sid_to_context(sid, &context, &len); 1091 if (!rc) { 1092 bool has_comma = strchr(context, ','); 1093 1094 seq_putc(m, '='); 1095 if (has_comma) 1096 seq_putc(m, '\"'); 1097 seq_escape(m, context, "\"\n\\"); 1098 if (has_comma) 1099 seq_putc(m, '\"'); 1100 } 1101 kfree(context); 1102 return rc; 1103 } 1104 1105 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 1106 { 1107 struct superblock_security_struct *sbsec = selinux_superblock(sb); 1108 int rc; 1109 1110 if (!(sbsec->flags & SE_SBINITIALIZED)) 1111 return 0; 1112 1113 if (!selinux_initialized()) 1114 return 0; 1115 1116 if (sbsec->flags & FSCONTEXT_MNT) { 1117 seq_putc(m, ','); 1118 seq_puts(m, FSCONTEXT_STR); 1119 rc = show_sid(m, sbsec->sid); 1120 if (rc) 1121 return rc; 1122 } 1123 if (sbsec->flags & CONTEXT_MNT) { 1124 seq_putc(m, ','); 1125 seq_puts(m, CONTEXT_STR); 1126 rc = show_sid(m, sbsec->mntpoint_sid); 1127 if (rc) 1128 return rc; 1129 } 1130 if (sbsec->flags & DEFCONTEXT_MNT) { 1131 seq_putc(m, ','); 1132 seq_puts(m, DEFCONTEXT_STR); 1133 rc = show_sid(m, sbsec->def_sid); 1134 if (rc) 1135 return rc; 1136 } 1137 if (sbsec->flags & ROOTCONTEXT_MNT) { 1138 struct dentry *root = sb->s_root; 1139 struct inode_security_struct *isec = backing_inode_security(root); 1140 seq_putc(m, ','); 1141 seq_puts(m, ROOTCONTEXT_STR); 1142 rc = show_sid(m, isec->sid); 1143 if (rc) 1144 return rc; 1145 } 1146 if (sbsec->flags & SBLABEL_MNT) { 1147 seq_putc(m, ','); 1148 seq_puts(m, SECLABEL_STR); 1149 } 1150 return 0; 1151 } 1152 1153 static inline u16 inode_mode_to_security_class(umode_t mode) 1154 { 1155 switch (mode & S_IFMT) { 1156 case S_IFSOCK: 1157 return SECCLASS_SOCK_FILE; 1158 case S_IFLNK: 1159 return SECCLASS_LNK_FILE; 1160 case S_IFREG: 1161 return SECCLASS_FILE; 1162 case S_IFBLK: 1163 return SECCLASS_BLK_FILE; 1164 case S_IFDIR: 1165 return SECCLASS_DIR; 1166 case S_IFCHR: 1167 return SECCLASS_CHR_FILE; 1168 case S_IFIFO: 1169 return SECCLASS_FIFO_FILE; 1170 1171 } 1172 1173 return SECCLASS_FILE; 1174 } 1175 1176 static inline int default_protocol_stream(int protocol) 1177 { 1178 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP || 1179 protocol == IPPROTO_MPTCP); 1180 } 1181 1182 static inline int default_protocol_dgram(int protocol) 1183 { 1184 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 1185 } 1186 1187 static inline u16 socket_type_to_security_class(int family, int type, int protocol) 1188 { 1189 bool extsockclass = selinux_policycap_extsockclass(); 1190 1191 switch (family) { 1192 case PF_UNIX: 1193 switch (type) { 1194 case SOCK_STREAM: 1195 case SOCK_SEQPACKET: 1196 return SECCLASS_UNIX_STREAM_SOCKET; 1197 case SOCK_DGRAM: 1198 case SOCK_RAW: 1199 return SECCLASS_UNIX_DGRAM_SOCKET; 1200 } 1201 break; 1202 case PF_INET: 1203 case PF_INET6: 1204 switch (type) { 1205 case SOCK_STREAM: 1206 case SOCK_SEQPACKET: 1207 if (default_protocol_stream(protocol)) 1208 return SECCLASS_TCP_SOCKET; 1209 else if (extsockclass && protocol == IPPROTO_SCTP) 1210 return SECCLASS_SCTP_SOCKET; 1211 else 1212 return SECCLASS_RAWIP_SOCKET; 1213 case SOCK_DGRAM: 1214 if (default_protocol_dgram(protocol)) 1215 return SECCLASS_UDP_SOCKET; 1216 else if (extsockclass && (protocol == IPPROTO_ICMP || 1217 protocol == IPPROTO_ICMPV6)) 1218 return SECCLASS_ICMP_SOCKET; 1219 else 1220 return SECCLASS_RAWIP_SOCKET; 1221 default: 1222 return SECCLASS_RAWIP_SOCKET; 1223 } 1224 break; 1225 case PF_NETLINK: 1226 switch (protocol) { 1227 case NETLINK_ROUTE: 1228 return SECCLASS_NETLINK_ROUTE_SOCKET; 1229 case NETLINK_SOCK_DIAG: 1230 return SECCLASS_NETLINK_TCPDIAG_SOCKET; 1231 case NETLINK_NFLOG: 1232 return SECCLASS_NETLINK_NFLOG_SOCKET; 1233 case NETLINK_XFRM: 1234 return SECCLASS_NETLINK_XFRM_SOCKET; 1235 case NETLINK_SELINUX: 1236 return SECCLASS_NETLINK_SELINUX_SOCKET; 1237 case NETLINK_ISCSI: 1238 return SECCLASS_NETLINK_ISCSI_SOCKET; 1239 case NETLINK_AUDIT: 1240 return SECCLASS_NETLINK_AUDIT_SOCKET; 1241 case NETLINK_FIB_LOOKUP: 1242 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 1243 case NETLINK_CONNECTOR: 1244 return SECCLASS_NETLINK_CONNECTOR_SOCKET; 1245 case NETLINK_NETFILTER: 1246 return SECCLASS_NETLINK_NETFILTER_SOCKET; 1247 case NETLINK_DNRTMSG: 1248 return SECCLASS_NETLINK_DNRT_SOCKET; 1249 case NETLINK_KOBJECT_UEVENT: 1250 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 1251 case NETLINK_GENERIC: 1252 return SECCLASS_NETLINK_GENERIC_SOCKET; 1253 case NETLINK_SCSITRANSPORT: 1254 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 1255 case NETLINK_RDMA: 1256 return SECCLASS_NETLINK_RDMA_SOCKET; 1257 case NETLINK_CRYPTO: 1258 return SECCLASS_NETLINK_CRYPTO_SOCKET; 1259 default: 1260 return SECCLASS_NETLINK_SOCKET; 1261 } 1262 case PF_PACKET: 1263 return SECCLASS_PACKET_SOCKET; 1264 case PF_KEY: 1265 return SECCLASS_KEY_SOCKET; 1266 case PF_APPLETALK: 1267 return SECCLASS_APPLETALK_SOCKET; 1268 } 1269 1270 if (extsockclass) { 1271 switch (family) { 1272 case PF_AX25: 1273 return SECCLASS_AX25_SOCKET; 1274 case PF_IPX: 1275 return SECCLASS_IPX_SOCKET; 1276 case PF_NETROM: 1277 return SECCLASS_NETROM_SOCKET; 1278 case PF_ATMPVC: 1279 return SECCLASS_ATMPVC_SOCKET; 1280 case PF_X25: 1281 return SECCLASS_X25_SOCKET; 1282 case PF_ROSE: 1283 return SECCLASS_ROSE_SOCKET; 1284 case PF_DECnet: 1285 return SECCLASS_DECNET_SOCKET; 1286 case PF_ATMSVC: 1287 return SECCLASS_ATMSVC_SOCKET; 1288 case PF_RDS: 1289 return SECCLASS_RDS_SOCKET; 1290 case PF_IRDA: 1291 return SECCLASS_IRDA_SOCKET; 1292 case PF_PPPOX: 1293 return SECCLASS_PPPOX_SOCKET; 1294 case PF_LLC: 1295 return SECCLASS_LLC_SOCKET; 1296 case PF_CAN: 1297 return SECCLASS_CAN_SOCKET; 1298 case PF_TIPC: 1299 return SECCLASS_TIPC_SOCKET; 1300 case PF_BLUETOOTH: 1301 return SECCLASS_BLUETOOTH_SOCKET; 1302 case PF_IUCV: 1303 return SECCLASS_IUCV_SOCKET; 1304 case PF_RXRPC: 1305 return SECCLASS_RXRPC_SOCKET; 1306 case PF_ISDN: 1307 return SECCLASS_ISDN_SOCKET; 1308 case PF_PHONET: 1309 return SECCLASS_PHONET_SOCKET; 1310 case PF_IEEE802154: 1311 return SECCLASS_IEEE802154_SOCKET; 1312 case PF_CAIF: 1313 return SECCLASS_CAIF_SOCKET; 1314 case PF_ALG: 1315 return SECCLASS_ALG_SOCKET; 1316 case PF_NFC: 1317 return SECCLASS_NFC_SOCKET; 1318 case PF_VSOCK: 1319 return SECCLASS_VSOCK_SOCKET; 1320 case PF_KCM: 1321 return SECCLASS_KCM_SOCKET; 1322 case PF_QIPCRTR: 1323 return SECCLASS_QIPCRTR_SOCKET; 1324 case PF_SMC: 1325 return SECCLASS_SMC_SOCKET; 1326 case PF_XDP: 1327 return SECCLASS_XDP_SOCKET; 1328 case PF_MCTP: 1329 return SECCLASS_MCTP_SOCKET; 1330 #if PF_MAX > 46 1331 #error New address family defined, please update this function. 1332 #endif 1333 } 1334 } 1335 1336 return SECCLASS_SOCKET; 1337 } 1338 1339 static int selinux_genfs_get_sid(struct dentry *dentry, 1340 u16 tclass, 1341 u16 flags, 1342 u32 *sid) 1343 { 1344 int rc; 1345 struct super_block *sb = dentry->d_sb; 1346 char *buffer, *path; 1347 1348 buffer = kmalloc(PATH_MAX, GFP_KERNEL); 1349 if (!buffer) 1350 return -ENOMEM; 1351 1352 path = dentry_path_raw(dentry, buffer, PATH_MAX); 1353 if (IS_ERR(path)) 1354 rc = PTR_ERR(path); 1355 else { 1356 if (flags & SE_SBPROC) { 1357 /* each process gets a /proc/PID/ entry. Strip off the 1358 * PID part to get a valid selinux labeling. 1359 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 1360 while (path[1] >= '0' && path[1] <= '9') { 1361 path[1] = '/'; 1362 path++; 1363 } 1364 } 1365 rc = security_genfs_sid(sb->s_type->name, 1366 path, tclass, sid); 1367 if (rc == -ENOENT) { 1368 /* No match in policy, mark as unlabeled. */ 1369 *sid = SECINITSID_UNLABELED; 1370 rc = 0; 1371 } 1372 } 1373 kfree(buffer); 1374 return rc; 1375 } 1376 1377 static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry, 1378 u32 def_sid, u32 *sid) 1379 { 1380 #define INITCONTEXTLEN 255 1381 char *context; 1382 unsigned int len; 1383 int rc; 1384 1385 len = INITCONTEXTLEN; 1386 context = kmalloc(len + 1, GFP_NOFS); 1387 if (!context) 1388 return -ENOMEM; 1389 1390 context[len] = '\0'; 1391 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 1392 if (rc == -ERANGE) { 1393 kfree(context); 1394 1395 /* Need a larger buffer. Query for the right size. */ 1396 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 1397 if (rc < 0) 1398 return rc; 1399 1400 len = rc; 1401 context = kmalloc(len + 1, GFP_NOFS); 1402 if (!context) 1403 return -ENOMEM; 1404 1405 context[len] = '\0'; 1406 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, 1407 context, len); 1408 } 1409 if (rc < 0) { 1410 kfree(context); 1411 if (rc != -ENODATA) { 1412 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%llu\n", 1413 __func__, -rc, inode->i_sb->s_id, inode->i_ino); 1414 return rc; 1415 } 1416 *sid = def_sid; 1417 return 0; 1418 } 1419 1420 rc = security_context_to_sid_default(context, rc, sid, 1421 def_sid, GFP_NOFS); 1422 if (rc) { 1423 char *dev = inode->i_sb->s_id; 1424 u64 ino = inode->i_ino; 1425 1426 if (rc == -EINVAL) { 1427 pr_notice_ratelimited("SELinux: inode=%llu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n", 1428 ino, dev, context); 1429 } else { 1430 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%llu\n", 1431 __func__, context, -rc, dev, ino); 1432 } 1433 } 1434 kfree(context); 1435 return 0; 1436 } 1437 1438 /* The inode's security attributes must be initialized before first use. */ 1439 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 1440 { 1441 struct superblock_security_struct *sbsec = NULL; 1442 struct inode_security_struct *isec = selinux_inode(inode); 1443 u32 task_sid, sid = 0; 1444 u16 sclass; 1445 struct dentry *dentry; 1446 int rc = 0; 1447 1448 /* check below is racy, but we will recheck with lock held */ 1449 if (data_race(isec->initialized == LABEL_INITIALIZED)) 1450 return 0; 1451 1452 spin_lock(&isec->lock); 1453 if (isec->initialized == LABEL_INITIALIZED) 1454 goto out_unlock; 1455 1456 if (isec->sclass == SECCLASS_FILE) 1457 isec->sclass = inode_mode_to_security_class(inode->i_mode); 1458 1459 sbsec = selinux_superblock(inode->i_sb); 1460 if (!(sbsec->flags & SE_SBINITIALIZED)) { 1461 /* Defer initialization until selinux_complete_init, 1462 after the initial policy is loaded and the security 1463 server is ready to handle calls. */ 1464 spin_lock(&sbsec->isec_lock); 1465 if (list_empty(&isec->list)) 1466 list_add(&isec->list, &sbsec->isec_head); 1467 spin_unlock(&sbsec->isec_lock); 1468 goto out_unlock; 1469 } 1470 1471 sclass = isec->sclass; 1472 task_sid = isec->task_sid; 1473 sid = isec->sid; 1474 isec->initialized = LABEL_PENDING; 1475 spin_unlock(&isec->lock); 1476 1477 switch (sbsec->behavior) { 1478 /* 1479 * In case of SECURITY_FS_USE_NATIVE we need to re-fetch the labels 1480 * via xattr when called from delayed_superblock_init(). 1481 */ 1482 case SECURITY_FS_USE_NATIVE: 1483 case SECURITY_FS_USE_XATTR: 1484 if (!(inode->i_opflags & IOP_XATTR)) { 1485 sid = sbsec->def_sid; 1486 break; 1487 } 1488 /* Need a dentry, since the xattr API requires one. 1489 Life would be simpler if we could just pass the inode. */ 1490 if (opt_dentry) { 1491 /* Called from d_instantiate or d_splice_alias. */ 1492 dentry = dget(opt_dentry); 1493 } else { 1494 /* 1495 * Called from selinux_complete_init, try to find a dentry. 1496 * Some filesystems really want a connected one, so try 1497 * that first. We could split SECURITY_FS_USE_XATTR in 1498 * two, depending upon that... 1499 */ 1500 dentry = d_find_alias(inode); 1501 if (!dentry) 1502 dentry = d_find_any_alias(inode); 1503 } 1504 if (!dentry) { 1505 /* 1506 * this is can be hit on boot when a file is accessed 1507 * before the policy is loaded. When we load policy we 1508 * may find inodes that have no dentry on the 1509 * sbsec->isec_head list. No reason to complain as these 1510 * will get fixed up the next time we go through 1511 * inode_doinit with a dentry, before these inodes could 1512 * be used again by userspace. 1513 */ 1514 goto out_invalid; 1515 } 1516 1517 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid, 1518 &sid); 1519 dput(dentry); 1520 if (rc) 1521 goto out; 1522 break; 1523 case SECURITY_FS_USE_TASK: 1524 sid = task_sid; 1525 break; 1526 case SECURITY_FS_USE_TRANS: 1527 /* Default to the fs SID. */ 1528 sid = sbsec->sid; 1529 1530 /* Try to obtain a transition SID. */ 1531 rc = security_transition_sid(task_sid, sid, 1532 sclass, NULL, &sid); 1533 if (rc) 1534 goto out; 1535 break; 1536 case SECURITY_FS_USE_MNTPOINT: 1537 sid = sbsec->mntpoint_sid; 1538 break; 1539 default: 1540 /* Default to the fs superblock SID. */ 1541 sid = sbsec->sid; 1542 1543 if ((sbsec->flags & SE_SBGENFS) && 1544 (!S_ISLNK(inode->i_mode) || 1545 selinux_policycap_genfs_seclabel_symlinks())) { 1546 /* We must have a dentry to determine the label on 1547 * procfs inodes */ 1548 if (opt_dentry) { 1549 /* Called from d_instantiate or 1550 * d_splice_alias. */ 1551 dentry = dget(opt_dentry); 1552 } else { 1553 /* Called from selinux_complete_init, try to 1554 * find a dentry. Some filesystems really want 1555 * a connected one, so try that first. 1556 */ 1557 dentry = d_find_alias(inode); 1558 if (!dentry) 1559 dentry = d_find_any_alias(inode); 1560 } 1561 /* 1562 * This can be hit on boot when a file is accessed 1563 * before the policy is loaded. When we load policy we 1564 * may find inodes that have no dentry on the 1565 * sbsec->isec_head list. No reason to complain as 1566 * these will get fixed up the next time we go through 1567 * inode_doinit() with a dentry, before these inodes 1568 * could be used again by userspace. 1569 */ 1570 if (!dentry) 1571 goto out_invalid; 1572 rc = selinux_genfs_get_sid(dentry, sclass, 1573 sbsec->flags, &sid); 1574 if (rc) { 1575 dput(dentry); 1576 goto out; 1577 } 1578 1579 if ((sbsec->flags & SE_SBGENFS_XATTR) && 1580 (inode->i_opflags & IOP_XATTR)) { 1581 rc = inode_doinit_use_xattr(inode, dentry, 1582 sid, &sid); 1583 if (rc) { 1584 dput(dentry); 1585 goto out; 1586 } 1587 } 1588 dput(dentry); 1589 } 1590 break; 1591 } 1592 1593 out: 1594 spin_lock(&isec->lock); 1595 if (isec->initialized == LABEL_PENDING) { 1596 if (rc) { 1597 isec->initialized = LABEL_INVALID; 1598 goto out_unlock; 1599 } 1600 isec->initialized = LABEL_INITIALIZED; 1601 isec->sid = sid; 1602 } 1603 1604 out_unlock: 1605 spin_unlock(&isec->lock); 1606 return rc; 1607 1608 out_invalid: 1609 spin_lock(&isec->lock); 1610 if (isec->initialized == LABEL_PENDING) { 1611 isec->initialized = LABEL_INVALID; 1612 isec->sid = sid; 1613 } 1614 spin_unlock(&isec->lock); 1615 return 0; 1616 } 1617 1618 /* Convert a Linux signal to an access vector. */ 1619 static inline u32 signal_to_av(int sig) 1620 { 1621 u32 perm = 0; 1622 1623 switch (sig) { 1624 case SIGCHLD: 1625 /* Commonly granted from child to parent. */ 1626 perm = PROCESS__SIGCHLD; 1627 break; 1628 case SIGKILL: 1629 /* Cannot be caught or ignored */ 1630 perm = PROCESS__SIGKILL; 1631 break; 1632 case SIGSTOP: 1633 /* Cannot be caught or ignored */ 1634 perm = PROCESS__SIGSTOP; 1635 break; 1636 default: 1637 /* All other signals. */ 1638 perm = PROCESS__SIGNAL; 1639 break; 1640 } 1641 1642 return perm; 1643 } 1644 1645 #if CAP_LAST_CAP > 63 1646 #error Fix SELinux to handle capabilities > 63. 1647 #endif 1648 1649 /* Check whether a task is allowed to use a capability. */ 1650 static int cred_has_capability(const struct cred *cred, 1651 int cap, unsigned int opts, bool initns) 1652 { 1653 struct common_audit_data ad; 1654 struct av_decision avd; 1655 u16 sclass; 1656 u32 sid = cred_sid(cred); 1657 u32 av = CAP_TO_MASK(cap); 1658 int rc; 1659 1660 ad.type = LSM_AUDIT_DATA_CAP; 1661 ad.u.cap = cap; 1662 1663 switch (CAP_TO_INDEX(cap)) { 1664 case 0: 1665 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1666 break; 1667 case 1: 1668 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1669 break; 1670 default: 1671 pr_err("SELinux: out of range capability %d\n", cap); 1672 return -EINVAL; 1673 } 1674 1675 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 1676 if (!(opts & CAP_OPT_NOAUDIT)) { 1677 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad); 1678 if (rc2) 1679 return rc2; 1680 } 1681 return rc; 1682 } 1683 1684 /* 1685 * Check whether a SID has a particular permission to an inode. The 'adp' 1686 * parameter is optional and allows other audit data to be passed (e.g. the 1687 * dentry). 1688 */ 1689 static int inode_sid_has_perm(u32 sid, struct inode *inode, u32 perms, 1690 struct common_audit_data *adp) 1691 { 1692 struct inode_security_struct *isec; 1693 1694 if (unlikely(IS_PRIVATE(inode))) 1695 return 0; 1696 1697 isec = selinux_inode(inode); 1698 1699 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 1700 } 1701 1702 static int inode_has_perm(const struct cred *cred, 1703 struct inode *inode, 1704 u32 perms, 1705 struct common_audit_data *adp) 1706 { 1707 return inode_sid_has_perm(cred_sid(cred), inode, perms, adp); 1708 } 1709 1710 /* Same as inode_has_perm, but pass explicit audit data containing 1711 the dentry to help the auditing code to more easily generate the 1712 pathname if needed. */ 1713 static inline int dentry_has_perm(const struct cred *cred, 1714 struct dentry *dentry, 1715 u32 av) 1716 { 1717 struct common_audit_data ad; 1718 struct inode *inode = d_backing_inode(dentry); 1719 struct inode_security_struct *isec = selinux_inode(inode); 1720 1721 ad.type = LSM_AUDIT_DATA_DENTRY; 1722 ad.u.dentry = dentry; 1723 /* check below is racy, but revalidate will recheck with lock held */ 1724 if (data_race(unlikely(isec->initialized != LABEL_INITIALIZED))) 1725 __inode_security_revalidate(inode, dentry, true); 1726 return inode_has_perm(cred, inode, av, &ad); 1727 } 1728 1729 /* Same as inode_has_perm, but pass explicit audit data containing 1730 the path to help the auditing code to more easily generate the 1731 pathname if needed. */ 1732 static inline int path_has_perm(const struct cred *cred, 1733 const struct path *path, 1734 u32 av) 1735 { 1736 struct common_audit_data ad; 1737 struct inode *inode = d_backing_inode(path->dentry); 1738 struct inode_security_struct *isec = selinux_inode(inode); 1739 1740 ad.type = LSM_AUDIT_DATA_PATH; 1741 ad.u.path = *path; 1742 /* check below is racy, but revalidate will recheck with lock held */ 1743 if (data_race(unlikely(isec->initialized != LABEL_INITIALIZED))) 1744 __inode_security_revalidate(inode, path->dentry, true); 1745 return inode_has_perm(cred, inode, av, &ad); 1746 } 1747 1748 /* Same as path_has_perm, but uses the inode from the file struct. */ 1749 static inline int file_path_has_perm(const struct cred *cred, 1750 struct file *file, 1751 u32 av) 1752 { 1753 struct common_audit_data ad; 1754 1755 ad.type = LSM_AUDIT_DATA_FILE; 1756 ad.u.file = file; 1757 return inode_has_perm(cred, file_inode(file), av, &ad); 1758 } 1759 1760 #ifdef CONFIG_BPF_SYSCALL 1761 static int bpf_fd_pass(const struct file *file, u32 sid); 1762 #endif 1763 1764 static int __file_has_perm(const struct cred *cred, const struct file *file, 1765 u32 av, bool bf_user_file) 1766 1767 { 1768 struct common_audit_data ad; 1769 struct inode *inode; 1770 u32 ssid = cred_sid(cred); 1771 u32 tsid_fd; 1772 int rc; 1773 1774 if (bf_user_file) { 1775 struct backing_file_security_struct *bfsec; 1776 const struct path *path; 1777 1778 if (WARN_ON(!(file->f_mode & FMODE_BACKING))) 1779 return -EIO; 1780 1781 bfsec = selinux_backing_file(file); 1782 path = backing_file_user_path(file); 1783 tsid_fd = bfsec->uf_sid; 1784 inode = d_inode(path->dentry); 1785 1786 ad.type = LSM_AUDIT_DATA_PATH; 1787 ad.u.path = *path; 1788 } else { 1789 struct file_security_struct *fsec = selinux_file(file); 1790 1791 tsid_fd = fsec->sid; 1792 inode = file_inode(file); 1793 1794 ad.type = LSM_AUDIT_DATA_FILE; 1795 ad.u.file = file; 1796 } 1797 1798 if (ssid != tsid_fd) { 1799 rc = avc_has_perm(ssid, tsid_fd, SECCLASS_FD, FD__USE, &ad); 1800 if (rc) 1801 return rc; 1802 } 1803 1804 #ifdef CONFIG_BPF_SYSCALL 1805 /* regardless of backing vs user file, use the underlying file here */ 1806 rc = bpf_fd_pass(file, ssid); 1807 if (rc) 1808 return rc; 1809 #endif 1810 1811 /* av is zero if only checking access to the descriptor. */ 1812 if (av) 1813 return inode_has_perm(cred, inode, av, &ad); 1814 1815 return 0; 1816 } 1817 1818 /* Check whether a task can use an open file descriptor to 1819 access an inode in a given way. Check access to the 1820 descriptor itself, and then use dentry_has_perm to 1821 check a particular permission to the file. 1822 Access to the descriptor is implicitly granted if it 1823 has the same SID as the process. If av is zero, then 1824 access to the file is not checked, e.g. for cases 1825 where only the descriptor is affected like seek. */ 1826 static inline int file_has_perm(const struct cred *cred, 1827 const struct file *file, u32 av) 1828 { 1829 return __file_has_perm(cred, file, av, false); 1830 } 1831 1832 /* 1833 * Determine the label for an inode that might be unioned. 1834 */ 1835 static int 1836 selinux_determine_inode_label(const struct cred_security_struct *crsec, 1837 struct inode *dir, 1838 const struct qstr *name, u16 tclass, 1839 u32 *_new_isid) 1840 { 1841 const struct superblock_security_struct *sbsec = 1842 selinux_superblock(dir->i_sb); 1843 1844 if ((sbsec->flags & SE_SBINITIALIZED) && 1845 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1846 *_new_isid = sbsec->mntpoint_sid; 1847 } else if ((sbsec->flags & SBLABEL_MNT) && 1848 crsec->create_sid) { 1849 *_new_isid = crsec->create_sid; 1850 } else { 1851 const struct inode_security_struct *dsec = inode_security(dir); 1852 return security_transition_sid(crsec->sid, 1853 dsec->sid, tclass, 1854 name, _new_isid); 1855 } 1856 1857 return 0; 1858 } 1859 1860 /* Check whether a task can create a file. */ 1861 static int may_create(struct inode *dir, 1862 struct dentry *dentry, 1863 u16 tclass) 1864 { 1865 const struct cred_security_struct *crsec = selinux_cred(current_cred()); 1866 struct inode_security_struct *dsec; 1867 struct superblock_security_struct *sbsec; 1868 u32 sid, newsid; 1869 struct common_audit_data ad; 1870 int rc; 1871 1872 dsec = inode_security(dir); 1873 sbsec = selinux_superblock(dir->i_sb); 1874 1875 sid = crsec->sid; 1876 1877 ad.type = LSM_AUDIT_DATA_DENTRY; 1878 ad.u.dentry = dentry; 1879 1880 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 1881 DIR__ADD_NAME | DIR__SEARCH, 1882 &ad); 1883 if (rc) 1884 return rc; 1885 1886 rc = selinux_determine_inode_label(crsec, dir, &dentry->d_name, tclass, 1887 &newsid); 1888 if (rc) 1889 return rc; 1890 1891 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 1892 if (rc) 1893 return rc; 1894 1895 return avc_has_perm(newsid, sbsec->sid, 1896 SECCLASS_FILESYSTEM, 1897 FILESYSTEM__ASSOCIATE, &ad); 1898 } 1899 1900 #define MAY_LINK 0 1901 #define MAY_UNLINK 1 1902 #define MAY_RMDIR 2 1903 1904 /* Check whether a task can link, unlink, or rmdir a file/directory. */ 1905 static int may_link(struct inode *dir, 1906 struct dentry *dentry, 1907 int kind) 1908 1909 { 1910 struct inode_security_struct *dsec, *isec; 1911 struct common_audit_data ad; 1912 u32 sid = current_sid(); 1913 u32 av; 1914 int rc; 1915 1916 dsec = inode_security(dir); 1917 isec = backing_inode_security(dentry); 1918 1919 ad.type = LSM_AUDIT_DATA_DENTRY; 1920 ad.u.dentry = dentry; 1921 1922 av = DIR__SEARCH; 1923 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1924 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 1925 if (rc) 1926 return rc; 1927 1928 switch (kind) { 1929 case MAY_LINK: 1930 av = FILE__LINK; 1931 break; 1932 case MAY_UNLINK: 1933 av = FILE__UNLINK; 1934 break; 1935 case MAY_RMDIR: 1936 av = DIR__RMDIR; 1937 break; 1938 default: 1939 pr_warn("SELinux: %s: unrecognized kind %d\n", 1940 __func__, kind); 1941 return 0; 1942 } 1943 1944 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 1945 return rc; 1946 } 1947 1948 static inline int may_rename(struct inode *old_dir, 1949 struct dentry *old_dentry, 1950 struct inode *new_dir, 1951 struct dentry *new_dentry) 1952 { 1953 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 1954 struct common_audit_data ad; 1955 u32 sid = current_sid(); 1956 u32 av; 1957 int old_is_dir, new_is_dir; 1958 int rc; 1959 1960 old_dsec = inode_security(old_dir); 1961 old_isec = backing_inode_security(old_dentry); 1962 old_is_dir = d_is_dir(old_dentry); 1963 new_dsec = inode_security(new_dir); 1964 1965 ad.type = LSM_AUDIT_DATA_DENTRY; 1966 1967 ad.u.dentry = old_dentry; 1968 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 1969 DIR__REMOVE_NAME | DIR__SEARCH, &ad); 1970 if (rc) 1971 return rc; 1972 rc = avc_has_perm(sid, old_isec->sid, 1973 old_isec->sclass, FILE__RENAME, &ad); 1974 if (rc) 1975 return rc; 1976 if (old_is_dir && new_dir != old_dir) { 1977 rc = avc_has_perm(sid, old_isec->sid, 1978 old_isec->sclass, DIR__REPARENT, &ad); 1979 if (rc) 1980 return rc; 1981 } 1982 1983 ad.u.dentry = new_dentry; 1984 av = DIR__ADD_NAME | DIR__SEARCH; 1985 if (d_is_positive(new_dentry)) 1986 av |= DIR__REMOVE_NAME; 1987 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 1988 if (rc) 1989 return rc; 1990 if (d_is_positive(new_dentry)) { 1991 new_isec = backing_inode_security(new_dentry); 1992 new_is_dir = d_is_dir(new_dentry); 1993 rc = avc_has_perm(sid, new_isec->sid, 1994 new_isec->sclass, 1995 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 1996 if (rc) 1997 return rc; 1998 } 1999 2000 return 0; 2001 } 2002 2003 /* Check whether a task can perform a filesystem operation. */ 2004 static int superblock_has_perm(const struct cred *cred, 2005 const struct super_block *sb, 2006 u32 perms, 2007 struct common_audit_data *ad) 2008 { 2009 struct superblock_security_struct *sbsec; 2010 u32 sid = cred_sid(cred); 2011 2012 sbsec = selinux_superblock(sb); 2013 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 2014 } 2015 2016 /* Convert a Linux mode and permission mask to an access vector. */ 2017 static inline u32 file_mask_to_av(int mode, int mask) 2018 { 2019 u32 av = 0; 2020 2021 if (!S_ISDIR(mode)) { 2022 if (mask & MAY_EXEC) 2023 av |= FILE__EXECUTE; 2024 if (mask & MAY_READ) 2025 av |= FILE__READ; 2026 2027 if (mask & MAY_APPEND) 2028 av |= FILE__APPEND; 2029 else if (mask & MAY_WRITE) 2030 av |= FILE__WRITE; 2031 2032 } else { 2033 if (mask & MAY_EXEC) 2034 av |= DIR__SEARCH; 2035 if (mask & MAY_WRITE) 2036 av |= DIR__WRITE; 2037 if (mask & MAY_READ) 2038 av |= DIR__READ; 2039 } 2040 2041 return av; 2042 } 2043 2044 /* Convert a Linux file to an access vector. */ 2045 static inline u32 file_to_av(const struct file *file) 2046 { 2047 u32 av = 0; 2048 2049 if (file->f_mode & FMODE_READ) 2050 av |= FILE__READ; 2051 if (file->f_mode & FMODE_WRITE) { 2052 if (file->f_flags & O_APPEND) 2053 av |= FILE__APPEND; 2054 else 2055 av |= FILE__WRITE; 2056 } 2057 if (!av) { 2058 /* 2059 * Special file opened with flags 3 for ioctl-only use. 2060 */ 2061 av = FILE__IOCTL; 2062 } 2063 2064 return av; 2065 } 2066 2067 /* 2068 * Convert a file to an access vector and include the correct 2069 * open permission. 2070 */ 2071 static inline u32 open_file_to_av(struct file *file) 2072 { 2073 u32 av = file_to_av(file); 2074 struct inode *inode = file_inode(file); 2075 2076 if (selinux_policycap_openperm() && 2077 inode->i_sb->s_magic != SOCKFS_MAGIC) 2078 av |= FILE__OPEN; 2079 2080 return av; 2081 } 2082 2083 /* Hook functions begin here. */ 2084 2085 static int selinux_binder_set_context_mgr(const struct cred *mgr) 2086 { 2087 return avc_has_perm(current_sid(), cred_sid(mgr), SECCLASS_BINDER, 2088 BINDER__SET_CONTEXT_MGR, NULL); 2089 } 2090 2091 static int selinux_binder_transaction(const struct cred *from, 2092 const struct cred *to) 2093 { 2094 u32 mysid = current_sid(); 2095 u32 fromsid = cred_sid(from); 2096 u32 tosid = cred_sid(to); 2097 int rc; 2098 2099 if (mysid != fromsid) { 2100 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, 2101 BINDER__IMPERSONATE, NULL); 2102 if (rc) 2103 return rc; 2104 } 2105 2106 return avc_has_perm(fromsid, tosid, 2107 SECCLASS_BINDER, BINDER__CALL, NULL); 2108 } 2109 2110 static int selinux_binder_transfer_binder(const struct cred *from, 2111 const struct cred *to) 2112 { 2113 return avc_has_perm(cred_sid(from), cred_sid(to), 2114 SECCLASS_BINDER, BINDER__TRANSFER, 2115 NULL); 2116 } 2117 2118 static int selinux_binder_transfer_file(const struct cred *from, 2119 const struct cred *to, 2120 const struct file *file) 2121 { 2122 u32 sid = cred_sid(to); 2123 struct file_security_struct *fsec = selinux_file(file); 2124 struct dentry *dentry = file->f_path.dentry; 2125 struct inode_security_struct *isec; 2126 struct common_audit_data ad; 2127 int rc; 2128 2129 ad.type = LSM_AUDIT_DATA_PATH; 2130 ad.u.path = file->f_path; 2131 2132 if (sid != fsec->sid) { 2133 rc = avc_has_perm(sid, fsec->sid, 2134 SECCLASS_FD, 2135 FD__USE, 2136 &ad); 2137 if (rc) 2138 return rc; 2139 } 2140 2141 #ifdef CONFIG_BPF_SYSCALL 2142 rc = bpf_fd_pass(file, sid); 2143 if (rc) 2144 return rc; 2145 #endif 2146 2147 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2148 return 0; 2149 2150 isec = backing_inode_security(dentry); 2151 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file), 2152 &ad); 2153 } 2154 2155 static int selinux_ptrace_access_check(struct task_struct *child, 2156 unsigned int mode) 2157 { 2158 u32 sid = current_sid(); 2159 u32 csid = task_sid_obj(child); 2160 2161 if (mode & PTRACE_MODE_READ) 2162 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, 2163 NULL); 2164 2165 return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, 2166 NULL); 2167 } 2168 2169 static int selinux_ptrace_traceme(struct task_struct *parent) 2170 { 2171 return avc_has_perm(task_sid_obj(parent), task_sid_obj(current), 2172 SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 2173 } 2174 2175 static int selinux_capget(const struct task_struct *target, kernel_cap_t *effective, 2176 kernel_cap_t *inheritable, kernel_cap_t *permitted) 2177 { 2178 return avc_has_perm(current_sid(), task_sid_obj(target), 2179 SECCLASS_PROCESS, PROCESS__GETCAP, NULL); 2180 } 2181 2182 static int selinux_capset(struct cred *new, const struct cred *old, 2183 const kernel_cap_t *effective, 2184 const kernel_cap_t *inheritable, 2185 const kernel_cap_t *permitted) 2186 { 2187 return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2188 PROCESS__SETCAP, NULL); 2189 } 2190 2191 /* 2192 * (This comment used to live with the selinux_task_setuid hook, 2193 * which was removed). 2194 * 2195 * Since setuid only affects the current process, and since the SELinux 2196 * controls are not based on the Linux identity attributes, SELinux does not 2197 * need to control this operation. However, SELinux does control the use of 2198 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 2199 */ 2200 2201 static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 2202 int cap, unsigned int opts) 2203 { 2204 return cred_has_capability(cred, cap, opts, ns == &init_user_ns); 2205 } 2206 2207 static int selinux_quotactl(int cmds, int type, int id, const struct super_block *sb) 2208 { 2209 const struct cred *cred = current_cred(); 2210 int rc = 0; 2211 2212 if (!sb) 2213 return 0; 2214 2215 switch (cmds) { 2216 case Q_SYNC: 2217 case Q_QUOTAON: 2218 case Q_QUOTAOFF: 2219 case Q_SETINFO: 2220 case Q_SETQUOTA: 2221 case Q_XQUOTAOFF: 2222 case Q_XQUOTAON: 2223 case Q_XSETQLIM: 2224 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 2225 break; 2226 case Q_GETFMT: 2227 case Q_GETINFO: 2228 case Q_GETQUOTA: 2229 case Q_XGETQUOTA: 2230 case Q_XGETQSTAT: 2231 case Q_XGETQSTATV: 2232 case Q_XGETNEXTQUOTA: 2233 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 2234 break; 2235 default: 2236 rc = 0; /* let the kernel handle invalid cmds */ 2237 break; 2238 } 2239 return rc; 2240 } 2241 2242 static int selinux_quota_on(struct dentry *dentry) 2243 { 2244 const struct cred *cred = current_cred(); 2245 2246 return dentry_has_perm(cred, dentry, FILE__QUOTAON); 2247 } 2248 2249 static int selinux_syslog(int type) 2250 { 2251 switch (type) { 2252 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2253 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 2254 return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2255 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2256 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2257 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2258 /* Set level of messages printed to console */ 2259 case SYSLOG_ACTION_CONSOLE_LEVEL: 2260 return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2261 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2262 NULL); 2263 } 2264 /* All other syslog types */ 2265 return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2266 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 2267 } 2268 2269 /* 2270 * Check permission for allocating a new virtual mapping. Returns 2271 * 0 if permission is granted, negative error code if not. 2272 * 2273 * Do not audit the selinux permission check, as this is applied to all 2274 * processes that allocate mappings. 2275 */ 2276 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 2277 { 2278 return cred_has_capability(current_cred(), CAP_SYS_ADMIN, 2279 CAP_OPT_NOAUDIT, true); 2280 } 2281 2282 /* binprm security operations */ 2283 2284 static u32 ptrace_parent_sid(void) 2285 { 2286 u32 sid = 0; 2287 struct task_struct *tracer; 2288 2289 rcu_read_lock(); 2290 tracer = ptrace_parent(current); 2291 if (tracer) 2292 sid = task_sid_obj(tracer); 2293 rcu_read_unlock(); 2294 2295 return sid; 2296 } 2297 2298 static int check_nnp_nosuid(const struct linux_binprm *bprm, 2299 const struct cred_security_struct *old_crsec, 2300 const struct cred_security_struct *new_crsec) 2301 { 2302 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2303 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 2304 int rc; 2305 u32 av; 2306 2307 if (!nnp && !nosuid) 2308 return 0; /* neither NNP nor nosuid */ 2309 2310 if (new_crsec->sid == old_crsec->sid) 2311 return 0; /* No change in credentials */ 2312 2313 /* 2314 * If the policy enables the nnp_nosuid_transition policy capability, 2315 * then we permit transitions under NNP or nosuid if the 2316 * policy allows the corresponding permission between 2317 * the old and new contexts. 2318 */ 2319 if (selinux_policycap_nnp_nosuid_transition()) { 2320 av = 0; 2321 if (nnp) 2322 av |= PROCESS2__NNP_TRANSITION; 2323 if (nosuid) 2324 av |= PROCESS2__NOSUID_TRANSITION; 2325 rc = avc_has_perm(old_crsec->sid, new_crsec->sid, 2326 SECCLASS_PROCESS2, av, NULL); 2327 if (!rc) 2328 return 0; 2329 } 2330 2331 /* 2332 * We also permit NNP or nosuid transitions to bounded SIDs, 2333 * i.e. SIDs that are guaranteed to only be allowed a subset 2334 * of the permissions of the current SID. 2335 */ 2336 rc = security_bounded_transition(old_crsec->sid, 2337 new_crsec->sid); 2338 if (!rc) 2339 return 0; 2340 2341 /* 2342 * On failure, preserve the errno values for NNP vs nosuid. 2343 * NNP: Operation not permitted for caller. 2344 * nosuid: Permission denied to file. 2345 */ 2346 if (nnp) 2347 return -EPERM; 2348 return -EACCES; 2349 } 2350 2351 static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm) 2352 { 2353 const struct cred_security_struct *old_crsec; 2354 struct cred_security_struct *new_crsec; 2355 struct inode_security_struct *isec; 2356 struct common_audit_data ad; 2357 struct inode *inode = file_inode(bprm->file); 2358 int rc; 2359 2360 /* SELinux context only depends on initial program or script and not 2361 * the script interpreter */ 2362 2363 old_crsec = selinux_cred(current_cred()); 2364 new_crsec = selinux_cred(bprm->cred); 2365 isec = inode_security(inode); 2366 2367 if (WARN_ON(isec->sclass != SECCLASS_FILE && 2368 isec->sclass != SECCLASS_MEMFD_FILE)) 2369 return -EACCES; 2370 2371 /* Default to the current task SID. */ 2372 new_crsec->sid = old_crsec->sid; 2373 new_crsec->osid = old_crsec->sid; 2374 2375 /* Reset fs, key, and sock SIDs on execve. */ 2376 new_crsec->create_sid = 0; 2377 new_crsec->keycreate_sid = 0; 2378 new_crsec->sockcreate_sid = 0; 2379 2380 /* 2381 * Before policy is loaded, label any task outside kernel space 2382 * as SECINITSID_INIT, so that any userspace tasks surviving from 2383 * early boot end up with a label different from SECINITSID_KERNEL 2384 * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL). 2385 */ 2386 if (!selinux_initialized()) { 2387 new_crsec->sid = SECINITSID_INIT; 2388 /* also clear the exec_sid just in case */ 2389 new_crsec->exec_sid = 0; 2390 return 0; 2391 } 2392 2393 if (old_crsec->exec_sid) { 2394 new_crsec->sid = old_crsec->exec_sid; 2395 /* Reset exec SID on execve. */ 2396 new_crsec->exec_sid = 0; 2397 2398 /* Fail on NNP or nosuid if not an allowed transition. */ 2399 rc = check_nnp_nosuid(bprm, old_crsec, new_crsec); 2400 if (rc) 2401 return rc; 2402 } else { 2403 /* Check for a default transition on this program. */ 2404 rc = security_transition_sid(old_crsec->sid, 2405 isec->sid, SECCLASS_PROCESS, NULL, 2406 &new_crsec->sid); 2407 if (rc) 2408 return rc; 2409 2410 /* 2411 * Fallback to old SID on NNP or nosuid if not an allowed 2412 * transition. 2413 */ 2414 rc = check_nnp_nosuid(bprm, old_crsec, new_crsec); 2415 if (rc) 2416 new_crsec->sid = old_crsec->sid; 2417 } 2418 2419 ad.type = LSM_AUDIT_DATA_FILE; 2420 ad.u.file = bprm->file; 2421 2422 if (new_crsec->sid == old_crsec->sid) { 2423 rc = avc_has_perm(old_crsec->sid, isec->sid, isec->sclass, 2424 FILE__EXECUTE_NO_TRANS, &ad); 2425 if (rc) 2426 return rc; 2427 } else { 2428 /* Check permissions for the transition. */ 2429 rc = avc_has_perm(old_crsec->sid, new_crsec->sid, 2430 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 2431 if (rc) 2432 return rc; 2433 2434 rc = avc_has_perm(new_crsec->sid, isec->sid, isec->sclass, 2435 FILE__ENTRYPOINT, &ad); 2436 if (rc) 2437 return rc; 2438 2439 /* Check for shared state */ 2440 if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2441 rc = avc_has_perm(old_crsec->sid, new_crsec->sid, 2442 SECCLASS_PROCESS, PROCESS__SHARE, 2443 NULL); 2444 if (rc) 2445 return -EPERM; 2446 } 2447 2448 /* Make sure that anyone attempting to ptrace over a task that 2449 * changes its SID has the appropriate permit */ 2450 if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2451 u32 ptsid = ptrace_parent_sid(); 2452 if (ptsid != 0) { 2453 rc = avc_has_perm(ptsid, new_crsec->sid, 2454 SECCLASS_PROCESS, 2455 PROCESS__PTRACE, NULL); 2456 if (rc) 2457 return -EPERM; 2458 } 2459 } 2460 2461 /* Clear any possibly unsafe personality bits on exec: */ 2462 bprm->per_clear |= PER_CLEAR_ON_SETID; 2463 2464 /* Enable secure mode for SIDs transitions unless 2465 the noatsecure permission is granted between 2466 the two SIDs, i.e. ahp returns 0. */ 2467 rc = avc_has_perm(old_crsec->sid, new_crsec->sid, 2468 SECCLASS_PROCESS, PROCESS__NOATSECURE, 2469 NULL); 2470 bprm->secureexec |= !!rc; 2471 } 2472 2473 return 0; 2474 } 2475 2476 static int match_file(const void *p, struct file *file, unsigned fd) 2477 { 2478 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2479 } 2480 2481 /* Derived from fs/exec.c:flush_old_files. */ 2482 static inline void flush_unauthorized_files(const struct cred *cred, 2483 struct files_struct *files) 2484 { 2485 struct file *file, *devnull = NULL; 2486 struct tty_struct *tty; 2487 int drop_tty = 0; 2488 unsigned n; 2489 2490 tty = get_current_tty(); 2491 if (tty) { 2492 spin_lock(&tty->files_lock); 2493 if (!list_empty(&tty->tty_files)) { 2494 struct tty_file_private *file_priv; 2495 2496 /* Revalidate access to controlling tty. 2497 Use file_path_has_perm on the tty path directly 2498 rather than using file_has_perm, as this particular 2499 open file may belong to another process and we are 2500 only interested in the inode-based check here. */ 2501 file_priv = list_first_entry(&tty->tty_files, 2502 struct tty_file_private, list); 2503 file = file_priv->file; 2504 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 2505 drop_tty = 1; 2506 } 2507 spin_unlock(&tty->files_lock); 2508 tty_kref_put(tty); 2509 } 2510 /* Reset controlling tty. */ 2511 if (drop_tty) 2512 no_tty(); 2513 2514 /* Revalidate access to inherited open files. */ 2515 n = iterate_fd(files, 0, match_file, cred); 2516 if (!n) /* none found? */ 2517 return; 2518 2519 devnull = dentry_open(&selinux_null, O_RDWR, cred); 2520 if (IS_ERR(devnull)) 2521 devnull = NULL; 2522 /* replace all the matching ones with this */ 2523 do { 2524 replace_fd(n - 1, devnull, 0); 2525 } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 2526 if (devnull) 2527 fput(devnull); 2528 } 2529 2530 /* 2531 * Prepare a process for imminent new credential changes due to exec 2532 */ 2533 static void selinux_bprm_committing_creds(const struct linux_binprm *bprm) 2534 { 2535 struct cred_security_struct *new_crsec; 2536 struct rlimit *rlim, *initrlim; 2537 int rc, i; 2538 2539 new_crsec = selinux_cred(bprm->cred); 2540 if (new_crsec->sid == new_crsec->osid) 2541 return; 2542 2543 /* Close files for which the new task SID is not authorized. */ 2544 flush_unauthorized_files(bprm->cred, current->files); 2545 2546 /* Always clear parent death signal on SID transitions. */ 2547 current->pdeath_signal = 0; 2548 2549 /* Check whether the new SID can inherit resource limits from the old 2550 * SID. If not, reset all soft limits to the lower of the current 2551 * task's hard limit and the init task's soft limit. 2552 * 2553 * Note that the setting of hard limits (even to lower them) can be 2554 * controlled by the setrlimit check. The inclusion of the init task's 2555 * soft limit into the computation is to avoid resetting soft limits 2556 * higher than the default soft limit for cases where the default is 2557 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2558 */ 2559 rc = avc_has_perm(new_crsec->osid, new_crsec->sid, SECCLASS_PROCESS, 2560 PROCESS__RLIMITINH, NULL); 2561 if (rc) { 2562 /* protect against do_prlimit() */ 2563 task_lock(current); 2564 for (i = 0; i < RLIM_NLIMITS; i++) { 2565 rlim = current->signal->rlim + i; 2566 initrlim = init_task.signal->rlim + i; 2567 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2568 } 2569 task_unlock(current); 2570 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2571 update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2572 } 2573 } 2574 2575 /* 2576 * Clean up the process immediately after the installation of new credentials 2577 * due to exec 2578 */ 2579 static void selinux_bprm_committed_creds(const struct linux_binprm *bprm) 2580 { 2581 const struct cred_security_struct *crsec = selinux_cred(current_cred()); 2582 u32 osid, sid; 2583 int rc; 2584 2585 osid = crsec->osid; 2586 sid = crsec->sid; 2587 2588 if (sid == osid) 2589 return; 2590 2591 /* Check whether the new SID can inherit signal state from the old SID. 2592 * If not, clear itimers to avoid subsequent signal generation and 2593 * flush and unblock signals. 2594 * 2595 * This must occur _after_ the task SID has been updated so that any 2596 * kill done after the flush will be checked against the new SID. 2597 */ 2598 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 2599 if (rc) { 2600 clear_itimer(); 2601 2602 spin_lock_irq(&unrcu_pointer(current->sighand)->siglock); 2603 if (!fatal_signal_pending(current)) { 2604 flush_sigqueue(¤t->pending); 2605 flush_sigqueue(¤t->signal->shared_pending); 2606 flush_signal_handlers(current, 1); 2607 sigemptyset(¤t->blocked); 2608 recalc_sigpending(); 2609 } 2610 spin_unlock_irq(&unrcu_pointer(current->sighand)->siglock); 2611 } 2612 2613 /* Wake up the parent if it is waiting so that it can recheck 2614 * wait permission to the new task SID. */ 2615 read_lock(&tasklist_lock); 2616 __wake_up_parent(current, unrcu_pointer(current->real_parent)); 2617 read_unlock(&tasklist_lock); 2618 } 2619 2620 /* superblock security operations */ 2621 2622 static int selinux_sb_alloc_security(struct super_block *sb) 2623 { 2624 struct superblock_security_struct *sbsec = selinux_superblock(sb); 2625 2626 mutex_init(&sbsec->lock); 2627 INIT_LIST_HEAD(&sbsec->isec_head); 2628 spin_lock_init(&sbsec->isec_lock); 2629 sbsec->sid = SECINITSID_UNLABELED; 2630 sbsec->def_sid = SECINITSID_FILE; 2631 sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2632 sbsec->creator_sid = SECINITSID_UNLABELED; 2633 2634 return 0; 2635 } 2636 2637 static inline int opt_len(const char *s) 2638 { 2639 bool open_quote = false; 2640 int len; 2641 char c; 2642 2643 for (len = 0; (c = s[len]) != '\0'; len++) { 2644 if (c == '"') 2645 open_quote = !open_quote; 2646 if (c == ',' && !open_quote) 2647 break; 2648 } 2649 return len; 2650 } 2651 2652 static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 2653 { 2654 char *from = options; 2655 char *to = options; 2656 bool first = true; 2657 int rc; 2658 2659 while (1) { 2660 int len = opt_len(from); 2661 int token; 2662 char *arg = NULL; 2663 2664 token = match_opt_prefix(from, len, &arg); 2665 2666 if (token != Opt_error) { 2667 char *p, *q; 2668 2669 /* strip quotes */ 2670 if (arg) { 2671 for (p = q = arg; p < from + len; p++) { 2672 char c = *p; 2673 if (c != '"') 2674 *q++ = c; 2675 } 2676 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 2677 if (!arg) { 2678 rc = -ENOMEM; 2679 goto free_opt; 2680 } 2681 } 2682 rc = selinux_add_opt(token, arg, mnt_opts); 2683 kfree(arg); 2684 arg = NULL; 2685 if (unlikely(rc)) { 2686 goto free_opt; 2687 } 2688 } else { 2689 if (!first) { // copy with preceding comma 2690 from--; 2691 len++; 2692 } 2693 if (to != from) 2694 memmove(to, from, len); 2695 to += len; 2696 first = false; 2697 } 2698 if (!from[len]) 2699 break; 2700 from += len + 1; 2701 } 2702 *to = '\0'; 2703 return 0; 2704 2705 free_opt: 2706 if (*mnt_opts) { 2707 selinux_free_mnt_opts(*mnt_opts); 2708 *mnt_opts = NULL; 2709 } 2710 return rc; 2711 } 2712 2713 static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts) 2714 { 2715 struct selinux_mnt_opts *opts = mnt_opts; 2716 struct superblock_security_struct *sbsec = selinux_superblock(sb); 2717 2718 /* 2719 * Superblock not initialized (i.e. no options) - reject if any 2720 * options specified, otherwise accept. 2721 */ 2722 if (!(sbsec->flags & SE_SBINITIALIZED)) 2723 return opts ? 1 : 0; 2724 2725 /* 2726 * Superblock initialized and no options specified - reject if 2727 * superblock has any options set, otherwise accept. 2728 */ 2729 if (!opts) 2730 return (sbsec->flags & SE_MNTMASK) ? 1 : 0; 2731 2732 if (opts->fscontext_sid) { 2733 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 2734 opts->fscontext_sid)) 2735 return 1; 2736 } 2737 if (opts->context_sid) { 2738 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 2739 opts->context_sid)) 2740 return 1; 2741 } 2742 if (opts->rootcontext_sid) { 2743 struct inode_security_struct *root_isec; 2744 2745 root_isec = backing_inode_security(sb->s_root); 2746 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 2747 opts->rootcontext_sid)) 2748 return 1; 2749 } 2750 if (opts->defcontext_sid) { 2751 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 2752 opts->defcontext_sid)) 2753 return 1; 2754 } 2755 return 0; 2756 } 2757 2758 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2759 { 2760 struct selinux_mnt_opts *opts = mnt_opts; 2761 struct superblock_security_struct *sbsec = selinux_superblock(sb); 2762 2763 if (!(sbsec->flags & SE_SBINITIALIZED)) 2764 return 0; 2765 2766 if (!opts) 2767 return 0; 2768 2769 if (opts->fscontext_sid) { 2770 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 2771 opts->fscontext_sid)) 2772 goto out_bad_option; 2773 } 2774 if (opts->context_sid) { 2775 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 2776 opts->context_sid)) 2777 goto out_bad_option; 2778 } 2779 if (opts->rootcontext_sid) { 2780 struct inode_security_struct *root_isec; 2781 root_isec = backing_inode_security(sb->s_root); 2782 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 2783 opts->rootcontext_sid)) 2784 goto out_bad_option; 2785 } 2786 if (opts->defcontext_sid) { 2787 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 2788 opts->defcontext_sid)) 2789 goto out_bad_option; 2790 } 2791 return 0; 2792 2793 out_bad_option: 2794 pr_warn("SELinux: unable to change security options " 2795 "during remount (dev %s, type=%s)\n", sb->s_id, 2796 sb->s_type->name); 2797 return -EINVAL; 2798 } 2799 2800 static int selinux_sb_kern_mount(const struct super_block *sb) 2801 { 2802 const struct cred *cred = current_cred(); 2803 struct common_audit_data ad; 2804 2805 ad.type = LSM_AUDIT_DATA_DENTRY; 2806 ad.u.dentry = sb->s_root; 2807 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 2808 } 2809 2810 static int selinux_sb_statfs(struct dentry *dentry) 2811 { 2812 const struct cred *cred = current_cred(); 2813 struct common_audit_data ad; 2814 2815 ad.type = LSM_AUDIT_DATA_DENTRY; 2816 ad.u.dentry = dentry->d_sb->s_root; 2817 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 2818 } 2819 2820 static int selinux_mount(const char *dev_name, 2821 const struct path *path, 2822 const char *type, 2823 unsigned long flags, 2824 void *data) 2825 { 2826 const struct cred *cred = current_cred(); 2827 2828 if (flags & MS_REMOUNT) 2829 return superblock_has_perm(cred, path->dentry->d_sb, 2830 FILESYSTEM__REMOUNT, NULL); 2831 else 2832 return path_has_perm(cred, path, FILE__MOUNTON); 2833 } 2834 2835 static int selinux_move_mount(const struct path *from_path, 2836 const struct path *to_path) 2837 { 2838 const struct cred *cred = current_cred(); 2839 2840 return path_has_perm(cred, to_path, FILE__MOUNTON); 2841 } 2842 2843 static int selinux_umount(struct vfsmount *mnt, int flags) 2844 { 2845 const struct cred *cred = current_cred(); 2846 2847 return superblock_has_perm(cred, mnt->mnt_sb, 2848 FILESYSTEM__UNMOUNT, NULL); 2849 } 2850 2851 static int selinux_fs_context_submount(struct fs_context *fc, 2852 struct super_block *reference) 2853 { 2854 const struct superblock_security_struct *sbsec = selinux_superblock(reference); 2855 struct selinux_mnt_opts *opts; 2856 2857 /* 2858 * Ensure that fc->security remains NULL when no options are set 2859 * as expected by selinux_set_mnt_opts(). 2860 */ 2861 if (!(sbsec->flags & (FSCONTEXT_MNT|CONTEXT_MNT|DEFCONTEXT_MNT))) 2862 return 0; 2863 2864 opts = kzalloc_obj(*opts); 2865 if (!opts) 2866 return -ENOMEM; 2867 2868 if (sbsec->flags & FSCONTEXT_MNT) 2869 opts->fscontext_sid = sbsec->sid; 2870 if (sbsec->flags & CONTEXT_MNT) 2871 opts->context_sid = sbsec->mntpoint_sid; 2872 if (sbsec->flags & DEFCONTEXT_MNT) 2873 opts->defcontext_sid = sbsec->def_sid; 2874 fc->security = opts; 2875 return 0; 2876 } 2877 2878 static int selinux_fs_context_dup(struct fs_context *fc, 2879 struct fs_context *src_fc) 2880 { 2881 const struct selinux_mnt_opts *src = src_fc->security; 2882 2883 if (!src) 2884 return 0; 2885 2886 fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL); 2887 return fc->security ? 0 : -ENOMEM; 2888 } 2889 2890 static const struct fs_parameter_spec selinux_fs_parameters[] = { 2891 fsparam_string(CONTEXT_STR, Opt_context), 2892 fsparam_string(DEFCONTEXT_STR, Opt_defcontext), 2893 fsparam_string(FSCONTEXT_STR, Opt_fscontext), 2894 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext), 2895 fsparam_flag (SECLABEL_STR, Opt_seclabel), 2896 {} 2897 }; 2898 2899 static int selinux_fs_context_parse_param(struct fs_context *fc, 2900 struct fs_parameter *param) 2901 { 2902 struct fs_parse_result result; 2903 int opt; 2904 2905 opt = fs_parse(fc, selinux_fs_parameters, param, &result); 2906 if (opt < 0) 2907 return opt; 2908 2909 return selinux_add_opt(opt, param->string, &fc->security); 2910 } 2911 2912 /* inode security operations */ 2913 2914 static int selinux_inode_alloc_security(struct inode *inode) 2915 { 2916 struct inode_security_struct *isec = selinux_inode(inode); 2917 u32 sid = current_sid(); 2918 2919 spin_lock_init(&isec->lock); 2920 INIT_LIST_HEAD(&isec->list); 2921 isec->inode = inode; 2922 isec->sid = SECINITSID_UNLABELED; 2923 isec->sclass = SECCLASS_FILE; 2924 isec->task_sid = sid; 2925 isec->initialized = LABEL_INVALID; 2926 2927 return 0; 2928 } 2929 2930 static void selinux_inode_free_security(struct inode *inode) 2931 { 2932 inode_free_security(inode); 2933 } 2934 2935 static int selinux_dentry_init_security(struct dentry *dentry, int mode, 2936 const struct qstr *name, 2937 const char **xattr_name, 2938 struct lsm_context *cp) 2939 { 2940 u32 newsid; 2941 int rc; 2942 2943 rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2944 d_inode(dentry->d_parent), name, 2945 inode_mode_to_security_class(mode), 2946 &newsid); 2947 if (rc) 2948 return rc; 2949 2950 if (xattr_name) 2951 *xattr_name = XATTR_NAME_SELINUX; 2952 2953 cp->id = LSM_ID_SELINUX; 2954 return security_sid_to_context(newsid, &cp->context, &cp->len); 2955 } 2956 2957 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2958 const struct qstr *name, 2959 const struct cred *old, 2960 struct cred *new) 2961 { 2962 u32 newsid; 2963 int rc; 2964 struct cred_security_struct *crsec; 2965 2966 rc = selinux_determine_inode_label(selinux_cred(old), 2967 d_inode(dentry->d_parent), name, 2968 inode_mode_to_security_class(mode), 2969 &newsid); 2970 if (rc) 2971 return rc; 2972 2973 crsec = selinux_cred(new); 2974 crsec->create_sid = newsid; 2975 return 0; 2976 } 2977 2978 static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 2979 const struct qstr *qstr, 2980 struct xattr *xattrs, int *xattr_count) 2981 { 2982 const struct cred_security_struct *crsec = selinux_cred(current_cred()); 2983 struct superblock_security_struct *sbsec; 2984 struct xattr *xattr; 2985 u32 newsid, clen; 2986 u16 newsclass; 2987 int rc; 2988 char *context; 2989 2990 sbsec = selinux_superblock(dir->i_sb); 2991 2992 if (!selinux_initialized() || 2993 !(sbsec->flags & SBLABEL_MNT)) 2994 return -EOPNOTSUPP; 2995 2996 newsid = crsec->create_sid; 2997 newsclass = inode_mode_to_security_class(inode->i_mode); 2998 rc = selinux_determine_inode_label(crsec, dir, qstr, newsclass, &newsid); 2999 if (rc) 3000 return rc; 3001 3002 /* Possibly defer initialization to selinux_complete_init. */ 3003 if (sbsec->flags & SE_SBINITIALIZED) { 3004 struct inode_security_struct *isec = selinux_inode(inode); 3005 isec->sclass = newsclass; 3006 isec->sid = newsid; 3007 isec->initialized = LABEL_INITIALIZED; 3008 } 3009 3010 xattr = lsm_get_xattr_slot(xattrs, xattr_count); 3011 if (xattr) { 3012 rc = security_sid_to_context_force(newsid, 3013 &context, &clen); 3014 if (rc) 3015 return rc; 3016 xattr->value = context; 3017 xattr->value_len = clen; 3018 xattr->name = XATTR_SELINUX_SUFFIX; 3019 } 3020 3021 return 0; 3022 } 3023 3024 static int selinux_inode_init_security_anon(struct inode *inode, 3025 const struct qstr *name, 3026 const struct inode *context_inode) 3027 { 3028 u32 sid = current_sid(); 3029 struct common_audit_data ad; 3030 struct inode_security_struct *isec; 3031 int rc; 3032 bool is_memfd = false; 3033 3034 if (unlikely(!selinux_initialized())) 3035 return 0; 3036 3037 if (name != NULL && name->name != NULL && 3038 !strcmp(name->name, MEMFD_ANON_NAME)) { 3039 if (!selinux_policycap_memfd_class()) 3040 return 0; 3041 is_memfd = true; 3042 } 3043 3044 isec = selinux_inode(inode); 3045 3046 /* 3047 * We only get here once per ephemeral inode. The inode has 3048 * been initialized via inode_alloc_security but is otherwise 3049 * untouched. 3050 */ 3051 3052 if (context_inode) { 3053 struct inode_security_struct *context_isec = 3054 selinux_inode(context_inode); 3055 if (context_isec->initialized != LABEL_INITIALIZED) { 3056 pr_err("SELinux: context_inode is not initialized\n"); 3057 return -EACCES; 3058 } 3059 3060 isec->sclass = context_isec->sclass; 3061 isec->sid = context_isec->sid; 3062 } else { 3063 if (is_memfd) 3064 isec->sclass = SECCLASS_MEMFD_FILE; 3065 else 3066 isec->sclass = SECCLASS_ANON_INODE; 3067 rc = security_transition_sid( 3068 sid, sid, 3069 isec->sclass, name, &isec->sid); 3070 if (rc) 3071 return rc; 3072 } 3073 3074 isec->initialized = LABEL_INITIALIZED; 3075 /* 3076 * Now that we've initialized security, check whether we're 3077 * allowed to actually create this type of anonymous inode. 3078 */ 3079 3080 ad.type = LSM_AUDIT_DATA_ANONINODE; 3081 ad.u.anonclass = name ? (const char *)name->name : "?"; 3082 3083 return avc_has_perm(sid, 3084 isec->sid, 3085 isec->sclass, 3086 FILE__CREATE, 3087 &ad); 3088 } 3089 3090 static int selinux_inode_create(const struct mnt_idmap *idmap, struct inode *dir, 3091 struct dentry *dentry, umode_t mode) 3092 { 3093 return may_create(dir, dentry, SECCLASS_FILE); 3094 } 3095 3096 static int selinux_inode_link(const struct mnt_idmap *idmap, struct dentry *old_dentry, 3097 struct inode *dir, struct dentry *new_dentry) 3098 { 3099 return may_link(dir, old_dentry, MAY_LINK); 3100 } 3101 3102 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 3103 { 3104 return may_link(dir, dentry, MAY_UNLINK); 3105 } 3106 3107 static int selinux_inode_symlink(const struct mnt_idmap *idmap, struct inode *dir, 3108 struct dentry *dentry, const char *name) 3109 { 3110 return may_create(dir, dentry, SECCLASS_LNK_FILE); 3111 } 3112 3113 static int selinux_inode_mkdir(const struct mnt_idmap *idmap, struct inode *dir, 3114 struct dentry *dentry, umode_t mask) 3115 { 3116 return may_create(dir, dentry, SECCLASS_DIR); 3117 } 3118 3119 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 3120 { 3121 return may_link(dir, dentry, MAY_RMDIR); 3122 } 3123 3124 static int selinux_inode_mknod(const struct mnt_idmap *idmap, struct inode *dir, 3125 struct dentry *dentry, umode_t mode, dev_t dev) 3126 { 3127 return may_create(dir, dentry, inode_mode_to_security_class(mode)); 3128 } 3129 3130 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 3131 struct inode *new_inode, struct dentry *new_dentry) 3132 { 3133 return may_rename(old_inode, old_dentry, new_inode, new_dentry); 3134 } 3135 3136 static int selinux_inode_readlink(struct dentry *dentry) 3137 { 3138 const struct cred *cred = current_cred(); 3139 3140 return dentry_has_perm(cred, dentry, FILE__READ); 3141 } 3142 3143 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 3144 bool rcu) 3145 { 3146 struct common_audit_data ad; 3147 struct inode_security_struct *isec; 3148 u32 sid = current_sid(); 3149 3150 ad.type = LSM_AUDIT_DATA_DENTRY; 3151 ad.u.dentry = dentry; 3152 isec = inode_security_rcu(inode, rcu); 3153 if (IS_ERR(isec)) 3154 return PTR_ERR(isec); 3155 3156 return avc_has_perm(sid, isec->sid, isec->sclass, FILE__READ, &ad); 3157 } 3158 3159 static noinline int audit_inode_permission(struct inode *inode, 3160 u32 perms, u32 audited, u32 denied, 3161 int result) 3162 { 3163 struct common_audit_data ad; 3164 struct inode_security_struct *isec = selinux_inode(inode); 3165 3166 ad.type = LSM_AUDIT_DATA_INODE; 3167 ad.u.inode = inode; 3168 3169 return slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms, 3170 audited, denied, result, &ad); 3171 } 3172 3173 /** 3174 * task_avdcache_reset - Reset the task's AVD cache 3175 * @tsec: the task's security state 3176 * 3177 * Clear the task's AVD cache in @tsec and reset it to the current policy's 3178 * and task's info. 3179 */ 3180 static inline void task_avdcache_reset(struct task_security_struct *tsec) 3181 { 3182 memset(&tsec->avdcache.dir, 0, sizeof(tsec->avdcache.dir)); 3183 tsec->avdcache.sid = current_sid(); 3184 tsec->avdcache.seqno = avc_policy_seqno(); 3185 tsec->avdcache.dir_spot = TSEC_AVDC_DIR_SIZE - 1; 3186 } 3187 3188 /** 3189 * task_avdcache_search - Search the task's AVD cache 3190 * @tsec: the task's security state 3191 * @isec: the inode to search for in the cache 3192 * @avdc: matching avd cache entry returned to the caller 3193 * 3194 * Search @tsec for a AVD cache entry that matches @isec and return it to the 3195 * caller via @avdc. Returns 0 if a match is found, negative values otherwise. 3196 */ 3197 static inline int task_avdcache_search(struct task_security_struct *tsec, 3198 struct inode_security_struct *isec, 3199 struct avdc_entry **avdc) 3200 { 3201 int orig, iter; 3202 3203 /* focused on path walk optimization, only cache directories */ 3204 if (isec->sclass != SECCLASS_DIR) 3205 return -ENOENT; 3206 3207 if (unlikely(current_sid() != tsec->avdcache.sid || 3208 tsec->avdcache.seqno != avc_policy_seqno())) { 3209 task_avdcache_reset(tsec); 3210 return -ENOENT; 3211 } 3212 3213 orig = iter = tsec->avdcache.dir_spot; 3214 do { 3215 if (tsec->avdcache.dir[iter].isid == isec->sid) { 3216 /* cache hit */ 3217 tsec->avdcache.dir_spot = iter; 3218 *avdc = &tsec->avdcache.dir[iter]; 3219 return 0; 3220 } 3221 iter = (iter - 1) & (TSEC_AVDC_DIR_SIZE - 1); 3222 } while (iter != orig); 3223 3224 return -ENOENT; 3225 } 3226 3227 /** 3228 * task_avdcache_update - Update the task's AVD cache 3229 * @tsec: the task's security state 3230 * @isec: the inode associated with the cache entry 3231 * @avd: the AVD to cache 3232 * 3233 * Update the AVD cache in @tsec with the @avd info associated 3234 * with @isec. 3235 */ 3236 static inline void task_avdcache_update(struct task_security_struct *tsec, 3237 struct inode_security_struct *isec, 3238 struct av_decision *avd) 3239 { 3240 int spot; 3241 3242 /* focused on path walk optimization, only cache directories */ 3243 if (isec->sclass != SECCLASS_DIR) 3244 return; 3245 3246 /* update cache */ 3247 spot = (tsec->avdcache.dir_spot + 1) & (TSEC_AVDC_DIR_SIZE - 1); 3248 tsec->avdcache.dir_spot = spot; 3249 tsec->avdcache.dir[spot].isid = isec->sid; 3250 tsec->avdcache.dir[spot].avd = *avd; 3251 tsec->avdcache.permissive_neveraudit = 3252 (avd->flags == (AVD_FLAGS_PERMISSIVE|AVD_FLAGS_NEVERAUDIT)); 3253 } 3254 3255 /** 3256 * selinux_inode_permission - Check if the current task can access an inode 3257 * @idmap: idmap of the mount 3258 * @inode: the inode that is being accessed 3259 * @requested: the accesses being requested 3260 * 3261 * Check if the current task is allowed to access @inode according to 3262 * @requested. Returns 0 if allowed, negative values otherwise. 3263 */ 3264 static int selinux_inode_permission(const struct mnt_idmap *idmap, 3265 struct inode *inode, int requested) 3266 { 3267 int mask; 3268 u32 perms; 3269 u32 sid = current_sid(); 3270 struct task_security_struct *tsec; 3271 struct inode_security_struct *isec; 3272 struct avdc_entry *avdc; 3273 struct av_decision avd, *avdp = &avd; 3274 int rc, rc2; 3275 u32 audited, denied; 3276 3277 mask = requested & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3278 3279 /* No permission to check. Existence test. */ 3280 if (!mask) 3281 return 0; 3282 3283 tsec = selinux_task(current); 3284 if (task_avdcache_permnoaudit(tsec, sid)) 3285 return 0; 3286 3287 isec = inode_security_rcu(inode, requested & MAY_NOT_BLOCK); 3288 if (IS_ERR(isec)) 3289 return PTR_ERR(isec); 3290 perms = file_mask_to_av(inode->i_mode, mask); 3291 3292 rc = task_avdcache_search(tsec, isec, &avdc); 3293 if (likely(!rc)) { 3294 /* Cache hit. */ 3295 avdp = &avdc->avd; 3296 denied = perms & ~avdp->allowed; 3297 if (unlikely(denied) && enforcing_enabled() && 3298 !(avdp->flags & AVD_FLAGS_PERMISSIVE)) 3299 rc = -EACCES; 3300 } else { 3301 /* Cache miss. */ 3302 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, 3303 perms, 0, avdp); 3304 task_avdcache_update(tsec, isec, avdp); 3305 } 3306 3307 audited = avc_audit_required(perms, avdp, rc, 3308 (requested & MAY_ACCESS) ? 3309 FILE__AUDIT_ACCESS : 0, &denied); 3310 if (likely(!audited)) 3311 return rc; 3312 3313 rc2 = audit_inode_permission(inode, perms, audited, denied, rc); 3314 if (rc2) 3315 return rc2; 3316 3317 return rc; 3318 } 3319 3320 static int selinux_inode_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 3321 struct iattr *iattr) 3322 { 3323 const struct cred *cred = current_cred(); 3324 struct inode *inode = d_backing_inode(dentry); 3325 unsigned int ia_valid = iattr->ia_valid; 3326 u32 av = FILE__WRITE; 3327 3328 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3329 if (ia_valid & ATTR_FORCE) { 3330 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3331 ATTR_FORCE); 3332 if (!ia_valid) 3333 return 0; 3334 } 3335 3336 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3337 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 3338 return dentry_has_perm(cred, dentry, FILE__SETATTR); 3339 3340 if (selinux_policycap_openperm() && 3341 inode->i_sb->s_magic != SOCKFS_MAGIC && 3342 (ia_valid & ATTR_SIZE) && 3343 !(ia_valid & ATTR_FILE)) 3344 av |= FILE__OPEN; 3345 3346 return dentry_has_perm(cred, dentry, av); 3347 } 3348 3349 static int selinux_inode_getattr(const struct path *path) 3350 { 3351 struct task_security_struct *tsec; 3352 3353 tsec = selinux_task(current); 3354 3355 if (task_avdcache_permnoaudit(tsec, current_sid())) 3356 return 0; 3357 3358 return path_has_perm(current_cred(), path, FILE__GETATTR); 3359 } 3360 3361 static bool has_cap_mac_admin(bool audit) 3362 { 3363 const struct cred *cred = current_cred(); 3364 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT; 3365 3366 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts)) 3367 return false; 3368 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true)) 3369 return false; 3370 return true; 3371 } 3372 3373 /** 3374 * selinux_inode_xattr_skipcap - Skip the xattr capability checks? 3375 * @name: name of the xattr 3376 * 3377 * Returns 1 to indicate that SELinux "owns" the access control rights to xattrs 3378 * named @name; the LSM layer should avoid enforcing any traditional 3379 * capability based access controls on this xattr. Returns 0 to indicate that 3380 * SELinux does not "own" the access control rights to xattrs named @name and is 3381 * deferring to the LSM layer for further access controls, including capability 3382 * based controls. 3383 */ 3384 static int selinux_inode_xattr_skipcap(const char *name) 3385 { 3386 /* require capability check if not a selinux xattr */ 3387 return !strcmp(name, XATTR_NAME_SELINUX); 3388 } 3389 3390 static int selinux_inode_setxattr(const struct mnt_idmap *idmap, 3391 struct dentry *dentry, const char *name, 3392 const void *value, size_t size, int flags) 3393 { 3394 struct inode *inode = d_backing_inode(dentry); 3395 struct inode_security_struct *isec; 3396 struct superblock_security_struct *sbsec; 3397 struct common_audit_data ad; 3398 u32 newsid, sid = current_sid(); 3399 int rc = 0; 3400 3401 /* if not a selinux xattr, only check the ordinary setattr perm */ 3402 if (strcmp(name, XATTR_NAME_SELINUX)) 3403 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 3404 3405 if (!selinux_initialized()) 3406 return (inode_owner_or_capable(idmap, inode) ? 0 : -EPERM); 3407 3408 sbsec = selinux_superblock(inode->i_sb); 3409 if (!(sbsec->flags & SBLABEL_MNT)) 3410 return -EOPNOTSUPP; 3411 3412 if (!inode_owner_or_capable(idmap, inode)) 3413 return -EPERM; 3414 3415 ad.type = LSM_AUDIT_DATA_DENTRY; 3416 ad.u.dentry = dentry; 3417 3418 isec = backing_inode_security(dentry); 3419 rc = avc_has_perm(sid, isec->sid, isec->sclass, 3420 FILE__RELABELFROM, &ad); 3421 if (rc) 3422 return rc; 3423 3424 rc = security_context_to_sid(value, size, &newsid, 3425 GFP_KERNEL); 3426 if (rc == -EINVAL) { 3427 if (!has_cap_mac_admin(true)) { 3428 struct audit_buffer *ab; 3429 size_t audit_size; 3430 3431 /* We strip a nul only if it is at the end, otherwise the 3432 * context contains a nul and we should audit that */ 3433 if (value) { 3434 const char *str = value; 3435 3436 if (str[size - 1] == '\0') 3437 audit_size = size - 1; 3438 else 3439 audit_size = size; 3440 } else { 3441 audit_size = 0; 3442 } 3443 ab = audit_log_start(audit_context(), 3444 GFP_ATOMIC, AUDIT_SELINUX_ERR); 3445 if (!ab) 3446 return rc; 3447 audit_log_format(ab, "op=setxattr invalid_context="); 3448 audit_log_n_untrustedstring(ab, value, audit_size); 3449 audit_log_end(ab); 3450 3451 return rc; 3452 } 3453 rc = security_context_to_sid_force(value, 3454 size, &newsid); 3455 } 3456 if (rc) 3457 return rc; 3458 3459 rc = avc_has_perm(sid, newsid, isec->sclass, 3460 FILE__RELABELTO, &ad); 3461 if (rc) 3462 return rc; 3463 3464 rc = security_validate_transition(isec->sid, newsid, 3465 sid, isec->sclass); 3466 if (rc) 3467 return rc; 3468 3469 return avc_has_perm(newsid, 3470 sbsec->sid, 3471 SECCLASS_FILESYSTEM, 3472 FILESYSTEM__ASSOCIATE, 3473 &ad); 3474 } 3475 3476 static int selinux_inode_set_acl(const struct mnt_idmap *idmap, 3477 struct dentry *dentry, const char *acl_name, 3478 struct posix_acl *kacl) 3479 { 3480 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 3481 } 3482 3483 static int selinux_inode_get_acl(const struct mnt_idmap *idmap, 3484 struct dentry *dentry, const char *acl_name) 3485 { 3486 return dentry_has_perm(current_cred(), dentry, FILE__GETATTR); 3487 } 3488 3489 static int selinux_inode_remove_acl(const struct mnt_idmap *idmap, 3490 struct dentry *dentry, const char *acl_name) 3491 { 3492 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 3493 } 3494 3495 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 3496 const void *value, size_t size, 3497 int flags) 3498 { 3499 struct inode *inode = d_backing_inode(dentry); 3500 struct inode_security_struct *isec; 3501 u32 newsid; 3502 int rc; 3503 3504 if (strcmp(name, XATTR_NAME_SELINUX)) { 3505 /* Not an attribute we recognize, so nothing to do. */ 3506 return; 3507 } 3508 3509 if (!selinux_initialized()) { 3510 /* If we haven't even been initialized, then we can't validate 3511 * against a policy, so leave the label as invalid. It may 3512 * resolve to a valid label on the next revalidation try if 3513 * we've since initialized. 3514 */ 3515 return; 3516 } 3517 3518 rc = security_context_to_sid_force(value, size, 3519 &newsid); 3520 if (rc) { 3521 pr_err("SELinux: unable to map context to SID" 3522 "for (%s, %llu), rc=%d\n", 3523 inode->i_sb->s_id, inode->i_ino, -rc); 3524 return; 3525 } 3526 3527 isec = backing_inode_security(dentry); 3528 spin_lock(&isec->lock); 3529 isec->sclass = inode_mode_to_security_class(inode->i_mode); 3530 isec->sid = newsid; 3531 isec->initialized = LABEL_INITIALIZED; 3532 spin_unlock(&isec->lock); 3533 } 3534 3535 static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 3536 { 3537 const struct cred *cred = current_cred(); 3538 3539 return dentry_has_perm(cred, dentry, FILE__GETATTR); 3540 } 3541 3542 static int selinux_inode_listxattr(struct dentry *dentry) 3543 { 3544 const struct cred *cred = current_cred(); 3545 3546 return dentry_has_perm(cred, dentry, FILE__GETATTR); 3547 } 3548 3549 static int selinux_inode_removexattr(const struct mnt_idmap *idmap, 3550 struct dentry *dentry, const char *name) 3551 { 3552 /* if not a selinux xattr, only check the ordinary setattr perm */ 3553 if (strcmp(name, XATTR_NAME_SELINUX)) 3554 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 3555 3556 if (!selinux_initialized()) 3557 return 0; 3558 3559 /* No one is allowed to remove a SELinux security label. 3560 You can change the label, but all data must be labeled. */ 3561 return -EACCES; 3562 } 3563 3564 static int selinux_inode_file_setattr(struct dentry *dentry, 3565 struct file_kattr *fa) 3566 { 3567 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 3568 } 3569 3570 static int selinux_inode_file_getattr(struct dentry *dentry, 3571 struct file_kattr *fa) 3572 { 3573 return dentry_has_perm(current_cred(), dentry, FILE__GETATTR); 3574 } 3575 3576 static int selinux_path_notify(const struct path *path, u64 mask, 3577 unsigned int obj_type) 3578 { 3579 int ret; 3580 u32 perm; 3581 3582 struct common_audit_data ad; 3583 3584 ad.type = LSM_AUDIT_DATA_PATH; 3585 ad.u.path = *path; 3586 3587 /* 3588 * Set permission needed based on the type of mark being set. 3589 * Performs an additional check for sb watches. 3590 */ 3591 switch (obj_type) { 3592 case FSNOTIFY_OBJ_TYPE_VFSMOUNT: 3593 perm = FILE__WATCH_MOUNT; 3594 break; 3595 case FSNOTIFY_OBJ_TYPE_SB: 3596 perm = FILE__WATCH_SB; 3597 ret = superblock_has_perm(current_cred(), path->dentry->d_sb, 3598 FILESYSTEM__WATCH, &ad); 3599 if (ret) 3600 return ret; 3601 break; 3602 case FSNOTIFY_OBJ_TYPE_INODE: 3603 perm = FILE__WATCH; 3604 break; 3605 case FSNOTIFY_OBJ_TYPE_MNTNS: 3606 perm = FILE__WATCH_MOUNTNS; 3607 break; 3608 default: 3609 return -EINVAL; 3610 } 3611 3612 /* blocking watches require the file:watch_with_perm permission */ 3613 if (mask & (ALL_FSNOTIFY_PERM_EVENTS)) 3614 perm |= FILE__WATCH_WITH_PERM; 3615 3616 /* watches on read-like events need the file:watch_reads permission */ 3617 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_PRE_ACCESS | 3618 FS_CLOSE_NOWRITE)) 3619 perm |= FILE__WATCH_READS; 3620 3621 return path_has_perm(current_cred(), path, perm); 3622 } 3623 3624 /* 3625 * Copy the inode security context value to the user. 3626 * 3627 * Permission check is handled by selinux_inode_getxattr hook. 3628 */ 3629 static int selinux_inode_getsecurity(const struct mnt_idmap *idmap, 3630 struct inode *inode, const char *name, 3631 void **buffer, bool alloc) 3632 { 3633 u32 size; 3634 int error; 3635 char *context = NULL; 3636 struct inode_security_struct *isec; 3637 3638 /* 3639 * If we're not initialized yet, then we can't validate contexts, so 3640 * just let vfs_getxattr fall back to using the on-disk xattr. 3641 */ 3642 if (!selinux_initialized() || 3643 strcmp(name, XATTR_SELINUX_SUFFIX)) 3644 return -EOPNOTSUPP; 3645 3646 /* 3647 * If the caller has CAP_MAC_ADMIN, then get the raw context 3648 * value even if it is not defined by current policy; otherwise, 3649 * use the in-core value under current policy. 3650 * Use the non-auditing forms of the permission checks since 3651 * getxattr may be called by unprivileged processes commonly 3652 * and lack of permission just means that we fall back to the 3653 * in-core context value, not a denial. 3654 */ 3655 isec = inode_security(inode); 3656 if (has_cap_mac_admin(false)) 3657 error = security_sid_to_context_force(isec->sid, &context, 3658 &size); 3659 else 3660 error = security_sid_to_context(isec->sid, 3661 &context, &size); 3662 if (error) 3663 return error; 3664 error = size; 3665 if (alloc) { 3666 *buffer = context; 3667 goto out_nofree; 3668 } 3669 kfree(context); 3670 out_nofree: 3671 return error; 3672 } 3673 3674 static int selinux_inode_setsecurity(struct inode *inode, const char *name, 3675 const void *value, size_t size, int flags) 3676 { 3677 struct inode_security_struct *isec = inode_security_novalidate(inode); 3678 struct superblock_security_struct *sbsec; 3679 u32 newsid; 3680 int rc; 3681 3682 if (strcmp(name, XATTR_SELINUX_SUFFIX)) 3683 return -EOPNOTSUPP; 3684 3685 sbsec = selinux_superblock(inode->i_sb); 3686 if (!(sbsec->flags & SBLABEL_MNT)) 3687 return -EOPNOTSUPP; 3688 3689 if (!value || !size) 3690 return -EACCES; 3691 3692 rc = security_context_to_sid(value, size, &newsid, 3693 GFP_KERNEL); 3694 if (rc) 3695 return rc; 3696 3697 spin_lock(&isec->lock); 3698 isec->sclass = inode_mode_to_security_class(inode->i_mode); 3699 isec->sid = newsid; 3700 isec->initialized = LABEL_INITIALIZED; 3701 spin_unlock(&isec->lock); 3702 return 0; 3703 } 3704 3705 static int selinux_inode_listsecurity(struct inode *inode, char **buffer, 3706 ssize_t *remaining_size) 3707 { 3708 if (!selinux_initialized()) 3709 return 0; 3710 return xattr_list_one(buffer, remaining_size, XATTR_NAME_SELINUX); 3711 } 3712 3713 static void selinux_inode_getlsmprop(struct inode *inode, struct lsm_prop *prop) 3714 { 3715 struct inode_security_struct *isec = inode_security_novalidate(inode); 3716 3717 prop->selinux.secid = isec->sid; 3718 } 3719 3720 static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 3721 { 3722 struct lsm_prop prop; 3723 struct cred_security_struct *crsec; 3724 struct cred *new_creds = *new; 3725 3726 if (new_creds == NULL) { 3727 new_creds = prepare_creds(); 3728 if (!new_creds) 3729 return -ENOMEM; 3730 } 3731 3732 crsec = selinux_cred(new_creds); 3733 /* Get label from overlay inode and set it in create_sid */ 3734 selinux_inode_getlsmprop(d_inode(src), &prop); 3735 crsec->create_sid = prop.selinux.secid; 3736 *new = new_creds; 3737 return 0; 3738 } 3739 3740 static int selinux_inode_copy_up_xattr(struct dentry *dentry, const char *name) 3741 { 3742 /* The copy_up hook above sets the initial context on an inode, but we 3743 * don't then want to overwrite it by blindly copying all the lower 3744 * xattrs up. Instead, filter out SELinux-related xattrs following 3745 * policy load. 3746 */ 3747 if (selinux_initialized() && !strcmp(name, XATTR_NAME_SELINUX)) 3748 return -ECANCELED; /* Discard */ 3749 /* 3750 * Any other attribute apart from SELINUX is not claimed, supported 3751 * by selinux. 3752 */ 3753 return -EOPNOTSUPP; 3754 } 3755 3756 /* kernfs node operations */ 3757 3758 static int selinux_kernfs_init_security(struct kernfs_node *kn_dir, 3759 struct kernfs_node *kn) 3760 { 3761 const struct cred_security_struct *crsec = selinux_cred(current_cred()); 3762 u32 parent_sid, newsid, clen; 3763 int rc; 3764 char *context; 3765 3766 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0); 3767 if (rc == -ENODATA) 3768 return 0; 3769 else if (rc < 0) 3770 return rc; 3771 3772 clen = (u32)rc; 3773 context = kmalloc(clen, GFP_KERNEL); 3774 if (!context) 3775 return -ENOMEM; 3776 3777 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen); 3778 if (rc < 0) { 3779 kfree(context); 3780 return rc; 3781 } 3782 3783 rc = security_context_to_sid(context, clen, &parent_sid, 3784 GFP_KERNEL); 3785 kfree(context); 3786 if (rc) 3787 return rc; 3788 3789 if (crsec->create_sid) { 3790 newsid = crsec->create_sid; 3791 } else { 3792 u16 secclass = inode_mode_to_security_class(kn->mode); 3793 const char *kn_name; 3794 struct qstr q; 3795 3796 /* kn is fresh, can't be renamed, name goes not away */ 3797 kn_name = rcu_dereference_check(kn->name, true); 3798 q.name = kn_name; 3799 q.hash_len = hashlen_string(kn_dir, kn_name); 3800 3801 rc = security_transition_sid(crsec->sid, 3802 parent_sid, secclass, &q, 3803 &newsid); 3804 if (rc) 3805 return rc; 3806 } 3807 3808 rc = security_sid_to_context_force(newsid, 3809 &context, &clen); 3810 if (rc) 3811 return rc; 3812 3813 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen, 3814 XATTR_CREATE); 3815 kfree(context); 3816 return rc; 3817 } 3818 3819 3820 /* file security operations */ 3821 3822 static int selinux_revalidate_file_permission(struct file *file, int mask) 3823 { 3824 const struct cred *cred = current_cred(); 3825 struct inode *inode = file_inode(file); 3826 3827 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 3828 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 3829 mask |= MAY_APPEND; 3830 3831 return file_has_perm(cred, file, 3832 file_mask_to_av(inode->i_mode, mask)); 3833 } 3834 3835 static int selinux_file_permission(struct file *file, int mask) 3836 { 3837 struct inode *inode = file_inode(file); 3838 struct file_security_struct *fsec = selinux_file(file); 3839 struct inode_security_struct *isec; 3840 u32 sid = current_sid(); 3841 3842 if (!mask) 3843 /* No permission to check. Existence test. */ 3844 return 0; 3845 3846 isec = inode_security(inode); 3847 if (sid == fsec->sid && fsec->isid == isec->sid && 3848 fsec->pseqno == avc_policy_seqno()) 3849 /* No change since file_open check. */ 3850 return 0; 3851 3852 return selinux_revalidate_file_permission(file, mask); 3853 } 3854 3855 static int selinux_file_alloc_security(struct file *file) 3856 { 3857 struct file_security_struct *fsec = selinux_file(file); 3858 u32 sid = current_sid(); 3859 3860 fsec->sid = sid; 3861 fsec->fown_sid = sid; 3862 3863 return 0; 3864 } 3865 3866 static inline u32 selinux_file_user_sid(const struct file *file) 3867 { 3868 if (unlikely(file->f_mode & FMODE_BACKING)) 3869 return selinux_backing_file(file)->uf_sid; 3870 return selinux_file(file)->sid; 3871 } 3872 3873 static int selinux_backing_file_alloc(struct file *backing_file, 3874 const struct file *user_file) 3875 { 3876 struct backing_file_security_struct *bfsec; 3877 const struct backing_file_security_struct *ubfsec; 3878 struct backing_file_security_layer *layer; 3879 u32 i; 3880 3881 bfsec = selinux_backing_file(backing_file); 3882 bfsec->uf_sid = selinux_file_user_sid(user_file); 3883 if (!(user_file->f_mode & FMODE_BACKING)) 3884 return 0; 3885 3886 ubfsec = selinux_backing_file(user_file); 3887 /* a wrapped count would make kmalloc_array() return ZERO_SIZE_PTR */ 3888 if (unlikely(ubfsec->layer_count == U32_MAX)) 3889 return -EOVERFLOW; 3890 3891 /* 3892 * The final VMA only retains the lowest backing file, so record the 3893 * whole chain here rather than in the mmap hook, where concurrent 3894 * mappings would have to be serialized. Size it dynamically: erofs 3895 * inode sharing adds a backing file without bumping s_stack_depth. 3896 */ 3897 bfsec->layers = kmalloc_array(ubfsec->layer_count + 1, 3898 sizeof(*bfsec->layers), GFP_KERNEL); 3899 if (!bfsec->layers) 3900 return -ENOMEM; 3901 3902 for (i = 0; i < ubfsec->layer_count; i++) { 3903 layer = &bfsec->layers[i]; 3904 *layer = ubfsec->layers[i]; 3905 path_get(&layer->path); 3906 } 3907 3908 /* f_path, not file_user_path(): this layer, not the top-level file */ 3909 layer = &bfsec->layers[i]; 3910 layer->path = user_file->f_path; 3911 layer->mounter_sid = cred_sid(user_file->f_cred); 3912 layer->fd_sid = selinux_file(user_file)->sid; 3913 path_get(&layer->path); 3914 bfsec->layer_count = ubfsec->layer_count + 1; 3915 3916 return 0; 3917 } 3918 3919 static void selinux_backing_file_free(struct file *backing_file) 3920 { 3921 struct backing_file_security_struct *bfsec; 3922 3923 /* security_backing_file_free() may be called twice after an error */ 3924 if (!backing_file_security(backing_file)) 3925 return; 3926 3927 bfsec = selinux_backing_file(backing_file); 3928 while (bfsec->layer_count) 3929 path_put(&bfsec->layers[--bfsec->layer_count].path); 3930 kfree(bfsec->layers); 3931 bfsec->layers = NULL; 3932 } 3933 3934 /* 3935 * Check whether a task has the ioctl permission and cmd 3936 * operation to an inode. 3937 */ 3938 static int ioctl_has_perm(const struct cred *cred, struct file *file, 3939 u32 requested, unsigned int cmd) 3940 { 3941 struct common_audit_data ad; 3942 struct file_security_struct *fsec = selinux_file(file); 3943 struct inode *inode = file_inode(file); 3944 struct inode_security_struct *isec; 3945 struct lsm_ioctlop_audit ioctl; 3946 u32 ssid = cred_sid(cred); 3947 int rc; 3948 u8 driver = cmd >> 8; 3949 u8 xperm = cmd & 0xff; 3950 3951 ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3952 ad.u.op = &ioctl; 3953 ad.u.op->cmd = cmd; 3954 ad.u.op->path = file->f_path; 3955 3956 if (ssid != fsec->sid) { 3957 rc = avc_has_perm(ssid, fsec->sid, 3958 SECCLASS_FD, 3959 FD__USE, 3960 &ad); 3961 if (rc) 3962 goto out; 3963 } 3964 3965 if (unlikely(IS_PRIVATE(inode))) 3966 return 0; 3967 3968 isec = inode_security(inode); 3969 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, requested, 3970 driver, AVC_EXT_IOCTL, xperm, &ad); 3971 out: 3972 return rc; 3973 } 3974 3975 static int selinux_file_ioctl(struct file *file, unsigned int cmd, 3976 unsigned long arg) 3977 { 3978 const struct cred *cred = current_cred(); 3979 int error = 0; 3980 3981 switch (cmd) { 3982 case FIONREAD: 3983 case FIBMAP: 3984 case FIGETBSZ: 3985 case FS_IOC_GETFLAGS: 3986 case FS_IOC_GETVERSION: 3987 error = file_has_perm(cred, file, FILE__GETATTR); 3988 break; 3989 3990 case FS_IOC_SETFLAGS: 3991 case FS_IOC_SETVERSION: 3992 error = file_has_perm(cred, file, FILE__SETATTR); 3993 break; 3994 3995 /* sys_ioctl() checks */ 3996 case FIONBIO: 3997 case FIOASYNC: 3998 error = file_has_perm(cred, file, 0); 3999 break; 4000 4001 case KDSKBENT: 4002 case KDSKBSENT: 4003 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 4004 CAP_OPT_NONE, true); 4005 break; 4006 4007 case FIOCLEX: 4008 case FIONCLEX: 4009 if (!selinux_policycap_ioctl_skip_cloexec()) 4010 error = ioctl_has_perm(cred, file, FILE__IOCTL, cmd); 4011 break; 4012 4013 /* default case assumes that the command will go 4014 * to the file's ioctl() function. 4015 */ 4016 default: 4017 error = ioctl_has_perm(cred, file, FILE__IOCTL, cmd); 4018 } 4019 return error; 4020 } 4021 4022 static int selinux_file_ioctl_compat(struct file *file, unsigned int cmd, 4023 unsigned long arg) 4024 { 4025 /* 4026 * If we are in a 64-bit kernel running 32-bit userspace, we need to 4027 * make sure we don't compare 32-bit flags to 64-bit flags. 4028 */ 4029 switch (cmd) { 4030 case FS_IOC32_GETFLAGS: 4031 cmd = FS_IOC_GETFLAGS; 4032 break; 4033 case FS_IOC32_SETFLAGS: 4034 cmd = FS_IOC_SETFLAGS; 4035 break; 4036 case FS_IOC32_GETVERSION: 4037 cmd = FS_IOC_GETVERSION; 4038 break; 4039 case FS_IOC32_SETVERSION: 4040 cmd = FS_IOC_SETVERSION; 4041 break; 4042 default: 4043 break; 4044 } 4045 4046 return selinux_file_ioctl(file, cmd, arg); 4047 } 4048 4049 static int default_noexec __ro_after_init; 4050 4051 static u32 file_map_prot_to_av(unsigned long prot, bool shared) 4052 { 4053 u32 av = FILE__READ; 4054 4055 if (shared && (prot & PROT_WRITE)) 4056 av |= FILE__WRITE; 4057 if (prot & PROT_EXEC) 4058 av |= FILE__EXECUTE; 4059 4060 return av; 4061 } 4062 4063 static int backing_mounters_has_perm(const struct file *file, u32 av) 4064 { 4065 const struct backing_file_security_struct *bfsec; 4066 const struct backing_file_security_layer *layer; 4067 struct common_audit_data ad; 4068 struct inode *inode; 4069 u32 i; 4070 int rc; 4071 4072 if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING))) 4073 return -EIO; 4074 4075 bfsec = selinux_backing_file(file); 4076 for (i = 0; i < bfsec->layer_count; i++) { 4077 layer = &bfsec->layers[i]; 4078 inode = d_inode(layer->path.dentry); 4079 4080 ad.type = LSM_AUDIT_DATA_PATH; 4081 ad.u.path = layer->path; 4082 4083 if (layer->mounter_sid != layer->fd_sid) { 4084 rc = avc_has_perm(layer->mounter_sid, layer->fd_sid, 4085 SECCLASS_FD, FD__USE, &ad); 4086 if (rc) 4087 return rc; 4088 } 4089 4090 rc = inode_sid_has_perm(layer->mounter_sid, inode, av, &ad); 4091 if (rc) 4092 return rc; 4093 } 4094 4095 return 0; 4096 } 4097 4098 static int __file_map_prot_check(const struct file *file, unsigned long prot, 4099 bool shared, bool mounter_check, 4100 bool bf_user_file) 4101 { 4102 struct inode *inode = NULL; 4103 bool prot_exec = prot & PROT_EXEC; 4104 bool prot_write = prot & PROT_WRITE; 4105 4106 if (file) { 4107 if (bf_user_file) 4108 inode = d_inode(backing_file_user_path(file)->dentry); 4109 else 4110 inode = file_inode(file); 4111 } 4112 4113 if (!mounter_check && default_noexec && prot_exec && 4114 (!file || IS_PRIVATE(inode) || (!shared && prot_write))) { 4115 int rc; 4116 u32 sid = current_sid(); 4117 4118 /* 4119 * We are making executable an anonymous mapping or a private 4120 * file mapping that will also be writable. 4121 */ 4122 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__EXECMEM, 4123 NULL); 4124 if (rc) 4125 return rc; 4126 } 4127 4128 if (file) { 4129 const struct cred *cred = mounter_check ? 4130 file->f_cred : current_cred(); 4131 4132 return __file_has_perm(cred, file, 4133 file_map_prot_to_av(prot, shared), 4134 bf_user_file); 4135 } 4136 4137 return 0; 4138 } 4139 4140 static inline int file_map_prot_check(const struct file *file, 4141 unsigned long prot, bool shared, 4142 bool mounter_check) 4143 { 4144 return __file_map_prot_check(file, prot, shared, mounter_check, false); 4145 } 4146 4147 static int selinux_mmap_addr(unsigned long addr) 4148 { 4149 int rc = 0; 4150 4151 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 4152 u32 sid = current_sid(); 4153 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 4154 MEMPROTECT__MMAP_ZERO, NULL); 4155 } 4156 4157 return rc; 4158 } 4159 4160 static int selinux_mmap_file_common(struct file *file, unsigned long prot, 4161 bool shared, bool mounter_check) 4162 { 4163 if (file) { 4164 int rc; 4165 struct common_audit_data ad; 4166 const struct cred *cred = mounter_check ? 4167 file->f_cred : current_cred(); 4168 4169 ad.type = LSM_AUDIT_DATA_FILE; 4170 ad.u.file = file; 4171 rc = inode_has_perm(cred, file_inode(file), FILE__MAP, &ad); 4172 if (rc) 4173 return rc; 4174 } 4175 4176 return file_map_prot_check(file, prot, shared, mounter_check); 4177 } 4178 4179 static int selinux_mmap_file(struct file *file, 4180 unsigned long reqprot __always_unused, 4181 unsigned long prot, unsigned long flags) 4182 { 4183 return selinux_mmap_file_common(file, prot, 4184 (flags & MAP_TYPE) == MAP_SHARED, 4185 false); 4186 } 4187 4188 /** 4189 * selinux_mmap_backing_file - Check mmap permissions on a backing file 4190 * @vma: memory region 4191 * @backing_file: stacked filesystem backing file 4192 * @user_file: user visible file 4193 * 4194 * This is called after selinux_mmap_file() on stacked filesystems, and it 4195 * is this function's responsibility to verify access to @backing_file and 4196 * setup the SELinux state for possible later use in the mprotect() code path. 4197 * 4198 * By the time this function is called, mmap() access to @user_file has already 4199 * been authorized and @vma->vm_file has been set to point to @backing_file. 4200 * 4201 * Return zero on success, negative values otherwise. 4202 */ 4203 static int selinux_mmap_backing_file(struct vm_area_struct *vma, 4204 struct file *backing_file, 4205 struct file *user_file __always_unused) 4206 { 4207 unsigned long prot = 0; 4208 4209 /* translate vma->vm_flags perms into PROT perms */ 4210 if (vma->vm_flags & VM_READ) 4211 prot |= PROT_READ; 4212 if (vma->vm_flags & VM_WRITE) 4213 prot |= PROT_WRITE; 4214 if (vma->vm_flags & VM_EXEC) 4215 prot |= PROT_EXEC; 4216 4217 return selinux_mmap_file_common(backing_file, prot, 4218 vma->vm_flags & VM_SHARED, 4219 true); 4220 } 4221 4222 static int selinux_file_mprotect(struct vm_area_struct *vma, 4223 unsigned long reqprot __always_unused, 4224 unsigned long prot) 4225 { 4226 int rc; 4227 const struct cred *cred = current_cred(); 4228 u32 sid = cred_sid(cred); 4229 u32 av; 4230 const struct file *file = vma->vm_file; 4231 bool backing_file; 4232 bool shared = vma->vm_flags & VM_SHARED; 4233 4234 /* check if we need to trigger the "backing files are awful" mode */ 4235 backing_file = file && (file->f_mode & FMODE_BACKING); 4236 4237 if (default_noexec && 4238 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 4239 /* 4240 * We don't use the vma_is_initial_heap() helper as it has 4241 * a history of problems and is currently broken on systems 4242 * where there is no heap, e.g. brk == start_brk. Before 4243 * replacing the conditional below with vma_is_initial_heap(), 4244 * or something similar, please ensure that the logic is the 4245 * same as what we have below or you have tested every possible 4246 * corner case you can think to test. 4247 */ 4248 if (vma->vm_start >= vma->vm_mm->start_brk && 4249 vma->vm_end <= vma->vm_mm->brk) { 4250 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 4251 PROCESS__EXECHEAP, NULL); 4252 if (rc) 4253 return rc; 4254 } else if (!file && (vma_is_initial_stack(vma) || 4255 vma_is_stack_for_current(vma))) { 4256 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 4257 PROCESS__EXECSTACK, NULL); 4258 if (rc) 4259 return rc; 4260 } else if (file && vma->anon_vma) { 4261 /* 4262 * We are making executable a file mapping that has 4263 * had some COW done. Since pages might have been 4264 * written, check ability to execute the possibly 4265 * modified content. This typically should only 4266 * occur for text relocations. 4267 */ 4268 rc = __file_has_perm(cred, file, FILE__EXECMOD, 4269 backing_file); 4270 if (rc) 4271 return rc; 4272 if (backing_file) { 4273 rc = backing_mounters_has_perm(file, 4274 FILE__EXECMOD); 4275 if (rc) 4276 return rc; 4277 rc = file_has_perm(file->f_cred, file, 4278 FILE__EXECMOD); 4279 if (rc) 4280 return rc; 4281 } 4282 } 4283 } 4284 4285 rc = __file_map_prot_check(file, prot, shared, false, backing_file); 4286 if (rc) 4287 return rc; 4288 if (backing_file) { 4289 av = file_map_prot_to_av(prot, shared); 4290 rc = backing_mounters_has_perm(file, av); 4291 if (rc) 4292 return rc; 4293 rc = file_map_prot_check(file, prot, shared, true); 4294 if (rc) 4295 return rc; 4296 } 4297 4298 return 0; 4299 } 4300 4301 static int selinux_file_lock(struct file *file, unsigned int cmd) 4302 { 4303 const struct cred *cred = current_cred(); 4304 4305 return file_has_perm(cred, file, FILE__LOCK); 4306 } 4307 4308 static int selinux_file_fcntl(struct file *file, unsigned int cmd, 4309 unsigned long arg) 4310 { 4311 const struct cred *cred = current_cred(); 4312 int err = 0; 4313 4314 switch (cmd) { 4315 case F_SETFL: 4316 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 4317 err = file_has_perm(cred, file, FILE__WRITE); 4318 break; 4319 } 4320 fallthrough; 4321 case F_SETOWN: 4322 case F_SETSIG: 4323 case F_GETFL: 4324 case F_GETOWN: 4325 case F_GETSIG: 4326 case F_GETOWNER_UIDS: 4327 /* Just check FD__USE permission */ 4328 err = file_has_perm(cred, file, 0); 4329 break; 4330 case F_GETLK: 4331 case F_SETLK: 4332 case F_SETLKW: 4333 case F_OFD_GETLK: 4334 case F_OFD_SETLK: 4335 case F_OFD_SETLKW: 4336 #if BITS_PER_LONG == 32 4337 case F_GETLK64: 4338 case F_SETLK64: 4339 case F_SETLKW64: 4340 #endif 4341 err = file_has_perm(cred, file, FILE__LOCK); 4342 break; 4343 } 4344 4345 return err; 4346 } 4347 4348 static void selinux_file_set_fowner(struct file *file) 4349 { 4350 struct file_security_struct *fsec; 4351 4352 fsec = selinux_file(file); 4353 fsec->fown_sid = current_sid(); 4354 } 4355 4356 static int selinux_file_send_sigiotask(struct task_struct *tsk, 4357 struct fown_struct *fown, int signum) 4358 { 4359 struct file *file; 4360 u32 sid = task_sid_obj(tsk); 4361 u32 perm; 4362 struct file_security_struct *fsec; 4363 4364 /* struct fown_struct is never outside the context of a struct file */ 4365 file = fown->file; 4366 4367 fsec = selinux_file(file); 4368 4369 if (!signum) 4370 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 4371 else 4372 perm = signal_to_av(signum); 4373 4374 return avc_has_perm(fsec->fown_sid, sid, 4375 SECCLASS_PROCESS, perm, NULL); 4376 } 4377 4378 static int selinux_file_receive(struct file *file) 4379 { 4380 const struct cred *cred = current_cred(); 4381 4382 return file_has_perm(cred, file, file_to_av(file)); 4383 } 4384 4385 static int selinux_file_open(struct file *file) 4386 { 4387 struct file_security_struct *fsec; 4388 struct inode_security_struct *isec; 4389 4390 fsec = selinux_file(file); 4391 isec = inode_security(file_inode(file)); 4392 /* 4393 * Save inode label and policy sequence number 4394 * at open-time so that selinux_file_permission 4395 * can determine whether revalidation is necessary. 4396 * Task label is already saved in the file security 4397 * struct as its SID. 4398 */ 4399 fsec->isid = isec->sid; 4400 fsec->pseqno = avc_policy_seqno(); 4401 /* 4402 * Since the inode label or policy seqno may have changed 4403 * between the selinux_inode_permission check and the saving 4404 * of state above, recheck that access is still permitted. 4405 * Otherwise, access might never be revalidated against the 4406 * new inode label or new policy. 4407 * This check is not redundant - do not remove. 4408 */ 4409 return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 4410 } 4411 4412 /* task security operations */ 4413 4414 static int selinux_task_alloc(struct task_struct *task, 4415 u64 clone_flags) 4416 { 4417 u32 sid = current_sid(); 4418 struct task_security_struct *old_tsec = selinux_task(current); 4419 struct task_security_struct *new_tsec = selinux_task(task); 4420 4421 *new_tsec = *old_tsec; 4422 return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 4423 } 4424 4425 /* 4426 * prepare a new set of credentials for modification 4427 */ 4428 static int selinux_cred_prepare(struct cred *new, const struct cred *old, 4429 gfp_t gfp) 4430 { 4431 const struct cred_security_struct *old_crsec = selinux_cred(old); 4432 struct cred_security_struct *crsec = selinux_cred(new); 4433 4434 *crsec = *old_crsec; 4435 return 0; 4436 } 4437 4438 /* 4439 * transfer the SELinux data to a blank set of creds 4440 */ 4441 static void selinux_cred_transfer(struct cred *new, const struct cred *old) 4442 { 4443 const struct cred_security_struct *old_crsec = selinux_cred(old); 4444 struct cred_security_struct *crsec = selinux_cred(new); 4445 4446 *crsec = *old_crsec; 4447 } 4448 4449 static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 4450 { 4451 *secid = cred_sid(c); 4452 } 4453 4454 static void selinux_cred_getlsmprop(const struct cred *c, struct lsm_prop *prop) 4455 { 4456 prop->selinux.secid = cred_sid(c); 4457 } 4458 4459 /* 4460 * set the security data for a kernel service 4461 * - all the creation contexts are set to unlabelled 4462 */ 4463 static int selinux_kernel_act_as(struct cred *new, u32 secid) 4464 { 4465 struct cred_security_struct *crsec = selinux_cred(new); 4466 u32 sid = current_sid(); 4467 int ret; 4468 4469 ret = avc_has_perm(sid, secid, 4470 SECCLASS_KERNEL_SERVICE, 4471 KERNEL_SERVICE__USE_AS_OVERRIDE, 4472 NULL); 4473 if (ret == 0) { 4474 crsec->sid = secid; 4475 crsec->create_sid = 0; 4476 crsec->keycreate_sid = 0; 4477 crsec->sockcreate_sid = 0; 4478 } 4479 return ret; 4480 } 4481 4482 /* 4483 * set the file creation context in a security record to the same as the 4484 * objective context of the specified inode 4485 */ 4486 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 4487 { 4488 struct inode_security_struct *isec = inode_security(inode); 4489 struct cred_security_struct *crsec = selinux_cred(new); 4490 u32 sid = current_sid(); 4491 int ret; 4492 4493 ret = avc_has_perm(sid, isec->sid, 4494 SECCLASS_KERNEL_SERVICE, 4495 KERNEL_SERVICE__CREATE_FILES_AS, 4496 NULL); 4497 4498 if (ret == 0) 4499 crsec->create_sid = isec->sid; 4500 return ret; 4501 } 4502 4503 static int selinux_kernel_module_request(char *kmod_name) 4504 { 4505 struct common_audit_data ad; 4506 4507 ad.type = LSM_AUDIT_DATA_KMOD; 4508 ad.u.kmod_name = kmod_name; 4509 4510 return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 4511 SYSTEM__MODULE_REQUEST, &ad); 4512 } 4513 4514 static int selinux_kernel_load_from_file(struct file *file, u32 requested) 4515 { 4516 struct common_audit_data ad; 4517 struct inode_security_struct *isec; 4518 struct file_security_struct *fsec; 4519 u32 sid = current_sid(); 4520 int rc; 4521 4522 if (file == NULL) 4523 return avc_has_perm(sid, sid, SECCLASS_SYSTEM, requested, NULL); 4524 4525 ad.type = LSM_AUDIT_DATA_FILE; 4526 ad.u.file = file; 4527 4528 fsec = selinux_file(file); 4529 if (sid != fsec->sid) { 4530 rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 4531 if (rc) 4532 return rc; 4533 } 4534 4535 isec = inode_security(file_inode(file)); 4536 return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM, requested, &ad); 4537 } 4538 4539 static int selinux_kernel_read_file(struct file *file, 4540 enum kernel_read_file_id id, 4541 bool contents) 4542 { 4543 int rc = 0; 4544 4545 BUILD_BUG_ON_MSG(READING_MAX_ID > 8, 4546 "New kernel_read_file_id introduced; update SELinux!"); 4547 4548 switch (id) { 4549 case READING_FIRMWARE: 4550 rc = selinux_kernel_load_from_file(file, SYSTEM__FIRMWARE_LOAD); 4551 break; 4552 case READING_MODULE: 4553 case READING_MODULE_COMPRESSED: 4554 rc = selinux_kernel_load_from_file(file, SYSTEM__MODULE_LOAD); 4555 break; 4556 case READING_KEXEC_IMAGE: 4557 rc = selinux_kernel_load_from_file(file, 4558 SYSTEM__KEXEC_IMAGE_LOAD); 4559 break; 4560 case READING_KEXEC_INITRAMFS: 4561 rc = selinux_kernel_load_from_file(file, 4562 SYSTEM__KEXEC_INITRAMFS_LOAD); 4563 break; 4564 case READING_POLICY: 4565 rc = selinux_kernel_load_from_file(file, SYSTEM__POLICY_LOAD); 4566 break; 4567 case READING_X509_CERTIFICATE: 4568 rc = selinux_kernel_load_from_file(file, 4569 SYSTEM__X509_CERTIFICATE_LOAD); 4570 break; 4571 default: 4572 break; 4573 } 4574 4575 return rc; 4576 } 4577 4578 static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents) 4579 { 4580 int rc = 0; 4581 4582 BUILD_BUG_ON_MSG(LOADING_MAX_ID > 8, 4583 "New kernel_load_data_id introduced; update SELinux!"); 4584 4585 switch (id) { 4586 case LOADING_FIRMWARE: 4587 rc = selinux_kernel_load_from_file(NULL, SYSTEM__FIRMWARE_LOAD); 4588 break; 4589 case LOADING_MODULE: 4590 rc = selinux_kernel_load_from_file(NULL, SYSTEM__MODULE_LOAD); 4591 break; 4592 case LOADING_KEXEC_IMAGE: 4593 rc = selinux_kernel_load_from_file(NULL, 4594 SYSTEM__KEXEC_IMAGE_LOAD); 4595 break; 4596 case LOADING_KEXEC_INITRAMFS: 4597 rc = selinux_kernel_load_from_file(NULL, 4598 SYSTEM__KEXEC_INITRAMFS_LOAD); 4599 break; 4600 case LOADING_POLICY: 4601 rc = selinux_kernel_load_from_file(NULL, 4602 SYSTEM__POLICY_LOAD); 4603 break; 4604 case LOADING_X509_CERTIFICATE: 4605 rc = selinux_kernel_load_from_file(NULL, 4606 SYSTEM__X509_CERTIFICATE_LOAD); 4607 break; 4608 default: 4609 break; 4610 } 4611 4612 return rc; 4613 } 4614 4615 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 4616 { 4617 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4618 PROCESS__SETPGID, NULL); 4619 } 4620 4621 static int selinux_task_getpgid(struct task_struct *p) 4622 { 4623 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4624 PROCESS__GETPGID, NULL); 4625 } 4626 4627 static int selinux_task_getsid(struct task_struct *p) 4628 { 4629 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4630 PROCESS__GETSESSION, NULL); 4631 } 4632 4633 static void selinux_current_getlsmprop_subj(struct lsm_prop *prop) 4634 { 4635 prop->selinux.secid = current_sid(); 4636 } 4637 4638 static void selinux_task_getlsmprop_obj(struct task_struct *p, 4639 struct lsm_prop *prop) 4640 { 4641 prop->selinux.secid = task_sid_obj(p); 4642 } 4643 4644 static int selinux_task_setnice(struct task_struct *p, int nice) 4645 { 4646 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4647 PROCESS__SETSCHED, NULL); 4648 } 4649 4650 static int selinux_task_setioprio(struct task_struct *p, int ioprio) 4651 { 4652 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4653 PROCESS__SETSCHED, NULL); 4654 } 4655 4656 static int selinux_task_getioprio(struct task_struct *p) 4657 { 4658 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4659 PROCESS__GETSCHED, NULL); 4660 } 4661 4662 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 4663 unsigned int flags) 4664 { 4665 u32 av = 0; 4666 4667 if (!flags) 4668 return 0; 4669 if (flags & LSM_PRLIMIT_WRITE) 4670 av |= PROCESS__SETRLIMIT; 4671 if (flags & LSM_PRLIMIT_READ) 4672 av |= PROCESS__GETRLIMIT; 4673 return avc_has_perm(cred_sid(cred), cred_sid(tcred), 4674 SECCLASS_PROCESS, av, NULL); 4675 } 4676 4677 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 4678 struct rlimit *new_rlim) 4679 { 4680 struct rlimit *old_rlim = p->signal->rlim + resource; 4681 4682 /* Control the ability to change the hard limit (whether 4683 lowering or raising it), so that the hard limit can 4684 later be used as a safe reset point for the soft limit 4685 upon context transitions. See selinux_bprm_committing_creds. */ 4686 if (old_rlim->rlim_max != new_rlim->rlim_max) 4687 return avc_has_perm(current_sid(), task_sid_obj(p), 4688 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 4689 4690 return 0; 4691 } 4692 4693 static int selinux_task_setscheduler(struct task_struct *p) 4694 { 4695 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4696 PROCESS__SETSCHED, NULL); 4697 } 4698 4699 static int selinux_task_getscheduler(struct task_struct *p) 4700 { 4701 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4702 PROCESS__GETSCHED, NULL); 4703 } 4704 4705 static int selinux_task_movememory(struct task_struct *p) 4706 { 4707 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS, 4708 PROCESS__SETSCHED, NULL); 4709 } 4710 4711 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 4712 int sig, const struct cred *cred) 4713 { 4714 u32 secid; 4715 u32 perm; 4716 4717 if (!sig) 4718 perm = PROCESS__SIGNULL; /* null signal; existence test */ 4719 else 4720 perm = signal_to_av(sig); 4721 if (!cred) 4722 secid = current_sid(); 4723 else 4724 secid = cred_sid(cred); 4725 return avc_has_perm(secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL); 4726 } 4727 4728 static void selinux_task_to_inode(struct task_struct *p, 4729 struct inode *inode) 4730 { 4731 struct inode_security_struct *isec = selinux_inode(inode); 4732 u32 sid = task_sid_obj(p); 4733 4734 spin_lock(&isec->lock); 4735 isec->sclass = inode_mode_to_security_class(inode->i_mode); 4736 isec->sid = sid; 4737 isec->initialized = LABEL_INITIALIZED; 4738 spin_unlock(&isec->lock); 4739 } 4740 4741 static int selinux_userns_create(const struct cred *cred) 4742 { 4743 u32 sid = current_sid(); 4744 4745 return avc_has_perm(sid, sid, SECCLASS_USER_NAMESPACE, 4746 USER_NAMESPACE__CREATE, NULL); 4747 } 4748 4749 /* Returns error only if unable to parse addresses */ 4750 static int selinux_parse_skb_ipv4(struct sk_buff *skb, 4751 struct common_audit_data *ad, u8 *proto) 4752 { 4753 int offset, ihlen, ret = -EINVAL; 4754 struct iphdr _iph, *ih; 4755 4756 offset = skb_network_offset(skb); 4757 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 4758 if (ih == NULL) 4759 goto out; 4760 4761 ihlen = ih->ihl * 4; 4762 if (ihlen < sizeof(_iph)) 4763 goto out; 4764 4765 ad->u.net->v4info.saddr = ih->saddr; 4766 ad->u.net->v4info.daddr = ih->daddr; 4767 ret = 0; 4768 4769 if (proto) 4770 *proto = ih->protocol; 4771 4772 switch (ih->protocol) { 4773 case IPPROTO_TCP: { 4774 struct tcphdr _tcph, *th; 4775 4776 if (ntohs(ih->frag_off) & IP_OFFSET) 4777 break; 4778 4779 offset += ihlen; 4780 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 4781 if (th == NULL) 4782 break; 4783 4784 ad->u.net->sport = th->source; 4785 ad->u.net->dport = th->dest; 4786 break; 4787 } 4788 4789 case IPPROTO_UDP: { 4790 struct udphdr _udph, *uh; 4791 4792 if (ntohs(ih->frag_off) & IP_OFFSET) 4793 break; 4794 4795 offset += ihlen; 4796 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 4797 if (uh == NULL) 4798 break; 4799 4800 ad->u.net->sport = uh->source; 4801 ad->u.net->dport = uh->dest; 4802 break; 4803 } 4804 4805 #if IS_ENABLED(CONFIG_IP_SCTP) 4806 case IPPROTO_SCTP: { 4807 struct sctphdr _sctph, *sh; 4808 4809 if (ntohs(ih->frag_off) & IP_OFFSET) 4810 break; 4811 4812 offset += ihlen; 4813 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4814 if (sh == NULL) 4815 break; 4816 4817 ad->u.net->sport = sh->source; 4818 ad->u.net->dport = sh->dest; 4819 break; 4820 } 4821 #endif 4822 default: 4823 break; 4824 } 4825 out: 4826 return ret; 4827 } 4828 4829 #if IS_ENABLED(CONFIG_IPV6) 4830 4831 /* Returns error only if unable to parse addresses */ 4832 static int selinux_parse_skb_ipv6(struct sk_buff *skb, 4833 struct common_audit_data *ad, u8 *proto) 4834 { 4835 u8 nexthdr; 4836 int ret = -EINVAL, offset; 4837 struct ipv6hdr _ipv6h, *ip6; 4838 __be16 frag_off; 4839 4840 offset = skb_network_offset(skb); 4841 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 4842 if (ip6 == NULL) 4843 goto out; 4844 4845 ad->u.net->v6info.saddr = ip6->saddr; 4846 ad->u.net->v6info.daddr = ip6->daddr; 4847 ret = 0; 4848 4849 nexthdr = ip6->nexthdr; 4850 offset += sizeof(_ipv6h); 4851 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 4852 if (offset < 0) 4853 goto out; 4854 4855 if (proto) 4856 *proto = nexthdr; 4857 4858 switch (nexthdr) { 4859 case IPPROTO_TCP: { 4860 struct tcphdr _tcph, *th; 4861 4862 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 4863 if (th == NULL) 4864 break; 4865 4866 ad->u.net->sport = th->source; 4867 ad->u.net->dport = th->dest; 4868 break; 4869 } 4870 4871 case IPPROTO_UDP: { 4872 struct udphdr _udph, *uh; 4873 4874 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 4875 if (uh == NULL) 4876 break; 4877 4878 ad->u.net->sport = uh->source; 4879 ad->u.net->dport = uh->dest; 4880 break; 4881 } 4882 4883 #if IS_ENABLED(CONFIG_IP_SCTP) 4884 case IPPROTO_SCTP: { 4885 struct sctphdr _sctph, *sh; 4886 4887 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4888 if (sh == NULL) 4889 break; 4890 4891 ad->u.net->sport = sh->source; 4892 ad->u.net->dport = sh->dest; 4893 break; 4894 } 4895 #endif 4896 /* includes fragments */ 4897 default: 4898 break; 4899 } 4900 out: 4901 return ret; 4902 } 4903 4904 #endif /* IPV6 */ 4905 4906 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4907 char **_addrp, int src, u8 *proto) 4908 { 4909 char *addrp; 4910 int ret; 4911 4912 switch (ad->u.net->family) { 4913 case PF_INET: 4914 ret = selinux_parse_skb_ipv4(skb, ad, proto); 4915 if (ret) 4916 goto parse_error; 4917 addrp = (char *)(src ? &ad->u.net->v4info.saddr : 4918 &ad->u.net->v4info.daddr); 4919 goto okay; 4920 4921 #if IS_ENABLED(CONFIG_IPV6) 4922 case PF_INET6: 4923 ret = selinux_parse_skb_ipv6(skb, ad, proto); 4924 if (ret) 4925 goto parse_error; 4926 addrp = (char *)(src ? &ad->u.net->v6info.saddr : 4927 &ad->u.net->v6info.daddr); 4928 goto okay; 4929 #endif /* IPV6 */ 4930 default: 4931 addrp = NULL; 4932 goto okay; 4933 } 4934 4935 parse_error: 4936 pr_warn( 4937 "SELinux: failure in selinux_parse_skb()," 4938 " unable to parse packet\n"); 4939 return ret; 4940 4941 okay: 4942 if (_addrp) 4943 *_addrp = addrp; 4944 return 0; 4945 } 4946 4947 /** 4948 * selinux_skb_peerlbl_sid - Determine the peer label of a packet 4949 * @skb: the packet 4950 * @family: protocol family 4951 * @sid: the packet's peer label SID 4952 * 4953 * Description: 4954 * Check the various different forms of network peer labeling and determine 4955 * the peer label/SID for the packet; most of the magic actually occurs in 4956 * the security server function security_net_peersid_cmp(). The function 4957 * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4958 * or -EACCES if @sid is invalid due to inconsistencies with the different 4959 * peer labels. 4960 * 4961 */ 4962 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 4963 { 4964 int err; 4965 u32 xfrm_sid; 4966 u32 nlbl_sid; 4967 u32 nlbl_type; 4968 4969 err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4970 if (unlikely(err)) 4971 return -EACCES; 4972 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4973 if (unlikely(err)) 4974 return -EACCES; 4975 4976 err = security_net_peersid_resolve(nlbl_sid, 4977 nlbl_type, xfrm_sid, sid); 4978 if (unlikely(err)) { 4979 pr_warn( 4980 "SELinux: failure in selinux_skb_peerlbl_sid()," 4981 " unable to determine packet's peer label\n"); 4982 return -EACCES; 4983 } 4984 4985 return 0; 4986 } 4987 4988 /** 4989 * selinux_conn_sid - Determine the child socket label for a connection 4990 * @sk_sid: the parent socket's SID 4991 * @skb_sid: the packet's SID 4992 * @conn_sid: the resulting connection SID 4993 * 4994 * If @skb_sid is valid then the user:role:type information from @sk_sid is 4995 * combined with the MLS information from @skb_sid in order to create 4996 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy 4997 * of @sk_sid. Returns zero on success, negative values on failure. 4998 * 4999 */ 5000 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 5001 { 5002 int err = 0; 5003 5004 if (skb_sid != SECSID_NULL) 5005 err = security_sid_mls_copy(sk_sid, skb_sid, 5006 conn_sid); 5007 else 5008 *conn_sid = sk_sid; 5009 5010 return err; 5011 } 5012 5013 /* socket security operations */ 5014 5015 static int socket_sockcreate_sid(const struct cred_security_struct *crsec, 5016 u16 secclass, u32 *socksid) 5017 { 5018 if (crsec->sockcreate_sid > SECSID_NULL) { 5019 *socksid = crsec->sockcreate_sid; 5020 return 0; 5021 } 5022 5023 return security_transition_sid(crsec->sid, crsec->sid, 5024 secclass, NULL, socksid); 5025 } 5026 5027 static bool sock_skip_has_perm(u32 sid) 5028 { 5029 if (sid == SECINITSID_KERNEL) 5030 return true; 5031 5032 /* 5033 * Before POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT, sockets that 5034 * inherited the kernel context from early boot used to be skipped 5035 * here, so preserve that behavior unless the capability is set. 5036 * 5037 * By setting the capability the policy signals that it is ready 5038 * for this quirk to be fixed. Note that sockets created by a kernel 5039 * thread or a usermode helper executed without a transition will 5040 * still be skipped in this check regardless of the policycap 5041 * setting. 5042 */ 5043 if (!selinux_policycap_userspace_initial_context() && 5044 sid == SECINITSID_INIT) 5045 return true; 5046 return false; 5047 } 5048 5049 5050 static int sock_has_perm(struct sock *sk, u32 perms) 5051 { 5052 struct sk_security_struct *sksec = selinux_sock(sk); 5053 struct common_audit_data ad; 5054 struct lsm_network_audit net; 5055 5056 if (sock_skip_has_perm(sksec->sid)) 5057 return 0; 5058 5059 ad_net_init_from_sk(&ad, &net, sk); 5060 5061 return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms, 5062 &ad); 5063 } 5064 5065 static int selinux_socket_create(int family, int type, 5066 int protocol, int kern) 5067 { 5068 const struct cred_security_struct *crsec = selinux_cred(current_cred()); 5069 u32 newsid; 5070 u16 secclass; 5071 int rc; 5072 5073 if (kern) 5074 return 0; 5075 5076 secclass = socket_type_to_security_class(family, type, protocol); 5077 rc = socket_sockcreate_sid(crsec, secclass, &newsid); 5078 if (rc) 5079 return rc; 5080 5081 return avc_has_perm(crsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 5082 } 5083 5084 static int selinux_socket_post_create(struct socket *sock, int family, 5085 int type, int protocol, int kern) 5086 { 5087 const struct cred_security_struct *crsec = selinux_cred(current_cred()); 5088 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 5089 struct sk_security_struct *sksec; 5090 u16 sclass = socket_type_to_security_class(family, type, protocol); 5091 u32 sid = SECINITSID_KERNEL; 5092 int err = 0; 5093 5094 if (!kern) { 5095 err = socket_sockcreate_sid(crsec, sclass, &sid); 5096 if (err) 5097 return err; 5098 } 5099 5100 isec->sclass = sclass; 5101 isec->sid = sid; 5102 isec->initialized = LABEL_INITIALIZED; 5103 5104 if (sock->sk) { 5105 sksec = selinux_sock(sock->sk); 5106 sksec->sclass = sclass; 5107 sksec->sid = sid; 5108 /* Allows detection of the first association on this socket */ 5109 if (sksec->sclass == SECCLASS_SCTP_SOCKET) 5110 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 5111 5112 err = selinux_netlbl_socket_post_create(sock->sk, family); 5113 } 5114 5115 return err; 5116 } 5117 5118 static int selinux_socket_socketpair(struct socket *socka, 5119 struct socket *sockb) 5120 { 5121 struct sk_security_struct *sksec_a = selinux_sock(socka->sk); 5122 struct sk_security_struct *sksec_b = selinux_sock(sockb->sk); 5123 5124 sksec_a->peer_sid = sksec_b->sid; 5125 sksec_b->peer_sid = sksec_a->sid; 5126 5127 return 0; 5128 } 5129 5130 /* Range of port numbers used to automatically bind. 5131 Need to determine whether we should perform a name_bind 5132 permission check between the socket and the port number. */ 5133 5134 static int __selinux_socket_bind(struct sock *sk, struct sockaddr *address, int addrlen) 5135 { 5136 struct sk_security_struct *sksec = selinux_sock(sk); 5137 u16 family; 5138 int err; 5139 5140 err = sock_has_perm(sk, SOCKET__BIND); 5141 if (err) 5142 goto out; 5143 5144 /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 5145 family = sk->sk_family; 5146 if (family == PF_INET || family == PF_INET6) { 5147 char *addrp; 5148 struct common_audit_data ad; 5149 struct lsm_network_audit net = {0,}; 5150 struct sockaddr_in *addr4 = NULL; 5151 struct sockaddr_in6 *addr6 = NULL; 5152 u16 family_sa; 5153 unsigned short snum; 5154 u32 sid, node_perm; 5155 5156 /* 5157 * sctp_bindx(3) calls via selinux_sctp_bind_connect() 5158 * that validates multiple binding addresses. Because of this 5159 * need to check address->sa_family as it is possible to have 5160 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 5161 */ 5162 if (addrlen < offsetofend(struct sockaddr, sa_family)) 5163 return -EINVAL; 5164 family_sa = address->sa_family; 5165 switch (family_sa) { 5166 case AF_UNSPEC: 5167 case AF_INET: 5168 if (addrlen < sizeof(struct sockaddr_in)) 5169 return -EINVAL; 5170 addr4 = (struct sockaddr_in *)address; 5171 if (family_sa == AF_UNSPEC) { 5172 if (family == PF_INET6) { 5173 /* Length check from inet6_bind_sk() */ 5174 if (addrlen < SIN6_LEN_RFC2133) 5175 return -EINVAL; 5176 /* Family check from __inet6_bind() */ 5177 goto err_af; 5178 } 5179 /* see __inet_bind(), we only want to allow 5180 * AF_UNSPEC if the address is INADDR_ANY 5181 */ 5182 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 5183 goto err_af; 5184 family_sa = AF_INET; 5185 } 5186 snum = ntohs(addr4->sin_port); 5187 addrp = (char *)&addr4->sin_addr.s_addr; 5188 break; 5189 case AF_INET6: 5190 if (addrlen < SIN6_LEN_RFC2133) 5191 return -EINVAL; 5192 addr6 = (struct sockaddr_in6 *)address; 5193 snum = ntohs(addr6->sin6_port); 5194 addrp = (char *)&addr6->sin6_addr.s6_addr; 5195 break; 5196 default: 5197 goto err_af; 5198 } 5199 5200 ad.type = LSM_AUDIT_DATA_NET; 5201 ad.u.net = &net; 5202 ad.u.net->sport = htons(snum); 5203 ad.u.net->family = family_sa; 5204 5205 if (snum) { 5206 int low, high; 5207 5208 inet_get_local_port_range(sock_net(sk), &low, &high); 5209 5210 if (inet_port_requires_bind_service(sock_net(sk), snum) || 5211 snum < low || snum > high) { 5212 err = sel_netport_sid(sk->sk_protocol, 5213 snum, &sid); 5214 if (err) 5215 goto out; 5216 err = avc_has_perm(sksec->sid, sid, 5217 sksec->sclass, 5218 SOCKET__NAME_BIND, &ad); 5219 if (err) 5220 goto out; 5221 } 5222 } 5223 5224 switch (sksec->sclass) { 5225 case SECCLASS_TCP_SOCKET: 5226 node_perm = TCP_SOCKET__NODE_BIND; 5227 break; 5228 5229 case SECCLASS_UDP_SOCKET: 5230 node_perm = UDP_SOCKET__NODE_BIND; 5231 break; 5232 5233 case SECCLASS_SCTP_SOCKET: 5234 node_perm = SCTP_SOCKET__NODE_BIND; 5235 break; 5236 5237 default: 5238 node_perm = RAWIP_SOCKET__NODE_BIND; 5239 break; 5240 } 5241 5242 err = sel_netnode_sid(addrp, family_sa, &sid); 5243 if (err) 5244 goto out; 5245 5246 if (family_sa == AF_INET) 5247 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 5248 else 5249 ad.u.net->v6info.saddr = addr6->sin6_addr; 5250 5251 err = avc_has_perm(sksec->sid, sid, 5252 sksec->sclass, node_perm, &ad); 5253 if (err) 5254 goto out; 5255 } 5256 out: 5257 return err; 5258 err_af: 5259 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 5260 if (sk->sk_protocol == IPPROTO_SCTP) 5261 return -EINVAL; 5262 return -EAFNOSUPPORT; 5263 } 5264 5265 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 5266 { 5267 return __selinux_socket_bind(sock->sk, address, addrlen); 5268 } 5269 5270 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 5271 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst 5272 */ 5273 static int selinux_socket_connect_helper(struct sock *sk, 5274 struct sockaddr *address, int addrlen) 5275 { 5276 struct sk_security_struct *sksec = selinux_sock(sk); 5277 int err; 5278 5279 err = sock_has_perm(sk, SOCKET__CONNECT); 5280 if (err) 5281 return err; 5282 if (addrlen < offsetofend(struct sockaddr, sa_family)) 5283 return -EINVAL; 5284 5285 /* connect(AF_UNSPEC) has special handling, as it is a documented 5286 * way to disconnect the socket 5287 */ 5288 if (address->sa_family == AF_UNSPEC) 5289 return 0; 5290 5291 /* 5292 * If a TCP or SCTP socket, check name_connect permission 5293 * for the port. 5294 */ 5295 if (sksec->sclass == SECCLASS_TCP_SOCKET || 5296 sksec->sclass == SECCLASS_SCTP_SOCKET) { 5297 struct common_audit_data ad; 5298 struct lsm_network_audit net = {0,}; 5299 struct sockaddr_in *addr4 = NULL; 5300 struct sockaddr_in6 *addr6 = NULL; 5301 unsigned short snum; 5302 u32 sid, perm; 5303 5304 /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 5305 * that validates multiple connect addresses. Because of this 5306 * need to check address->sa_family as it is possible to have 5307 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 5308 */ 5309 switch (address->sa_family) { 5310 case AF_INET: 5311 addr4 = (struct sockaddr_in *)address; 5312 if (addrlen < sizeof(struct sockaddr_in)) 5313 return -EINVAL; 5314 snum = ntohs(addr4->sin_port); 5315 break; 5316 case AF_INET6: 5317 addr6 = (struct sockaddr_in6 *)address; 5318 if (addrlen < SIN6_LEN_RFC2133) 5319 return -EINVAL; 5320 snum = ntohs(addr6->sin6_port); 5321 break; 5322 default: 5323 /* Note that SCTP services expect -EINVAL, whereas 5324 * others expect -EAFNOSUPPORT. 5325 */ 5326 if (sksec->sclass == SECCLASS_SCTP_SOCKET) 5327 return -EINVAL; 5328 else 5329 return -EAFNOSUPPORT; 5330 } 5331 5332 err = sel_netport_sid(sk->sk_protocol, snum, &sid); 5333 if (err) 5334 return err; 5335 5336 switch (sksec->sclass) { 5337 case SECCLASS_TCP_SOCKET: 5338 perm = TCP_SOCKET__NAME_CONNECT; 5339 break; 5340 case SECCLASS_SCTP_SOCKET: 5341 perm = SCTP_SOCKET__NAME_CONNECT; 5342 break; 5343 } 5344 5345 ad.type = LSM_AUDIT_DATA_NET; 5346 ad.u.net = &net; 5347 ad.u.net->dport = htons(snum); 5348 ad.u.net->family = address->sa_family; 5349 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); 5350 if (err) 5351 return err; 5352 } 5353 5354 return 0; 5355 } 5356 5357 /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 5358 static int selinux_socket_connect(struct socket *sock, 5359 struct sockaddr *address, int addrlen) 5360 { 5361 int err; 5362 struct sock *sk = sock->sk; 5363 5364 err = selinux_socket_connect_helper(sk, address, addrlen); 5365 if (err) 5366 return err; 5367 5368 return selinux_netlbl_socket_connect(sk, address); 5369 } 5370 5371 static int selinux_socket_listen(struct socket *sock, int backlog) 5372 { 5373 return sock_has_perm(sock->sk, SOCKET__LISTEN); 5374 } 5375 5376 static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 5377 { 5378 int err; 5379 struct inode_security_struct *isec; 5380 struct inode_security_struct *newisec; 5381 u16 sclass; 5382 u32 sid; 5383 5384 err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 5385 if (err) 5386 return err; 5387 5388 isec = inode_security_novalidate(SOCK_INODE(sock)); 5389 spin_lock(&isec->lock); 5390 sclass = isec->sclass; 5391 sid = isec->sid; 5392 spin_unlock(&isec->lock); 5393 5394 newisec = inode_security_novalidate(SOCK_INODE(newsock)); 5395 newisec->sclass = sclass; 5396 newisec->sid = sid; 5397 newisec->initialized = LABEL_INITIALIZED; 5398 5399 return 0; 5400 } 5401 5402 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 5403 int size) 5404 { 5405 int rc; 5406 struct sockaddr *const addr = msg->msg_name; 5407 const int addrlen = msg->msg_namelen; 5408 5409 rc = sock_has_perm(sock->sk, SOCKET__WRITE); 5410 if (rc) 5411 return rc; 5412 5413 if (addr && (msg->msg_flags & MSG_FASTOPEN) && 5414 (sk_is_tcp(sock->sk) || 5415 (sk_is_inet(sock->sk) && sock->sk->sk_type == SOCK_STREAM && 5416 sock->sk->sk_protocol == IPPROTO_MPTCP))) { 5417 rc = selinux_socket_connect(sock, addr, addrlen); 5418 if (rc) 5419 return rc; 5420 } 5421 5422 return 0; 5423 } 5424 5425 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 5426 int size, int flags) 5427 { 5428 return sock_has_perm(sock->sk, SOCKET__READ); 5429 } 5430 5431 static int selinux_socket_getsockname(struct socket *sock) 5432 { 5433 return sock_has_perm(sock->sk, SOCKET__GETATTR); 5434 } 5435 5436 static int selinux_socket_getpeername(struct socket *sock) 5437 { 5438 return sock_has_perm(sock->sk, SOCKET__GETATTR); 5439 } 5440 5441 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 5442 { 5443 int err; 5444 5445 err = sock_has_perm(sock->sk, SOCKET__SETOPT); 5446 if (err) 5447 return err; 5448 5449 return selinux_netlbl_socket_setsockopt(sock, level, optname); 5450 } 5451 5452 static int selinux_socket_getsockopt(struct socket *sock, int level, 5453 int optname) 5454 { 5455 return sock_has_perm(sock->sk, SOCKET__GETOPT); 5456 } 5457 5458 static int selinux_socket_shutdown(struct socket *sock, int how) 5459 { 5460 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 5461 } 5462 5463 static int selinux_socket_unix_stream_connect(struct sock *sock, 5464 struct sock *other, 5465 struct sock *newsk) 5466 { 5467 struct sk_security_struct *sksec_sock = selinux_sock(sock); 5468 struct sk_security_struct *sksec_other = selinux_sock(other); 5469 struct sk_security_struct *sksec_new = selinux_sock(newsk); 5470 struct common_audit_data ad; 5471 struct lsm_network_audit net; 5472 int err; 5473 5474 ad_net_init_from_sk(&ad, &net, other); 5475 5476 err = avc_has_perm(sksec_sock->sid, sksec_other->sid, 5477 sksec_other->sclass, 5478 UNIX_STREAM_SOCKET__CONNECTTO, &ad); 5479 if (err) 5480 return err; 5481 5482 /* server child socket */ 5483 sksec_new->peer_sid = sksec_sock->sid; 5484 err = security_sid_mls_copy(sksec_other->sid, 5485 sksec_sock->sid, &sksec_new->sid); 5486 if (err) 5487 return err; 5488 5489 /* connecting socket */ 5490 sksec_sock->peer_sid = sksec_new->sid; 5491 5492 return 0; 5493 } 5494 5495 static int selinux_socket_unix_may_send(struct socket *sock, 5496 struct socket *other) 5497 { 5498 struct sk_security_struct *ssec = selinux_sock(sock->sk); 5499 struct sk_security_struct *osec = selinux_sock(other->sk); 5500 struct common_audit_data ad; 5501 struct lsm_network_audit net; 5502 5503 ad_net_init_from_sk(&ad, &net, other->sk); 5504 5505 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 5506 &ad); 5507 } 5508 5509 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 5510 char *addrp, u16 family, u32 peer_sid, 5511 struct common_audit_data *ad) 5512 { 5513 int err; 5514 u32 if_sid; 5515 u32 node_sid; 5516 5517 err = sel_netif_sid(ns, ifindex, &if_sid); 5518 if (err) 5519 return err; 5520 err = avc_has_perm(peer_sid, if_sid, 5521 SECCLASS_NETIF, NETIF__INGRESS, ad); 5522 if (err) 5523 return err; 5524 5525 err = sel_netnode_sid(addrp, family, &node_sid); 5526 if (err) 5527 return err; 5528 return avc_has_perm(peer_sid, node_sid, 5529 SECCLASS_NODE, NODE__RECVFROM, ad); 5530 } 5531 5532 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 5533 u16 family) 5534 { 5535 int err = 0; 5536 struct sk_security_struct *sksec = selinux_sock(sk); 5537 u32 sk_sid = sksec->sid; 5538 struct common_audit_data ad; 5539 struct lsm_network_audit net; 5540 char *addrp; 5541 5542 ad_net_init_from_iif(&ad, &net, skb->skb_iif, family); 5543 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 5544 if (err) 5545 return err; 5546 5547 if (selinux_secmark_enabled()) { 5548 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 5549 PACKET__RECV, &ad); 5550 if (err) 5551 return err; 5552 } 5553 5554 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 5555 if (err) 5556 return err; 5557 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 5558 5559 return err; 5560 } 5561 5562 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 5563 { 5564 int err, peerlbl_active, secmark_active; 5565 struct sk_security_struct *sksec = selinux_sock(sk); 5566 u16 family = sk->sk_family; 5567 u32 sk_sid = sksec->sid; 5568 struct common_audit_data ad; 5569 struct lsm_network_audit net; 5570 char *addrp; 5571 5572 if (family != PF_INET && family != PF_INET6) 5573 return 0; 5574 5575 /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 5576 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5577 family = PF_INET; 5578 5579 /* If any sort of compatibility mode is enabled then handoff processing 5580 * to the selinux_sock_rcv_skb_compat() function to deal with the 5581 * special handling. We do this in an attempt to keep this function 5582 * as fast and as clean as possible. */ 5583 if (!selinux_policycap_netpeer()) 5584 return selinux_sock_rcv_skb_compat(sk, skb, family); 5585 5586 secmark_active = selinux_secmark_enabled(); 5587 peerlbl_active = selinux_peerlbl_enabled(); 5588 if (!secmark_active && !peerlbl_active) 5589 return 0; 5590 5591 ad_net_init_from_iif(&ad, &net, skb->skb_iif, family); 5592 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 5593 if (err) 5594 return err; 5595 5596 if (peerlbl_active) { 5597 u32 peer_sid; 5598 5599 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 5600 if (err) 5601 return err; 5602 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 5603 addrp, family, peer_sid, &ad); 5604 if (err) { 5605 selinux_netlbl_err(skb, family, err, 0); 5606 return err; 5607 } 5608 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 5609 PEER__RECV, &ad); 5610 if (err) { 5611 selinux_netlbl_err(skb, family, err, 0); 5612 return err; 5613 } 5614 } 5615 5616 if (secmark_active) { 5617 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 5618 PACKET__RECV, &ad); 5619 if (err) 5620 return err; 5621 } 5622 5623 return err; 5624 } 5625 5626 static int selinux_socket_getpeersec_stream(struct socket *sock, 5627 sockptr_t optval, sockptr_t optlen, 5628 unsigned int len) 5629 { 5630 int err = 0; 5631 char *scontext = NULL; 5632 u32 scontext_len; 5633 struct sk_security_struct *sksec = selinux_sock(sock->sk); 5634 u32 peer_sid = SECSID_NULL; 5635 5636 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 5637 sksec->sclass == SECCLASS_TCP_SOCKET || 5638 sksec->sclass == SECCLASS_SCTP_SOCKET) 5639 peer_sid = sksec->peer_sid; 5640 if (peer_sid == SECSID_NULL) 5641 return -ENOPROTOOPT; 5642 5643 err = security_sid_to_context(peer_sid, &scontext, 5644 &scontext_len); 5645 if (err) 5646 return err; 5647 if (scontext_len > len) { 5648 err = -ERANGE; 5649 goto out_len; 5650 } 5651 5652 if (copy_to_sockptr(optval, scontext, scontext_len)) 5653 err = -EFAULT; 5654 out_len: 5655 if (copy_to_sockptr(optlen, &scontext_len, sizeof(scontext_len))) 5656 err = -EFAULT; 5657 kfree(scontext); 5658 return err; 5659 } 5660 5661 static int selinux_socket_getpeersec_dgram(struct socket *sock, 5662 struct sk_buff *skb, u32 *secid) 5663 { 5664 u32 peer_secid = SECSID_NULL; 5665 u16 family; 5666 5667 if (skb && skb->protocol == htons(ETH_P_IP)) 5668 family = PF_INET; 5669 else if (skb && skb->protocol == htons(ETH_P_IPV6)) 5670 family = PF_INET6; 5671 else if (sock) 5672 family = sock->sk->sk_family; 5673 else { 5674 *secid = SECSID_NULL; 5675 return -EINVAL; 5676 } 5677 5678 if (sock && family == PF_UNIX) { 5679 struct inode_security_struct *isec; 5680 isec = inode_security_novalidate(SOCK_INODE(sock)); 5681 peer_secid = isec->sid; 5682 } else if (skb) 5683 selinux_skb_peerlbl_sid(skb, family, &peer_secid); 5684 5685 *secid = peer_secid; 5686 if (peer_secid == SECSID_NULL) 5687 return -ENOPROTOOPT; 5688 return 0; 5689 } 5690 5691 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 5692 { 5693 struct sk_security_struct *sksec = selinux_sock(sk); 5694 5695 sksec->peer_sid = SECINITSID_UNLABELED; 5696 sksec->sid = SECINITSID_UNLABELED; 5697 sksec->sclass = SECCLASS_SOCKET; 5698 selinux_netlbl_sk_security_reset(sksec); 5699 5700 return 0; 5701 } 5702 5703 static void selinux_sk_free_security(struct sock *sk) 5704 { 5705 struct sk_security_struct *sksec = selinux_sock(sk); 5706 5707 selinux_netlbl_sk_security_free(sksec); 5708 } 5709 5710 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 5711 { 5712 struct sk_security_struct *sksec = selinux_sock(sk); 5713 struct sk_security_struct *newsksec = selinux_sock(newsk); 5714 5715 newsksec->sid = sksec->sid; 5716 newsksec->peer_sid = sksec->peer_sid; 5717 newsksec->sclass = sksec->sclass; 5718 5719 selinux_netlbl_sk_security_reset(newsksec); 5720 } 5721 5722 static void selinux_sk_getsecid(const struct sock *sk, u32 *secid) 5723 { 5724 if (!sk) 5725 *secid = SECINITSID_ANY_SOCKET; 5726 else { 5727 const struct sk_security_struct *sksec = selinux_sock(sk); 5728 5729 *secid = sksec->sid; 5730 } 5731 } 5732 5733 static void selinux_sock_graft(struct sock *sk, struct socket *parent) 5734 { 5735 struct inode_security_struct *isec = 5736 inode_security_novalidate(SOCK_INODE(parent)); 5737 struct sk_security_struct *sksec = selinux_sock(sk); 5738 5739 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 5740 sk->sk_family == PF_UNIX) 5741 isec->sid = sksec->sid; 5742 sksec->sclass = isec->sclass; 5743 } 5744 5745 /* 5746 * Determines peer_secid for the asoc and updates socket's peer label 5747 * if it's the first association on the socket. 5748 */ 5749 static int selinux_sctp_process_new_assoc(struct sctp_association *asoc, 5750 struct sk_buff *skb) 5751 { 5752 struct sock *sk = asoc->base.sk; 5753 u16 family = sk->sk_family; 5754 struct sk_security_struct *sksec = selinux_sock(sk); 5755 struct common_audit_data ad; 5756 struct lsm_network_audit net; 5757 int err; 5758 5759 /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5760 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5761 family = PF_INET; 5762 5763 if (selinux_peerlbl_enabled()) { 5764 asoc->peer_secid = SECSID_NULL; 5765 5766 /* This will return peer_sid = SECSID_NULL if there are 5767 * no peer labels, see security_net_peersid_resolve(). 5768 */ 5769 err = selinux_skb_peerlbl_sid(skb, family, &asoc->peer_secid); 5770 if (err) 5771 return err; 5772 5773 if (asoc->peer_secid == SECSID_NULL) 5774 asoc->peer_secid = SECINITSID_UNLABELED; 5775 } else { 5776 asoc->peer_secid = SECINITSID_UNLABELED; 5777 } 5778 5779 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 5780 sksec->sctp_assoc_state = SCTP_ASSOC_SET; 5781 5782 /* Here as first association on socket. As the peer SID 5783 * was allowed by peer recv (and the netif/node checks), 5784 * then it is approved by policy and used as the primary 5785 * peer SID for getpeercon(3). 5786 */ 5787 sksec->peer_sid = asoc->peer_secid; 5788 } else if (sksec->peer_sid != asoc->peer_secid) { 5789 /* Other association peer SIDs are checked to enforce 5790 * consistency among the peer SIDs. 5791 */ 5792 ad_net_init_from_sk(&ad, &net, asoc->base.sk); 5793 err = avc_has_perm(sksec->peer_sid, asoc->peer_secid, 5794 sksec->sclass, SCTP_SOCKET__ASSOCIATION, 5795 &ad); 5796 if (err) 5797 return err; 5798 } 5799 return 0; 5800 } 5801 5802 /* Called whenever SCTP receives an INIT or COOKIE ECHO chunk. This 5803 * happens on an incoming connect(2), sctp_connectx(3) or 5804 * sctp_sendmsg(3) (with no association already present). 5805 */ 5806 static int selinux_sctp_assoc_request(struct sctp_association *asoc, 5807 struct sk_buff *skb) 5808 { 5809 struct sk_security_struct *sksec = selinux_sock(asoc->base.sk); 5810 u32 conn_sid; 5811 int err; 5812 5813 if (!selinux_policycap_extsockclass()) 5814 return 0; 5815 5816 err = selinux_sctp_process_new_assoc(asoc, skb); 5817 if (err) 5818 return err; 5819 5820 /* Compute the MLS component for the connection and store 5821 * the information in asoc. This will be used by SCTP TCP type 5822 * sockets and peeled off connections as they cause a new 5823 * socket to be generated. selinux_sctp_sk_clone() will then 5824 * plug this into the new socket. 5825 */ 5826 err = selinux_conn_sid(sksec->sid, asoc->peer_secid, &conn_sid); 5827 if (err) 5828 return err; 5829 5830 asoc->secid = conn_sid; 5831 5832 /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 5833 return selinux_netlbl_sctp_assoc_request(asoc, skb); 5834 } 5835 5836 /* Called when SCTP receives a COOKIE ACK chunk as the final 5837 * response to an association request (initited by us). 5838 */ 5839 static int selinux_sctp_assoc_established(struct sctp_association *asoc, 5840 struct sk_buff *skb) 5841 { 5842 struct sk_security_struct *sksec = selinux_sock(asoc->base.sk); 5843 5844 if (!selinux_policycap_extsockclass()) 5845 return 0; 5846 5847 /* Inherit secid from the parent socket - this will be picked up 5848 * by selinux_sctp_sk_clone() if the association gets peeled off 5849 * into a new socket. 5850 */ 5851 asoc->secid = sksec->sid; 5852 5853 return selinux_sctp_process_new_assoc(asoc, skb); 5854 } 5855 5856 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5857 * based on their @optname. 5858 */ 5859 static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5860 struct sockaddr *address, 5861 int addrlen) 5862 { 5863 int len, err = 0, walk_size = 0; 5864 void *addr_buf; 5865 struct sockaddr *addr; 5866 5867 if (!selinux_policycap_extsockclass()) 5868 return 0; 5869 5870 /* Process one or more addresses that may be IPv4 or IPv6 */ 5871 addr_buf = address; 5872 5873 while (walk_size < addrlen) { 5874 if (walk_size + sizeof(sa_family_t) > addrlen) 5875 return -EINVAL; 5876 5877 addr = addr_buf; 5878 switch (addr->sa_family) { 5879 case AF_UNSPEC: 5880 case AF_INET: 5881 len = sizeof(struct sockaddr_in); 5882 break; 5883 case AF_INET6: 5884 len = sizeof(struct sockaddr_in6); 5885 break; 5886 default: 5887 return -EINVAL; 5888 } 5889 5890 if (walk_size + len > addrlen) 5891 return -EINVAL; 5892 5893 err = -EINVAL; 5894 switch (optname) { 5895 /* Bind checks */ 5896 case SCTP_PRIMARY_ADDR: 5897 case SCTP_SET_PEER_PRIMARY_ADDR: 5898 case SCTP_SOCKOPT_BINDX_ADD: 5899 err = __selinux_socket_bind(sk, addr, len); 5900 break; 5901 /* Connect checks */ 5902 case SCTP_SOCKOPT_CONNECTX: 5903 case SCTP_PARAM_SET_PRIMARY: 5904 case SCTP_PARAM_ADD_IP: 5905 case SCTP_SENDMSG_CONNECT: 5906 err = selinux_socket_connect_helper(sk, addr, len); 5907 if (err) 5908 return err; 5909 5910 /* As selinux_sctp_bind_connect() is called by the 5911 * SCTP protocol layer, the socket is already locked, 5912 * therefore selinux_netlbl_socket_connect_locked() 5913 * is called here. The situations handled are: 5914 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5915 * whenever a new IP address is added or when a new 5916 * primary address is selected. 5917 * Note that an SCTP connect(2) call happens before 5918 * the SCTP protocol layer and is handled via 5919 * selinux_socket_connect(). 5920 */ 5921 err = selinux_netlbl_socket_connect_locked(sk, addr); 5922 break; 5923 } 5924 5925 if (err) 5926 return err; 5927 5928 addr_buf += len; 5929 walk_size += len; 5930 } 5931 5932 return 0; 5933 } 5934 5935 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5936 static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk, 5937 struct sock *newsk) 5938 { 5939 struct sk_security_struct *sksec = selinux_sock(sk); 5940 struct sk_security_struct *newsksec = selinux_sock(newsk); 5941 5942 /* If policy does not support SECCLASS_SCTP_SOCKET then call 5943 * the non-sctp clone version. 5944 */ 5945 if (!selinux_policycap_extsockclass()) 5946 return selinux_sk_clone_security(sk, newsk); 5947 5948 newsksec->sid = asoc->secid; 5949 newsksec->peer_sid = asoc->peer_secid; 5950 newsksec->sclass = sksec->sclass; 5951 selinux_netlbl_sctp_sk_clone(sk, newsk); 5952 } 5953 5954 static int selinux_mptcp_add_subflow(struct sock *sk, struct sock *ssk) 5955 { 5956 struct sk_security_struct *ssksec = selinux_sock(ssk); 5957 struct sk_security_struct *sksec = selinux_sock(sk); 5958 5959 ssksec->sclass = sksec->sclass; 5960 ssksec->sid = sksec->sid; 5961 5962 /* replace the existing subflow label deleting the existing one 5963 * and re-recreating a new label using the updated context 5964 */ 5965 selinux_netlbl_sk_security_free(ssksec); 5966 return selinux_netlbl_socket_post_create(ssk, ssk->sk_family); 5967 } 5968 5969 static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb, 5970 struct request_sock *req) 5971 { 5972 struct sk_security_struct *sksec = selinux_sock(sk); 5973 int err; 5974 u16 family = req->rsk_ops->family; 5975 u32 connsid; 5976 u32 peersid; 5977 5978 err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5979 if (err) 5980 return err; 5981 err = selinux_conn_sid(sksec->sid, peersid, &connsid); 5982 if (err) 5983 return err; 5984 req->secid = connsid; 5985 req->peer_secid = peersid; 5986 5987 return selinux_netlbl_inet_conn_request(req, family); 5988 } 5989 5990 static void selinux_inet_csk_clone(struct sock *newsk, 5991 const struct request_sock *req) 5992 { 5993 struct sk_security_struct *newsksec = selinux_sock(newsk); 5994 5995 newsksec->sid = req->secid; 5996 newsksec->peer_sid = req->peer_secid; 5997 /* NOTE: Ideally, we should also get the isec->sid for the 5998 new socket in sync, but we don't have the isec available yet. 5999 So we will wait until sock_graft to do it, by which 6000 time it will have been created and available. */ 6001 6002 /* We don't need to take any sort of lock here as we are the only 6003 * thread with access to newsksec */ 6004 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 6005 } 6006 6007 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 6008 { 6009 u16 family = sk->sk_family; 6010 struct sk_security_struct *sksec = selinux_sock(sk); 6011 6012 /* handle mapped IPv4 packets arriving via IPv6 sockets */ 6013 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 6014 family = PF_INET; 6015 6016 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 6017 } 6018 6019 static int selinux_secmark_relabel_packet(u32 sid) 6020 { 6021 return avc_has_perm(current_sid(), sid, SECCLASS_PACKET, PACKET__RELABELTO, 6022 NULL); 6023 } 6024 6025 static void selinux_secmark_refcount_inc(void) 6026 { 6027 atomic_inc(&selinux_secmark_refcount); 6028 } 6029 6030 static void selinux_secmark_refcount_dec(void) 6031 { 6032 atomic_dec(&selinux_secmark_refcount); 6033 } 6034 6035 static void selinux_req_classify_flow(const struct request_sock *req, 6036 struct flowi_common *flic) 6037 { 6038 flic->flowic_secid = req->secid; 6039 } 6040 6041 static int selinux_tun_dev_alloc_security(void *security) 6042 { 6043 struct tun_security_struct *tunsec = selinux_tun_dev(security); 6044 6045 tunsec->sid = current_sid(); 6046 return 0; 6047 } 6048 6049 static int selinux_tun_dev_create(void) 6050 { 6051 u32 sid = current_sid(); 6052 6053 /* we aren't taking into account the "sockcreate" SID since the socket 6054 * that is being created here is not a socket in the traditional sense, 6055 * instead it is a private sock, accessible only to the kernel, and 6056 * representing a wide range of network traffic spanning multiple 6057 * connections unlike traditional sockets - check the TUN driver to 6058 * get a better understanding of why this socket is special */ 6059 6060 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 6061 NULL); 6062 } 6063 6064 static int selinux_tun_dev_attach_queue(void *security) 6065 { 6066 struct tun_security_struct *tunsec = selinux_tun_dev(security); 6067 6068 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 6069 TUN_SOCKET__ATTACH_QUEUE, NULL); 6070 } 6071 6072 static int selinux_tun_dev_attach(struct sock *sk, void *security) 6073 { 6074 struct tun_security_struct *tunsec = selinux_tun_dev(security); 6075 struct sk_security_struct *sksec = selinux_sock(sk); 6076 6077 /* we don't currently perform any NetLabel based labeling here and it 6078 * isn't clear that we would want to do so anyway; while we could apply 6079 * labeling without the support of the TUN user the resulting labeled 6080 * traffic from the other end of the connection would almost certainly 6081 * cause confusion to the TUN user that had no idea network labeling 6082 * protocols were being used */ 6083 6084 sksec->sid = tunsec->sid; 6085 sksec->sclass = SECCLASS_TUN_SOCKET; 6086 6087 return 0; 6088 } 6089 6090 static int selinux_tun_dev_open(void *security) 6091 { 6092 struct tun_security_struct *tunsec = selinux_tun_dev(security); 6093 u32 sid = current_sid(); 6094 int err; 6095 6096 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET, 6097 TUN_SOCKET__RELABELFROM, NULL); 6098 if (err) 6099 return err; 6100 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 6101 TUN_SOCKET__RELABELTO, NULL); 6102 if (err) 6103 return err; 6104 tunsec->sid = sid; 6105 6106 return 0; 6107 } 6108 6109 #ifdef CONFIG_NETFILTER 6110 6111 static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb, 6112 const struct nf_hook_state *state) 6113 { 6114 int ifindex; 6115 u16 family; 6116 char *addrp; 6117 u32 peer_sid; 6118 struct common_audit_data ad; 6119 struct lsm_network_audit net; 6120 int secmark_active, peerlbl_active; 6121 6122 if (!selinux_policycap_netpeer()) 6123 return NF_ACCEPT; 6124 6125 secmark_active = selinux_secmark_enabled(); 6126 peerlbl_active = selinux_peerlbl_enabled(); 6127 if (!secmark_active && !peerlbl_active) 6128 return NF_ACCEPT; 6129 6130 family = state->pf; 6131 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 6132 return NF_DROP; 6133 6134 ifindex = state->in->ifindex; 6135 ad_net_init_from_iif(&ad, &net, ifindex, family); 6136 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 6137 return NF_DROP; 6138 6139 if (peerlbl_active) { 6140 int err; 6141 6142 err = selinux_inet_sys_rcv_skb(state->net, ifindex, 6143 addrp, family, peer_sid, &ad); 6144 if (err) { 6145 selinux_netlbl_err(skb, family, err, 1); 6146 return NF_DROP; 6147 } 6148 } 6149 6150 if (secmark_active) 6151 if (avc_has_perm(peer_sid, skb->secmark, 6152 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 6153 return NF_DROP; 6154 6155 if (netlbl_enabled()) 6156 /* we do this in the FORWARD path and not the POST_ROUTING 6157 * path because we want to make sure we apply the necessary 6158 * labeling before IPsec is applied so we can leverage AH 6159 * protection */ 6160 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 6161 return NF_DROP; 6162 6163 return NF_ACCEPT; 6164 } 6165 6166 static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb, 6167 const struct nf_hook_state *state) 6168 { 6169 struct sock *sk; 6170 u32 sid; 6171 6172 if (!netlbl_enabled()) 6173 return NF_ACCEPT; 6174 6175 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 6176 * because we want to make sure we apply the necessary labeling 6177 * before IPsec is applied so we can leverage AH protection */ 6178 sk = skb_to_full_sk(skb); 6179 if (sk) { 6180 struct sk_security_struct *sksec; 6181 6182 if (sk_listener(sk)) 6183 /* if the socket is the listening state then this 6184 * packet is a SYN-ACK packet which means it needs to 6185 * be labeled based on the connection/request_sock and 6186 * not the parent socket. unfortunately, we can't 6187 * lookup the request_sock yet as it isn't queued on 6188 * the parent socket until after the SYN-ACK is sent. 6189 * the "solution" is to simply pass the packet as-is 6190 * as any IP option based labeling should be copied 6191 * from the initial connection request (in the IP 6192 * layer). it is far from ideal, but until we get a 6193 * security label in the packet itself this is the 6194 * best we can do. */ 6195 return NF_ACCEPT; 6196 6197 /* standard practice, label using the parent socket */ 6198 sksec = selinux_sock(sk); 6199 sid = sksec->sid; 6200 } else 6201 sid = SECINITSID_KERNEL; 6202 if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0) 6203 return NF_DROP; 6204 6205 return NF_ACCEPT; 6206 } 6207 6208 6209 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 6210 const struct nf_hook_state *state) 6211 { 6212 struct sock *sk; 6213 struct sk_security_struct *sksec; 6214 struct common_audit_data ad; 6215 struct lsm_network_audit net; 6216 u8 proto = 0; 6217 6218 sk = skb_to_full_sk(skb); 6219 if (sk == NULL) 6220 return NF_ACCEPT; 6221 sksec = selinux_sock(sk); 6222 6223 ad_net_init_from_iif(&ad, &net, state->out->ifindex, state->pf); 6224 if (selinux_parse_skb(skb, &ad, NULL, 0, &proto)) 6225 return NF_DROP; 6226 6227 if (selinux_secmark_enabled()) 6228 if (avc_has_perm(sksec->sid, skb->secmark, 6229 SECCLASS_PACKET, PACKET__SEND, &ad)) 6230 return NF_DROP_ERR(-ECONNREFUSED); 6231 6232 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 6233 return NF_DROP_ERR(-ECONNREFUSED); 6234 6235 return NF_ACCEPT; 6236 } 6237 6238 static unsigned int selinux_ip_postroute(void *priv, 6239 struct sk_buff *skb, 6240 const struct nf_hook_state *state) 6241 { 6242 u16 family; 6243 u32 secmark_perm; 6244 u32 peer_sid; 6245 int ifindex; 6246 struct sock *sk; 6247 struct common_audit_data ad; 6248 struct lsm_network_audit net; 6249 char *addrp; 6250 int secmark_active, peerlbl_active; 6251 6252 /* If any sort of compatibility mode is enabled then handoff processing 6253 * to the selinux_ip_postroute_compat() function to deal with the 6254 * special handling. We do this in an attempt to keep this function 6255 * as fast and as clean as possible. */ 6256 if (!selinux_policycap_netpeer()) 6257 return selinux_ip_postroute_compat(skb, state); 6258 6259 secmark_active = selinux_secmark_enabled(); 6260 peerlbl_active = selinux_peerlbl_enabled(); 6261 if (!secmark_active && !peerlbl_active) 6262 return NF_ACCEPT; 6263 6264 sk = skb_to_full_sk(skb); 6265 6266 #ifdef CONFIG_XFRM 6267 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 6268 * packet transformation so allow the packet to pass without any checks 6269 * since we'll have another chance to perform access control checks 6270 * when the packet is on it's final way out. 6271 * NOTE: there appear to be some IPv6 multicast cases where skb->dst 6272 * is NULL, in this case go ahead and apply access control. 6273 * NOTE: if this is a local socket (skb->sk != NULL) that is in the 6274 * TCP listening state we cannot wait until the XFRM processing 6275 * is done as we will miss out on the SA label if we do; 6276 * unfortunately, this means more work, but it is only once per 6277 * connection. */ 6278 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 6279 !(sk && sk_listener(sk))) 6280 return NF_ACCEPT; 6281 #endif 6282 6283 family = state->pf; 6284 if (sk == NULL) { 6285 /* Without an associated socket the packet is either coming 6286 * from the kernel or it is being forwarded; check the packet 6287 * to determine which and if the packet is being forwarded 6288 * query the packet directly to determine the security label. */ 6289 if (skb->skb_iif) { 6290 secmark_perm = PACKET__FORWARD_OUT; 6291 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 6292 return NF_DROP; 6293 } else { 6294 secmark_perm = PACKET__SEND; 6295 peer_sid = SECINITSID_KERNEL; 6296 } 6297 } else if (sk_listener(sk)) { 6298 /* Locally generated packet but the associated socket is in the 6299 * listening state which means this is a SYN-ACK packet. In 6300 * this particular case the correct security label is assigned 6301 * to the connection/request_sock but unfortunately we can't 6302 * query the request_sock as it isn't queued on the parent 6303 * socket until after the SYN-ACK packet is sent; the only 6304 * viable choice is to regenerate the label like we do in 6305 * selinux_inet_conn_request(). See also selinux_ip_output() 6306 * for similar problems. */ 6307 u32 skb_sid; 6308 struct sk_security_struct *sksec; 6309 6310 sksec = selinux_sock(sk); 6311 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 6312 return NF_DROP; 6313 /* At this point, if the returned skb peerlbl is SECSID_NULL 6314 * and the packet has been through at least one XFRM 6315 * transformation then we must be dealing with the "final" 6316 * form of labeled IPsec packet; since we've already applied 6317 * all of our access controls on this packet we can safely 6318 * pass the packet. */ 6319 if (skb_sid == SECSID_NULL) { 6320 switch (family) { 6321 case PF_INET: 6322 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 6323 return NF_ACCEPT; 6324 break; 6325 case PF_INET6: 6326 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 6327 return NF_ACCEPT; 6328 break; 6329 default: 6330 return NF_DROP_ERR(-ECONNREFUSED); 6331 } 6332 } 6333 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 6334 return NF_DROP; 6335 secmark_perm = PACKET__SEND; 6336 } else { 6337 /* Locally generated packet, fetch the security label from the 6338 * associated socket. */ 6339 struct sk_security_struct *sksec = selinux_sock(sk); 6340 peer_sid = sksec->sid; 6341 secmark_perm = PACKET__SEND; 6342 } 6343 6344 ifindex = state->out->ifindex; 6345 ad_net_init_from_iif(&ad, &net, ifindex, family); 6346 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 6347 return NF_DROP; 6348 6349 if (secmark_active) 6350 if (avc_has_perm(peer_sid, skb->secmark, 6351 SECCLASS_PACKET, secmark_perm, &ad)) 6352 return NF_DROP_ERR(-ECONNREFUSED); 6353 6354 if (peerlbl_active) { 6355 u32 if_sid; 6356 u32 node_sid; 6357 6358 if (sel_netif_sid(state->net, ifindex, &if_sid)) 6359 return NF_DROP; 6360 if (avc_has_perm(peer_sid, if_sid, 6361 SECCLASS_NETIF, NETIF__EGRESS, &ad)) 6362 return NF_DROP_ERR(-ECONNREFUSED); 6363 6364 if (sel_netnode_sid(addrp, family, &node_sid)) 6365 return NF_DROP; 6366 if (avc_has_perm(peer_sid, node_sid, 6367 SECCLASS_NODE, NODE__SENDTO, &ad)) 6368 return NF_DROP_ERR(-ECONNREFUSED); 6369 } 6370 6371 return NF_ACCEPT; 6372 } 6373 #endif /* CONFIG_NETFILTER */ 6374 6375 static int nlmsg_sock_has_extended_perms(struct sock *sk, u32 perms, u16 nlmsg_type) 6376 { 6377 struct sk_security_struct *sksec = selinux_sock(sk); 6378 struct common_audit_data ad; 6379 u8 driver; 6380 u8 xperm; 6381 6382 if (sock_skip_has_perm(sksec->sid)) 6383 return 0; 6384 6385 ad.type = LSM_AUDIT_DATA_NLMSGTYPE; 6386 ad.u.nlmsg_type = nlmsg_type; 6387 6388 driver = nlmsg_type >> 8; 6389 xperm = nlmsg_type & 0xff; 6390 6391 return avc_has_extended_perms(current_sid(), sksec->sid, sksec->sclass, 6392 perms, driver, AVC_EXT_NLMSG, xperm, &ad); 6393 } 6394 6395 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 6396 { 6397 int rc = 0; 6398 unsigned int msg_len; 6399 unsigned int data_len = skb->len; 6400 unsigned char *data = skb->data; 6401 struct nlmsghdr *nlh; 6402 struct sk_security_struct *sksec = selinux_sock(sk); 6403 u16 sclass = sksec->sclass; 6404 u32 perm; 6405 6406 while (data_len >= nlmsg_total_size(0)) { 6407 nlh = (struct nlmsghdr *)data; 6408 6409 /* NOTE: the nlmsg_len field isn't reliably set by some netlink 6410 * users which means we can't reject skb's with bogus 6411 * length fields; our solution is to follow what 6412 * netlink_rcv_skb() does and simply skip processing at 6413 * messages with length fields that are clearly junk 6414 */ 6415 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len) 6416 return 0; 6417 6418 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm); 6419 if (rc == 0) { 6420 if (selinux_policycap_netlink_xperm()) { 6421 rc = nlmsg_sock_has_extended_perms( 6422 sk, perm, nlh->nlmsg_type); 6423 } else { 6424 rc = sock_has_perm(sk, perm); 6425 } 6426 if (rc) 6427 return rc; 6428 } else if (rc == -EINVAL) { 6429 /* -EINVAL is a missing msg/perm mapping */ 6430 if (sclass == SECCLASS_NETLINK_TCPDIAG_SOCKET && 6431 nlh->nlmsg_type == DCCPDIAG_GETSOCK) 6432 pr_warn_once("SELinux: DCCP has been removed, pid=%d comm=%s\n", 6433 task_pid_nr(current), current->comm); 6434 else 6435 pr_warn_ratelimited("SELinux: unrecognized netlink" 6436 " message: protocol=%hu nlmsg_type=%hu sclass=%s" 6437 " pid=%d comm=%s\n", 6438 sk->sk_protocol, nlh->nlmsg_type, 6439 secclass_map[sclass - 1].name, 6440 task_pid_nr(current), current->comm); 6441 if (enforcing_enabled() && 6442 !security_get_allow_unknown()) 6443 return rc; 6444 rc = 0; 6445 } else if (rc == -ENOENT) { 6446 /* -ENOENT is a missing socket/class mapping, ignore */ 6447 rc = 0; 6448 } else { 6449 return rc; 6450 } 6451 6452 /* move to the next message after applying netlink padding */ 6453 msg_len = NLMSG_ALIGN(nlh->nlmsg_len); 6454 if (msg_len >= data_len) 6455 return 0; 6456 data_len -= msg_len; 6457 data += msg_len; 6458 } 6459 6460 return rc; 6461 } 6462 6463 static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass) 6464 { 6465 isec->sclass = sclass; 6466 isec->sid = current_sid(); 6467 } 6468 6469 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 6470 u32 perms) 6471 { 6472 struct ipc_security_struct *isec; 6473 struct common_audit_data ad; 6474 u32 sid = current_sid(); 6475 6476 isec = selinux_ipc(ipc_perms); 6477 6478 ad.type = LSM_AUDIT_DATA_IPC; 6479 ad.u.ipc_id = ipc_perms->key; 6480 6481 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 6482 } 6483 6484 static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 6485 { 6486 struct msg_security_struct *msec; 6487 6488 msec = selinux_msg_msg(msg); 6489 msec->sid = SECINITSID_UNLABELED; 6490 6491 return 0; 6492 } 6493 6494 /* message queue security operations */ 6495 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 6496 { 6497 struct ipc_security_struct *isec; 6498 struct common_audit_data ad; 6499 u32 sid = current_sid(); 6500 6501 isec = selinux_ipc(msq); 6502 ipc_init_security(isec, SECCLASS_MSGQ); 6503 6504 ad.type = LSM_AUDIT_DATA_IPC; 6505 ad.u.ipc_id = msq->key; 6506 6507 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 6508 MSGQ__CREATE, &ad); 6509 } 6510 6511 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 6512 { 6513 struct ipc_security_struct *isec; 6514 struct common_audit_data ad; 6515 u32 sid = current_sid(); 6516 6517 isec = selinux_ipc(msq); 6518 6519 ad.type = LSM_AUDIT_DATA_IPC; 6520 ad.u.ipc_id = msq->key; 6521 6522 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 6523 MSGQ__ASSOCIATE, &ad); 6524 } 6525 6526 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 6527 { 6528 u32 perms; 6529 6530 switch (cmd) { 6531 case IPC_INFO: 6532 case MSG_INFO: 6533 /* No specific object, just general system-wide information. */ 6534 return avc_has_perm(current_sid(), SECINITSID_KERNEL, 6535 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 6536 case IPC_STAT: 6537 case MSG_STAT: 6538 case MSG_STAT_ANY: 6539 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 6540 break; 6541 case IPC_SET: 6542 perms = MSGQ__SETATTR; 6543 break; 6544 case IPC_RMID: 6545 perms = MSGQ__DESTROY; 6546 break; 6547 default: 6548 return 0; 6549 } 6550 6551 return ipc_has_perm(msq, perms); 6552 } 6553 6554 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 6555 { 6556 struct ipc_security_struct *isec; 6557 struct msg_security_struct *msec; 6558 struct common_audit_data ad; 6559 u32 sid = current_sid(); 6560 int rc; 6561 6562 isec = selinux_ipc(msq); 6563 msec = selinux_msg_msg(msg); 6564 6565 /* 6566 * First time through, need to assign label to the message 6567 */ 6568 if (msec->sid == SECINITSID_UNLABELED) { 6569 /* 6570 * Compute new sid based on current process and 6571 * message queue this message will be stored in 6572 */ 6573 rc = security_transition_sid(sid, isec->sid, 6574 SECCLASS_MSG, NULL, &msec->sid); 6575 if (rc) 6576 return rc; 6577 } 6578 6579 ad.type = LSM_AUDIT_DATA_IPC; 6580 ad.u.ipc_id = msq->key; 6581 6582 /* Can this process write to the queue? */ 6583 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 6584 MSGQ__WRITE, &ad); 6585 if (!rc) 6586 /* Can this process send the message */ 6587 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 6588 MSG__SEND, &ad); 6589 if (!rc) 6590 /* Can the message be put in the queue? */ 6591 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 6592 MSGQ__ENQUEUE, &ad); 6593 6594 return rc; 6595 } 6596 6597 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 6598 struct task_struct *target, 6599 long type, int mode) 6600 { 6601 struct ipc_security_struct *isec; 6602 struct msg_security_struct *msec; 6603 struct common_audit_data ad; 6604 u32 sid = task_sid_obj(target); 6605 int rc; 6606 6607 isec = selinux_ipc(msq); 6608 msec = selinux_msg_msg(msg); 6609 6610 ad.type = LSM_AUDIT_DATA_IPC; 6611 ad.u.ipc_id = msq->key; 6612 6613 rc = avc_has_perm(sid, isec->sid, 6614 SECCLASS_MSGQ, MSGQ__READ, &ad); 6615 if (!rc) 6616 rc = avc_has_perm(sid, msec->sid, 6617 SECCLASS_MSG, MSG__RECEIVE, &ad); 6618 return rc; 6619 } 6620 6621 /* Shared Memory security operations */ 6622 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 6623 { 6624 struct ipc_security_struct *isec; 6625 struct common_audit_data ad; 6626 u32 sid = current_sid(); 6627 6628 isec = selinux_ipc(shp); 6629 ipc_init_security(isec, SECCLASS_SHM); 6630 6631 ad.type = LSM_AUDIT_DATA_IPC; 6632 ad.u.ipc_id = shp->key; 6633 6634 return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 6635 SHM__CREATE, &ad); 6636 } 6637 6638 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 6639 { 6640 struct ipc_security_struct *isec; 6641 struct common_audit_data ad; 6642 u32 sid = current_sid(); 6643 6644 isec = selinux_ipc(shp); 6645 6646 ad.type = LSM_AUDIT_DATA_IPC; 6647 ad.u.ipc_id = shp->key; 6648 6649 return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 6650 SHM__ASSOCIATE, &ad); 6651 } 6652 6653 /* Note, at this point, shp is locked down */ 6654 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 6655 { 6656 u32 perms; 6657 6658 switch (cmd) { 6659 case IPC_INFO: 6660 case SHM_INFO: 6661 /* No specific object, just general system-wide information. */ 6662 return avc_has_perm(current_sid(), SECINITSID_KERNEL, 6663 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 6664 case IPC_STAT: 6665 case SHM_STAT: 6666 case SHM_STAT_ANY: 6667 perms = SHM__GETATTR | SHM__ASSOCIATE; 6668 break; 6669 case IPC_SET: 6670 perms = SHM__SETATTR; 6671 break; 6672 case SHM_LOCK: 6673 case SHM_UNLOCK: 6674 perms = SHM__LOCK; 6675 break; 6676 case IPC_RMID: 6677 perms = SHM__DESTROY; 6678 break; 6679 default: 6680 return 0; 6681 } 6682 6683 return ipc_has_perm(shp, perms); 6684 } 6685 6686 static int selinux_shm_shmat(struct kern_ipc_perm *shp, 6687 char __user *shmaddr, int shmflg) 6688 { 6689 u32 perms; 6690 6691 if (shmflg & SHM_RDONLY) 6692 perms = SHM__READ; 6693 else 6694 perms = SHM__READ | SHM__WRITE; 6695 6696 return ipc_has_perm(shp, perms); 6697 } 6698 6699 /* Semaphore security operations */ 6700 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 6701 { 6702 struct ipc_security_struct *isec; 6703 struct common_audit_data ad; 6704 u32 sid = current_sid(); 6705 6706 isec = selinux_ipc(sma); 6707 ipc_init_security(isec, SECCLASS_SEM); 6708 6709 ad.type = LSM_AUDIT_DATA_IPC; 6710 ad.u.ipc_id = sma->key; 6711 6712 return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 6713 SEM__CREATE, &ad); 6714 } 6715 6716 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 6717 { 6718 struct ipc_security_struct *isec; 6719 struct common_audit_data ad; 6720 u32 sid = current_sid(); 6721 6722 isec = selinux_ipc(sma); 6723 6724 ad.type = LSM_AUDIT_DATA_IPC; 6725 ad.u.ipc_id = sma->key; 6726 6727 return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 6728 SEM__ASSOCIATE, &ad); 6729 } 6730 6731 /* Note, at this point, sma is locked down */ 6732 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 6733 { 6734 int err; 6735 u32 perms; 6736 6737 switch (cmd) { 6738 case IPC_INFO: 6739 case SEM_INFO: 6740 /* No specific object, just general system-wide information. */ 6741 return avc_has_perm(current_sid(), SECINITSID_KERNEL, 6742 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 6743 case GETPID: 6744 case GETNCNT: 6745 case GETZCNT: 6746 perms = SEM__GETATTR; 6747 break; 6748 case GETVAL: 6749 case GETALL: 6750 perms = SEM__READ; 6751 break; 6752 case SETVAL: 6753 case SETALL: 6754 perms = SEM__WRITE; 6755 break; 6756 case IPC_RMID: 6757 perms = SEM__DESTROY; 6758 break; 6759 case IPC_SET: 6760 perms = SEM__SETATTR; 6761 break; 6762 case IPC_STAT: 6763 case SEM_STAT: 6764 case SEM_STAT_ANY: 6765 perms = SEM__GETATTR | SEM__ASSOCIATE; 6766 break; 6767 default: 6768 return 0; 6769 } 6770 6771 err = ipc_has_perm(sma, perms); 6772 return err; 6773 } 6774 6775 static int selinux_sem_semop(struct kern_ipc_perm *sma, 6776 struct sembuf *sops, unsigned nsops, int alter) 6777 { 6778 u32 perms; 6779 6780 if (alter) 6781 perms = SEM__READ | SEM__WRITE; 6782 else 6783 perms = SEM__READ; 6784 6785 return ipc_has_perm(sma, perms); 6786 } 6787 6788 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 6789 { 6790 u32 av = 0; 6791 6792 av = 0; 6793 if (flag & S_IRUGO) 6794 av |= IPC__UNIX_READ; 6795 if (flag & S_IWUGO) 6796 av |= IPC__UNIX_WRITE; 6797 6798 if (av == 0) 6799 return 0; 6800 6801 return ipc_has_perm(ipcp, av); 6802 } 6803 6804 static void selinux_ipc_getlsmprop(struct kern_ipc_perm *ipcp, 6805 struct lsm_prop *prop) 6806 { 6807 struct ipc_security_struct *isec = selinux_ipc(ipcp); 6808 prop->selinux.secid = isec->sid; 6809 } 6810 6811 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 6812 { 6813 if (inode) 6814 inode_doinit_with_dentry(inode, dentry); 6815 } 6816 6817 static int selinux_lsm_getattr(unsigned int attr, struct task_struct *p, 6818 char **value) 6819 { 6820 const struct cred_security_struct *crsec; 6821 int error; 6822 u32 sid; 6823 u32 len; 6824 6825 rcu_read_lock(); 6826 crsec = selinux_cred(__task_cred(p)); 6827 if (p != current) { 6828 error = avc_has_perm(current_sid(), crsec->sid, 6829 SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6830 if (error) 6831 goto err_unlock; 6832 } 6833 switch (attr) { 6834 case LSM_ATTR_CURRENT: 6835 sid = crsec->sid; 6836 break; 6837 case LSM_ATTR_PREV: 6838 sid = crsec->osid; 6839 break; 6840 case LSM_ATTR_EXEC: 6841 sid = crsec->exec_sid; 6842 break; 6843 case LSM_ATTR_FSCREATE: 6844 sid = crsec->create_sid; 6845 break; 6846 case LSM_ATTR_KEYCREATE: 6847 sid = crsec->keycreate_sid; 6848 break; 6849 case LSM_ATTR_SOCKCREATE: 6850 sid = crsec->sockcreate_sid; 6851 break; 6852 default: 6853 error = -EOPNOTSUPP; 6854 goto err_unlock; 6855 } 6856 rcu_read_unlock(); 6857 6858 if (sid == SECSID_NULL) { 6859 *value = NULL; 6860 return 0; 6861 } 6862 6863 error = security_sid_to_context(sid, value, &len); 6864 if (error) 6865 return error; 6866 return len; 6867 6868 err_unlock: 6869 rcu_read_unlock(); 6870 return error; 6871 } 6872 6873 static int selinux_lsm_setattr(u64 attr, void *value, size_t size) 6874 { 6875 struct cred_security_struct *crsec; 6876 struct cred *new; 6877 u32 mysid = current_sid(), sid = 0, ptsid; 6878 int error; 6879 char *str = value; 6880 6881 /* 6882 * Basic control over ability to set these attributes at all. 6883 */ 6884 switch (attr) { 6885 case LSM_ATTR_EXEC: 6886 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 6887 PROCESS__SETEXEC, NULL); 6888 break; 6889 case LSM_ATTR_FSCREATE: 6890 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 6891 PROCESS__SETFSCREATE, NULL); 6892 break; 6893 case LSM_ATTR_KEYCREATE: 6894 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 6895 PROCESS__SETKEYCREATE, NULL); 6896 break; 6897 case LSM_ATTR_SOCKCREATE: 6898 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 6899 PROCESS__SETSOCKCREATE, NULL); 6900 break; 6901 case LSM_ATTR_CURRENT: 6902 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 6903 PROCESS__SETCURRENT, NULL); 6904 break; 6905 default: 6906 error = -EOPNOTSUPP; 6907 break; 6908 } 6909 if (error) 6910 return error; 6911 6912 /* Obtain a SID for the context, if one was specified. */ 6913 if (size && str[0] && str[0] != '\n') { 6914 if (str[size-1] == '\n') { 6915 str[size-1] = 0; 6916 size--; 6917 } 6918 error = security_context_to_sid(value, size, 6919 &sid, GFP_KERNEL); 6920 if (error == -EINVAL && attr == LSM_ATTR_FSCREATE) { 6921 if (!has_cap_mac_admin(true)) { 6922 struct audit_buffer *ab; 6923 size_t audit_size; 6924 6925 /* We strip a nul only if it is at the end, 6926 * otherwise the context contains a nul and 6927 * we should audit that */ 6928 if (str[size - 1] == '\0') 6929 audit_size = size - 1; 6930 else 6931 audit_size = size; 6932 ab = audit_log_start(audit_context(), 6933 GFP_ATOMIC, 6934 AUDIT_SELINUX_ERR); 6935 if (!ab) 6936 return error; 6937 audit_log_format(ab, "op=fscreate invalid_context="); 6938 audit_log_n_untrustedstring(ab, value, 6939 audit_size); 6940 audit_log_end(ab); 6941 6942 return error; 6943 } 6944 error = security_context_to_sid_force(value, size, 6945 &sid); 6946 } 6947 if (error) 6948 return error; 6949 } 6950 6951 new = prepare_creds(); 6952 if (!new) 6953 return -ENOMEM; 6954 6955 /* Permission checking based on the specified context is 6956 performed during the actual operation (execve, 6957 open/mkdir/...), when we know the full context of the 6958 operation. See selinux_bprm_creds_for_exec for the execve 6959 checks and may_create for the file creation checks. The 6960 operation will then fail if the context is not permitted. */ 6961 crsec = selinux_cred(new); 6962 if (attr == LSM_ATTR_EXEC) { 6963 crsec->exec_sid = sid; 6964 } else if (attr == LSM_ATTR_FSCREATE) { 6965 crsec->create_sid = sid; 6966 } else if (attr == LSM_ATTR_KEYCREATE) { 6967 if (sid) { 6968 error = avc_has_perm(mysid, sid, 6969 SECCLASS_KEY, KEY__CREATE, NULL); 6970 if (error) 6971 goto abort_change; 6972 } 6973 crsec->keycreate_sid = sid; 6974 } else if (attr == LSM_ATTR_SOCKCREATE) { 6975 crsec->sockcreate_sid = sid; 6976 } else if (attr == LSM_ATTR_CURRENT) { 6977 error = -EINVAL; 6978 if (sid == 0) 6979 goto abort_change; 6980 6981 if (!current_is_single_threaded()) { 6982 error = security_bounded_transition(crsec->sid, sid); 6983 if (error) 6984 goto abort_change; 6985 } 6986 6987 /* Check permissions for the transition. */ 6988 error = avc_has_perm(crsec->sid, sid, SECCLASS_PROCESS, 6989 PROCESS__DYNTRANSITION, NULL); 6990 if (error) 6991 goto abort_change; 6992 6993 /* Check for ptracing, and update the task SID if ok. 6994 Otherwise, leave SID unchanged and fail. */ 6995 ptsid = ptrace_parent_sid(); 6996 if (ptsid != 0) { 6997 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 6998 PROCESS__PTRACE, NULL); 6999 if (error) 7000 goto abort_change; 7001 } 7002 7003 crsec->sid = sid; 7004 } else { 7005 error = -EINVAL; 7006 goto abort_change; 7007 } 7008 7009 commit_creds(new); 7010 return size; 7011 7012 abort_change: 7013 abort_creds(new); 7014 return error; 7015 } 7016 7017 /** 7018 * selinux_getselfattr - Get SELinux current task attributes 7019 * @attr: the requested attribute 7020 * @ctx: buffer to receive the result 7021 * @size: buffer size (input), buffer size used (output) 7022 * @flags: unused 7023 * 7024 * Fill the passed user space @ctx with the details of the requested 7025 * attribute. 7026 * 7027 * Returns the number of attributes on success, an error code otherwise. 7028 * There will only ever be one attribute. 7029 */ 7030 static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx, 7031 u32 *size, u32 flags) 7032 { 7033 int rc; 7034 char *val = NULL; 7035 int val_len; 7036 7037 val_len = selinux_lsm_getattr(attr, current, &val); 7038 if (val_len < 0) 7039 return val_len; 7040 rc = lsm_fill_user_ctx(ctx, size, val, val_len, LSM_ID_SELINUX, 0); 7041 kfree(val); 7042 return (!rc ? 1 : rc); 7043 } 7044 7045 static int selinux_setselfattr(unsigned int attr, struct lsm_ctx *ctx, 7046 u32 size, u32 flags) 7047 { 7048 int rc; 7049 7050 rc = selinux_lsm_setattr(attr, ctx->ctx, ctx->ctx_len); 7051 if (rc > 0) 7052 return 0; 7053 return rc; 7054 } 7055 7056 static int selinux_getprocattr(struct task_struct *p, 7057 const char *name, char **value) 7058 { 7059 unsigned int attr = lsm_name_to_attr(name); 7060 int rc; 7061 7062 if (attr) { 7063 rc = selinux_lsm_getattr(attr, p, value); 7064 if (rc != -EOPNOTSUPP) 7065 return rc; 7066 } 7067 7068 return -EINVAL; 7069 } 7070 7071 static int selinux_setprocattr(const char *name, void *value, size_t size) 7072 { 7073 int attr = lsm_name_to_attr(name); 7074 7075 if (attr) 7076 return selinux_lsm_setattr(attr, value, size); 7077 return -EINVAL; 7078 } 7079 7080 static int selinux_ismaclabel(const char *name) 7081 { 7082 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 7083 } 7084 7085 static int selinux_secid_to_secctx(u32 secid, struct lsm_context *cp) 7086 { 7087 u32 seclen; 7088 int ret; 7089 7090 if (cp) { 7091 cp->id = LSM_ID_SELINUX; 7092 ret = security_sid_to_context(secid, &cp->context, &cp->len); 7093 if (ret < 0) 7094 return ret; 7095 return cp->len; 7096 } 7097 ret = security_sid_to_context(secid, NULL, &seclen); 7098 if (ret < 0) 7099 return ret; 7100 return seclen; 7101 } 7102 7103 static int selinux_lsmprop_to_secctx(struct lsm_prop *prop, 7104 struct lsm_context *cp) 7105 { 7106 return selinux_secid_to_secctx(prop->selinux.secid, cp); 7107 } 7108 7109 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 7110 { 7111 return security_context_to_sid(secdata, seclen, 7112 secid, GFP_KERNEL); 7113 } 7114 7115 static void selinux_release_secctx(struct lsm_context *cp) 7116 { 7117 if (cp->id == LSM_ID_SELINUX) { 7118 kfree(cp->context); 7119 cp->context = NULL; 7120 cp->id = LSM_ID_UNDEF; 7121 } 7122 } 7123 7124 static void selinux_inode_invalidate_secctx(struct inode *inode) 7125 { 7126 struct inode_security_struct *isec = selinux_inode(inode); 7127 7128 spin_lock(&isec->lock); 7129 isec->initialized = LABEL_INVALID; 7130 spin_unlock(&isec->lock); 7131 } 7132 7133 /* 7134 * called with inode->i_mutex locked 7135 */ 7136 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 7137 { 7138 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, 7139 ctx, ctxlen, 0); 7140 /* Do not return error when suppressing label (SBLABEL_MNT not set). */ 7141 return rc == -EOPNOTSUPP ? 0 : rc; 7142 } 7143 7144 /* 7145 * called with inode->i_mutex locked 7146 */ 7147 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 7148 { 7149 return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX, 7150 ctx, ctxlen, 0, NULL); 7151 } 7152 7153 static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) 7154 { 7155 int len; 7156 len = selinux_inode_getsecurity(&nop_mnt_idmap, inode, 7157 XATTR_SELINUX_SUFFIX, 7158 (void **)&cp->context, true); 7159 if (len < 0) 7160 return len; 7161 cp->len = len; 7162 cp->id = LSM_ID_SELINUX; 7163 return 0; 7164 } 7165 #ifdef CONFIG_KEYS 7166 7167 static int selinux_key_alloc(struct key *k, const struct cred *cred, 7168 unsigned long flags) 7169 { 7170 const struct cred_security_struct *crsec; 7171 struct key_security_struct *ksec = selinux_key(k); 7172 7173 crsec = selinux_cred(cred); 7174 if (crsec->keycreate_sid) 7175 ksec->sid = crsec->keycreate_sid; 7176 else 7177 ksec->sid = crsec->sid; 7178 7179 return 0; 7180 } 7181 7182 static int selinux_key_permission(key_ref_t key_ref, 7183 const struct cred *cred, 7184 enum key_need_perm need_perm) 7185 { 7186 struct key *key; 7187 struct key_security_struct *ksec; 7188 u32 perm, sid; 7189 7190 switch (need_perm) { 7191 case KEY_NEED_VIEW: 7192 perm = KEY__VIEW; 7193 break; 7194 case KEY_NEED_READ: 7195 perm = KEY__READ; 7196 break; 7197 case KEY_NEED_WRITE: 7198 perm = KEY__WRITE; 7199 break; 7200 case KEY_NEED_SEARCH: 7201 perm = KEY__SEARCH; 7202 break; 7203 case KEY_NEED_LINK: 7204 perm = KEY__LINK; 7205 break; 7206 case KEY_NEED_SETATTR: 7207 perm = KEY__SETATTR; 7208 break; 7209 case KEY_NEED_UNLINK: 7210 case KEY_SYSADMIN_OVERRIDE: 7211 case KEY_AUTHTOKEN_OVERRIDE: 7212 case KEY_DEFER_PERM_CHECK: 7213 return 0; 7214 default: 7215 WARN_ON(1); 7216 return -EPERM; 7217 7218 } 7219 7220 sid = cred_sid(cred); 7221 key = key_ref_to_ptr(key_ref); 7222 ksec = selinux_key(key); 7223 7224 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 7225 } 7226 7227 static int selinux_key_getsecurity(struct key *key, char **_buffer) 7228 { 7229 struct key_security_struct *ksec = selinux_key(key); 7230 char *context = NULL; 7231 unsigned len; 7232 int rc; 7233 7234 rc = security_sid_to_context(ksec->sid, 7235 &context, &len); 7236 if (!rc) 7237 rc = len; 7238 *_buffer = context; 7239 return rc; 7240 } 7241 7242 #ifdef CONFIG_KEY_NOTIFICATIONS 7243 static int selinux_watch_key(struct key *key) 7244 { 7245 struct key_security_struct *ksec = selinux_key(key); 7246 u32 sid = current_sid(); 7247 7248 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL); 7249 } 7250 #endif 7251 #endif 7252 7253 #ifdef CONFIG_SECURITY_INFINIBAND 7254 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 7255 { 7256 struct common_audit_data ad; 7257 int err; 7258 u32 sid = 0; 7259 struct ib_security_struct *sec = ib_sec; 7260 struct lsm_ibpkey_audit ibpkey; 7261 7262 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 7263 if (err) 7264 return err; 7265 7266 ad.type = LSM_AUDIT_DATA_IBPKEY; 7267 ibpkey.subnet_prefix = subnet_prefix; 7268 ibpkey.pkey = pkey_val; 7269 ad.u.ibpkey = &ibpkey; 7270 return avc_has_perm(sec->sid, sid, 7271 SECCLASS_INFINIBAND_PKEY, 7272 INFINIBAND_PKEY__ACCESS, &ad); 7273 } 7274 7275 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 7276 u8 port_num) 7277 { 7278 struct common_audit_data ad; 7279 int err; 7280 u32 sid = 0; 7281 struct ib_security_struct *sec = ib_sec; 7282 struct lsm_ibendport_audit ibendport; 7283 7284 err = security_ib_endport_sid(dev_name, port_num, 7285 &sid); 7286 7287 if (err) 7288 return err; 7289 7290 ad.type = LSM_AUDIT_DATA_IBENDPORT; 7291 ibendport.dev_name = dev_name; 7292 ibendport.port = port_num; 7293 ad.u.ibendport = &ibendport; 7294 return avc_has_perm(sec->sid, sid, 7295 SECCLASS_INFINIBAND_ENDPORT, 7296 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 7297 } 7298 7299 static int selinux_ib_alloc_security(void *ib_sec) 7300 { 7301 struct ib_security_struct *sec = selinux_ib(ib_sec); 7302 7303 sec->sid = current_sid(); 7304 return 0; 7305 } 7306 #endif 7307 7308 #ifdef CONFIG_BPF_SYSCALL 7309 static int selinux_bpf(int cmd, union bpf_attr *attr, 7310 unsigned int size, bool kernel) 7311 { 7312 u32 sid = current_sid(); 7313 int ret; 7314 7315 if (selinux_policycap_bpf_token_perms()) 7316 return 0; 7317 7318 switch (cmd) { 7319 case BPF_MAP_CREATE: 7320 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 7321 NULL); 7322 break; 7323 case BPF_PROG_LOAD: 7324 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 7325 NULL); 7326 break; 7327 default: 7328 ret = 0; 7329 break; 7330 } 7331 7332 return ret; 7333 } 7334 7335 static u32 bpf_map_fmode_to_av(fmode_t fmode) 7336 { 7337 u32 av = 0; 7338 7339 if (fmode & FMODE_READ) 7340 av |= BPF__MAP_READ; 7341 if (fmode & FMODE_WRITE) 7342 av |= BPF__MAP_WRITE; 7343 return av; 7344 } 7345 7346 /* This function will check the file pass through unix socket or binder to see 7347 * if it is a bpf related object. And apply corresponding checks on the bpf 7348 * object based on the type. The bpf maps and programs, not like other files and 7349 * socket, are using a shared anonymous inode inside the kernel as their inode. 7350 * So checking that inode cannot identify if the process have privilege to 7351 * access the bpf object and that's why we have to add this additional check in 7352 * selinux_file_receive and selinux_binder_transfer_files. 7353 */ 7354 static int bpf_fd_pass(const struct file *file, u32 sid) 7355 { 7356 struct bpf_security_struct *bpfsec; 7357 struct bpf_prog *prog; 7358 struct bpf_map *map; 7359 int ret; 7360 7361 if (file->f_op == &bpf_map_fops) { 7362 map = file->private_data; 7363 bpfsec = selinux_bpf_map_security(map); 7364 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 7365 bpf_map_fmode_to_av(file->f_mode), NULL); 7366 if (ret) 7367 return ret; 7368 } else if (file->f_op == &bpf_prog_fops) { 7369 prog = file->private_data; 7370 bpfsec = selinux_bpf_prog_security(prog); 7371 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 7372 BPF__PROG_RUN, NULL); 7373 if (ret) 7374 return ret; 7375 } 7376 return 0; 7377 } 7378 7379 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 7380 { 7381 u32 sid = current_sid(); 7382 struct bpf_security_struct *bpfsec; 7383 7384 bpfsec = selinux_bpf_map_security(map); 7385 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 7386 bpf_map_fmode_to_av(fmode), NULL); 7387 } 7388 7389 static int selinux_bpf_prog(struct bpf_prog *prog) 7390 { 7391 u32 sid = current_sid(); 7392 struct bpf_security_struct *bpfsec; 7393 7394 bpfsec = selinux_bpf_prog_security(prog); 7395 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 7396 BPF__PROG_RUN, NULL); 7397 } 7398 7399 static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr, 7400 struct bpf_token *token, bool kernel) 7401 { 7402 struct bpf_security_struct *bpfsec; 7403 u32 ssid; 7404 7405 bpfsec = selinux_bpf_map_security(map); 7406 bpfsec->sid = current_sid(); 7407 7408 if (!token) 7409 ssid = bpfsec->sid; 7410 else 7411 ssid = selinux_bpf_token_security(token)->grantor_sid; 7412 7413 return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__MAP_CREATE, 7414 NULL); 7415 } 7416 7417 static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr, 7418 struct bpf_token *token, bool kernel) 7419 { 7420 struct bpf_security_struct *bpfsec; 7421 u32 ssid; 7422 7423 bpfsec = selinux_bpf_prog_security(prog); 7424 bpfsec->sid = current_sid(); 7425 7426 if (!token) 7427 ssid = bpfsec->sid; 7428 else 7429 ssid = selinux_bpf_token_security(token)->grantor_sid; 7430 7431 return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__PROG_LOAD, 7432 NULL); 7433 } 7434 7435 #define bpf_token_cmd(T, C) \ 7436 ((T)->allowed_cmds & (1ULL << (C))) 7437 7438 static int selinux_bpf_token_create(struct bpf_token *token, 7439 union bpf_attr *attr, 7440 const struct path *path) 7441 { 7442 struct bpf_security_struct *bpfsec; 7443 struct superblock_security_struct *sbsec; 7444 int err; 7445 7446 sbsec = selinux_superblock(path->dentry->d_sb); 7447 7448 bpfsec = selinux_bpf_token_security(token); 7449 bpfsec->sid = current_sid(); 7450 bpfsec->grantor_sid = sbsec->creator_sid; 7451 7452 bpfsec->perms = 0; 7453 /** 7454 * 'token->allowed_cmds' is a bit mask of allowed commands 7455 * Convert the BPF command enum to a bitmask representing its position 7456 * in the allowed_cmds bitmap. 7457 */ 7458 if (bpf_token_cmd(token, BPF_MAP_CREATE)) { 7459 err = avc_has_perm(bpfsec->sid, bpfsec->grantor_sid, 7460 SECCLASS_BPF, BPF__MAP_CREATE_AS, NULL); 7461 if (err) 7462 return err; 7463 bpfsec->perms |= BPF__MAP_CREATE; 7464 } 7465 if (bpf_token_cmd(token, BPF_PROG_LOAD)) { 7466 err = avc_has_perm(bpfsec->sid, bpfsec->grantor_sid, 7467 SECCLASS_BPF, BPF__PROG_LOAD_AS, NULL); 7468 if (err) 7469 return err; 7470 bpfsec->perms |= BPF__PROG_LOAD; 7471 } 7472 7473 return 0; 7474 } 7475 7476 static int selinux_bpf_token_cmd(const struct bpf_token *token, 7477 enum bpf_cmd cmd) 7478 { 7479 struct bpf_security_struct *bpfsec; 7480 7481 bpfsec = token->security; 7482 switch (cmd) { 7483 case BPF_MAP_CREATE: 7484 if (!(bpfsec->perms & BPF__MAP_CREATE)) 7485 return -EACCES; 7486 break; 7487 case BPF_PROG_LOAD: 7488 if (!(bpfsec->perms & BPF__PROG_LOAD)) 7489 return -EACCES; 7490 break; 7491 default: 7492 break; 7493 } 7494 7495 return 0; 7496 } 7497 7498 static int selinux_bpf_token_capable(const struct bpf_token *token, int cap) 7499 { 7500 u16 sclass; 7501 struct bpf_security_struct *bpfsec = token->security; 7502 bool initns = (token->userns == &init_user_ns); 7503 u32 av = CAP_TO_MASK(cap); 7504 7505 switch (CAP_TO_INDEX(cap)) { 7506 case 0: 7507 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 7508 break; 7509 case 1: 7510 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 7511 break; 7512 default: 7513 pr_err("SELinux: out of range capability %d\n", cap); 7514 return -EINVAL; 7515 } 7516 7517 return avc_has_perm(current_sid(), bpfsec->grantor_sid, sclass, av, 7518 NULL); 7519 } 7520 #endif 7521 7522 #ifdef CONFIG_PERF_EVENTS 7523 static int selinux_perf_event_open(int type) 7524 { 7525 u32 requested, sid = current_sid(); 7526 7527 if (type == PERF_SECURITY_OPEN) 7528 requested = PERF_EVENT__OPEN; 7529 else if (type == PERF_SECURITY_CPU) 7530 requested = PERF_EVENT__CPU; 7531 else if (type == PERF_SECURITY_KERNEL) 7532 requested = PERF_EVENT__KERNEL; 7533 else if (type == PERF_SECURITY_TRACEPOINT) 7534 requested = PERF_EVENT__TRACEPOINT; 7535 else 7536 return -EINVAL; 7537 7538 return avc_has_perm(sid, sid, SECCLASS_PERF_EVENT, 7539 requested, NULL); 7540 } 7541 7542 static int selinux_perf_event_alloc(struct perf_event *event) 7543 { 7544 struct perf_event_security_struct *perfsec; 7545 7546 perfsec = selinux_perf_event(event->security); 7547 perfsec->sid = current_sid(); 7548 7549 return 0; 7550 } 7551 7552 static int selinux_perf_event_read(struct perf_event *event) 7553 { 7554 struct perf_event_security_struct *perfsec = event->security; 7555 u32 sid = current_sid(); 7556 7557 return avc_has_perm(sid, perfsec->sid, 7558 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL); 7559 } 7560 7561 static int selinux_perf_event_write(struct perf_event *event) 7562 { 7563 struct perf_event_security_struct *perfsec = event->security; 7564 u32 sid = current_sid(); 7565 7566 return avc_has_perm(sid, perfsec->sid, 7567 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL); 7568 } 7569 #endif 7570 7571 #ifdef CONFIG_IO_URING 7572 /** 7573 * selinux_uring_override_creds - check the requested cred override 7574 * @new: the target creds 7575 * 7576 * Check to see if the current task is allowed to override it's credentials 7577 * to service an io_uring operation. 7578 */ 7579 static int selinux_uring_override_creds(const struct cred *new) 7580 { 7581 return avc_has_perm(current_sid(), cred_sid(new), 7582 SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL); 7583 } 7584 7585 /** 7586 * selinux_uring_sqpoll - check if a io_uring polling thread can be created 7587 * 7588 * Check to see if the current task is allowed to create a new io_uring 7589 * kernel polling thread. 7590 */ 7591 static int selinux_uring_sqpoll(void) 7592 { 7593 u32 sid = current_sid(); 7594 7595 return avc_has_perm(sid, sid, 7596 SECCLASS_IO_URING, IO_URING__SQPOLL, NULL); 7597 } 7598 7599 /** 7600 * selinux_uring_cmd - check if IORING_OP_URING_CMD is allowed 7601 * @ioucmd: the io_uring command structure 7602 * 7603 * Check to see if the current domain is allowed to execute an 7604 * IORING_OP_URING_CMD against the device/file specified in @ioucmd. 7605 * 7606 */ 7607 static int selinux_uring_cmd(struct io_uring_cmd *ioucmd) 7608 { 7609 struct file *file = ioucmd->file; 7610 struct inode *inode = file_inode(file); 7611 struct inode_security_struct *isec = selinux_inode(inode); 7612 struct common_audit_data ad; 7613 7614 ad.type = LSM_AUDIT_DATA_FILE; 7615 ad.u.file = file; 7616 7617 return avc_has_perm(current_sid(), isec->sid, 7618 SECCLASS_IO_URING, IO_URING__CMD, &ad); 7619 } 7620 7621 /** 7622 * selinux_uring_allowed - check if io_uring_setup() can be called 7623 * 7624 * Check to see if the current task is allowed to call io_uring_setup(). 7625 */ 7626 static int selinux_uring_allowed(void) 7627 { 7628 u32 sid = current_sid(); 7629 7630 return avc_has_perm(sid, sid, SECCLASS_IO_URING, IO_URING__ALLOWED, 7631 NULL); 7632 } 7633 #endif /* CONFIG_IO_URING */ 7634 7635 static const struct lsm_id selinux_lsmid = { 7636 .name = "selinux", 7637 .id = LSM_ID_SELINUX, 7638 }; 7639 7640 struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = { 7641 .lbs_cred = sizeof(struct cred_security_struct), 7642 .lbs_task = sizeof(struct task_security_struct), 7643 .lbs_file = sizeof(struct file_security_struct), 7644 .lbs_backing_file = sizeof(struct backing_file_security_struct), 7645 .lbs_inode = sizeof(struct inode_security_struct), 7646 .lbs_ipc = sizeof(struct ipc_security_struct), 7647 .lbs_key = sizeof(struct key_security_struct), 7648 .lbs_msg_msg = sizeof(struct msg_security_struct), 7649 #ifdef CONFIG_PERF_EVENTS 7650 .lbs_perf_event = sizeof(struct perf_event_security_struct), 7651 #endif 7652 .lbs_sock = sizeof(struct sk_security_struct), 7653 .lbs_superblock = sizeof(struct superblock_security_struct), 7654 .lbs_xattr_count = SELINUX_INODE_INIT_XATTRS, 7655 .lbs_tun_dev = sizeof(struct tun_security_struct), 7656 .lbs_ib = sizeof(struct ib_security_struct), 7657 .lbs_bpf_map = sizeof(struct bpf_security_struct), 7658 .lbs_bpf_prog = sizeof(struct bpf_security_struct), 7659 .lbs_bpf_token = sizeof(struct bpf_security_struct), 7660 }; 7661 7662 /* 7663 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order: 7664 * 1. any hooks that don't belong to (2.) or (3.) below, 7665 * 2. hooks that both access structures allocated by other hooks, and allocate 7666 * structures that can be later accessed by other hooks (mostly "cloning" 7667 * hooks), 7668 * 3. hooks that only allocate structures that can be later accessed by other 7669 * hooks ("allocating" hooks). 7670 * 7671 * Please follow block comment delimiters in the list to keep this order. 7672 */ 7673 static struct security_hook_list selinux_hooks[] __ro_after_init = { 7674 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 7675 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 7676 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 7677 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 7678 7679 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 7680 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 7681 LSM_HOOK_INIT(capget, selinux_capget), 7682 LSM_HOOK_INIT(capset, selinux_capset), 7683 LSM_HOOK_INIT(capable, selinux_capable), 7684 LSM_HOOK_INIT(quotactl, selinux_quotactl), 7685 LSM_HOOK_INIT(quota_on, selinux_quota_on), 7686 LSM_HOOK_INIT(syslog, selinux_syslog), 7687 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 7688 7689 LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 7690 7691 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec), 7692 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 7693 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 7694 7695 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 7696 LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat), 7697 LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 7698 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 7699 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 7700 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 7701 LSM_HOOK_INIT(sb_mount, selinux_mount), 7702 LSM_HOOK_INIT(sb_umount, selinux_umount), 7703 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 7704 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 7705 7706 LSM_HOOK_INIT(move_mount, selinux_move_mount), 7707 7708 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 7709 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 7710 7711 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 7712 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 7713 LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon), 7714 LSM_HOOK_INIT(inode_create, selinux_inode_create), 7715 LSM_HOOK_INIT(inode_link, selinux_inode_link), 7716 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 7717 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 7718 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 7719 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 7720 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 7721 LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 7722 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 7723 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 7724 LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 7725 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 7726 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 7727 LSM_HOOK_INIT(inode_xattr_skipcap, selinux_inode_xattr_skipcap), 7728 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 7729 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 7730 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 7731 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 7732 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 7733 LSM_HOOK_INIT(inode_file_getattr, selinux_inode_file_getattr), 7734 LSM_HOOK_INIT(inode_file_setattr, selinux_inode_file_setattr), 7735 LSM_HOOK_INIT(inode_set_acl, selinux_inode_set_acl), 7736 LSM_HOOK_INIT(inode_get_acl, selinux_inode_get_acl), 7737 LSM_HOOK_INIT(inode_remove_acl, selinux_inode_remove_acl), 7738 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 7739 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 7740 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 7741 LSM_HOOK_INIT(inode_getlsmprop, selinux_inode_getlsmprop), 7742 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 7743 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 7744 LSM_HOOK_INIT(path_notify, selinux_path_notify), 7745 7746 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security), 7747 7748 LSM_HOOK_INIT(file_permission, selinux_file_permission), 7749 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 7750 LSM_HOOK_INIT(backing_file_alloc, selinux_backing_file_alloc), 7751 LSM_HOOK_INIT(backing_file_free, selinux_backing_file_free), 7752 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 7753 LSM_HOOK_INIT(file_ioctl_compat, selinux_file_ioctl_compat), 7754 LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 7755 LSM_HOOK_INIT(mmap_backing_file, selinux_mmap_backing_file), 7756 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 7757 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 7758 LSM_HOOK_INIT(file_lock, selinux_file_lock), 7759 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 7760 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 7761 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 7762 LSM_HOOK_INIT(file_receive, selinux_file_receive), 7763 7764 LSM_HOOK_INIT(file_open, selinux_file_open), 7765 7766 LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 7767 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 7768 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 7769 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 7770 LSM_HOOK_INIT(cred_getlsmprop, selinux_cred_getlsmprop), 7771 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 7772 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 7773 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 7774 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 7775 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 7776 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 7777 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 7778 LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 7779 LSM_HOOK_INIT(current_getlsmprop_subj, selinux_current_getlsmprop_subj), 7780 LSM_HOOK_INIT(task_getlsmprop_obj, selinux_task_getlsmprop_obj), 7781 LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 7782 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 7783 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 7784 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 7785 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 7786 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 7787 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 7788 LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 7789 LSM_HOOK_INIT(task_kill, selinux_task_kill), 7790 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 7791 LSM_HOOK_INIT(userns_create, selinux_userns_create), 7792 7793 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 7794 LSM_HOOK_INIT(ipc_getlsmprop, selinux_ipc_getlsmprop), 7795 7796 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 7797 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 7798 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 7799 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 7800 7801 LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 7802 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 7803 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 7804 7805 LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 7806 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 7807 LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 7808 7809 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 7810 7811 LSM_HOOK_INIT(getselfattr, selinux_getselfattr), 7812 LSM_HOOK_INIT(setselfattr, selinux_setselfattr), 7813 LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 7814 LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 7815 7816 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 7817 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 7818 LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 7819 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 7820 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 7821 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 7822 7823 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 7824 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 7825 7826 LSM_HOOK_INIT(socket_create, selinux_socket_create), 7827 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 7828 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 7829 LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 7830 LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 7831 LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 7832 LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 7833 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 7834 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 7835 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 7836 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 7837 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 7838 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 7839 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 7840 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 7841 LSM_HOOK_INIT(socket_getpeersec_stream, 7842 selinux_socket_getpeersec_stream), 7843 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 7844 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 7845 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 7846 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 7847 LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 7848 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 7849 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 7850 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 7851 LSM_HOOK_INIT(sctp_assoc_established, selinux_sctp_assoc_established), 7852 LSM_HOOK_INIT(mptcp_add_subflow, selinux_mptcp_add_subflow), 7853 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 7854 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 7855 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 7856 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 7857 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 7858 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 7859 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 7860 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 7861 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 7862 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 7863 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 7864 #ifdef CONFIG_SECURITY_INFINIBAND 7865 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 7866 LSM_HOOK_INIT(ib_endport_manage_subnet, 7867 selinux_ib_endport_manage_subnet), 7868 #endif 7869 #ifdef CONFIG_SECURITY_NETWORK_XFRM 7870 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 7871 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 7872 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 7873 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 7874 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 7875 LSM_HOOK_INIT(xfrm_state_pol_flow_match, 7876 selinux_xfrm_state_pol_flow_match), 7877 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 7878 #endif 7879 7880 #ifdef CONFIG_KEYS 7881 LSM_HOOK_INIT(key_permission, selinux_key_permission), 7882 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 7883 #ifdef CONFIG_KEY_NOTIFICATIONS 7884 LSM_HOOK_INIT(watch_key, selinux_watch_key), 7885 #endif 7886 #endif 7887 7888 #ifdef CONFIG_AUDIT 7889 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 7890 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 7891 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 7892 #endif 7893 7894 #ifdef CONFIG_BPF_SYSCALL 7895 LSM_HOOK_INIT(bpf, selinux_bpf), 7896 LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 7897 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 7898 #endif 7899 7900 #ifdef CONFIG_PERF_EVENTS 7901 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open), 7902 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read), 7903 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write), 7904 #endif 7905 7906 #ifdef CONFIG_IO_URING 7907 LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds), 7908 LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll), 7909 LSM_HOOK_INIT(uring_cmd, selinux_uring_cmd), 7910 LSM_HOOK_INIT(uring_allowed, selinux_uring_allowed), 7911 #endif 7912 7913 /* 7914 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE 7915 */ 7916 LSM_HOOK_INIT(fs_context_submount, selinux_fs_context_submount), 7917 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), 7918 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), 7919 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 7920 #ifdef CONFIG_SECURITY_NETWORK_XFRM 7921 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 7922 #endif 7923 7924 /* 7925 * PUT "ALLOCATING" HOOKS HERE 7926 */ 7927 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 7928 LSM_HOOK_INIT(msg_queue_alloc_security, 7929 selinux_msg_queue_alloc_security), 7930 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 7931 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 7932 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 7933 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 7934 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 7935 LSM_HOOK_INIT(lsmprop_to_secctx, selinux_lsmprop_to_secctx), 7936 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 7937 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 7938 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 7939 #ifdef CONFIG_SECURITY_INFINIBAND 7940 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 7941 #endif 7942 #ifdef CONFIG_SECURITY_NETWORK_XFRM 7943 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 7944 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 7945 LSM_HOOK_INIT(xfrm_state_alloc_acquire, 7946 selinux_xfrm_state_alloc_acquire), 7947 #endif 7948 #ifdef CONFIG_KEYS 7949 LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 7950 #endif 7951 #ifdef CONFIG_AUDIT 7952 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 7953 #endif 7954 #ifdef CONFIG_BPF_SYSCALL 7955 LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create), 7956 LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load), 7957 LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create), 7958 LSM_HOOK_INIT(bpf_token_cmd, selinux_bpf_token_cmd), 7959 LSM_HOOK_INIT(bpf_token_capable, selinux_bpf_token_capable), 7960 #endif 7961 #ifdef CONFIG_PERF_EVENTS 7962 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc), 7963 #endif 7964 }; 7965 7966 static __init int selinux_init(void) 7967 { 7968 vma_flags_t data_default_flags = VMA_DATA_DEFAULT_FLAGS; 7969 7970 pr_info("SELinux: Initializing.\n"); 7971 7972 memset(&selinux_state, 0, sizeof(selinux_state)); 7973 enforcing_set(selinux_enforcing_boot); 7974 selinux_avc_init(); 7975 mutex_init(&selinux_state.status_lock); 7976 mutex_init(&selinux_state.policy_mutex); 7977 7978 /* Set the security state for the initial task. */ 7979 cred_init_security(); 7980 7981 /* Inform the audit system that secctx is used */ 7982 audit_cfg_lsm(&selinux_lsmid, 7983 AUDIT_CFG_LSM_SECCTX_SUBJECT | 7984 AUDIT_CFG_LSM_SECCTX_OBJECT); 7985 7986 default_noexec = !vma_flags_test(&data_default_flags, VMA_EXEC_BIT); 7987 if (!default_noexec) 7988 pr_notice("SELinux: virtual memory is executable by default\n"); 7989 7990 avc_init(); 7991 7992 avtab_cache_init(); 7993 7994 ebitmap_cache_init(); 7995 7996 hashtab_cache_init(); 7997 7998 selinux_ima_config_len_init(); 7999 8000 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), 8001 &selinux_lsmid); 8002 8003 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 8004 panic("SELinux: Unable to register AVC netcache callback\n"); 8005 8006 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 8007 panic("SELinux: Unable to register AVC LSM notifier callback\n"); 8008 8009 if (avc_add_callback(selinux_audit_rule_avc_callback, 8010 AVC_CALLBACK_RESET)) 8011 panic("SELinux: Unable to register AVC audit callback\n"); 8012 8013 if (selinux_enforcing_boot) 8014 pr_debug("SELinux: Starting in enforcing mode\n"); 8015 else 8016 pr_debug("SELinux: Starting in permissive mode\n"); 8017 8018 fs_validate_description("selinux", selinux_fs_parameters); 8019 8020 return 0; 8021 } 8022 8023 static void delayed_superblock_init(struct super_block *sb, void *unused) 8024 { 8025 selinux_set_mnt_opts(sb, NULL, 0, NULL); 8026 } 8027 8028 void selinux_complete_init(void) 8029 { 8030 pr_debug("SELinux: Completing initialization.\n"); 8031 8032 /* Set up any superblocks initialized prior to the policy load. */ 8033 pr_debug("SELinux: Setting up existing superblocks.\n"); 8034 iterate_supers(delayed_superblock_init, NULL); 8035 } 8036 8037 /* SELinux requires early initialization in order to label 8038 all processes and objects when they are created. */ 8039 DEFINE_LSM(selinux) = { 8040 .id = &selinux_lsmid, 8041 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 8042 .enabled = &selinux_enabled_boot, 8043 .blobs = &selinux_blob_sizes, 8044 .init = selinux_init, 8045 .initcall_device = selinux_initcall, 8046 }; 8047 8048 #if defined(CONFIG_NETFILTER) 8049 static const struct nf_hook_ops selinux_nf_ops[] = { 8050 { 8051 .hook = selinux_ip_postroute, 8052 .pf = NFPROTO_IPV4, 8053 .hooknum = NF_INET_POST_ROUTING, 8054 .priority = NF_IP_PRI_SELINUX_LAST, 8055 }, 8056 { 8057 .hook = selinux_ip_forward, 8058 .pf = NFPROTO_IPV4, 8059 .hooknum = NF_INET_FORWARD, 8060 .priority = NF_IP_PRI_SELINUX_FIRST, 8061 }, 8062 { 8063 .hook = selinux_ip_output, 8064 .pf = NFPROTO_IPV4, 8065 .hooknum = NF_INET_LOCAL_OUT, 8066 .priority = NF_IP_PRI_SELINUX_FIRST, 8067 }, 8068 #if IS_ENABLED(CONFIG_IPV6) 8069 { 8070 .hook = selinux_ip_postroute, 8071 .pf = NFPROTO_IPV6, 8072 .hooknum = NF_INET_POST_ROUTING, 8073 .priority = NF_IP6_PRI_SELINUX_LAST, 8074 }, 8075 { 8076 .hook = selinux_ip_forward, 8077 .pf = NFPROTO_IPV6, 8078 .hooknum = NF_INET_FORWARD, 8079 .priority = NF_IP6_PRI_SELINUX_FIRST, 8080 }, 8081 { 8082 .hook = selinux_ip_output, 8083 .pf = NFPROTO_IPV6, 8084 .hooknum = NF_INET_LOCAL_OUT, 8085 .priority = NF_IP6_PRI_SELINUX_FIRST, 8086 }, 8087 #endif /* IPV6 */ 8088 }; 8089 8090 static int __net_init selinux_nf_register(struct net *net) 8091 { 8092 return nf_register_net_hooks(net, selinux_nf_ops, 8093 ARRAY_SIZE(selinux_nf_ops)); 8094 } 8095 8096 static void __net_exit selinux_nf_unregister(struct net *net) 8097 { 8098 nf_unregister_net_hooks(net, selinux_nf_ops, 8099 ARRAY_SIZE(selinux_nf_ops)); 8100 } 8101 8102 static struct pernet_operations selinux_net_ops = { 8103 .init = selinux_nf_register, 8104 .exit = selinux_nf_unregister, 8105 }; 8106 8107 int __init selinux_nf_ip_init(void) 8108 { 8109 int err; 8110 8111 if (!selinux_enabled_boot) 8112 return 0; 8113 8114 pr_debug("SELinux: Registering netfilter hooks\n"); 8115 8116 err = register_pernet_subsys(&selinux_net_ops); 8117 if (err) 8118 panic("SELinux: register_pernet_subsys: error %d\n", err); 8119 8120 return 0; 8121 } 8122 #endif /* CONFIG_NETFILTER */ 8123