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