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