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