LCOV - code coverage report
Current view: top level - security/selinux - hooks.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 2041 0.0 %
Date: 2014-02-18 Functions: 0 217 0.0 %
Branches: 0 1528 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  NSA Security-Enhanced Linux (SELinux) security module
       3                 :            :  *
       4                 :            :  *  This file contains the SELinux hook function implementations.
       5                 :            :  *
       6                 :            :  *  Authors:  Stephen Smalley, <sds@epoch.ncsc.mil>
       7                 :            :  *            Chris Vance, <cvance@nai.com>
       8                 :            :  *            Wayne Salamon, <wsalamon@nai.com>
       9                 :            :  *            James Morris <jmorris@redhat.com>
      10                 :            :  *
      11                 :            :  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
      12                 :            :  *  Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
      13                 :            :  *                                         Eric Paris <eparis@redhat.com>
      14                 :            :  *  Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
      15                 :            :  *                          <dgoeddel@trustedcs.com>
      16                 :            :  *  Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
      17                 :            :  *      Paul Moore <paul@paul-moore.com>
      18                 :            :  *  Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
      19                 :            :  *                     Yuichi Nakamura <ynakam@hitachisoft.jp>
      20                 :            :  *
      21                 :            :  *      This program is free software; you can redistribute it and/or modify
      22                 :            :  *      it under the terms of the GNU General Public License version 2,
      23                 :            :  *      as published by the Free Software Foundation.
      24                 :            :  */
      25                 :            : 
      26                 :            : #include <linux/init.h>
      27                 :            : #include <linux/kd.h>
      28                 :            : #include <linux/kernel.h>
      29                 :            : #include <linux/tracehook.h>
      30                 :            : #include <linux/errno.h>
      31                 :            : #include <linux/sched.h>
      32                 :            : #include <linux/security.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/netfilter_ipv4.h>
      50                 :            : #include <linux/netfilter_ipv6.h>
      51                 :            : #include <linux/tty.h>
      52                 :            : #include <net/icmp.h>
      53                 :            : #include <net/ip.h>               /* for local_port_range[] */
      54                 :            : #include <net/sock.h>
      55                 :            : #include <net/tcp.h>              /* struct or_callable used in sock_rcv_skb */
      56                 :            : #include <net/inet_connection_sock.h>
      57                 :            : #include <net/net_namespace.h>
      58                 :            : #include <net/netlabel.h>
      59                 :            : #include <linux/uaccess.h>
      60                 :            : #include <asm/ioctls.h>
      61                 :            : #include <linux/atomic.h>
      62                 :            : #include <linux/bitops.h>
      63                 :            : #include <linux/interrupt.h>
      64                 :            : #include <linux/netdevice.h>      /* for network interface checks */
      65                 :            : #include <net/netlink.h>
      66                 :            : #include <linux/tcp.h>
      67                 :            : #include <linux/udp.h>
      68                 :            : #include <linux/dccp.h>
      69                 :            : #include <linux/quota.h>
      70                 :            : #include <linux/un.h>             /* for Unix socket types */
      71                 :            : #include <net/af_unix.h>  /* for Unix socket types */
      72                 :            : #include <linux/parser.h>
      73                 :            : #include <linux/nfs_mount.h>
      74                 :            : #include <net/ipv6.h>
      75                 :            : #include <linux/hugetlb.h>
      76                 :            : #include <linux/personality.h>
      77                 :            : #include <linux/audit.h>
      78                 :            : #include <linux/string.h>
      79                 :            : #include <linux/selinux.h>
      80                 :            : #include <linux/mutex.h>
      81                 :            : #include <linux/posix-timers.h>
      82                 :            : #include <linux/syslog.h>
      83                 :            : #include <linux/user_namespace.h>
      84                 :            : #include <linux/export.h>
      85                 :            : #include <linux/security.h>
      86                 :            : #include <linux/msg.h>
      87                 :            : #include <linux/shm.h>
      88                 :            : 
      89                 :            : #include "avc.h"
      90                 :            : #include "objsec.h"
      91                 :            : #include "netif.h"
      92                 :            : #include "netnode.h"
      93                 :            : #include "netport.h"
      94                 :            : #include "xfrm.h"
      95                 :            : #include "netlabel.h"
      96                 :            : #include "audit.h"
      97                 :            : #include "avc_ss.h"
      98                 :            : 
      99                 :            : extern struct security_operations *security_ops;
     100                 :            : 
     101                 :            : /* SECMARK reference count */
     102                 :            : static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
     103                 :            : 
     104                 :            : #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
     105                 :            : int selinux_enforcing;
     106                 :            : 
     107                 :          0 : static int __init enforcing_setup(char *str)
     108                 :            : {
     109                 :            :         unsigned long enforcing;
     110         [ #  # ]:          0 :         if (!strict_strtoul(str, 0, &enforcing))
     111                 :          0 :                 selinux_enforcing = enforcing ? 1 : 0;
     112                 :          0 :         return 1;
     113                 :            : }
     114                 :            : __setup("enforcing=", enforcing_setup);
     115                 :            : #endif
     116                 :            : 
     117                 :            : #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
     118                 :            : int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
     119                 :            : 
     120                 :            : static int __init selinux_enabled_setup(char *str)
     121                 :            : {
     122                 :            :         unsigned long enabled;
     123                 :            :         if (!strict_strtoul(str, 0, &enabled))
     124                 :            :                 selinux_enabled = enabled ? 1 : 0;
     125                 :            :         return 1;
     126                 :            : }
     127                 :            : __setup("selinux=", selinux_enabled_setup);
     128                 :            : #else
     129                 :            : int selinux_enabled = 1;
     130                 :            : #endif
     131                 :            : 
     132                 :            : static struct kmem_cache *sel_inode_cache;
     133                 :            : 
     134                 :            : /**
     135                 :            :  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
     136                 :            :  *
     137                 :            :  * Description:
     138                 :            :  * This function checks the SECMARK reference counter to see if any SECMARK
     139                 :            :  * targets are currently configured, if the reference counter is greater than
     140                 :            :  * zero SECMARK is considered to be enabled.  Returns true (1) if SECMARK is
     141                 :            :  * enabled, false (0) if SECMARK is disabled.  If the always_check_network
     142                 :            :  * policy capability is enabled, SECMARK is always considered enabled.
     143                 :            :  *
     144                 :            :  */
     145                 :            : static int selinux_secmark_enabled(void)
     146                 :            : {
     147 [ #  # ][ #  # ]:          0 :         return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     148                 :            : }
     149                 :            : 
     150                 :            : /**
     151                 :            :  * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
     152                 :            :  *
     153                 :            :  * Description:
     154                 :            :  * This function checks if NetLabel or labeled IPSEC is enabled.  Returns true
     155                 :            :  * (1) if any are enabled or false (0) if neither are enabled.  If the
     156                 :            :  * always_check_network policy capability is enabled, peer labeling
     157                 :            :  * is always considered enabled.
     158                 :            :  *
     159                 :            :  */
     160                 :            : static int selinux_peerlbl_enabled(void)
     161                 :            : {
     162 [ #  # ][ #  # ]:          0 :         return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     163                 :            : }
     164                 :            : 
     165                 :            : /*
     166                 :            :  * initialise the security for the init task
     167                 :            :  */
     168                 :          0 : static void cred_init_security(void)
     169                 :            : {
     170                 :          0 :         struct cred *cred = (struct cred *) current->real_cred;
     171                 :            :         struct task_security_struct *tsec;
     172                 :            : 
     173                 :            :         tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
     174         [ #  # ]:          0 :         if (!tsec)
     175                 :          0 :                 panic("SELinux:  Failed to initialize initial task.\n");
     176                 :            : 
     177                 :          0 :         tsec->osid = tsec->sid = SECINITSID_KERNEL;
     178                 :          0 :         cred->security = tsec;
     179                 :          0 : }
     180                 :            : 
     181                 :            : /*
     182                 :            :  * get the security ID of a set of credentials
     183                 :            :  */
     184                 :            : static inline u32 cred_sid(const struct cred *cred)
     185                 :            : {
     186                 :            :         const struct task_security_struct *tsec;
     187                 :            : 
     188                 :            :         tsec = cred->security;
     189                 :          0 :         return tsec->sid;
     190                 :            : }
     191                 :            : 
     192                 :            : /*
     193                 :            :  * get the objective security ID of a task
     194                 :            :  */
     195                 :            : static inline u32 task_sid(const struct task_struct *task)
     196                 :            : {
     197                 :            :         u32 sid;
     198                 :            : 
     199                 :            :         rcu_read_lock();
     200                 :          0 :         sid = cred_sid(__task_cred(task));
     201                 :            :         rcu_read_unlock();
     202                 :            :         return sid;
     203                 :            : }
     204                 :            : 
     205                 :            : /*
     206                 :            :  * get the subjective security ID of the current task
     207                 :            :  */
     208                 :            : static inline u32 current_sid(void)
     209                 :            : {
     210                 :          0 :         const struct task_security_struct *tsec = current_security();
     211                 :            : 
     212                 :          0 :         return tsec->sid;
     213                 :            : }
     214                 :            : 
     215                 :            : /* Allocate and free functions for each kind of security blob. */
     216                 :            : 
     217                 :          0 : static int inode_alloc_security(struct inode *inode)
     218                 :            : {
     219                 :            :         struct inode_security_struct *isec;
     220                 :            :         u32 sid = current_sid();
     221                 :            : 
     222                 :          0 :         isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
     223         [ #  # ]:          0 :         if (!isec)
     224                 :            :                 return -ENOMEM;
     225                 :            : 
     226                 :          0 :         mutex_init(&isec->lock);
     227                 :          0 :         INIT_LIST_HEAD(&isec->list);
     228                 :          0 :         isec->inode = inode;
     229                 :          0 :         isec->sid = SECINITSID_UNLABELED;
     230                 :          0 :         isec->sclass = SECCLASS_FILE;
     231                 :          0 :         isec->task_sid = sid;
     232                 :          0 :         inode->i_security = isec;
     233                 :            : 
     234                 :          0 :         return 0;
     235                 :            : }
     236                 :            : 
     237                 :          0 : static void inode_free_rcu(struct rcu_head *head)
     238                 :            : {
     239                 :            :         struct inode_security_struct *isec;
     240                 :            : 
     241                 :          0 :         isec = container_of(head, struct inode_security_struct, rcu);
     242                 :          0 :         kmem_cache_free(sel_inode_cache, isec);
     243                 :          0 : }
     244                 :            : 
     245                 :          0 : static void inode_free_security(struct inode *inode)
     246                 :            : {
     247                 :          0 :         struct inode_security_struct *isec = inode->i_security;
     248                 :          0 :         struct superblock_security_struct *sbsec = inode->i_sb->s_security;
     249                 :            : 
     250                 :            :         spin_lock(&sbsec->isec_lock);
     251         [ #  # ]:          0 :         if (!list_empty(&isec->list))
     252                 :            :                 list_del_init(&isec->list);
     253                 :            :         spin_unlock(&sbsec->isec_lock);
     254                 :            : 
     255                 :            :         /*
     256                 :            :          * The inode may still be referenced in a path walk and
     257                 :            :          * a call to selinux_inode_permission() can be made
     258                 :            :          * after inode_free_security() is called. Ideally, the VFS
     259                 :            :          * wouldn't do this, but fixing that is a much harder
     260                 :            :          * job. For now, simply free the i_security via RCU, and
     261                 :            :          * leave the current inode->i_security pointer intact.
     262                 :            :          * The inode will be freed after the RCU grace period too.
     263                 :            :          */
     264                 :          0 :         call_rcu(&isec->rcu, inode_free_rcu);
     265                 :          0 : }
     266                 :            : 
     267                 :          0 : static int file_alloc_security(struct file *file)
     268                 :            : {
     269                 :            :         struct file_security_struct *fsec;
     270                 :            :         u32 sid = current_sid();
     271                 :            : 
     272                 :            :         fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
     273         [ #  # ]:          0 :         if (!fsec)
     274                 :            :                 return -ENOMEM;
     275                 :            : 
     276                 :          0 :         fsec->sid = sid;
     277                 :          0 :         fsec->fown_sid = sid;
     278                 :          0 :         file->f_security = fsec;
     279                 :            : 
     280                 :            :         return 0;
     281                 :            : }
     282                 :            : 
     283                 :            : static void file_free_security(struct file *file)
     284                 :            : {
     285                 :          0 :         struct file_security_struct *fsec = file->f_security;
     286                 :          0 :         file->f_security = NULL;
     287                 :          0 :         kfree(fsec);
     288                 :            : }
     289                 :            : 
     290                 :          0 : static int superblock_alloc_security(struct super_block *sb)
     291                 :            : {
     292                 :            :         struct superblock_security_struct *sbsec;
     293                 :            : 
     294                 :            :         sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
     295         [ #  # ]:          0 :         if (!sbsec)
     296                 :            :                 return -ENOMEM;
     297                 :            : 
     298                 :          0 :         mutex_init(&sbsec->lock);
     299                 :          0 :         INIT_LIST_HEAD(&sbsec->isec_head);
     300                 :          0 :         spin_lock_init(&sbsec->isec_lock);
     301                 :          0 :         sbsec->sb = sb;
     302                 :          0 :         sbsec->sid = SECINITSID_UNLABELED;
     303                 :          0 :         sbsec->def_sid = SECINITSID_FILE;
     304                 :          0 :         sbsec->mntpoint_sid = SECINITSID_UNLABELED;
     305                 :          0 :         sb->s_security = sbsec;
     306                 :            : 
     307                 :          0 :         return 0;
     308                 :            : }
     309                 :            : 
     310                 :            : static void superblock_free_security(struct super_block *sb)
     311                 :            : {
     312                 :          0 :         struct superblock_security_struct *sbsec = sb->s_security;
     313                 :          0 :         sb->s_security = NULL;
     314                 :          0 :         kfree(sbsec);
     315                 :            : }
     316                 :            : 
     317                 :            : /* The file system's label must be initialized prior to use. */
     318                 :            : 
     319                 :            : static const char *labeling_behaviors[7] = {
     320                 :            :         "uses xattr",
     321                 :            :         "uses transition SIDs",
     322                 :            :         "uses task SIDs",
     323                 :            :         "uses genfs_contexts",
     324                 :            :         "not configured for labeling",
     325                 :            :         "uses mountpoint labeling",
     326                 :            :         "uses native labeling",
     327                 :            : };
     328                 :            : 
     329                 :            : static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
     330                 :            : 
     331                 :            : static inline int inode_doinit(struct inode *inode)
     332                 :            : {
     333                 :          0 :         return inode_doinit_with_dentry(inode, NULL);
     334                 :            : }
     335                 :            : 
     336                 :            : enum {
     337                 :            :         Opt_error = -1,
     338                 :            :         Opt_context = 1,
     339                 :            :         Opt_fscontext = 2,
     340                 :            :         Opt_defcontext = 3,
     341                 :            :         Opt_rootcontext = 4,
     342                 :            :         Opt_labelsupport = 5,
     343                 :            :         Opt_nextmntopt = 6,
     344                 :            : };
     345                 :            : 
     346                 :            : #define NUM_SEL_MNT_OPTS        (Opt_nextmntopt - 1)
     347                 :            : 
     348                 :            : static const match_table_t tokens = {
     349                 :            :         {Opt_context, CONTEXT_STR "%s"},
     350                 :            :         {Opt_fscontext, FSCONTEXT_STR "%s"},
     351                 :            :         {Opt_defcontext, DEFCONTEXT_STR "%s"},
     352                 :            :         {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
     353                 :            :         {Opt_labelsupport, LABELSUPP_STR},
     354                 :            :         {Opt_error, NULL},
     355                 :            : };
     356                 :            : 
     357                 :            : #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
     358                 :            : 
     359                 :          0 : static int may_context_mount_sb_relabel(u32 sid,
     360                 :            :                         struct superblock_security_struct *sbsec,
     361                 :            :                         const struct cred *cred)
     362                 :            : {
     363                 :          0 :         const struct task_security_struct *tsec = cred->security;
     364                 :            :         int rc;
     365                 :            : 
     366                 :          0 :         rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
     367                 :            :                           FILESYSTEM__RELABELFROM, NULL);
     368         [ #  # ]:          0 :         if (rc)
     369                 :            :                 return rc;
     370                 :            : 
     371                 :          0 :         rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
     372                 :            :                           FILESYSTEM__RELABELTO, NULL);
     373                 :            :         return rc;
     374                 :            : }
     375                 :            : 
     376                 :          0 : static int may_context_mount_inode_relabel(u32 sid,
     377                 :            :                         struct superblock_security_struct *sbsec,
     378                 :            :                         const struct cred *cred)
     379                 :            : {
     380                 :          0 :         const struct task_security_struct *tsec = cred->security;
     381                 :            :         int rc;
     382                 :          0 :         rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
     383                 :            :                           FILESYSTEM__RELABELFROM, NULL);
     384         [ #  # ]:          0 :         if (rc)
     385                 :            :                 return rc;
     386                 :            : 
     387                 :          0 :         rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
     388                 :            :                           FILESYSTEM__ASSOCIATE, NULL);
     389                 :            :         return rc;
     390                 :            : }
     391                 :            : 
     392                 :          0 : static int selinux_is_sblabel_mnt(struct super_block *sb)
     393                 :            : {
     394                 :          0 :         struct superblock_security_struct *sbsec = sb->s_security;
     395                 :            : 
     396         [ #  # ]:          0 :         if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
     397                 :          0 :             sbsec->behavior == SECURITY_FS_USE_TRANS ||
     398                 :            :             sbsec->behavior == SECURITY_FS_USE_TASK)
     399                 :            :                 return 1;
     400                 :            : 
     401                 :            :         /* Special handling for sysfs. Is genfs but also has setxattr handler*/
     402         [ #  # ]:          0 :         if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
     403                 :            :                 return 1;
     404                 :            : 
     405                 :            :         /*
     406                 :            :          * Special handling for rootfs. Is genfs but supports
     407                 :            :          * setting SELinux context on in-core inodes.
     408                 :            :          */
     409         [ #  # ]:          0 :         if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
     410                 :            :                 return 1;
     411                 :            : 
     412                 :            :         return 0;
     413                 :            : }
     414                 :            : 
     415                 :          0 : static int sb_finish_set_opts(struct super_block *sb)
     416                 :            : {
     417                 :          0 :         struct superblock_security_struct *sbsec = sb->s_security;
     418                 :          0 :         struct dentry *root = sb->s_root;
     419                 :          0 :         struct inode *root_inode = root->d_inode;
     420                 :            :         int rc = 0;
     421                 :            : 
     422         [ #  # ]:          0 :         if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
     423                 :            :                 /* Make sure that the xattr handler exists and that no
     424                 :            :                    error other than -ENODATA is returned by getxattr on
     425                 :            :                    the root directory.  -ENODATA is ok, as this may be
     426                 :            :                    the first boot of the SELinux kernel before we have
     427                 :            :                    assigned xattr values to the filesystem. */
     428         [ #  # ]:          0 :                 if (!root_inode->i_op->getxattr) {
     429                 :          0 :                         printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
     430                 :          0 :                                "xattr support\n", sb->s_id, sb->s_type->name);
     431                 :            :                         rc = -EOPNOTSUPP;
     432                 :          0 :                         goto out;
     433                 :            :                 }
     434                 :          0 :                 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
     435         [ #  # ]:          0 :                 if (rc < 0 && rc != -ENODATA) {
     436         [ #  # ]:          0 :                         if (rc == -EOPNOTSUPP)
     437                 :          0 :                                 printk(KERN_WARNING "SELinux: (dev %s, type "
     438                 :            :                                        "%s) has no security xattr handler\n",
     439                 :          0 :                                        sb->s_id, sb->s_type->name);
     440                 :            :                         else
     441                 :          0 :                                 printk(KERN_WARNING "SELinux: (dev %s, type "
     442                 :          0 :                                        "%s) getxattr errno %d\n", sb->s_id,
     443                 :          0 :                                        sb->s_type->name, -rc);
     444                 :            :                         goto out;
     445                 :            :                 }
     446                 :            :         }
     447                 :            : 
     448         [ #  # ]:          0 :         if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
     449                 :          0 :                 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
     450                 :          0 :                        sb->s_id, sb->s_type->name);
     451                 :            :         else
     452                 :          0 :                 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
     453                 :          0 :                        sb->s_id, sb->s_type->name,
     454                 :          0 :                        labeling_behaviors[sbsec->behavior-1]);
     455                 :            : 
     456                 :          0 :         sbsec->flags |= SE_SBINITIALIZED;
     457         [ #  # ]:          0 :         if (selinux_is_sblabel_mnt(sb))
     458                 :          0 :                 sbsec->flags |= SBLABEL_MNT;
     459                 :            : 
     460                 :            :         /* Initialize the root inode. */
     461                 :          0 :         rc = inode_doinit_with_dentry(root_inode, root);
     462                 :            : 
     463                 :            :         /* Initialize any other inodes associated with the superblock, e.g.
     464                 :            :            inodes created prior to initial policy load or inodes created
     465                 :            :            during get_sb by a pseudo filesystem that directly
     466                 :            :            populates itself. */
     467                 :            :         spin_lock(&sbsec->isec_lock);
     468                 :            : next_inode:
     469         [ #  # ]:          0 :         if (!list_empty(&sbsec->isec_head)) {
     470                 :            :                 struct inode_security_struct *isec =
     471                 :            :                                 list_entry(sbsec->isec_head.next,
     472                 :            :                                            struct inode_security_struct, list);
     473                 :          0 :                 struct inode *inode = isec->inode;
     474                 :            :                 spin_unlock(&sbsec->isec_lock);
     475                 :          0 :                 inode = igrab(inode);
     476         [ #  # ]:          0 :                 if (inode) {
     477         [ #  # ]:          0 :                         if (!IS_PRIVATE(inode))
     478                 :            :                                 inode_doinit(inode);
     479                 :          0 :                         iput(inode);
     480                 :            :                 }
     481                 :            :                 spin_lock(&sbsec->isec_lock);
     482                 :          0 :                 list_del_init(&isec->list);
     483                 :            :                 goto next_inode;
     484                 :            :         }
     485                 :            :         spin_unlock(&sbsec->isec_lock);
     486                 :            : out:
     487                 :          0 :         return rc;
     488                 :            : }
     489                 :            : 
     490                 :            : /*
     491                 :            :  * This function should allow an FS to ask what it's mount security
     492                 :            :  * options were so it can use those later for submounts, displaying
     493                 :            :  * mount options, or whatever.
     494                 :            :  */
     495                 :          0 : static int selinux_get_mnt_opts(const struct super_block *sb,
     496                 :            :                                 struct security_mnt_opts *opts)
     497                 :            : {
     498                 :            :         int rc = 0, i;
     499                 :          0 :         struct superblock_security_struct *sbsec = sb->s_security;
     500                 :          0 :         char *context = NULL;
     501                 :            :         u32 len;
     502                 :            :         char tmp;
     503                 :            : 
     504                 :            :         security_init_mnt_opts(opts);
     505                 :            : 
     506         [ #  # ]:          0 :         if (!(sbsec->flags & SE_SBINITIALIZED))
     507                 :            :                 return -EINVAL;
     508                 :            : 
     509         [ #  # ]:          0 :         if (!ss_initialized)
     510                 :            :                 return -EINVAL;
     511                 :            : 
     512                 :            :         /* make sure we always check enough bits to cover the mask */
     513                 :            :         BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
     514                 :            : 
     515                 :          0 :         tmp = sbsec->flags & SE_MNTMASK;
     516                 :            :         /* count the number of mount options for this sb */
     517         [ #  # ]:          0 :         for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
     518         [ #  # ]:          0 :                 if (tmp & 0x01)
     519                 :          0 :                         opts->num_mnt_opts++;
     520                 :          0 :                 tmp >>= 1;
     521                 :            :         }
     522                 :            :         /* Check if the Label support flag is set */
     523         [ #  # ]:          0 :         if (sbsec->flags & SBLABEL_MNT)
     524                 :          0 :                 opts->num_mnt_opts++;
     525                 :            : 
     526                 :          0 :         opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
     527         [ #  # ]:          0 :         if (!opts->mnt_opts) {
     528                 :            :                 rc = -ENOMEM;
     529                 :            :                 goto out_free;
     530                 :            :         }
     531                 :            : 
     532                 :          0 :         opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
     533         [ #  # ]:          0 :         if (!opts->mnt_opts_flags) {
     534                 :            :                 rc = -ENOMEM;
     535                 :            :                 goto out_free;
     536                 :            :         }
     537                 :            : 
     538                 :            :         i = 0;
     539         [ #  # ]:          0 :         if (sbsec->flags & FSCONTEXT_MNT) {
     540                 :          0 :                 rc = security_sid_to_context(sbsec->sid, &context, &len);
     541         [ #  # ]:          0 :                 if (rc)
     542                 :            :                         goto out_free;
     543                 :          0 :                 opts->mnt_opts[i] = context;
     544                 :          0 :                 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
     545                 :            :         }
     546         [ #  # ]:          0 :         if (sbsec->flags & CONTEXT_MNT) {
     547                 :          0 :                 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
     548         [ #  # ]:          0 :                 if (rc)
     549                 :            :                         goto out_free;
     550                 :          0 :                 opts->mnt_opts[i] = context;
     551                 :          0 :                 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
     552                 :            :         }
     553         [ #  # ]:          0 :         if (sbsec->flags & DEFCONTEXT_MNT) {
     554                 :          0 :                 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
     555         [ #  # ]:          0 :                 if (rc)
     556                 :            :                         goto out_free;
     557                 :          0 :                 opts->mnt_opts[i] = context;
     558                 :          0 :                 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
     559                 :            :         }
     560         [ #  # ]:          0 :         if (sbsec->flags & ROOTCONTEXT_MNT) {
     561                 :          0 :                 struct inode *root = sbsec->sb->s_root->d_inode;
     562                 :          0 :                 struct inode_security_struct *isec = root->i_security;
     563                 :            : 
     564                 :          0 :                 rc = security_sid_to_context(isec->sid, &context, &len);
     565         [ #  # ]:          0 :                 if (rc)
     566                 :            :                         goto out_free;
     567                 :          0 :                 opts->mnt_opts[i] = context;
     568                 :          0 :                 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
     569                 :            :         }
     570         [ #  # ]:          0 :         if (sbsec->flags & SBLABEL_MNT) {
     571                 :          0 :                 opts->mnt_opts[i] = NULL;
     572                 :          0 :                 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
     573                 :            :         }
     574                 :            : 
     575         [ #  # ]:          0 :         BUG_ON(i != opts->num_mnt_opts);
     576                 :            : 
     577                 :            :         return 0;
     578                 :            : 
     579                 :            : out_free:
     580                 :            :         security_free_mnt_opts(opts);
     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                 :          0 :         char mnt_flags = sbsec->flags & SE_MNTMASK;
     588                 :            : 
     589                 :            :         /* check if the old mount command had the same options */
     590 [ #  # ][ #  # ]:          0 :         if (sbsec->flags & SE_SBINITIALIZED)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     591 [ #  # ][ #  # ]:          0 :                 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 [ #  # ][ #  # ]:          0 :         if (!(sbsec->flags & SE_SBINITIALIZED))
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     599 [ #  # ][ #  # ]:          0 :                 if (mnt_flags & flag)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     600                 :            :                         return 1;
     601                 :            :         return 0;
     602                 :            : }
     603                 :            : 
     604                 :            : /*
     605                 :            :  * Allow filesystems with binary mount data to explicitly set mount point
     606                 :            :  * labeling information.
     607                 :            :  */
     608                 :          0 : static int selinux_set_mnt_opts(struct super_block *sb,
     609                 :            :                                 struct security_mnt_opts *opts,
     610                 :            :                                 unsigned long kern_flags,
     611                 :            :                                 unsigned long *set_kern_flags)
     612                 :            : {
     613                 :          0 :         const struct cred *cred = current_cred();
     614                 :            :         int rc = 0, i;
     615                 :          0 :         struct superblock_security_struct *sbsec = sb->s_security;
     616                 :          0 :         const char *name = sb->s_type->name;
     617                 :          0 :         struct inode *inode = sbsec->sb->s_root->d_inode;
     618                 :          0 :         struct inode_security_struct *root_isec = inode->i_security;
     619                 :            :         u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
     620                 :            :         u32 defcontext_sid = 0;
     621                 :          0 :         char **mount_options = opts->mnt_opts;
     622                 :          0 :         int *flags = opts->mnt_opts_flags;
     623                 :          0 :         int num_opts = opts->num_mnt_opts;
     624                 :            : 
     625                 :          0 :         mutex_lock(&sbsec->lock);
     626                 :            : 
     627         [ #  # ]:          0 :         if (!ss_initialized) {
     628         [ #  # ]:          0 :                 if (!num_opts) {
     629                 :            :                         /* Defer initialization until selinux_complete_init,
     630                 :            :                            after the initial policy is loaded and the security
     631                 :            :                            server is ready to handle calls. */
     632                 :            :                         goto out;
     633                 :            :                 }
     634                 :            :                 rc = -EINVAL;
     635                 :          0 :                 printk(KERN_WARNING "SELinux: Unable to set superblock options "
     636                 :            :                         "before the security server is initialized\n");
     637                 :          0 :                 goto out;
     638                 :            :         }
     639         [ #  # ]:          0 :         if (kern_flags && !set_kern_flags) {
     640                 :            :                 /* Specifying internal flags without providing a place to
     641                 :            :                  * place the results is not allowed */
     642                 :            :                 rc = -EINVAL;
     643                 :            :                 goto out;
     644                 :            :         }
     645                 :            : 
     646                 :            :         /*
     647                 :            :          * Binary mount data FS will come through this function twice.  Once
     648                 :            :          * from an explicit call and once from the generic calls from the vfs.
     649                 :            :          * Since the generic VFS calls will not contain any security mount data
     650                 :            :          * we need to skip the double mount verification.
     651                 :            :          *
     652                 :            :          * This does open a hole in which we will not notice if the first
     653                 :            :          * mount using this sb set explict options and a second mount using
     654                 :            :          * this sb does not set any security options.  (The first options
     655                 :            :          * will be used for both mounts)
     656                 :            :          */
     657 [ #  # ][ #  # ]:          0 :         if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
     658         [ #  # ]:          0 :             && (num_opts == 0))
     659                 :            :                 goto out;
     660                 :            : 
     661                 :            :         /*
     662                 :            :          * parse the mount options, check if they are valid sids.
     663                 :            :          * also check if someone is trying to mount the same sb more
     664                 :            :          * than once with different security options.
     665                 :            :          */
     666         [ #  # ]:          0 :         for (i = 0; i < num_opts; i++) {
     667                 :            :                 u32 sid;
     668                 :            : 
     669         [ #  # ]:          0 :                 if (flags[i] == SBLABEL_MNT)
     670                 :          0 :                         continue;
     671                 :          0 :                 rc = security_context_to_sid(mount_options[i],
     672                 :          0 :                                              strlen(mount_options[i]), &sid);
     673         [ #  # ]:          0 :                 if (rc) {
     674                 :          0 :                         printk(KERN_WARNING "SELinux: security_context_to_sid"
     675                 :            :                                "(%s) failed for (dev %s, type %s) errno=%d\n",
     676                 :          0 :                                mount_options[i], sb->s_id, name, rc);
     677                 :          0 :                         goto out;
     678                 :            :                 }
     679   [ #  #  #  #  :          0 :                 switch (flags[i]) {
                      # ]
     680                 :            :                 case FSCONTEXT_MNT:
     681                 :          0 :                         fscontext_sid = sid;
     682                 :            : 
     683         [ #  # ]:          0 :                         if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
     684                 :            :                                         fscontext_sid))
     685                 :            :                                 goto out_double_mount;
     686                 :            : 
     687                 :          0 :                         sbsec->flags |= FSCONTEXT_MNT;
     688                 :          0 :                         break;
     689                 :            :                 case CONTEXT_MNT:
     690                 :          0 :                         context_sid = sid;
     691                 :            : 
     692         [ #  # ]:          0 :                         if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
     693                 :            :                                         context_sid))
     694                 :            :                                 goto out_double_mount;
     695                 :            : 
     696                 :          0 :                         sbsec->flags |= CONTEXT_MNT;
     697                 :          0 :                         break;
     698                 :            :                 case ROOTCONTEXT_MNT:
     699                 :          0 :                         rootcontext_sid = sid;
     700                 :            : 
     701         [ #  # ]:          0 :                         if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
     702                 :            :                                         rootcontext_sid))
     703                 :            :                                 goto out_double_mount;
     704                 :            : 
     705                 :          0 :                         sbsec->flags |= ROOTCONTEXT_MNT;
     706                 :            : 
     707                 :          0 :                         break;
     708                 :            :                 case DEFCONTEXT_MNT:
     709                 :          0 :                         defcontext_sid = sid;
     710                 :            : 
     711         [ #  # ]:          0 :                         if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
     712                 :            :                                         defcontext_sid))
     713                 :            :                                 goto out_double_mount;
     714                 :            : 
     715                 :          0 :                         sbsec->flags |= DEFCONTEXT_MNT;
     716                 :            : 
     717                 :          0 :                         break;
     718                 :            :                 default:
     719                 :            :                         rc = -EINVAL;
     720                 :            :                         goto out;
     721                 :            :                 }
     722                 :            :         }
     723                 :            : 
     724         [ #  # ]:          0 :         if (sbsec->flags & SE_SBINITIALIZED) {
     725                 :            :                 /* previously mounted with options, but not on this attempt? */
     726 [ #  # ][ #  # ]:          0 :                 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
     727                 :            :                         goto out_double_mount;
     728                 :            :                 rc = 0;
     729                 :            :                 goto out;
     730                 :            :         }
     731                 :            : 
     732         [ #  # ]:          0 :         if (strcmp(sb->s_type->name, "proc") == 0)
     733                 :          0 :                 sbsec->flags |= SE_SBPROC;
     734                 :            : 
     735         [ #  # ]:          0 :         if (!sbsec->behavior) {
     736                 :            :                 /*
     737                 :            :                  * Determine the labeling behavior to use for this
     738                 :            :                  * filesystem type.
     739                 :            :                  */
     740                 :          0 :                 rc = security_fs_use(sb);
     741         [ #  # ]:          0 :                 if (rc) {
     742                 :          0 :                         printk(KERN_WARNING
     743                 :            :                                 "%s: security_fs_use(%s) returned %d\n",
     744                 :          0 :                                         __func__, sb->s_type->name, rc);
     745                 :          0 :                         goto out;
     746                 :            :                 }
     747                 :            :         }
     748                 :            :         /* sets the context of the superblock for the fs being mounted. */
     749         [ #  # ]:          0 :         if (fscontext_sid) {
     750                 :          0 :                 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
     751         [ #  # ]:          0 :                 if (rc)
     752                 :            :                         goto out;
     753                 :            : 
     754                 :          0 :                 sbsec->sid = fscontext_sid;
     755                 :            :         }
     756                 :            : 
     757                 :            :         /*
     758                 :            :          * Switch to using mount point labeling behavior.
     759                 :            :          * sets the label used on all file below the mountpoint, and will set
     760                 :            :          * the superblock context if not already set.
     761                 :            :          */
     762 [ #  # ][ #  # ]:          0 :         if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
     763                 :          0 :                 sbsec->behavior = SECURITY_FS_USE_NATIVE;
     764                 :          0 :                 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
     765                 :            :         }
     766                 :            : 
     767         [ #  # ]:          0 :         if (context_sid) {
     768         [ #  # ]:          0 :                 if (!fscontext_sid) {
     769                 :          0 :                         rc = may_context_mount_sb_relabel(context_sid, sbsec,
     770                 :            :                                                           cred);
     771         [ #  # ]:          0 :                         if (rc)
     772                 :            :                                 goto out;
     773                 :          0 :                         sbsec->sid = context_sid;
     774                 :            :                 } else {
     775                 :          0 :                         rc = may_context_mount_inode_relabel(context_sid, sbsec,
     776                 :            :                                                              cred);
     777         [ #  # ]:          0 :                         if (rc)
     778                 :            :                                 goto out;
     779                 :            :                 }
     780         [ #  # ]:          0 :                 if (!rootcontext_sid)
     781                 :            :                         rootcontext_sid = context_sid;
     782                 :            : 
     783                 :          0 :                 sbsec->mntpoint_sid = context_sid;
     784                 :          0 :                 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
     785                 :            :         }
     786                 :            : 
     787         [ #  # ]:          0 :         if (rootcontext_sid) {
     788                 :          0 :                 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
     789                 :            :                                                      cred);
     790         [ #  # ]:          0 :                 if (rc)
     791                 :            :                         goto out;
     792                 :            : 
     793                 :          0 :                 root_isec->sid = rootcontext_sid;
     794                 :          0 :                 root_isec->initialized = 1;
     795                 :            :         }
     796                 :            : 
     797         [ #  # ]:          0 :         if (defcontext_sid) {
     798         [ #  # ]:          0 :                 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
     799                 :            :                         sbsec->behavior != SECURITY_FS_USE_NATIVE) {
     800                 :            :                         rc = -EINVAL;
     801                 :          0 :                         printk(KERN_WARNING "SELinux: defcontext option is "
     802                 :            :                                "invalid for this filesystem type\n");
     803                 :          0 :                         goto out;
     804                 :            :                 }
     805                 :            : 
     806         [ #  # ]:          0 :                 if (defcontext_sid != sbsec->def_sid) {
     807                 :          0 :                         rc = may_context_mount_inode_relabel(defcontext_sid,
     808                 :            :                                                              sbsec, cred);
     809         [ #  # ]:          0 :                         if (rc)
     810                 :            :                                 goto out;
     811                 :            :                 }
     812                 :            : 
     813                 :          0 :                 sbsec->def_sid = defcontext_sid;
     814                 :            :         }
     815                 :            : 
     816                 :          0 :         rc = sb_finish_set_opts(sb);
     817                 :            : out:
     818                 :          0 :         mutex_unlock(&sbsec->lock);
     819                 :          0 :         return rc;
     820                 :            : out_double_mount:
     821                 :            :         rc = -EINVAL;
     822                 :          0 :         printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, different "
     823                 :          0 :                "security settings for (dev %s, type %s)\n", sb->s_id, name);
     824                 :          0 :         goto out;
     825                 :            : }
     826                 :            : 
     827                 :          0 : static int selinux_cmp_sb_context(const struct super_block *oldsb,
     828                 :            :                                     const struct super_block *newsb)
     829                 :            : {
     830                 :          0 :         struct superblock_security_struct *old = oldsb->s_security;
     831                 :          0 :         struct superblock_security_struct *new = newsb->s_security;
     832                 :          0 :         char oldflags = old->flags & SE_MNTMASK;
     833                 :          0 :         char newflags = new->flags & SE_MNTMASK;
     834                 :            : 
     835         [ #  # ]:          0 :         if (oldflags != newflags)
     836                 :            :                 goto mismatch;
     837 [ #  # ][ #  # ]:          0 :         if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
     838                 :            :                 goto mismatch;
     839 [ #  # ][ #  # ]:          0 :         if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
     840                 :            :                 goto mismatch;
     841 [ #  # ][ #  # ]:          0 :         if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
     842                 :            :                 goto mismatch;
     843         [ #  # ]:          0 :         if (oldflags & ROOTCONTEXT_MNT) {
     844                 :          0 :                 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
     845                 :          0 :                 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
     846         [ #  # ]:          0 :                 if (oldroot->sid != newroot->sid)
     847                 :            :                         goto mismatch;
     848                 :            :         }
     849                 :            :         return 0;
     850                 :            : mismatch:
     851                 :          0 :         printk(KERN_WARNING "SELinux: mount invalid.  Same superblock, "
     852                 :            :                             "different security settings for (dev %s, "
     853                 :          0 :                             "type %s)\n", newsb->s_id, newsb->s_type->name);
     854                 :            :         return -EBUSY;
     855                 :            : }
     856                 :            : 
     857                 :          0 : static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
     858                 :            :                                         struct super_block *newsb)
     859                 :            : {
     860                 :          0 :         const struct superblock_security_struct *oldsbsec = oldsb->s_security;
     861                 :          0 :         struct superblock_security_struct *newsbsec = newsb->s_security;
     862                 :            : 
     863                 :          0 :         int set_fscontext =     (oldsbsec->flags & FSCONTEXT_MNT);
     864                 :          0 :         int set_context =       (oldsbsec->flags & CONTEXT_MNT);
     865                 :          0 :         int set_rootcontext =   (oldsbsec->flags & ROOTCONTEXT_MNT);
     866                 :            : 
     867                 :            :         /*
     868                 :            :          * if the parent was able to be mounted it clearly had no special lsm
     869                 :            :          * mount options.  thus we can safely deal with this superblock later
     870                 :            :          */
     871         [ #  # ]:          0 :         if (!ss_initialized)
     872                 :            :                 return 0;
     873                 :            : 
     874                 :            :         /* how can we clone if the old one wasn't set up?? */
     875         [ #  # ]:          0 :         BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
     876                 :            : 
     877                 :            :         /* if fs is reusing a sb, make sure that the contexts match */
     878         [ #  # ]:          0 :         if (newsbsec->flags & SE_SBINITIALIZED)
     879                 :          0 :                 return selinux_cmp_sb_context(oldsb, newsb);
     880                 :            : 
     881                 :          0 :         mutex_lock(&newsbsec->lock);
     882                 :            : 
     883                 :          0 :         newsbsec->flags = oldsbsec->flags;
     884                 :            : 
     885                 :          0 :         newsbsec->sid = oldsbsec->sid;
     886                 :          0 :         newsbsec->def_sid = oldsbsec->def_sid;
     887                 :          0 :         newsbsec->behavior = oldsbsec->behavior;
     888                 :            : 
     889         [ #  # ]:          0 :         if (set_context) {
     890                 :          0 :                 u32 sid = oldsbsec->mntpoint_sid;
     891                 :            : 
     892         [ #  # ]:          0 :                 if (!set_fscontext)
     893                 :          0 :                         newsbsec->sid = sid;
     894         [ #  # ]:          0 :                 if (!set_rootcontext) {
     895                 :          0 :                         struct inode *newinode = newsb->s_root->d_inode;
     896                 :          0 :                         struct inode_security_struct *newisec = newinode->i_security;
     897                 :          0 :                         newisec->sid = sid;
     898                 :            :                 }
     899                 :          0 :                 newsbsec->mntpoint_sid = sid;
     900                 :            :         }
     901         [ #  # ]:          0 :         if (set_rootcontext) {
     902                 :          0 :                 const struct inode *oldinode = oldsb->s_root->d_inode;
     903                 :          0 :                 const struct inode_security_struct *oldisec = oldinode->i_security;
     904                 :          0 :                 struct inode *newinode = newsb->s_root->d_inode;
     905                 :          0 :                 struct inode_security_struct *newisec = newinode->i_security;
     906                 :            : 
     907                 :          0 :                 newisec->sid = oldisec->sid;
     908                 :            :         }
     909                 :            : 
     910                 :          0 :         sb_finish_set_opts(newsb);
     911                 :          0 :         mutex_unlock(&newsbsec->lock);
     912                 :          0 :         return 0;
     913                 :            : }
     914                 :            : 
     915                 :          0 : static int selinux_parse_opts_str(char *options,
     916                 :            :                                   struct security_mnt_opts *opts)
     917                 :            : {
     918                 :            :         char *p;
     919                 :            :         char *context = NULL, *defcontext = NULL;
     920                 :            :         char *fscontext = NULL, *rootcontext = NULL;
     921                 :            :         int rc, num_mnt_opts = 0;
     922                 :            : 
     923                 :          0 :         opts->num_mnt_opts = 0;
     924                 :            : 
     925                 :            :         /* Standard string-based options. */
     926         [ #  # ]:          0 :         while ((p = strsep(&options, "|")) != NULL) {
     927                 :            :                 int token;
     928                 :            :                 substring_t args[MAX_OPT_ARGS];
     929                 :            : 
     930         [ #  # ]:          0 :                 if (!*p)
     931                 :          0 :                         continue;
     932                 :            : 
     933                 :          0 :                 token = match_token(p, tokens, args);
     934                 :            : 
     935   [ #  #  #  #  :          0 :                 switch (token) {
                   #  # ]
     936                 :            :                 case Opt_context:
     937         [ #  # ]:          0 :                         if (context || defcontext) {
     938                 :            :                                 rc = -EINVAL;
     939                 :          0 :                                 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
     940                 :          0 :                                 goto out_err;
     941                 :            :                         }
     942                 :          0 :                         context = match_strdup(&args[0]);
     943         [ #  # ]:          0 :                         if (!context) {
     944                 :            :                                 rc = -ENOMEM;
     945                 :            :                                 goto out_err;
     946                 :            :                         }
     947                 :            :                         break;
     948                 :            : 
     949                 :            :                 case Opt_fscontext:
     950         [ #  # ]:          0 :                         if (fscontext) {
     951                 :            :                                 rc = -EINVAL;
     952                 :          0 :                                 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
     953                 :          0 :                                 goto out_err;
     954                 :            :                         }
     955                 :          0 :                         fscontext = match_strdup(&args[0]);
     956         [ #  # ]:          0 :                         if (!fscontext) {
     957                 :            :                                 rc = -ENOMEM;
     958                 :            :                                 goto out_err;
     959                 :            :                         }
     960                 :            :                         break;
     961                 :            : 
     962                 :            :                 case Opt_rootcontext:
     963         [ #  # ]:          0 :                         if (rootcontext) {
     964                 :            :                                 rc = -EINVAL;
     965                 :          0 :                                 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
     966                 :          0 :                                 goto out_err;
     967                 :            :                         }
     968                 :          0 :                         rootcontext = match_strdup(&args[0]);
     969         [ #  # ]:          0 :                         if (!rootcontext) {
     970                 :            :                                 rc = -ENOMEM;
     971                 :            :                                 goto out_err;
     972                 :            :                         }
     973                 :            :                         break;
     974                 :            : 
     975                 :            :                 case Opt_defcontext:
     976         [ #  # ]:          0 :                         if (context || defcontext) {
     977                 :            :                                 rc = -EINVAL;
     978                 :          0 :                                 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
     979                 :          0 :                                 goto out_err;
     980                 :            :                         }
     981                 :          0 :                         defcontext = match_strdup(&args[0]);
     982         [ #  # ]:          0 :                         if (!defcontext) {
     983                 :            :                                 rc = -ENOMEM;
     984                 :            :                                 goto out_err;
     985                 :            :                         }
     986                 :            :                         break;
     987                 :            :                 case Opt_labelsupport:
     988                 :            :                         break;
     989                 :            :                 default:
     990                 :            :                         rc = -EINVAL;
     991                 :          0 :                         printk(KERN_WARNING "SELinux:  unknown mount option\n");
     992                 :          0 :                         goto out_err;
     993                 :            : 
     994                 :            :                 }
     995                 :            :         }
     996                 :            : 
     997                 :            :         rc = -ENOMEM;
     998                 :          0 :         opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
     999         [ #  # ]:          0 :         if (!opts->mnt_opts)
    1000                 :            :                 goto out_err;
    1001                 :            : 
    1002                 :          0 :         opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
    1003         [ #  # ]:          0 :         if (!opts->mnt_opts_flags) {
    1004                 :          0 :                 kfree(opts->mnt_opts);
    1005                 :          0 :                 goto out_err;
    1006                 :            :         }
    1007                 :            : 
    1008         [ #  # ]:          0 :         if (fscontext) {
    1009                 :          0 :                 opts->mnt_opts[num_mnt_opts] = fscontext;
    1010                 :          0 :                 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
    1011                 :            :         }
    1012         [ #  # ]:          0 :         if (context) {
    1013                 :          0 :                 opts->mnt_opts[num_mnt_opts] = context;
    1014                 :          0 :                 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
    1015                 :            :         }
    1016         [ #  # ]:          0 :         if (rootcontext) {
    1017                 :          0 :                 opts->mnt_opts[num_mnt_opts] = rootcontext;
    1018                 :          0 :                 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
    1019                 :            :         }
    1020         [ #  # ]:          0 :         if (defcontext) {
    1021                 :          0 :                 opts->mnt_opts[num_mnt_opts] = defcontext;
    1022                 :          0 :                 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
    1023                 :            :         }
    1024                 :            : 
    1025                 :          0 :         opts->num_mnt_opts = num_mnt_opts;
    1026                 :          0 :         return 0;
    1027                 :            : 
    1028                 :            : out_err:
    1029                 :          0 :         kfree(context);
    1030                 :          0 :         kfree(defcontext);
    1031                 :          0 :         kfree(fscontext);
    1032                 :          0 :         kfree(rootcontext);
    1033                 :          0 :         return rc;
    1034                 :            : }
    1035                 :            : /*
    1036                 :            :  * string mount options parsing and call set the sbsec
    1037                 :            :  */
    1038                 :          0 : static int superblock_doinit(struct super_block *sb, void *data)
    1039                 :            : {
    1040                 :            :         int rc = 0;
    1041                 :            :         char *options = data;
    1042                 :            :         struct security_mnt_opts opts;
    1043                 :            : 
    1044                 :            :         security_init_mnt_opts(&opts);
    1045                 :            : 
    1046         [ #  # ]:          0 :         if (!data)
    1047                 :            :                 goto out;
    1048                 :            : 
    1049         [ #  # ]:          0 :         BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
    1050                 :            : 
    1051                 :          0 :         rc = selinux_parse_opts_str(options, &opts);
    1052         [ #  # ]:          0 :         if (rc)
    1053                 :            :                 goto out_err;
    1054                 :            : 
    1055                 :            : out:
    1056                 :          0 :         rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
    1057                 :            : 
    1058                 :            : out_err:
    1059                 :            :         security_free_mnt_opts(&opts);
    1060                 :          0 :         return rc;
    1061                 :            : }
    1062                 :            : 
    1063                 :          0 : static void selinux_write_opts(struct seq_file *m,
    1064                 :            :                                struct security_mnt_opts *opts)
    1065                 :            : {
    1066                 :            :         int i;
    1067                 :            :         char *prefix;
    1068                 :            : 
    1069         [ #  # ]:          0 :         for (i = 0; i < opts->num_mnt_opts; i++) {
    1070                 :            :                 char *has_comma;
    1071                 :            : 
    1072         [ #  # ]:          0 :                 if (opts->mnt_opts[i])
    1073                 :          0 :                         has_comma = strchr(opts->mnt_opts[i], ',');
    1074                 :            :                 else
    1075                 :            :                         has_comma = NULL;
    1076                 :            : 
    1077   [ #  #  #  #  :          0 :                 switch (opts->mnt_opts_flags[i]) {
                   #  # ]
    1078                 :            :                 case CONTEXT_MNT:
    1079                 :            :                         prefix = CONTEXT_STR;
    1080                 :            :                         break;
    1081                 :            :                 case FSCONTEXT_MNT:
    1082                 :            :                         prefix = FSCONTEXT_STR;
    1083                 :          0 :                         break;
    1084                 :            :                 case ROOTCONTEXT_MNT:
    1085                 :            :                         prefix = ROOTCONTEXT_STR;
    1086                 :          0 :                         break;
    1087                 :            :                 case DEFCONTEXT_MNT:
    1088                 :            :                         prefix = DEFCONTEXT_STR;
    1089                 :          0 :                         break;
    1090                 :            :                 case SBLABEL_MNT:
    1091                 :          0 :                         seq_putc(m, ',');
    1092                 :          0 :                         seq_puts(m, LABELSUPP_STR);
    1093                 :          0 :                         continue;
    1094                 :            :                 default:
    1095                 :          0 :                         BUG();
    1096                 :            :                         return;
    1097                 :            :                 };
    1098                 :            :                 /* we need a comma before each option */
    1099                 :          0 :                 seq_putc(m, ',');
    1100                 :          0 :                 seq_puts(m, prefix);
    1101         [ #  # ]:          0 :                 if (has_comma)
    1102                 :          0 :                         seq_putc(m, '\"');
    1103                 :          0 :                 seq_puts(m, opts->mnt_opts[i]);
    1104         [ #  # ]:          0 :                 if (has_comma)
    1105                 :          0 :                         seq_putc(m, '\"');
    1106                 :            :         }
    1107                 :          0 : }
    1108                 :            : 
    1109                 :          0 : static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
    1110                 :            : {
    1111                 :            :         struct security_mnt_opts opts;
    1112                 :            :         int rc;
    1113                 :            : 
    1114                 :          0 :         rc = selinux_get_mnt_opts(sb, &opts);
    1115         [ #  # ]:          0 :         if (rc) {
    1116                 :            :                 /* before policy load we may get EINVAL, don't show anything */
    1117         [ #  # ]:          0 :                 if (rc == -EINVAL)
    1118                 :            :                         rc = 0;
    1119                 :          0 :                 return rc;
    1120                 :            :         }
    1121                 :            : 
    1122                 :          0 :         selinux_write_opts(m, &opts);
    1123                 :            : 
    1124                 :            :         security_free_mnt_opts(&opts);
    1125                 :            : 
    1126                 :          0 :         return rc;
    1127                 :            : }
    1128                 :            : 
    1129                 :            : static inline u16 inode_mode_to_security_class(umode_t mode)
    1130                 :            : {
    1131   [ #  #  #  #  :          0 :         switch (mode & S_IFMT) {
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
    1132                 :            :         case S_IFSOCK:
    1133                 :            :                 return SECCLASS_SOCK_FILE;
    1134                 :            :         case S_IFLNK:
    1135                 :            :                 return SECCLASS_LNK_FILE;
    1136                 :            :         case S_IFREG:
    1137                 :            :                 return SECCLASS_FILE;
    1138                 :            :         case S_IFBLK:
    1139                 :            :                 return SECCLASS_BLK_FILE;
    1140                 :            :         case S_IFDIR:
    1141                 :            :                 return SECCLASS_DIR;
    1142                 :            :         case S_IFCHR:
    1143                 :            :                 return SECCLASS_CHR_FILE;
    1144                 :            :         case S_IFIFO:
    1145                 :            :                 return SECCLASS_FIFO_FILE;
    1146                 :            : 
    1147                 :            :         }
    1148                 :            : 
    1149                 :            :         return SECCLASS_FILE;
    1150                 :            : }
    1151                 :            : 
    1152                 :            : static inline int default_protocol_stream(int protocol)
    1153                 :            : {
    1154                 :          0 :         return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
    1155                 :            : }
    1156                 :            : 
    1157                 :            : static inline int default_protocol_dgram(int protocol)
    1158                 :            : {
    1159                 :          0 :         return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
    1160                 :            : }
    1161                 :            : 
    1162                 :            : static inline u16 socket_type_to_security_class(int family, int type, int protocol)
    1163                 :            : {
    1164   [ #  #  #  #  :          0 :         switch (family) {
           #  #  # ][ #  
          #  #  #  #  #  
                      # ]
    1165                 :            :         case PF_UNIX:
    1166      [ #  #  # ]:          0 :                 switch (type) {
              [ #  #  # ]
    1167                 :            :                 case SOCK_STREAM:
    1168                 :            :                 case SOCK_SEQPACKET:
    1169                 :            :                         return SECCLASS_UNIX_STREAM_SOCKET;
    1170                 :            :                 case SOCK_DGRAM:
    1171                 :            :                         return SECCLASS_UNIX_DGRAM_SOCKET;
    1172                 :            :                 }
    1173                 :            :                 break;
    1174                 :            :         case PF_INET:
    1175                 :            :         case PF_INET6:
    1176   [ #  #  #  # ]:          0 :                 switch (type) {
           [ #  #  #  # ]
    1177                 :            :                 case SOCK_STREAM:
    1178 [ #  # ][ #  # ]:          0 :                         if (default_protocol_stream(protocol))
    1179                 :            :                                 return SECCLASS_TCP_SOCKET;
    1180                 :            :                         else
    1181                 :            :                                 return SECCLASS_RAWIP_SOCKET;
    1182                 :            :                 case SOCK_DGRAM:
    1183 [ #  # ][ #  # ]:          0 :                         if (default_protocol_dgram(protocol))
         [ #  # ][ #  # ]
    1184                 :            :                                 return SECCLASS_UDP_SOCKET;
    1185                 :            :                         else
    1186                 :            :                                 return SECCLASS_RAWIP_SOCKET;
    1187                 :            :                 case SOCK_DCCP:
    1188                 :            :                         return SECCLASS_DCCP_SOCKET;
    1189                 :            :                 default:
    1190                 :            :                         return SECCLASS_RAWIP_SOCKET;
    1191                 :            :                 }
    1192                 :            :                 break;
    1193                 :            :         case PF_NETLINK:
    1194                 :            :                 switch (protocol) {
    1195                 :            :                 case NETLINK_ROUTE:
    1196                 :            :                         return SECCLASS_NETLINK_ROUTE_SOCKET;
    1197                 :            :                 case NETLINK_FIREWALL:
    1198                 :            :                         return SECCLASS_NETLINK_FIREWALL_SOCKET;
    1199                 :            :                 case NETLINK_SOCK_DIAG:
    1200                 :            :                         return SECCLASS_NETLINK_TCPDIAG_SOCKET;
    1201                 :            :                 case NETLINK_NFLOG:
    1202                 :            :                         return SECCLASS_NETLINK_NFLOG_SOCKET;
    1203                 :            :                 case NETLINK_XFRM:
    1204                 :            :                         return SECCLASS_NETLINK_XFRM_SOCKET;
    1205                 :            :                 case NETLINK_SELINUX:
    1206                 :            :                         return SECCLASS_NETLINK_SELINUX_SOCKET;
    1207                 :            :                 case NETLINK_AUDIT:
    1208                 :            :                         return SECCLASS_NETLINK_AUDIT_SOCKET;
    1209                 :            :                 case NETLINK_IP6_FW:
    1210                 :            :                         return SECCLASS_NETLINK_IP6FW_SOCKET;
    1211                 :            :                 case NETLINK_DNRTMSG:
    1212                 :            :                         return SECCLASS_NETLINK_DNRT_SOCKET;
    1213                 :            :                 case NETLINK_KOBJECT_UEVENT:
    1214                 :            :                         return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
    1215                 :            :                 default:
    1216                 :            :                         return SECCLASS_NETLINK_SOCKET;
    1217                 :            :                 }
    1218                 :            :         case PF_PACKET:
    1219                 :            :                 return SECCLASS_PACKET_SOCKET;
    1220                 :            :         case PF_KEY:
    1221                 :            :                 return SECCLASS_KEY_SOCKET;
    1222                 :            :         case PF_APPLETALK:
    1223                 :            :                 return SECCLASS_APPLETALK_SOCKET;
    1224                 :            :         }
    1225                 :            : 
    1226                 :            :         return SECCLASS_SOCKET;
    1227                 :            : }
    1228                 :            : 
    1229                 :            : #ifdef CONFIG_PROC_FS
    1230                 :          0 : static int selinux_proc_get_sid(struct dentry *dentry,
    1231                 :            :                                 u16 tclass,
    1232                 :            :                                 u32 *sid)
    1233                 :            : {
    1234                 :            :         int rc;
    1235                 :            :         char *buffer, *path;
    1236                 :            : 
    1237                 :          0 :         buffer = (char *)__get_free_page(GFP_KERNEL);
    1238         [ #  # ]:          0 :         if (!buffer)
    1239                 :            :                 return -ENOMEM;
    1240                 :            : 
    1241                 :          0 :         path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
    1242         [ #  # ]:          0 :         if (IS_ERR(path))
    1243                 :            :                 rc = PTR_ERR(path);
    1244                 :            :         else {
    1245                 :            :                 /* each process gets a /proc/PID/ entry. Strip off the
    1246                 :            :                  * PID part to get a valid selinux labeling.
    1247                 :            :                  * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
    1248         [ #  # ]:          0 :                 while (path[1] >= '0' && path[1] <= '9') {
    1249                 :          0 :                         path[1] = '/';
    1250                 :          0 :                         path++;
    1251                 :            :                 }
    1252                 :          0 :                 rc = security_genfs_sid("proc", path, tclass, sid);
    1253                 :            :         }
    1254                 :          0 :         free_page((unsigned long)buffer);
    1255                 :          0 :         return rc;
    1256                 :            : }
    1257                 :            : #else
    1258                 :            : static int selinux_proc_get_sid(struct dentry *dentry,
    1259                 :            :                                 u16 tclass,
    1260                 :            :                                 u32 *sid)
    1261                 :            : {
    1262                 :            :         return -EINVAL;
    1263                 :            : }
    1264                 :            : #endif
    1265                 :            : 
    1266                 :            : /* The inode's security attributes must be initialized before first use. */
    1267                 :          0 : static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
    1268                 :            : {
    1269                 :            :         struct superblock_security_struct *sbsec = NULL;
    1270                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    1271                 :            :         u32 sid;
    1272                 :            :         struct dentry *dentry;
    1273                 :            : #define INITCONTEXTLEN 255
    1274                 :            :         char *context = NULL;
    1275                 :            :         unsigned len = 0;
    1276                 :            :         int rc = 0;
    1277                 :            : 
    1278         [ #  # ]:          0 :         if (isec->initialized)
    1279                 :            :                 goto out;
    1280                 :            : 
    1281                 :          0 :         mutex_lock(&isec->lock);
    1282         [ #  # ]:          0 :         if (isec->initialized)
    1283                 :            :                 goto out_unlock;
    1284                 :            : 
    1285                 :          0 :         sbsec = inode->i_sb->s_security;
    1286         [ #  # ]:          0 :         if (!(sbsec->flags & SE_SBINITIALIZED)) {
    1287                 :            :                 /* Defer initialization until selinux_complete_init,
    1288                 :            :                    after the initial policy is loaded and the security
    1289                 :            :                    server is ready to handle calls. */
    1290                 :            :                 spin_lock(&sbsec->isec_lock);
    1291         [ #  # ]:          0 :                 if (list_empty(&isec->list))
    1292                 :          0 :                         list_add(&isec->list, &sbsec->isec_head);
    1293                 :            :                 spin_unlock(&sbsec->isec_lock);
    1294                 :            :                 goto out_unlock;
    1295                 :            :         }
    1296                 :            : 
    1297   [ #  #  #  #  :          0 :         switch (sbsec->behavior) {
                   #  # ]
    1298                 :            :         case SECURITY_FS_USE_NATIVE:
    1299                 :            :                 break;
    1300                 :            :         case SECURITY_FS_USE_XATTR:
    1301         [ #  # ]:          0 :                 if (!inode->i_op->getxattr) {
    1302                 :          0 :                         isec->sid = sbsec->def_sid;
    1303                 :          0 :                         break;
    1304                 :            :                 }
    1305                 :            : 
    1306                 :            :                 /* Need a dentry, since the xattr API requires one.
    1307                 :            :                    Life would be simpler if we could just pass the inode. */
    1308         [ #  # ]:          0 :                 if (opt_dentry) {
    1309                 :            :                         /* Called from d_instantiate or d_splice_alias. */
    1310                 :            :                         dentry = dget(opt_dentry);
    1311                 :            :                 } else {
    1312                 :            :                         /* Called from selinux_complete_init, try to find a dentry. */
    1313                 :          0 :                         dentry = d_find_alias(inode);
    1314                 :            :                 }
    1315         [ #  # ]:          0 :                 if (!dentry) {
    1316                 :            :                         /*
    1317                 :            :                          * this is can be hit on boot when a file is accessed
    1318                 :            :                          * before the policy is loaded.  When we load policy we
    1319                 :            :                          * may find inodes that have no dentry on the
    1320                 :            :                          * sbsec->isec_head list.  No reason to complain as these
    1321                 :            :                          * will get fixed up the next time we go through
    1322                 :            :                          * inode_doinit with a dentry, before these inodes could
    1323                 :            :                          * be used again by userspace.
    1324                 :            :                          */
    1325                 :            :                         goto out_unlock;
    1326                 :            :                 }
    1327                 :            : 
    1328                 :            :                 len = INITCONTEXTLEN;
    1329                 :            :                 context = kmalloc(len+1, GFP_NOFS);
    1330         [ #  # ]:          0 :                 if (!context) {
    1331                 :            :                         rc = -ENOMEM;
    1332                 :          0 :                         dput(dentry);
    1333                 :          0 :                         goto out_unlock;
    1334                 :            :                 }
    1335                 :          0 :                 context[len] = '\0';
    1336                 :          0 :                 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
    1337                 :            :                                            context, len);
    1338         [ #  # ]:          0 :                 if (rc == -ERANGE) {
    1339                 :          0 :                         kfree(context);
    1340                 :            : 
    1341                 :            :                         /* Need a larger buffer.  Query for the right size. */
    1342                 :          0 :                         rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
    1343                 :            :                                                    NULL, 0);
    1344         [ #  # ]:          0 :                         if (rc < 0) {
    1345                 :          0 :                                 dput(dentry);
    1346                 :          0 :                                 goto out_unlock;
    1347                 :            :                         }
    1348                 :          0 :                         len = rc;
    1349                 :          0 :                         context = kmalloc(len+1, GFP_NOFS);
    1350         [ #  # ]:          0 :                         if (!context) {
    1351                 :            :                                 rc = -ENOMEM;
    1352                 :          0 :                                 dput(dentry);
    1353                 :          0 :                                 goto out_unlock;
    1354                 :            :                         }
    1355                 :          0 :                         context[len] = '\0';
    1356                 :          0 :                         rc = inode->i_op->getxattr(dentry,
    1357                 :            :                                                    XATTR_NAME_SELINUX,
    1358                 :            :                                                    context, len);
    1359                 :            :                 }
    1360                 :          0 :                 dput(dentry);
    1361         [ #  # ]:          0 :                 if (rc < 0) {
    1362         [ #  # ]:          0 :                         if (rc != -ENODATA) {
    1363                 :          0 :                                 printk(KERN_WARNING "SELinux: %s:  getxattr returned "
    1364                 :            :                                        "%d for dev=%s ino=%ld\n", __func__,
    1365                 :          0 :                                        -rc, inode->i_sb->s_id, inode->i_ino);
    1366                 :          0 :                                 kfree(context);
    1367                 :          0 :                                 goto out_unlock;
    1368                 :            :                         }
    1369                 :            :                         /* Map ENODATA to the default file SID */
    1370                 :          0 :                         sid = sbsec->def_sid;
    1371                 :            :                         rc = 0;
    1372                 :            :                 } else {
    1373                 :          0 :                         rc = security_context_to_sid_default(context, rc, &sid,
    1374                 :            :                                                              sbsec->def_sid,
    1375                 :            :                                                              GFP_NOFS);
    1376         [ #  # ]:          0 :                         if (rc) {
    1377                 :          0 :                                 char *dev = inode->i_sb->s_id;
    1378                 :          0 :                                 unsigned long ino = inode->i_ino;
    1379                 :            : 
    1380         [ #  # ]:          0 :                                 if (rc == -EINVAL) {
    1381         [ #  # ]:          0 :                                         if (printk_ratelimit())
    1382                 :          0 :                                                 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
    1383                 :            :                                                         "context=%s.  This indicates you may need to relabel the inode or the "
    1384                 :            :                                                         "filesystem in question.\n", ino, dev, context);
    1385                 :            :                                 } else {
    1386                 :          0 :                                         printk(KERN_WARNING "SELinux: %s:  context_to_sid(%s) "
    1387                 :            :                                                "returned %d for dev=%s ino=%ld\n",
    1388                 :            :                                                __func__, context, -rc, dev, ino);
    1389                 :            :                                 }
    1390                 :          0 :                                 kfree(context);
    1391                 :            :                                 /* Leave with the unlabeled SID */
    1392                 :            :                                 rc = 0;
    1393                 :          0 :                                 break;
    1394                 :            :                         }
    1395                 :            :                 }
    1396                 :          0 :                 kfree(context);
    1397                 :          0 :                 isec->sid = sid;
    1398                 :          0 :                 break;
    1399                 :            :         case SECURITY_FS_USE_TASK:
    1400                 :          0 :                 isec->sid = isec->task_sid;
    1401                 :          0 :                 break;
    1402                 :            :         case SECURITY_FS_USE_TRANS:
    1403                 :            :                 /* Default to the fs SID. */
    1404                 :          0 :                 isec->sid = sbsec->sid;
    1405                 :            : 
    1406                 :            :                 /* Try to obtain a transition SID. */
    1407                 :          0 :                 isec->sclass = inode_mode_to_security_class(inode->i_mode);
    1408                 :          0 :                 rc = security_transition_sid(isec->task_sid, sbsec->sid,
    1409                 :            :                                              isec->sclass, NULL, &sid);
    1410         [ #  # ]:          0 :                 if (rc)
    1411                 :            :                         goto out_unlock;
    1412                 :          0 :                 isec->sid = sid;
    1413                 :          0 :                 break;
    1414                 :            :         case SECURITY_FS_USE_MNTPOINT:
    1415                 :          0 :                 isec->sid = sbsec->mntpoint_sid;
    1416                 :          0 :                 break;
    1417                 :            :         default:
    1418                 :            :                 /* Default to the fs superblock SID. */
    1419                 :          0 :                 isec->sid = sbsec->sid;
    1420                 :            : 
    1421 [ #  # ][ #  # ]:          0 :                 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
    1422         [ #  # ]:          0 :                         if (opt_dentry) {
    1423                 :          0 :                                 isec->sclass = inode_mode_to_security_class(inode->i_mode);
    1424                 :          0 :                                 rc = selinux_proc_get_sid(opt_dentry,
    1425                 :            :                                                           isec->sclass,
    1426                 :            :                                                           &sid);
    1427         [ #  # ]:          0 :                                 if (rc)
    1428                 :            :                                         goto out_unlock;
    1429                 :          0 :                                 isec->sid = sid;
    1430                 :            :                         }
    1431                 :            :                 }
    1432                 :            :                 break;
    1433                 :            :         }
    1434                 :            : 
    1435                 :          0 :         isec->initialized = 1;
    1436                 :            : 
    1437                 :            : out_unlock:
    1438                 :          0 :         mutex_unlock(&isec->lock);
    1439                 :            : out:
    1440         [ #  # ]:          0 :         if (isec->sclass == SECCLASS_FILE)
    1441                 :          0 :                 isec->sclass = inode_mode_to_security_class(inode->i_mode);
    1442                 :          0 :         return rc;
    1443                 :            : }
    1444                 :            : 
    1445                 :            : /* Convert a Linux signal to an access vector. */
    1446                 :            : static inline u32 signal_to_av(int sig)
    1447                 :            : {
    1448                 :            :         u32 perm = 0;
    1449                 :            : 
    1450 [ #  # ][ #  # ]:          0 :         switch (sig) {
    1451                 :            :         case SIGCHLD:
    1452                 :            :                 /* Commonly granted from child to parent. */
    1453                 :            :                 perm = PROCESS__SIGCHLD;
    1454                 :            :                 break;
    1455                 :            :         case SIGKILL:
    1456                 :            :                 /* Cannot be caught or ignored */
    1457                 :            :                 perm = PROCESS__SIGKILL;
    1458                 :            :                 break;
    1459                 :            :         case SIGSTOP:
    1460                 :            :                 /* Cannot be caught or ignored */
    1461                 :            :                 perm = PROCESS__SIGSTOP;
    1462                 :            :                 break;
    1463                 :            :         default:
    1464                 :            :                 /* All other signals. */
    1465                 :            :                 perm = PROCESS__SIGNAL;
    1466                 :            :                 break;
    1467                 :            :         }
    1468                 :            : 
    1469                 :            :         return perm;
    1470                 :            : }
    1471                 :            : 
    1472                 :            : /*
    1473                 :            :  * Check permission between a pair of credentials
    1474                 :            :  * fork check, ptrace check, etc.
    1475                 :            :  */
    1476                 :          0 : static int cred_has_perm(const struct cred *actor,
    1477                 :          0 :                          const struct cred *target,
    1478                 :            :                          u32 perms)
    1479                 :            : {
    1480                 :            :         u32 asid = cred_sid(actor), tsid = cred_sid(target);
    1481                 :            : 
    1482                 :          0 :         return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
    1483                 :            : }
    1484                 :            : 
    1485                 :            : /*
    1486                 :            :  * Check permission between a pair of tasks, e.g. signal checks,
    1487                 :            :  * fork check, ptrace check, etc.
    1488                 :            :  * tsk1 is the actor and tsk2 is the target
    1489                 :            :  * - this uses the default subjective creds of tsk1
    1490                 :            :  */
    1491                 :          0 : static int task_has_perm(const struct task_struct *tsk1,
    1492                 :            :                          const struct task_struct *tsk2,
    1493                 :            :                          u32 perms)
    1494                 :            : {
    1495                 :            :         const struct task_security_struct *__tsec1, *__tsec2;
    1496                 :            :         u32 sid1, sid2;
    1497                 :            : 
    1498                 :            :         rcu_read_lock();
    1499                 :          0 :         __tsec1 = __task_cred(tsk1)->security;       sid1 = __tsec1->sid;
    1500                 :          0 :         __tsec2 = __task_cred(tsk2)->security;       sid2 = __tsec2->sid;
    1501                 :            :         rcu_read_unlock();
    1502                 :          0 :         return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
    1503                 :            : }
    1504                 :            : 
    1505                 :            : /*
    1506                 :            :  * Check permission between current and another task, e.g. signal checks,
    1507                 :            :  * fork check, ptrace check, etc.
    1508                 :            :  * current is the actor and tsk2 is the target
    1509                 :            :  * - this uses current's subjective creds
    1510                 :            :  */
    1511                 :          0 : static int current_has_perm(const struct task_struct *tsk,
    1512                 :            :                             u32 perms)
    1513                 :            : {
    1514                 :            :         u32 sid, tsid;
    1515                 :            : 
    1516                 :            :         sid = current_sid();
    1517                 :            :         tsid = task_sid(tsk);
    1518                 :          0 :         return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
    1519                 :            : }
    1520                 :            : 
    1521                 :            : #if CAP_LAST_CAP > 63
    1522                 :            : #error Fix SELinux to handle capabilities > 63.
    1523                 :            : #endif
    1524                 :            : 
    1525                 :            : /* Check whether a task is allowed to use a capability. */
    1526                 :          0 : static int cred_has_capability(const struct cred *cred,
    1527                 :            :                                int cap, int audit)
    1528                 :            : {
    1529                 :            :         struct common_audit_data ad;
    1530                 :            :         struct av_decision avd;
    1531                 :            :         u16 sclass;
    1532                 :            :         u32 sid = cred_sid(cred);
    1533                 :          0 :         u32 av = CAP_TO_MASK(cap);
    1534                 :            :         int rc;
    1535                 :            : 
    1536                 :          0 :         ad.type = LSM_AUDIT_DATA_CAP;
    1537                 :          0 :         ad.u.cap = cap;
    1538                 :            : 
    1539      [ #  #  # ]:          0 :         switch (CAP_TO_INDEX(cap)) {
    1540                 :            :         case 0:
    1541                 :            :                 sclass = SECCLASS_CAPABILITY;
    1542                 :            :                 break;
    1543                 :            :         case 1:
    1544                 :            :                 sclass = SECCLASS_CAPABILITY2;
    1545                 :          0 :                 break;
    1546                 :            :         default:
    1547                 :          0 :                 printk(KERN_ERR
    1548                 :            :                        "SELinux:  out of range capability %d\n", cap);
    1549                 :          0 :                 BUG();
    1550                 :            :                 return -EINVAL;
    1551                 :            :         }
    1552                 :            : 
    1553                 :          0 :         rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
    1554         [ #  # ]:          0 :         if (audit == SECURITY_CAP_AUDIT) {
    1555                 :            :                 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
    1556         [ #  # ]:          0 :                 if (rc2)
    1557                 :            :                         return rc2;
    1558                 :            :         }
    1559                 :          0 :         return rc;
    1560                 :            : }
    1561                 :            : 
    1562                 :            : /* Check whether a task is allowed to use a system operation. */
    1563                 :          0 : static int task_has_system(struct task_struct *tsk,
    1564                 :            :                            u32 perms)
    1565                 :            : {
    1566                 :            :         u32 sid = task_sid(tsk);
    1567                 :            : 
    1568                 :          0 :         return avc_has_perm(sid, SECINITSID_KERNEL,
    1569                 :            :                             SECCLASS_SYSTEM, perms, NULL);
    1570                 :            : }
    1571                 :            : 
    1572                 :            : /* Check whether a task has a particular permission to an inode.
    1573                 :            :    The 'adp' parameter is optional and allows other audit
    1574                 :            :    data to be passed (e.g. the dentry). */
    1575                 :          0 : static int inode_has_perm(const struct cred *cred,
    1576                 :            :                           struct inode *inode,
    1577                 :            :                           u32 perms,
    1578                 :            :                           struct common_audit_data *adp)
    1579                 :            : {
    1580                 :            :         struct inode_security_struct *isec;
    1581                 :            :         u32 sid;
    1582                 :            : 
    1583                 :            :         validate_creds(cred);
    1584                 :            : 
    1585         [ #  # ]:          0 :         if (unlikely(IS_PRIVATE(inode)))
    1586                 :            :                 return 0;
    1587                 :            : 
    1588                 :            :         sid = cred_sid(cred);
    1589                 :          0 :         isec = inode->i_security;
    1590                 :            : 
    1591                 :          0 :         return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
    1592                 :            : }
    1593                 :            : 
    1594                 :            : /* Same as inode_has_perm, but pass explicit audit data containing
    1595                 :            :    the dentry to help the auditing code to more easily generate the
    1596                 :            :    pathname if needed. */
    1597                 :            : static inline int dentry_has_perm(const struct cred *cred,
    1598                 :            :                                   struct dentry *dentry,
    1599                 :            :                                   u32 av)
    1600                 :            : {
    1601                 :          0 :         struct inode *inode = dentry->d_inode;
    1602                 :            :         struct common_audit_data ad;
    1603                 :            : 
    1604                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    1605                 :          0 :         ad.u.dentry = dentry;
    1606                 :          0 :         return inode_has_perm(cred, inode, av, &ad);
    1607                 :            : }
    1608                 :            : 
    1609                 :            : /* Same as inode_has_perm, but pass explicit audit data containing
    1610                 :            :    the path to help the auditing code to more easily generate the
    1611                 :            :    pathname if needed. */
    1612                 :            : static inline int path_has_perm(const struct cred *cred,
    1613                 :            :                                 struct path *path,
    1614                 :            :                                 u32 av)
    1615                 :            : {
    1616                 :          0 :         struct inode *inode = path->dentry->d_inode;
    1617                 :            :         struct common_audit_data ad;
    1618                 :            : 
    1619                 :          0 :         ad.type = LSM_AUDIT_DATA_PATH;
    1620                 :          0 :         ad.u.path = *path;
    1621                 :          0 :         return inode_has_perm(cred, inode, av, &ad);
    1622                 :            : }
    1623                 :            : 
    1624                 :            : /* Same as path_has_perm, but uses the inode from the file struct. */
    1625                 :            : static inline int file_path_has_perm(const struct cred *cred,
    1626                 :          0 :                                      struct file *file,
    1627                 :            :                                      u32 av)
    1628                 :            : {
    1629                 :            :         struct common_audit_data ad;
    1630                 :            : 
    1631                 :          0 :         ad.type = LSM_AUDIT_DATA_PATH;
    1632                 :          0 :         ad.u.path = file->f_path;
    1633                 :          0 :         return inode_has_perm(cred, file_inode(file), av, &ad);
    1634                 :            : }
    1635                 :            : 
    1636                 :            : /* Check whether a task can use an open file descriptor to
    1637                 :            :    access an inode in a given way.  Check access to the
    1638                 :            :    descriptor itself, and then use dentry_has_perm to
    1639                 :            :    check a particular permission to the file.
    1640                 :            :    Access to the descriptor is implicitly granted if it
    1641                 :            :    has the same SID as the process.  If av is zero, then
    1642                 :            :    access to the file is not checked, e.g. for cases
    1643                 :            :    where only the descriptor is affected like seek. */
    1644                 :          0 : static int file_has_perm(const struct cred *cred,
    1645                 :          0 :                          struct file *file,
    1646                 :            :                          u32 av)
    1647                 :            : {
    1648                 :          0 :         struct file_security_struct *fsec = file->f_security;
    1649                 :          0 :         struct inode *inode = file_inode(file);
    1650                 :            :         struct common_audit_data ad;
    1651                 :            :         u32 sid = cred_sid(cred);
    1652                 :            :         int rc;
    1653                 :            : 
    1654                 :          0 :         ad.type = LSM_AUDIT_DATA_PATH;
    1655                 :          0 :         ad.u.path = file->f_path;
    1656                 :            : 
    1657         [ #  # ]:          0 :         if (sid != fsec->sid) {
    1658                 :          0 :                 rc = avc_has_perm(sid, fsec->sid,
    1659                 :            :                                   SECCLASS_FD,
    1660                 :            :                                   FD__USE,
    1661                 :            :                                   &ad);
    1662         [ #  # ]:          0 :                 if (rc)
    1663                 :            :                         goto out;
    1664                 :            :         }
    1665                 :            : 
    1666                 :            :         /* av is zero if only checking access to the descriptor. */
    1667                 :            :         rc = 0;
    1668         [ #  # ]:          0 :         if (av)
    1669                 :          0 :                 rc = inode_has_perm(cred, inode, av, &ad);
    1670                 :            : 
    1671                 :            : out:
    1672                 :          0 :         return rc;
    1673                 :            : }
    1674                 :            : 
    1675                 :            : /* Check whether a task can create a file. */
    1676                 :          0 : static int may_create(struct inode *dir,
    1677                 :            :                       struct dentry *dentry,
    1678                 :            :                       u16 tclass)
    1679                 :            : {
    1680                 :          0 :         const struct task_security_struct *tsec = current_security();
    1681                 :            :         struct inode_security_struct *dsec;
    1682                 :            :         struct superblock_security_struct *sbsec;
    1683                 :            :         u32 sid, newsid;
    1684                 :            :         struct common_audit_data ad;
    1685                 :            :         int rc;
    1686                 :            : 
    1687                 :          0 :         dsec = dir->i_security;
    1688                 :          0 :         sbsec = dir->i_sb->s_security;
    1689                 :            : 
    1690                 :          0 :         sid = tsec->sid;
    1691                 :          0 :         newsid = tsec->create_sid;
    1692                 :            : 
    1693                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    1694                 :          0 :         ad.u.dentry = dentry;
    1695                 :            : 
    1696                 :          0 :         rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
    1697                 :            :                           DIR__ADD_NAME | DIR__SEARCH,
    1698                 :            :                           &ad);
    1699         [ #  # ]:          0 :         if (rc)
    1700                 :            :                 return rc;
    1701                 :            : 
    1702 [ #  # ][ #  # ]:          0 :         if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
    1703                 :          0 :                 rc = security_transition_sid(sid, dsec->sid, tclass,
    1704                 :          0 :                                              &dentry->d_name, &newsid);
    1705         [ #  # ]:          0 :                 if (rc)
    1706                 :            :                         return rc;
    1707                 :            :         }
    1708                 :            : 
    1709                 :          0 :         rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
    1710         [ #  # ]:          0 :         if (rc)
    1711                 :            :                 return rc;
    1712                 :            : 
    1713                 :          0 :         return avc_has_perm(newsid, sbsec->sid,
    1714                 :            :                             SECCLASS_FILESYSTEM,
    1715                 :            :                             FILESYSTEM__ASSOCIATE, &ad);
    1716                 :            : }
    1717                 :            : 
    1718                 :            : /* Check whether a task can create a key. */
    1719                 :          0 : static int may_create_key(u32 ksid,
    1720                 :            :                           struct task_struct *ctx)
    1721                 :            : {
    1722                 :            :         u32 sid = task_sid(ctx);
    1723                 :            : 
    1724                 :          0 :         return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
    1725                 :            : }
    1726                 :            : 
    1727                 :            : #define MAY_LINK        0
    1728                 :            : #define MAY_UNLINK      1
    1729                 :            : #define MAY_RMDIR       2
    1730                 :            : 
    1731                 :            : /* Check whether a task can link, unlink, or rmdir a file/directory. */
    1732                 :          0 : static int may_link(struct inode *dir,
    1733                 :            :                     struct dentry *dentry,
    1734                 :            :                     int kind)
    1735                 :            : 
    1736                 :            : {
    1737                 :            :         struct inode_security_struct *dsec, *isec;
    1738                 :            :         struct common_audit_data ad;
    1739                 :            :         u32 sid = current_sid();
    1740                 :            :         u32 av;
    1741                 :            :         int rc;
    1742                 :            : 
    1743                 :          0 :         dsec = dir->i_security;
    1744                 :          0 :         isec = dentry->d_inode->i_security;
    1745                 :            : 
    1746                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    1747                 :          0 :         ad.u.dentry = dentry;
    1748                 :            : 
    1749                 :            :         av = DIR__SEARCH;
    1750         [ #  # ]:          0 :         av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
    1751                 :          0 :         rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
    1752         [ #  # ]:          0 :         if (rc)
    1753                 :            :                 return rc;
    1754                 :            : 
    1755   [ #  #  #  # ]:          0 :         switch (kind) {
    1756                 :            :         case MAY_LINK:
    1757                 :            :                 av = FILE__LINK;
    1758                 :            :                 break;
    1759                 :            :         case MAY_UNLINK:
    1760                 :            :                 av = FILE__UNLINK;
    1761                 :            :                 break;
    1762                 :            :         case MAY_RMDIR:
    1763                 :            :                 av = DIR__RMDIR;
    1764                 :            :                 break;
    1765                 :            :         default:
    1766                 :          0 :                 printk(KERN_WARNING "SELinux: %s:  unrecognized kind %d\n",
    1767                 :            :                         __func__, kind);
    1768                 :            :                 return 0;
    1769                 :            :         }
    1770                 :            : 
    1771                 :          0 :         rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
    1772                 :            :         return rc;
    1773                 :            : }
    1774                 :            : 
    1775                 :            : static inline int may_rename(struct inode *old_dir,
    1776                 :            :                              struct dentry *old_dentry,
    1777                 :            :                              struct inode *new_dir,
    1778                 :            :                              struct dentry *new_dentry)
    1779                 :            : {
    1780                 :            :         struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
    1781                 :            :         struct common_audit_data ad;
    1782                 :            :         u32 sid = current_sid();
    1783                 :            :         u32 av;
    1784                 :            :         int old_is_dir, new_is_dir;
    1785                 :            :         int rc;
    1786                 :            : 
    1787                 :          0 :         old_dsec = old_dir->i_security;
    1788                 :          0 :         old_isec = old_dentry->d_inode->i_security;
    1789                 :          0 :         old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
    1790                 :          0 :         new_dsec = new_dir->i_security;
    1791                 :            : 
    1792                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    1793                 :            : 
    1794                 :          0 :         ad.u.dentry = old_dentry;
    1795                 :          0 :         rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
    1796                 :            :                           DIR__REMOVE_NAME | DIR__SEARCH, &ad);
    1797         [ #  # ]:          0 :         if (rc)
    1798                 :            :                 return rc;
    1799                 :          0 :         rc = avc_has_perm(sid, old_isec->sid,
    1800                 :            :                           old_isec->sclass, FILE__RENAME, &ad);
    1801         [ #  # ]:          0 :         if (rc)
    1802                 :            :                 return rc;
    1803         [ #  # ]:          0 :         if (old_is_dir && new_dir != old_dir) {
    1804                 :          0 :                 rc = avc_has_perm(sid, old_isec->sid,
    1805                 :            :                                   old_isec->sclass, DIR__REPARENT, &ad);
    1806         [ #  # ]:          0 :                 if (rc)
    1807                 :            :                         return rc;
    1808                 :            :         }
    1809                 :            : 
    1810                 :          0 :         ad.u.dentry = new_dentry;
    1811                 :            :         av = DIR__ADD_NAME | DIR__SEARCH;
    1812         [ #  # ]:          0 :         if (new_dentry->d_inode)
    1813                 :            :                 av |= DIR__REMOVE_NAME;
    1814                 :          0 :         rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
    1815         [ #  # ]:          0 :         if (rc)
    1816                 :            :                 return rc;
    1817         [ #  # ]:          0 :         if (new_dentry->d_inode) {
    1818                 :          0 :                 new_isec = new_dentry->d_inode->i_security;
    1819                 :          0 :                 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
    1820         [ #  # ]:          0 :                 rc = avc_has_perm(sid, new_isec->sid,
    1821                 :            :                                   new_isec->sclass,
    1822                 :            :                                   (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
    1823         [ #  # ]:          0 :                 if (rc)
    1824                 :            :                         return rc;
    1825                 :            :         }
    1826                 :            : 
    1827                 :            :         return 0;
    1828                 :            : }
    1829                 :            : 
    1830                 :            : /* Check whether a task can perform a filesystem operation. */
    1831                 :          0 : static int superblock_has_perm(const struct cred *cred,
    1832                 :            :                                struct super_block *sb,
    1833                 :            :                                u32 perms,
    1834                 :            :                                struct common_audit_data *ad)
    1835                 :            : {
    1836                 :            :         struct superblock_security_struct *sbsec;
    1837                 :            :         u32 sid = cred_sid(cred);
    1838                 :            : 
    1839                 :            :         sbsec = sb->s_security;
    1840                 :          0 :         return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
    1841                 :            : }
    1842                 :            : 
    1843                 :            : /* Convert a Linux mode and permission mask to an access vector. */
    1844                 :            : static inline u32 file_mask_to_av(int mode, int mask)
    1845                 :            : {
    1846                 :            :         u32 av = 0;
    1847                 :            : 
    1848 [ #  # ][ #  # ]:          0 :         if (!S_ISDIR(mode)) {
    1849 [ #  # ][ #  # ]:          0 :                 if (mask & MAY_EXEC)
    1850                 :            :                         av |= FILE__EXECUTE;
    1851 [ #  # ][ #  # ]:          0 :                 if (mask & MAY_READ)
    1852                 :          0 :                         av |= FILE__READ;
    1853                 :            : 
    1854 [ #  # ][ #  # ]:          0 :                 if (mask & MAY_APPEND)
    1855                 :          0 :                         av |= FILE__APPEND;
    1856 [ #  # ][ #  # ]:          0 :                 else if (mask & MAY_WRITE)
    1857                 :          0 :                         av |= FILE__WRITE;
    1858                 :            : 
    1859                 :            :         } else {
    1860 [ #  # ][ #  # ]:          0 :                 if (mask & MAY_EXEC)
    1861                 :            :                         av |= DIR__SEARCH;
    1862 [ #  # ][ #  # ]:          0 :                 if (mask & MAY_WRITE)
    1863                 :          0 :                         av |= DIR__WRITE;
    1864 [ #  # ][ #  # ]:          0 :                 if (mask & MAY_READ)
    1865                 :          0 :                         av |= DIR__READ;
    1866                 :            :         }
    1867                 :            : 
    1868                 :            :         return av;
    1869                 :            : }
    1870                 :            : 
    1871                 :            : /* Convert a Linux file to an access vector. */
    1872                 :            : static inline u32 file_to_av(struct file *file)
    1873                 :            : {
    1874                 :            :         u32 av = 0;
    1875                 :            : 
    1876 [ #  # ][ #  # ]:          0 :         if (file->f_mode & FMODE_READ)
         [ #  # ][ #  # ]
    1877                 :            :                 av |= FILE__READ;
    1878 [ #  # ][ #  # ]:          0 :         if (file->f_mode & FMODE_WRITE) {
         [ #  # ][ #  # ]
    1879 [ #  # ][ #  # ]:          0 :                 if (file->f_flags & O_APPEND)
         [ #  # ][ #  # ]
    1880                 :          0 :                         av |= FILE__APPEND;
    1881                 :            :                 else
    1882                 :          0 :                         av |= FILE__WRITE;
    1883                 :            :         }
    1884 [ #  # ][ #  # ]:          0 :         if (!av) {
         [ #  # ][ #  # ]
    1885                 :            :                 /*
    1886                 :            :                  * Special file opened with flags 3 for ioctl-only use.
    1887                 :            :                  */
    1888                 :            :                 av = FILE__IOCTL;
    1889                 :            :         }
    1890                 :            : 
    1891                 :            :         return av;
    1892                 :            : }
    1893                 :            : 
    1894                 :            : /*
    1895                 :            :  * Convert a file to an access vector and include the correct open
    1896                 :            :  * open permission.
    1897                 :            :  */
    1898                 :          0 : static inline u32 open_file_to_av(struct file *file)
    1899                 :            : {
    1900                 :            :         u32 av = file_to_av(file);
    1901                 :            : 
    1902         [ #  # ]:          0 :         if (selinux_policycap_openperm)
    1903                 :          0 :                 av |= FILE__OPEN;
    1904                 :            : 
    1905                 :            :         return av;
    1906                 :            : }
    1907                 :            : 
    1908                 :            : /* Hook functions begin here. */
    1909                 :            : 
    1910                 :          0 : static int selinux_binder_set_context_mgr(struct task_struct *mgr)
    1911                 :            : {
    1912                 :            :         u32 mysid = current_sid();
    1913                 :            :         u32 mgrsid = task_sid(mgr);
    1914                 :            : 
    1915                 :          0 :         return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
    1916                 :            : }
    1917                 :            : 
    1918                 :          0 : static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
    1919                 :            : {
    1920                 :            :         u32 mysid = current_sid();
    1921                 :            :         u32 fromsid = task_sid(from);
    1922                 :            :         u32 tosid = task_sid(to);
    1923                 :            :         int rc;
    1924                 :            : 
    1925         [ #  # ]:          0 :         if (mysid != fromsid) {
    1926                 :          0 :                 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
    1927         [ #  # ]:          0 :                 if (rc)
    1928                 :            :                         return rc;
    1929                 :            :         }
    1930                 :            : 
    1931                 :          0 :         return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
    1932                 :            : }
    1933                 :            : 
    1934                 :          0 : static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
    1935                 :            : {
    1936                 :            :         u32 fromsid = task_sid(from);
    1937                 :            :         u32 tosid = task_sid(to);
    1938                 :          0 :         return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
    1939                 :            : }
    1940                 :            : 
    1941                 :          0 : static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
    1942                 :            : {
    1943                 :            :         u32 sid = task_sid(to);
    1944                 :          0 :         struct file_security_struct *fsec = file->f_security;
    1945                 :          0 :         struct inode *inode = file->f_path.dentry->d_inode;
    1946                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    1947                 :            :         struct common_audit_data ad;
    1948                 :          0 :         struct selinux_audit_data sad = {0,};
    1949                 :            :         int rc;
    1950                 :            : 
    1951                 :          0 :         ad.type = LSM_AUDIT_DATA_PATH;
    1952                 :          0 :         ad.u.path = file->f_path;
    1953                 :          0 :         ad.selinux_audit_data = &sad;
    1954                 :            : 
    1955         [ #  # ]:          0 :         if (sid != fsec->sid) {
    1956                 :          0 :                 rc = avc_has_perm(sid, fsec->sid,
    1957                 :            :                                   SECCLASS_FD,
    1958                 :            :                                   FD__USE,
    1959                 :            :                                   &ad);
    1960         [ #  # ]:          0 :                 if (rc)
    1961                 :            :                         return rc;
    1962                 :            :         }
    1963                 :            : 
    1964         [ #  # ]:          0 :         if (unlikely(IS_PRIVATE(inode)))
    1965                 :            :                 return 0;
    1966                 :            : 
    1967                 :          0 :         return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
    1968                 :            :                             &ad);
    1969                 :            : }
    1970                 :            : 
    1971                 :          0 : static int selinux_ptrace_access_check(struct task_struct *child,
    1972                 :            :                                      unsigned int mode)
    1973                 :            : {
    1974                 :            :         int rc;
    1975                 :            : 
    1976                 :          0 :         rc = cap_ptrace_access_check(child, mode);
    1977         [ #  # ]:          0 :         if (rc)
    1978                 :            :                 return rc;
    1979                 :            : 
    1980         [ #  # ]:          0 :         if (mode & PTRACE_MODE_READ) {
    1981                 :            :                 u32 sid = current_sid();
    1982                 :            :                 u32 csid = task_sid(child);
    1983                 :          0 :                 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
    1984                 :            :         }
    1985                 :            : 
    1986                 :          0 :         return current_has_perm(child, PROCESS__PTRACE);
    1987                 :            : }
    1988                 :            : 
    1989                 :          0 : static int selinux_ptrace_traceme(struct task_struct *parent)
    1990                 :            : {
    1991                 :            :         int rc;
    1992                 :            : 
    1993                 :          0 :         rc = cap_ptrace_traceme(parent);
    1994         [ #  # ]:          0 :         if (rc)
    1995                 :            :                 return rc;
    1996                 :            : 
    1997                 :          0 :         return task_has_perm(parent, current, PROCESS__PTRACE);
    1998                 :            : }
    1999                 :            : 
    2000                 :          0 : static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
    2001                 :            :                           kernel_cap_t *inheritable, kernel_cap_t *permitted)
    2002                 :            : {
    2003                 :            :         int error;
    2004                 :            : 
    2005                 :          0 :         error = current_has_perm(target, PROCESS__GETCAP);
    2006         [ #  # ]:          0 :         if (error)
    2007                 :            :                 return error;
    2008                 :            : 
    2009                 :          0 :         return cap_capget(target, effective, inheritable, permitted);
    2010                 :            : }
    2011                 :            : 
    2012                 :          0 : static int selinux_capset(struct cred *new, const struct cred *old,
    2013                 :            :                           const kernel_cap_t *effective,
    2014                 :            :                           const kernel_cap_t *inheritable,
    2015                 :            :                           const kernel_cap_t *permitted)
    2016                 :            : {
    2017                 :            :         int error;
    2018                 :            : 
    2019                 :          0 :         error = cap_capset(new, old,
    2020                 :            :                                       effective, inheritable, permitted);
    2021         [ #  # ]:          0 :         if (error)
    2022                 :            :                 return error;
    2023                 :            : 
    2024                 :          0 :         return cred_has_perm(old, new, PROCESS__SETCAP);
    2025                 :            : }
    2026                 :            : 
    2027                 :            : /*
    2028                 :            :  * (This comment used to live with the selinux_task_setuid hook,
    2029                 :            :  * which was removed).
    2030                 :            :  *
    2031                 :            :  * Since setuid only affects the current process, and since the SELinux
    2032                 :            :  * controls are not based on the Linux identity attributes, SELinux does not
    2033                 :            :  * need to control this operation.  However, SELinux does control the use of
    2034                 :            :  * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
    2035                 :            :  */
    2036                 :            : 
    2037                 :          0 : static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
    2038                 :            :                            int cap, int audit)
    2039                 :            : {
    2040                 :            :         int rc;
    2041                 :            : 
    2042                 :          0 :         rc = cap_capable(cred, ns, cap, audit);
    2043         [ #  # ]:          0 :         if (rc)
    2044                 :            :                 return rc;
    2045                 :            : 
    2046                 :          0 :         return cred_has_capability(cred, cap, audit);
    2047                 :            : }
    2048                 :            : 
    2049                 :          0 : static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
    2050                 :            : {
    2051                 :          0 :         const struct cred *cred = current_cred();
    2052                 :            :         int rc = 0;
    2053                 :            : 
    2054 [ #  # ][ #  # ]:          0 :         if (!sb)
         [ #  # ][ #  # ]
    2055                 :            :                 return 0;
    2056                 :            : 
    2057                 :            :         switch (cmds) {
    2058                 :            :         case Q_SYNC:
    2059                 :            :         case Q_QUOTAON:
    2060                 :            :         case Q_QUOTAOFF:
    2061                 :            :         case Q_SETINFO:
    2062                 :            :         case Q_SETQUOTA:
    2063                 :            :                 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
    2064                 :          0 :                 break;
    2065                 :            :         case Q_GETFMT:
    2066                 :            :         case Q_GETINFO:
    2067                 :            :         case Q_GETQUOTA:
    2068                 :            :                 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
    2069                 :          0 :                 break;
    2070                 :            :         default:
    2071                 :            :                 rc = 0;  /* let the kernel handle invalid cmds */
    2072                 :            :                 break;
    2073                 :            :         }
    2074                 :          0 :         return rc;
    2075                 :            : }
    2076                 :            : 
    2077                 :          0 : static int selinux_quota_on(struct dentry *dentry)
    2078                 :            : {
    2079                 :          0 :         const struct cred *cred = current_cred();
    2080                 :            : 
    2081                 :          0 :         return dentry_has_perm(cred, dentry, FILE__QUOTAON);
    2082                 :            : }
    2083                 :            : 
    2084                 :          0 : static int selinux_syslog(int type)
    2085                 :            : {
    2086                 :            :         int rc;
    2087                 :            : 
    2088      [ #  #  # ]:          0 :         switch (type) {
    2089                 :            :         case SYSLOG_ACTION_READ_ALL:    /* Read last kernel messages */
    2090                 :            :         case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
    2091                 :          0 :                 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
    2092                 :          0 :                 break;
    2093                 :            :         case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
    2094                 :            :         case SYSLOG_ACTION_CONSOLE_ON:  /* Enable logging to console */
    2095                 :            :         /* Set level of messages printed to console */
    2096                 :            :         case SYSLOG_ACTION_CONSOLE_LEVEL:
    2097                 :          0 :                 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
    2098                 :          0 :                 break;
    2099                 :            :         case SYSLOG_ACTION_CLOSE:       /* Close log */
    2100                 :            :         case SYSLOG_ACTION_OPEN:        /* Open log */
    2101                 :            :         case SYSLOG_ACTION_READ:        /* Read from log */
    2102                 :            :         case SYSLOG_ACTION_READ_CLEAR:  /* Read/clear last kernel messages */
    2103                 :            :         case SYSLOG_ACTION_CLEAR:       /* Clear ring buffer */
    2104                 :            :         default:
    2105                 :          0 :                 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
    2106                 :          0 :                 break;
    2107                 :            :         }
    2108                 :          0 :         return rc;
    2109                 :            : }
    2110                 :            : 
    2111                 :            : /*
    2112                 :            :  * Check that a process has enough memory to allocate a new virtual
    2113                 :            :  * mapping. 0 means there is enough memory for the allocation to
    2114                 :            :  * succeed and -ENOMEM implies there is not.
    2115                 :            :  *
    2116                 :            :  * Do not audit the selinux permission check, as this is applied to all
    2117                 :            :  * processes that allocate mappings.
    2118                 :            :  */
    2119                 :          0 : static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
    2120                 :            : {
    2121                 :            :         int rc, cap_sys_admin = 0;
    2122                 :            : 
    2123                 :          0 :         rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
    2124                 :            :                              SECURITY_CAP_NOAUDIT);
    2125         [ #  # ]:          0 :         if (rc == 0)
    2126                 :            :                 cap_sys_admin = 1;
    2127                 :            : 
    2128                 :          0 :         return __vm_enough_memory(mm, pages, cap_sys_admin);
    2129                 :            : }
    2130                 :            : 
    2131                 :            : /* binprm security operations */
    2132                 :            : 
    2133                 :          0 : static int selinux_bprm_set_creds(struct linux_binprm *bprm)
    2134                 :            : {
    2135                 :            :         const struct task_security_struct *old_tsec;
    2136                 :            :         struct task_security_struct *new_tsec;
    2137                 :            :         struct inode_security_struct *isec;
    2138                 :            :         struct common_audit_data ad;
    2139                 :          0 :         struct inode *inode = file_inode(bprm->file);
    2140                 :            :         int rc;
    2141                 :            : 
    2142                 :          0 :         rc = cap_bprm_set_creds(bprm);
    2143         [ #  # ]:          0 :         if (rc)
    2144                 :            :                 return rc;
    2145                 :            : 
    2146                 :            :         /* SELinux context only depends on initial program or script and not
    2147                 :            :          * the script interpreter */
    2148         [ #  # ]:          0 :         if (bprm->cred_prepared)
    2149                 :            :                 return 0;
    2150                 :            : 
    2151                 :          0 :         old_tsec = current_security();
    2152                 :          0 :         new_tsec = bprm->cred->security;
    2153                 :          0 :         isec = inode->i_security;
    2154                 :            : 
    2155                 :            :         /* Default to the current task SID. */
    2156                 :          0 :         new_tsec->sid = old_tsec->sid;
    2157                 :          0 :         new_tsec->osid = old_tsec->sid;
    2158                 :            : 
    2159                 :            :         /* Reset fs, key, and sock SIDs on execve. */
    2160                 :          0 :         new_tsec->create_sid = 0;
    2161                 :          0 :         new_tsec->keycreate_sid = 0;
    2162                 :          0 :         new_tsec->sockcreate_sid = 0;
    2163                 :            : 
    2164         [ #  # ]:          0 :         if (old_tsec->exec_sid) {
    2165                 :          0 :                 new_tsec->sid = old_tsec->exec_sid;
    2166                 :            :                 /* Reset exec SID on execve. */
    2167                 :          0 :                 new_tsec->exec_sid = 0;
    2168                 :            : 
    2169                 :            :                 /*
    2170                 :            :                  * Minimize confusion: if no_new_privs and a transition is
    2171                 :            :                  * explicitly requested, then fail the exec.
    2172                 :            :                  */
    2173         [ #  # ]:          0 :                 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
    2174                 :            :                         return -EPERM;
    2175                 :            :         } else {
    2176                 :            :                 /* Check for a default transition on this program. */
    2177                 :          0 :                 rc = security_transition_sid(old_tsec->sid, isec->sid,
    2178                 :            :                                              SECCLASS_PROCESS, NULL,
    2179                 :            :                                              &new_tsec->sid);
    2180         [ #  # ]:          0 :                 if (rc)
    2181                 :            :                         return rc;
    2182                 :            :         }
    2183                 :            : 
    2184                 :          0 :         ad.type = LSM_AUDIT_DATA_PATH;
    2185                 :          0 :         ad.u.path = bprm->file->f_path;
    2186                 :            : 
    2187 [ #  # ][ #  # ]:          0 :         if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
    2188                 :          0 :             (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
    2189                 :          0 :                 new_tsec->sid = old_tsec->sid;
    2190                 :            : 
    2191         [ #  # ]:          0 :         if (new_tsec->sid == old_tsec->sid) {
    2192                 :          0 :                 rc = avc_has_perm(old_tsec->sid, isec->sid,
    2193                 :            :                                   SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
    2194         [ #  # ]:          0 :                 if (rc)
    2195                 :          0 :                         return rc;
    2196                 :            :         } else {
    2197                 :            :                 /* Check permissions for the transition. */
    2198                 :          0 :                 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
    2199                 :            :                                   SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
    2200         [ #  # ]:          0 :                 if (rc)
    2201                 :            :                         return rc;
    2202                 :            : 
    2203                 :          0 :                 rc = avc_has_perm(new_tsec->sid, isec->sid,
    2204                 :            :                                   SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
    2205         [ #  # ]:          0 :                 if (rc)
    2206                 :            :                         return rc;
    2207                 :            : 
    2208                 :            :                 /* Check for shared state */
    2209         [ #  # ]:          0 :                 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
    2210                 :          0 :                         rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
    2211                 :            :                                           SECCLASS_PROCESS, PROCESS__SHARE,
    2212                 :            :                                           NULL);
    2213         [ #  # ]:          0 :                         if (rc)
    2214                 :            :                                 return -EPERM;
    2215                 :            :                 }
    2216                 :            : 
    2217                 :            :                 /* Make sure that anyone attempting to ptrace over a task that
    2218                 :            :                  * changes its SID has the appropriate permit */
    2219         [ #  # ]:          0 :                 if (bprm->unsafe &
    2220                 :            :                     (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
    2221                 :            :                         struct task_struct *tracer;
    2222                 :            :                         struct task_security_struct *sec;
    2223                 :            :                         u32 ptsid = 0;
    2224                 :            : 
    2225                 :            :                         rcu_read_lock();
    2226                 :          0 :                         tracer = ptrace_parent(current);
    2227         [ #  # ]:          0 :                         if (likely(tracer != NULL)) {
    2228                 :          0 :                                 sec = __task_cred(tracer)->security;
    2229                 :          0 :                                 ptsid = sec->sid;
    2230                 :            :                         }
    2231                 :            :                         rcu_read_unlock();
    2232                 :            : 
    2233         [ #  # ]:          0 :                         if (ptsid != 0) {
    2234                 :          0 :                                 rc = avc_has_perm(ptsid, new_tsec->sid,
    2235                 :            :                                                   SECCLASS_PROCESS,
    2236                 :            :                                                   PROCESS__PTRACE, NULL);
    2237         [ #  # ]:          0 :                                 if (rc)
    2238                 :            :                                         return -EPERM;
    2239                 :            :                         }
    2240                 :            :                 }
    2241                 :            : 
    2242                 :            :                 /* Clear any possibly unsafe personality bits on exec: */
    2243                 :          0 :                 bprm->per_clear |= PER_CLEAR_ON_SETID;
    2244                 :            :         }
    2245                 :            : 
    2246                 :            :         return 0;
    2247                 :            : }
    2248                 :            : 
    2249                 :          0 : static int selinux_bprm_secureexec(struct linux_binprm *bprm)
    2250                 :            : {
    2251                 :          0 :         const struct task_security_struct *tsec = current_security();
    2252                 :            :         u32 sid, osid;
    2253                 :            :         int atsecure = 0;
    2254                 :            : 
    2255                 :          0 :         sid = tsec->sid;
    2256                 :          0 :         osid = tsec->osid;
    2257                 :            : 
    2258         [ #  # ]:          0 :         if (osid != sid) {
    2259                 :            :                 /* Enable secure mode for SIDs transitions unless
    2260                 :            :                    the noatsecure permission is granted between
    2261                 :            :                    the two SIDs, i.e. ahp returns 0. */
    2262                 :          0 :                 atsecure = avc_has_perm(osid, sid,
    2263                 :            :                                         SECCLASS_PROCESS,
    2264                 :            :                                         PROCESS__NOATSECURE, NULL);
    2265                 :            :         }
    2266                 :            : 
    2267 [ #  # ][ #  # ]:          0 :         return (atsecure || cap_bprm_secureexec(bprm));
    2268                 :            : }
    2269                 :            : 
    2270                 :          0 : static int match_file(const void *p, struct file *file, unsigned fd)
    2271                 :            : {
    2272         [ #  # ]:          0 :         return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
    2273                 :            : }
    2274                 :            : 
    2275                 :            : /* Derived from fs/exec.c:flush_old_files. */
    2276                 :            : static inline void flush_unauthorized_files(const struct cred *cred,
    2277                 :            :                                             struct files_struct *files)
    2278                 :            : {
    2279                 :            :         struct file *file, *devnull = NULL;
    2280                 :            :         struct tty_struct *tty;
    2281                 :            :         int drop_tty = 0;
    2282                 :            :         unsigned n;
    2283                 :            : 
    2284                 :          0 :         tty = get_current_tty();
    2285         [ #  # ]:          0 :         if (tty) {
    2286                 :            :                 spin_lock(&tty_files_lock);
    2287         [ #  # ]:          0 :                 if (!list_empty(&tty->tty_files)) {
    2288                 :            :                         struct tty_file_private *file_priv;
    2289                 :            : 
    2290                 :            :                         /* Revalidate access to controlling tty.
    2291                 :            :                            Use file_path_has_perm on the tty path directly
    2292                 :            :                            rather than using file_has_perm, as this particular
    2293                 :            :                            open file may belong to another process and we are
    2294                 :            :                            only interested in the inode-based check here. */
    2295                 :            :                         file_priv = list_first_entry(&tty->tty_files,
    2296                 :            :                                                 struct tty_file_private, list);
    2297                 :          0 :                         file = file_priv->file;
    2298         [ #  # ]:          0 :                         if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
    2299                 :            :                                 drop_tty = 1;
    2300                 :            :                 }
    2301                 :            :                 spin_unlock(&tty_files_lock);
    2302                 :          0 :                 tty_kref_put(tty);
    2303                 :            :         }
    2304                 :            :         /* Reset controlling tty. */
    2305         [ #  # ]:          0 :         if (drop_tty)
    2306                 :          0 :                 no_tty();
    2307                 :            : 
    2308                 :            :         /* Revalidate access to inherited open files. */
    2309                 :          0 :         n = iterate_fd(files, 0, match_file, cred);
    2310         [ #  # ]:          0 :         if (!n) /* none found? */
    2311                 :            :                 return;
    2312                 :            : 
    2313                 :          0 :         devnull = dentry_open(&selinux_null, O_RDWR, cred);
    2314         [ #  # ]:          0 :         if (IS_ERR(devnull))
    2315                 :            :                 devnull = NULL;
    2316                 :            :         /* replace all the matching ones with this */
    2317                 :            :         do {
    2318                 :          0 :                 replace_fd(n - 1, devnull, 0);
    2319         [ #  # ]:          0 :         } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
    2320         [ #  # ]:          0 :         if (devnull)
    2321                 :          0 :                 fput(devnull);
    2322                 :            : }
    2323                 :            : 
    2324                 :            : /*
    2325                 :            :  * Prepare a process for imminent new credential changes due to exec
    2326                 :            :  */
    2327                 :          0 : static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
    2328                 :            : {
    2329                 :            :         struct task_security_struct *new_tsec;
    2330                 :            :         struct rlimit *rlim, *initrlim;
    2331                 :            :         int rc, i;
    2332                 :            : 
    2333                 :          0 :         new_tsec = bprm->cred->security;
    2334         [ #  # ]:          0 :         if (new_tsec->sid == new_tsec->osid)
    2335                 :          0 :                 return;
    2336                 :            : 
    2337                 :            :         /* Close files for which the new task SID is not authorized. */
    2338                 :          0 :         flush_unauthorized_files(bprm->cred, current->files);
    2339                 :            : 
    2340                 :            :         /* Always clear parent death signal on SID transitions. */
    2341                 :          0 :         current->pdeath_signal = 0;
    2342                 :            : 
    2343                 :            :         /* Check whether the new SID can inherit resource limits from the old
    2344                 :            :          * SID.  If not, reset all soft limits to the lower of the current
    2345                 :            :          * task's hard limit and the init task's soft limit.
    2346                 :            :          *
    2347                 :            :          * Note that the setting of hard limits (even to lower them) can be
    2348                 :            :          * controlled by the setrlimit check.  The inclusion of the init task's
    2349                 :            :          * soft limit into the computation is to avoid resetting soft limits
    2350                 :            :          * higher than the default soft limit for cases where the default is
    2351                 :            :          * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
    2352                 :            :          */
    2353                 :          0 :         rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
    2354                 :            :                           PROCESS__RLIMITINH, NULL);
    2355         [ #  # ]:          0 :         if (rc) {
    2356                 :            :                 /* protect against do_prlimit() */
    2357                 :          0 :                 task_lock(current);
    2358         [ #  # ]:          0 :                 for (i = 0; i < RLIM_NLIMITS; i++) {
    2359                 :          0 :                         rlim = current->signal->rlim + i;
    2360                 :          0 :                         initrlim = init_task.signal->rlim + i;
    2361                 :          0 :                         rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
    2362                 :            :                 }
    2363                 :          0 :                 task_unlock(current);
    2364                 :          0 :                 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
    2365                 :            :         }
    2366                 :            : }
    2367                 :            : 
    2368                 :            : /*
    2369                 :            :  * Clean up the process immediately after the installation of new credentials
    2370                 :            :  * due to exec
    2371                 :            :  */
    2372                 :          0 : static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
    2373                 :            : {
    2374                 :          0 :         const struct task_security_struct *tsec = current_security();
    2375                 :            :         struct itimerval itimer;
    2376                 :            :         u32 osid, sid;
    2377                 :            :         int rc, i;
    2378                 :            : 
    2379                 :          0 :         osid = tsec->osid;
    2380                 :          0 :         sid = tsec->sid;
    2381                 :            : 
    2382         [ #  # ]:          0 :         if (sid == osid)
    2383                 :          0 :                 return;
    2384                 :            : 
    2385                 :            :         /* Check whether the new SID can inherit signal state from the old SID.
    2386                 :            :          * If not, clear itimers to avoid subsequent signal generation and
    2387                 :            :          * flush and unblock signals.
    2388                 :            :          *
    2389                 :            :          * This must occur _after_ the task SID has been updated so that any
    2390                 :            :          * kill done after the flush will be checked against the new SID.
    2391                 :            :          */
    2392                 :          0 :         rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
    2393         [ #  # ]:          0 :         if (rc) {
    2394                 :          0 :                 memset(&itimer, 0, sizeof itimer);
    2395         [ #  # ]:          0 :                 for (i = 0; i < 3; i++)
    2396                 :          0 :                         do_setitimer(i, &itimer, NULL);
    2397                 :          0 :                 spin_lock_irq(&current->sighand->siglock);
    2398         [ #  # ]:          0 :                 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
    2399                 :          0 :                         __flush_signals(current);
    2400                 :          0 :                         flush_signal_handlers(current, 1);
    2401                 :          0 :                         sigemptyset(&current->blocked);
    2402                 :            :                 }
    2403                 :          0 :                 spin_unlock_irq(&current->sighand->siglock);
    2404                 :            :         }
    2405                 :            : 
    2406                 :            :         /* Wake up the parent if it is waiting so that it can recheck
    2407                 :            :          * wait permission to the new task SID. */
    2408                 :          0 :         read_lock(&tasklist_lock);
    2409                 :          0 :         __wake_up_parent(current, current->real_parent);
    2410                 :            :         read_unlock(&tasklist_lock);
    2411                 :            : }
    2412                 :            : 
    2413                 :            : /* superblock security operations */
    2414                 :            : 
    2415                 :          0 : static int selinux_sb_alloc_security(struct super_block *sb)
    2416                 :            : {
    2417                 :          0 :         return superblock_alloc_security(sb);
    2418                 :            : }
    2419                 :            : 
    2420                 :          0 : static void selinux_sb_free_security(struct super_block *sb)
    2421                 :            : {
    2422                 :            :         superblock_free_security(sb);
    2423                 :          0 : }
    2424                 :            : 
    2425                 :            : static inline int match_prefix(char *prefix, int plen, char *option, int olen)
    2426                 :            : {
    2427 [ #  # ][ #  # ]:          0 :         if (plen > olen)
         [ #  # ][ #  # ]
                 [ #  # ]
    2428                 :            :                 return 0;
    2429                 :            : 
    2430                 :          0 :         return !memcmp(prefix, option, plen);
    2431                 :            : }
    2432                 :            : 
    2433                 :            : static inline int selinux_option(char *option, int len)
    2434                 :            : {
    2435         [ #  # ]:          0 :         return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
    2436         [ #  # ]:          0 :                 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
    2437         [ #  # ]:          0 :                 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
    2438 [ #  # ][ #  # ]:          0 :                 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
    2439                 :            :                 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
    2440                 :            : }
    2441                 :            : 
    2442                 :            : static inline void take_option(char **to, char *from, int *first, int len)
    2443                 :            : {
    2444         [ #  # ]:          0 :         if (!*first) {
    2445                 :          0 :                 **to = ',';
    2446                 :          0 :                 *to += 1;
    2447                 :            :         } else
    2448                 :            :                 *first = 0;
    2449                 :          0 :         memcpy(*to, from, len);
    2450                 :          0 :         *to += len;
    2451                 :            : }
    2452                 :            : 
    2453                 :            : static inline void take_selinux_option(char **to, char *from, int *first,
    2454                 :            :                                        int len)
    2455                 :            : {
    2456                 :            :         int current_size = 0;
    2457                 :            : 
    2458         [ #  # ]:          0 :         if (!*first) {
    2459                 :          0 :                 **to = '|';
    2460                 :          0 :                 *to += 1;
    2461                 :            :         } else
    2462                 :            :                 *first = 0;
    2463                 :            : 
    2464         [ #  # ]:          0 :         while (current_size < len) {
    2465         [ #  # ]:          0 :                 if (*from != '"') {
    2466                 :          0 :                         **to = *from;
    2467                 :          0 :                         *to += 1;
    2468                 :            :                 }
    2469                 :          0 :                 from += 1;
    2470                 :          0 :                 current_size += 1;
    2471                 :            :         }
    2472                 :            : }
    2473                 :            : 
    2474                 :          0 : static int selinux_sb_copy_data(char *orig, char *copy)
    2475                 :            : {
    2476                 :            :         int fnosec, fsec, rc = 0;
    2477                 :            :         char *in_save, *in_curr, *in_end;
    2478                 :            :         char *sec_curr, *nosec_save, *nosec;
    2479                 :            :         int open_quote = 0;
    2480                 :            : 
    2481                 :            :         in_curr = orig;
    2482                 :            :         sec_curr = copy;
    2483                 :            : 
    2484                 :          0 :         nosec = (char *)get_zeroed_page(GFP_KERNEL);
    2485         [ #  # ]:          0 :         if (!nosec) {
    2486                 :            :                 rc = -ENOMEM;
    2487                 :            :                 goto out;
    2488                 :            :         }
    2489                 :            : 
    2490                 :            :         nosec_save = nosec;
    2491                 :            :         fnosec = fsec = 1;
    2492                 :            :         in_save = in_end = orig;
    2493                 :            : 
    2494                 :            :         do {
    2495         [ #  # ]:          0 :                 if (*in_end == '"')
    2496                 :          0 :                         open_quote = !open_quote;
    2497 [ #  # ][ #  # ]:          0 :                 if ((*in_end == ',' && open_quote == 0) ||
                 [ #  # ]
    2498                 :            :                                 *in_end == '\0') {
    2499                 :          0 :                         int len = in_end - in_curr;
    2500                 :            : 
    2501         [ #  # ]:          0 :                         if (selinux_option(in_curr, len))
    2502                 :            :                                 take_selinux_option(&sec_curr, in_curr, &fsec, len);
    2503                 :            :                         else
    2504                 :            :                                 take_option(&nosec, in_curr, &fnosec, len);
    2505                 :            : 
    2506                 :          0 :                         in_curr = in_end + 1;
    2507                 :            :                 }
    2508         [ #  # ]:          0 :         } while (*in_end++);
    2509                 :            : 
    2510                 :          0 :         strcpy(in_save, nosec_save);
    2511                 :          0 :         free_page((unsigned long)nosec_save);
    2512                 :            : out:
    2513                 :          0 :         return rc;
    2514                 :            : }
    2515                 :            : 
    2516                 :          0 : static int selinux_sb_remount(struct super_block *sb, void *data)
    2517                 :            : {
    2518                 :            :         int rc, i, *flags;
    2519                 :            :         struct security_mnt_opts opts;
    2520                 :            :         char *secdata, **mount_options;
    2521                 :          0 :         struct superblock_security_struct *sbsec = sb->s_security;
    2522                 :            : 
    2523         [ #  # ]:          0 :         if (!(sbsec->flags & SE_SBINITIALIZED))
    2524                 :            :                 return 0;
    2525                 :            : 
    2526         [ #  # ]:          0 :         if (!data)
    2527                 :            :                 return 0;
    2528                 :            : 
    2529         [ #  # ]:          0 :         if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
    2530                 :            :                 return 0;
    2531                 :            : 
    2532                 :            :         security_init_mnt_opts(&opts);
    2533                 :            :         secdata = alloc_secdata();
    2534         [ #  # ]:          0 :         if (!secdata)
    2535                 :            :                 return -ENOMEM;
    2536                 :          0 :         rc = selinux_sb_copy_data(data, secdata);
    2537         [ #  # ]:          0 :         if (rc)
    2538                 :            :                 goto out_free_secdata;
    2539                 :            : 
    2540                 :          0 :         rc = selinux_parse_opts_str(secdata, &opts);
    2541         [ #  # ]:          0 :         if (rc)
    2542                 :            :                 goto out_free_secdata;
    2543                 :            : 
    2544                 :          0 :         mount_options = opts.mnt_opts;
    2545                 :          0 :         flags = opts.mnt_opts_flags;
    2546                 :            : 
    2547         [ #  # ]:          0 :         for (i = 0; i < opts.num_mnt_opts; i++) {
    2548                 :            :                 u32 sid;
    2549                 :            :                 size_t len;
    2550                 :            : 
    2551         [ #  # ]:          0 :                 if (flags[i] == SBLABEL_MNT)
    2552                 :          0 :                         continue;
    2553                 :          0 :                 len = strlen(mount_options[i]);
    2554                 :          0 :                 rc = security_context_to_sid(mount_options[i], len, &sid);
    2555         [ #  # ]:          0 :                 if (rc) {
    2556                 :          0 :                         printk(KERN_WARNING "SELinux: security_context_to_sid"
    2557                 :            :                                "(%s) failed for (dev %s, type %s) errno=%d\n",
    2558                 :          0 :                                mount_options[i], sb->s_id, sb->s_type->name, rc);
    2559                 :          0 :                         goto out_free_opts;
    2560                 :            :                 }
    2561                 :            :                 rc = -EINVAL;
    2562   [ #  #  #  #  :          0 :                 switch (flags[i]) {
                      # ]
    2563                 :            :                 case FSCONTEXT_MNT:
    2564         [ #  # ]:          0 :                         if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
    2565                 :            :                                 goto out_bad_option;
    2566                 :            :                         break;
    2567                 :            :                 case CONTEXT_MNT:
    2568         [ #  # ]:          0 :                         if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
    2569                 :            :                                 goto out_bad_option;
    2570                 :            :                         break;
    2571                 :            :                 case ROOTCONTEXT_MNT: {
    2572                 :            :                         struct inode_security_struct *root_isec;
    2573                 :          0 :                         root_isec = sb->s_root->d_inode->i_security;
    2574                 :            : 
    2575         [ #  # ]:          0 :                         if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
    2576                 :            :                                 goto out_bad_option;
    2577                 :            :                         break;
    2578                 :            :                 }
    2579                 :            :                 case DEFCONTEXT_MNT:
    2580         [ #  # ]:          0 :                         if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
    2581                 :            :                                 goto out_bad_option;
    2582                 :            :                         break;
    2583                 :            :                 default:
    2584                 :            :                         goto out_free_opts;
    2585                 :            :                 }
    2586                 :            :         }
    2587                 :            : 
    2588                 :            :         rc = 0;
    2589                 :            : out_free_opts:
    2590                 :            :         security_free_mnt_opts(&opts);
    2591                 :            : out_free_secdata:
    2592                 :            :         free_secdata(secdata);
    2593                 :          0 :         return rc;
    2594                 :            : out_bad_option:
    2595                 :          0 :         printk(KERN_WARNING "SELinux: unable to change security options "
    2596                 :          0 :                "during remount (dev %s, type=%s)\n", sb->s_id,
    2597                 :          0 :                sb->s_type->name);
    2598                 :          0 :         goto out_free_opts;
    2599                 :            : }
    2600                 :            : 
    2601                 :          0 : static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
    2602                 :            : {
    2603                 :          0 :         const struct cred *cred = current_cred();
    2604                 :            :         struct common_audit_data ad;
    2605                 :            :         int rc;
    2606                 :            : 
    2607                 :          0 :         rc = superblock_doinit(sb, data);
    2608         [ #  # ]:          0 :         if (rc)
    2609                 :            :                 return rc;
    2610                 :            : 
    2611                 :            :         /* Allow all mounts performed by the kernel */
    2612         [ #  # ]:          0 :         if (flags & MS_KERNMOUNT)
    2613                 :            :                 return 0;
    2614                 :            : 
    2615                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    2616                 :          0 :         ad.u.dentry = sb->s_root;
    2617                 :          0 :         return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
    2618                 :            : }
    2619                 :            : 
    2620                 :          0 : static int selinux_sb_statfs(struct dentry *dentry)
    2621                 :            : {
    2622                 :          0 :         const struct cred *cred = current_cred();
    2623                 :            :         struct common_audit_data ad;
    2624                 :            : 
    2625                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    2626                 :          0 :         ad.u.dentry = dentry->d_sb->s_root;
    2627                 :          0 :         return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
    2628                 :            : }
    2629                 :            : 
    2630                 :          0 : static int selinux_mount(const char *dev_name,
    2631                 :            :                          struct path *path,
    2632                 :            :                          const char *type,
    2633                 :            :                          unsigned long flags,
    2634                 :            :                          void *data)
    2635                 :            : {
    2636                 :          0 :         const struct cred *cred = current_cred();
    2637                 :            : 
    2638         [ #  # ]:          0 :         if (flags & MS_REMOUNT)
    2639                 :          0 :                 return superblock_has_perm(cred, path->dentry->d_sb,
    2640                 :            :                                            FILESYSTEM__REMOUNT, NULL);
    2641                 :            :         else
    2642                 :          0 :                 return path_has_perm(cred, path, FILE__MOUNTON);
    2643                 :            : }
    2644                 :            : 
    2645                 :          0 : static int selinux_umount(struct vfsmount *mnt, int flags)
    2646                 :            : {
    2647                 :          0 :         const struct cred *cred = current_cred();
    2648                 :            : 
    2649                 :          0 :         return superblock_has_perm(cred, mnt->mnt_sb,
    2650                 :            :                                    FILESYSTEM__UNMOUNT, NULL);
    2651                 :            : }
    2652                 :            : 
    2653                 :            : /* inode security operations */
    2654                 :            : 
    2655                 :          0 : static int selinux_inode_alloc_security(struct inode *inode)
    2656                 :            : {
    2657                 :          0 :         return inode_alloc_security(inode);
    2658                 :            : }
    2659                 :            : 
    2660                 :          0 : static void selinux_inode_free_security(struct inode *inode)
    2661                 :            : {
    2662                 :          0 :         inode_free_security(inode);
    2663                 :          0 : }
    2664                 :            : 
    2665                 :          0 : static int selinux_dentry_init_security(struct dentry *dentry, int mode,
    2666                 :            :                                         struct qstr *name, void **ctx,
    2667                 :            :                                         u32 *ctxlen)
    2668                 :            : {
    2669                 :          0 :         const struct cred *cred = current_cred();
    2670                 :            :         struct task_security_struct *tsec;
    2671                 :            :         struct inode_security_struct *dsec;
    2672                 :            :         struct superblock_security_struct *sbsec;
    2673                 :          0 :         struct inode *dir = dentry->d_parent->d_inode;
    2674                 :            :         u32 newsid;
    2675                 :            :         int rc;
    2676                 :            : 
    2677                 :          0 :         tsec = cred->security;
    2678                 :          0 :         dsec = dir->i_security;
    2679                 :          0 :         sbsec = dir->i_sb->s_security;
    2680                 :            : 
    2681 [ #  # ][ #  # ]:          0 :         if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
    2682                 :          0 :                 newsid = tsec->create_sid;
    2683                 :            :         } else {
    2684                 :          0 :                 rc = security_transition_sid(tsec->sid, dsec->sid,
    2685                 :            :                                              inode_mode_to_security_class(mode),
    2686                 :            :                                              name,
    2687                 :            :                                              &newsid);
    2688         [ #  # ]:          0 :                 if (rc) {
    2689                 :          0 :                         printk(KERN_WARNING
    2690                 :            :                                 "%s: security_transition_sid failed, rc=%d\n",
    2691                 :            :                                __func__, -rc);
    2692                 :          0 :                         return rc;
    2693                 :            :                 }
    2694                 :            :         }
    2695                 :            : 
    2696                 :          0 :         return security_sid_to_context(newsid, (char **)ctx, ctxlen);
    2697                 :            : }
    2698                 :            : 
    2699                 :          0 : static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
    2700                 :            :                                        const struct qstr *qstr,
    2701                 :            :                                        const char **name,
    2702                 :            :                                        void **value, size_t *len)
    2703                 :            : {
    2704                 :          0 :         const struct task_security_struct *tsec = current_security();
    2705                 :            :         struct inode_security_struct *dsec;
    2706                 :            :         struct superblock_security_struct *sbsec;
    2707                 :            :         u32 sid, newsid, clen;
    2708                 :            :         int rc;
    2709                 :            :         char *context;
    2710                 :            : 
    2711                 :          0 :         dsec = dir->i_security;
    2712                 :          0 :         sbsec = dir->i_sb->s_security;
    2713                 :            : 
    2714                 :          0 :         sid = tsec->sid;
    2715                 :          0 :         newsid = tsec->create_sid;
    2716                 :            : 
    2717 [ #  # ][ #  # ]:          0 :         if ((sbsec->flags & SE_SBINITIALIZED) &&
    2718                 :          0 :             (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
    2719                 :          0 :                 newsid = sbsec->mntpoint_sid;
    2720 [ #  # ][ #  # ]:          0 :         else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
    2721                 :          0 :                 rc = security_transition_sid(sid, dsec->sid,
    2722                 :            :                                              inode_mode_to_security_class(inode->i_mode),
    2723                 :            :                                              qstr, &newsid);
    2724         [ #  # ]:          0 :                 if (rc) {
    2725                 :          0 :                         printk(KERN_WARNING "%s:  "
    2726                 :            :                                "security_transition_sid failed, rc=%d (dev=%s "
    2727                 :            :                                "ino=%ld)\n",
    2728                 :            :                                __func__,
    2729                 :          0 :                                -rc, inode->i_sb->s_id, inode->i_ino);
    2730                 :          0 :                         return rc;
    2731                 :            :                 }
    2732                 :            :         }
    2733                 :            : 
    2734                 :            :         /* Possibly defer initialization to selinux_complete_init. */
    2735         [ #  # ]:          0 :         if (sbsec->flags & SE_SBINITIALIZED) {
    2736                 :          0 :                 struct inode_security_struct *isec = inode->i_security;
    2737                 :          0 :                 isec->sclass = inode_mode_to_security_class(inode->i_mode);
    2738                 :          0 :                 isec->sid = newsid;
    2739                 :          0 :                 isec->initialized = 1;
    2740                 :            :         }
    2741                 :            : 
    2742 [ #  # ][ #  # ]:          0 :         if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
    2743                 :            :                 return -EOPNOTSUPP;
    2744                 :            : 
    2745         [ #  # ]:          0 :         if (name)
    2746                 :          0 :                 *name = XATTR_SELINUX_SUFFIX;
    2747                 :            : 
    2748         [ #  # ]:          0 :         if (value && len) {
    2749                 :          0 :                 rc = security_sid_to_context_force(newsid, &context, &clen);
    2750         [ #  # ]:          0 :                 if (rc)
    2751                 :            :                         return rc;
    2752                 :          0 :                 *value = context;
    2753                 :          0 :                 *len = clen;
    2754                 :            :         }
    2755                 :            : 
    2756                 :            :         return 0;
    2757                 :            : }
    2758                 :            : 
    2759                 :          0 : static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
    2760                 :            : {
    2761                 :          0 :         return may_create(dir, dentry, SECCLASS_FILE);
    2762                 :            : }
    2763                 :            : 
    2764                 :          0 : static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
    2765                 :            : {
    2766                 :          0 :         return may_link(dir, old_dentry, MAY_LINK);
    2767                 :            : }
    2768                 :            : 
    2769                 :          0 : static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
    2770                 :            : {
    2771                 :          0 :         return may_link(dir, dentry, MAY_UNLINK);
    2772                 :            : }
    2773                 :            : 
    2774                 :          0 : static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
    2775                 :            : {
    2776                 :          0 :         return may_create(dir, dentry, SECCLASS_LNK_FILE);
    2777                 :            : }
    2778                 :            : 
    2779                 :          0 : static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
    2780                 :            : {
    2781                 :          0 :         return may_create(dir, dentry, SECCLASS_DIR);
    2782                 :            : }
    2783                 :            : 
    2784                 :          0 : static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
    2785                 :            : {
    2786                 :          0 :         return may_link(dir, dentry, MAY_RMDIR);
    2787                 :            : }
    2788                 :            : 
    2789                 :          0 : static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
    2790                 :            : {
    2791                 :          0 :         return may_create(dir, dentry, inode_mode_to_security_class(mode));
    2792                 :            : }
    2793                 :            : 
    2794                 :          0 : static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
    2795                 :            :                                 struct inode *new_inode, struct dentry *new_dentry)
    2796                 :            : {
    2797                 :          0 :         return may_rename(old_inode, old_dentry, new_inode, new_dentry);
    2798                 :            : }
    2799                 :            : 
    2800                 :          0 : static int selinux_inode_readlink(struct dentry *dentry)
    2801                 :            : {
    2802                 :          0 :         const struct cred *cred = current_cred();
    2803                 :            : 
    2804                 :          0 :         return dentry_has_perm(cred, dentry, FILE__READ);
    2805                 :            : }
    2806                 :            : 
    2807                 :          0 : static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
    2808                 :            : {
    2809                 :          0 :         const struct cred *cred = current_cred();
    2810                 :            : 
    2811                 :          0 :         return dentry_has_perm(cred, dentry, FILE__READ);
    2812                 :            : }
    2813                 :            : 
    2814                 :          0 : static noinline int audit_inode_permission(struct inode *inode,
    2815                 :            :                                            u32 perms, u32 audited, u32 denied,
    2816                 :            :                                            unsigned flags)
    2817                 :            : {
    2818                 :            :         struct common_audit_data ad;
    2819                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    2820                 :            :         int rc;
    2821                 :            : 
    2822                 :          0 :         ad.type = LSM_AUDIT_DATA_INODE;
    2823                 :          0 :         ad.u.inode = inode;
    2824                 :            : 
    2825                 :          0 :         rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
    2826                 :            :                             audited, denied, &ad, flags);
    2827         [ #  # ]:          0 :         if (rc)
    2828                 :          0 :                 return rc;
    2829                 :            :         return 0;
    2830                 :            : }
    2831                 :            : 
    2832                 :          0 : static int selinux_inode_permission(struct inode *inode, int mask)
    2833                 :            : {
    2834                 :          0 :         const struct cred *cred = current_cred();
    2835                 :            :         u32 perms;
    2836                 :            :         bool from_access;
    2837                 :          0 :         unsigned flags = mask & MAY_NOT_BLOCK;
    2838                 :            :         struct inode_security_struct *isec;
    2839                 :            :         u32 sid;
    2840                 :            :         struct av_decision avd;
    2841                 :            :         int rc, rc2;
    2842                 :            :         u32 audited, denied;
    2843                 :            : 
    2844                 :          0 :         from_access = mask & MAY_ACCESS;
    2845                 :          0 :         mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
    2846                 :            : 
    2847                 :            :         /* No permission to check.  Existence test. */
    2848         [ #  # ]:          0 :         if (!mask)
    2849                 :            :                 return 0;
    2850                 :            : 
    2851                 :            :         validate_creds(cred);
    2852                 :            : 
    2853         [ #  # ]:          0 :         if (unlikely(IS_PRIVATE(inode)))
    2854                 :            :                 return 0;
    2855                 :            : 
    2856                 :          0 :         perms = file_mask_to_av(inode->i_mode, mask);
    2857                 :            : 
    2858                 :            :         sid = cred_sid(cred);
    2859                 :          0 :         isec = inode->i_security;
    2860                 :            : 
    2861                 :          0 :         rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
    2862         [ #  # ]:          0 :         audited = avc_audit_required(perms, &avd, rc,
    2863                 :            :                                      from_access ? FILE__AUDIT_ACCESS : 0,
    2864                 :            :                                      &denied);
    2865         [ #  # ]:          0 :         if (likely(!audited))
    2866                 :            :                 return rc;
    2867                 :            : 
    2868                 :          0 :         rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
    2869         [ #  # ]:          0 :         if (rc2)
    2870                 :            :                 return rc2;
    2871                 :          0 :         return rc;
    2872                 :            : }
    2873                 :            : 
    2874                 :          0 : static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
    2875                 :            : {
    2876                 :          0 :         const struct cred *cred = current_cred();
    2877                 :          0 :         unsigned int ia_valid = iattr->ia_valid;
    2878                 :            :         __u32 av = FILE__WRITE;
    2879                 :            : 
    2880                 :            :         /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
    2881         [ #  # ]:          0 :         if (ia_valid & ATTR_FORCE) {
    2882                 :          0 :                 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
    2883                 :            :                               ATTR_FORCE);
    2884         [ #  # ]:          0 :                 if (!ia_valid)
    2885                 :            :                         return 0;
    2886                 :            :         }
    2887                 :            : 
    2888         [ #  # ]:          0 :         if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
    2889                 :            :                         ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
    2890                 :          0 :                 return dentry_has_perm(cred, dentry, FILE__SETATTR);
    2891                 :            : 
    2892 [ #  # ][ #  # ]:          0 :         if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
    2893                 :            :                 av |= FILE__OPEN;
    2894                 :            : 
    2895                 :          0 :         return dentry_has_perm(cred, dentry, av);
    2896                 :            : }
    2897                 :            : 
    2898                 :          0 : static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
    2899                 :            : {
    2900                 :          0 :         const struct cred *cred = current_cred();
    2901                 :            :         struct path path;
    2902                 :            : 
    2903                 :            :         path.dentry = dentry;
    2904                 :            :         path.mnt = mnt;
    2905                 :            : 
    2906                 :          0 :         return path_has_perm(cred, &path, FILE__GETATTR);
    2907                 :            : }
    2908                 :            : 
    2909                 :          0 : static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
    2910                 :            : {
    2911                 :          0 :         const struct cred *cred = current_cred();
    2912                 :            : 
    2913         [ #  # ]:          0 :         if (!strncmp(name, XATTR_SECURITY_PREFIX,
    2914                 :            :                      sizeof XATTR_SECURITY_PREFIX - 1)) {
    2915         [ #  # ]:          0 :                 if (!strcmp(name, XATTR_NAME_CAPS)) {
    2916         [ #  # ]:          0 :                         if (!capable(CAP_SETFCAP))
    2917                 :            :                                 return -EPERM;
    2918         [ #  # ]:          0 :                 } else if (!capable(CAP_SYS_ADMIN)) {
    2919                 :            :                         /* A different attribute in the security namespace.
    2920                 :            :                            Restrict to administrator. */
    2921                 :            :                         return -EPERM;
    2922                 :            :                 }
    2923                 :            :         }
    2924                 :            : 
    2925                 :            :         /* Not an attribute we recognize, so just check the
    2926                 :            :            ordinary setattr permission. */
    2927                 :          0 :         return dentry_has_perm(cred, dentry, FILE__SETATTR);
    2928                 :            : }
    2929                 :            : 
    2930                 :          0 : static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
    2931                 :            :                                   const void *value, size_t size, int flags)
    2932                 :            : {
    2933                 :          0 :         struct inode *inode = dentry->d_inode;
    2934                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    2935                 :            :         struct superblock_security_struct *sbsec;
    2936                 :            :         struct common_audit_data ad;
    2937                 :            :         u32 newsid, sid = current_sid();
    2938                 :            :         int rc = 0;
    2939                 :            : 
    2940         [ #  # ]:          0 :         if (strcmp(name, XATTR_NAME_SELINUX))
    2941                 :          0 :                 return selinux_inode_setotherxattr(dentry, name);
    2942                 :            : 
    2943                 :          0 :         sbsec = inode->i_sb->s_security;
    2944         [ #  # ]:          0 :         if (!(sbsec->flags & SBLABEL_MNT))
    2945                 :            :                 return -EOPNOTSUPP;
    2946                 :            : 
    2947         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    2948                 :            :                 return -EPERM;
    2949                 :            : 
    2950                 :          0 :         ad.type = LSM_AUDIT_DATA_DENTRY;
    2951                 :          0 :         ad.u.dentry = dentry;
    2952                 :            : 
    2953                 :          0 :         rc = avc_has_perm(sid, isec->sid, isec->sclass,
    2954                 :            :                           FILE__RELABELFROM, &ad);
    2955         [ #  # ]:          0 :         if (rc)
    2956                 :            :                 return rc;
    2957                 :            : 
    2958                 :          0 :         rc = security_context_to_sid(value, size, &newsid);
    2959         [ #  # ]:          0 :         if (rc == -EINVAL) {
    2960         [ #  # ]:          0 :                 if (!capable(CAP_MAC_ADMIN)) {
    2961                 :            :                         struct audit_buffer *ab;
    2962                 :            :                         size_t audit_size;
    2963                 :            :                         const char *str;
    2964                 :            : 
    2965                 :            :                         /* We strip a nul only if it is at the end, otherwise the
    2966                 :            :                          * context contains a nul and we should audit that */
    2967         [ #  # ]:          0 :                         if (value) {
    2968                 :            :                                 str = value;
    2969         [ #  # ]:          0 :                                 if (str[size - 1] == '\0')
    2970                 :            :                                         audit_size = size - 1;
    2971                 :            :                                 else
    2972                 :            :                                         audit_size = size;
    2973                 :            :                         } else {
    2974                 :            :                                 str = "";
    2975                 :            :                                 audit_size = 0;
    2976                 :            :                         }
    2977                 :          0 :                         ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
    2978                 :          0 :                         audit_log_format(ab, "op=setxattr invalid_context=");
    2979                 :          0 :                         audit_log_n_untrustedstring(ab, value, audit_size);
    2980                 :          0 :                         audit_log_end(ab);
    2981                 :            : 
    2982                 :          0 :                         return rc;
    2983                 :            :                 }
    2984                 :          0 :                 rc = security_context_to_sid_force(value, size, &newsid);
    2985                 :            :         }
    2986         [ #  # ]:          0 :         if (rc)
    2987                 :            :                 return rc;
    2988                 :            : 
    2989                 :          0 :         rc = avc_has_perm(sid, newsid, isec->sclass,
    2990                 :            :                           FILE__RELABELTO, &ad);
    2991         [ #  # ]:          0 :         if (rc)
    2992                 :            :                 return rc;
    2993                 :            : 
    2994                 :          0 :         rc = security_validate_transition(isec->sid, newsid, sid,
    2995                 :            :                                           isec->sclass);
    2996         [ #  # ]:          0 :         if (rc)
    2997                 :            :                 return rc;
    2998                 :            : 
    2999                 :          0 :         return avc_has_perm(newsid,
    3000                 :            :                             sbsec->sid,
    3001                 :            :                             SECCLASS_FILESYSTEM,
    3002                 :            :                             FILESYSTEM__ASSOCIATE,
    3003                 :            :                             &ad);
    3004                 :            : }
    3005                 :            : 
    3006                 :          0 : static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
    3007                 :            :                                         const void *value, size_t size,
    3008                 :            :                                         int flags)
    3009                 :            : {
    3010                 :          0 :         struct inode *inode = dentry->d_inode;
    3011                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3012                 :            :         u32 newsid;
    3013                 :            :         int rc;
    3014                 :            : 
    3015         [ #  # ]:          0 :         if (strcmp(name, XATTR_NAME_SELINUX)) {
    3016                 :            :                 /* Not an attribute we recognize, so nothing to do. */
    3017                 :            :                 return;
    3018                 :            :         }
    3019                 :            : 
    3020                 :          0 :         rc = security_context_to_sid_force(value, size, &newsid);
    3021         [ #  # ]:          0 :         if (rc) {
    3022                 :          0 :                 printk(KERN_ERR "SELinux:  unable to map context to SID"
    3023                 :            :                        "for (%s, %lu), rc=%d\n",
    3024                 :          0 :                        inode->i_sb->s_id, inode->i_ino, -rc);
    3025                 :          0 :                 return;
    3026                 :            :         }
    3027                 :            : 
    3028                 :          0 :         isec->sclass = inode_mode_to_security_class(inode->i_mode);
    3029                 :          0 :         isec->sid = newsid;
    3030                 :          0 :         isec->initialized = 1;
    3031                 :            : 
    3032                 :          0 :         return;
    3033                 :            : }
    3034                 :            : 
    3035                 :          0 : static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
    3036                 :            : {
    3037                 :          0 :         const struct cred *cred = current_cred();
    3038                 :            : 
    3039                 :          0 :         return dentry_has_perm(cred, dentry, FILE__GETATTR);
    3040                 :            : }
    3041                 :            : 
    3042                 :          0 : static int selinux_inode_listxattr(struct dentry *dentry)
    3043                 :            : {
    3044                 :          0 :         const struct cred *cred = current_cred();
    3045                 :            : 
    3046                 :          0 :         return dentry_has_perm(cred, dentry, FILE__GETATTR);
    3047                 :            : }
    3048                 :            : 
    3049                 :          0 : static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
    3050                 :            : {
    3051         [ #  # ]:          0 :         if (strcmp(name, XATTR_NAME_SELINUX))
    3052                 :          0 :                 return selinux_inode_setotherxattr(dentry, name);
    3053                 :            : 
    3054                 :            :         /* No one is allowed to remove a SELinux security label.
    3055                 :            :            You can change the label, but all data must be labeled. */
    3056                 :            :         return -EACCES;
    3057                 :            : }
    3058                 :            : 
    3059                 :            : /*
    3060                 :            :  * Copy the inode security context value to the user.
    3061                 :            :  *
    3062                 :            :  * Permission check is handled by selinux_inode_getxattr hook.
    3063                 :            :  */
    3064                 :          0 : static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
    3065                 :            : {
    3066                 :            :         u32 size;
    3067                 :            :         int error;
    3068                 :          0 :         char *context = NULL;
    3069                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3070                 :            : 
    3071         [ #  # ]:          0 :         if (strcmp(name, XATTR_SELINUX_SUFFIX))
    3072                 :            :                 return -EOPNOTSUPP;
    3073                 :            : 
    3074                 :            :         /*
    3075                 :            :          * If the caller has CAP_MAC_ADMIN, then get the raw context
    3076                 :            :          * value even if it is not defined by current policy; otherwise,
    3077                 :            :          * use the in-core value under current policy.
    3078                 :            :          * Use the non-auditing forms of the permission checks since
    3079                 :            :          * getxattr may be called by unprivileged processes commonly
    3080                 :            :          * and lack of permission just means that we fall back to the
    3081                 :            :          * in-core context value, not a denial.
    3082                 :            :          */
    3083                 :          0 :         error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
    3084                 :            :                                 SECURITY_CAP_NOAUDIT);
    3085         [ #  # ]:          0 :         if (!error)
    3086                 :          0 :                 error = security_sid_to_context_force(isec->sid, &context,
    3087                 :            :                                                       &size);
    3088                 :            :         else
    3089                 :          0 :                 error = security_sid_to_context(isec->sid, &context, &size);
    3090         [ #  # ]:          0 :         if (error)
    3091                 :            :                 return error;
    3092                 :          0 :         error = size;
    3093         [ #  # ]:          0 :         if (alloc) {
    3094                 :          0 :                 *buffer = context;
    3095                 :          0 :                 goto out_nofree;
    3096                 :            :         }
    3097                 :          0 :         kfree(context);
    3098                 :            : out_nofree:
    3099                 :          0 :         return error;
    3100                 :            : }
    3101                 :            : 
    3102                 :          0 : static int selinux_inode_setsecurity(struct inode *inode, const char *name,
    3103                 :            :                                      const void *value, size_t size, int flags)
    3104                 :            : {
    3105                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3106                 :            :         u32 newsid;
    3107                 :            :         int rc;
    3108                 :            : 
    3109         [ #  # ]:          0 :         if (strcmp(name, XATTR_SELINUX_SUFFIX))
    3110                 :            :                 return -EOPNOTSUPP;
    3111                 :            : 
    3112         [ #  # ]:          0 :         if (!value || !size)
    3113                 :            :                 return -EACCES;
    3114                 :            : 
    3115                 :          0 :         rc = security_context_to_sid((void *)value, size, &newsid);
    3116         [ #  # ]:          0 :         if (rc)
    3117                 :            :                 return rc;
    3118                 :            : 
    3119                 :          0 :         isec->sclass = inode_mode_to_security_class(inode->i_mode);
    3120                 :          0 :         isec->sid = newsid;
    3121                 :          0 :         isec->initialized = 1;
    3122                 :          0 :         return 0;
    3123                 :            : }
    3124                 :            : 
    3125                 :          0 : static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
    3126                 :            : {
    3127                 :            :         const int len = sizeof(XATTR_NAME_SELINUX);
    3128         [ #  # ]:          0 :         if (buffer && len <= buffer_size)
    3129                 :          0 :                 memcpy(buffer, XATTR_NAME_SELINUX, len);
    3130                 :          0 :         return len;
    3131                 :            : }
    3132                 :            : 
    3133                 :          0 : static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
    3134                 :            : {
    3135                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3136                 :          0 :         *secid = isec->sid;
    3137                 :          0 : }
    3138                 :            : 
    3139                 :            : /* file security operations */
    3140                 :            : 
    3141                 :          0 : static int selinux_revalidate_file_permission(struct file *file, int mask)
    3142                 :            : {
    3143                 :          0 :         const struct cred *cred = current_cred();
    3144                 :            :         struct inode *inode = file_inode(file);
    3145                 :            : 
    3146                 :            :         /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
    3147 [ #  # ][ #  # ]:          0 :         if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
    3148                 :          0 :                 mask |= MAY_APPEND;
    3149                 :            : 
    3150                 :          0 :         return file_has_perm(cred, file,
    3151                 :          0 :                              file_mask_to_av(inode->i_mode, mask));
    3152                 :            : }
    3153                 :            : 
    3154                 :          0 : static int selinux_file_permission(struct file *file, int mask)
    3155                 :            : {
    3156                 :            :         struct inode *inode = file_inode(file);
    3157                 :          0 :         struct file_security_struct *fsec = file->f_security;
    3158                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3159                 :            :         u32 sid = current_sid();
    3160                 :            : 
    3161         [ #  # ]:          0 :         if (!mask)
    3162                 :            :                 /* No permission to check.  Existence test. */
    3163                 :            :                 return 0;
    3164                 :            : 
    3165         [ #  # ]:          0 :         if (sid == fsec->sid && fsec->isid == isec->sid &&
           [ #  #  #  # ]
    3166                 :          0 :             fsec->pseqno == avc_policy_seqno())
    3167                 :            :                 /* No change since file_open check. */
    3168                 :            :                 return 0;
    3169                 :            : 
    3170                 :          0 :         return selinux_revalidate_file_permission(file, mask);
    3171                 :            : }
    3172                 :            : 
    3173                 :          0 : static int selinux_file_alloc_security(struct file *file)
    3174                 :            : {
    3175                 :          0 :         return file_alloc_security(file);
    3176                 :            : }
    3177                 :            : 
    3178                 :          0 : static void selinux_file_free_security(struct file *file)
    3179                 :            : {
    3180                 :            :         file_free_security(file);
    3181                 :          0 : }
    3182                 :            : 
    3183                 :          0 : static int selinux_file_ioctl(struct file *file, unsigned int cmd,
    3184                 :            :                               unsigned long arg)
    3185                 :            : {
    3186                 :          0 :         const struct cred *cred = current_cred();
    3187                 :            :         int error = 0;
    3188                 :            : 
    3189   [ #  #  #  #  :          0 :         switch (cmd) {
                      # ]
    3190                 :            :         case FIONREAD:
    3191                 :            :         /* fall through */
    3192                 :            :         case FIBMAP:
    3193                 :            :         /* fall through */
    3194                 :            :         case FIGETBSZ:
    3195                 :            :         /* fall through */
    3196                 :            :         case FS_IOC_GETFLAGS:
    3197                 :            :         /* fall through */
    3198                 :            :         case FS_IOC_GETVERSION:
    3199                 :          0 :                 error = file_has_perm(cred, file, FILE__GETATTR);
    3200                 :          0 :                 break;
    3201                 :            : 
    3202                 :            :         case FS_IOC_SETFLAGS:
    3203                 :            :         /* fall through */
    3204                 :            :         case FS_IOC_SETVERSION:
    3205                 :          0 :                 error = file_has_perm(cred, file, FILE__SETATTR);
    3206                 :          0 :                 break;
    3207                 :            : 
    3208                 :            :         /* sys_ioctl() checks */
    3209                 :            :         case FIONBIO:
    3210                 :            :         /* fall through */
    3211                 :            :         case FIOASYNC:
    3212                 :          0 :                 error = file_has_perm(cred, file, 0);
    3213                 :          0 :                 break;
    3214                 :            : 
    3215                 :            :         case KDSKBENT:
    3216                 :            :         case KDSKBSENT:
    3217                 :          0 :                 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
    3218                 :            :                                             SECURITY_CAP_AUDIT);
    3219                 :          0 :                 break;
    3220                 :            : 
    3221                 :            :         /* default case assumes that the command will go
    3222                 :            :          * to the file's ioctl() function.
    3223                 :            :          */
    3224                 :            :         default:
    3225                 :          0 :                 error = file_has_perm(cred, file, FILE__IOCTL);
    3226                 :            :         }
    3227                 :          0 :         return error;
    3228                 :            : }
    3229                 :            : 
    3230                 :            : static int default_noexec;
    3231                 :            : 
    3232                 :          0 : static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
    3233                 :            : {
    3234                 :          0 :         const struct cred *cred = current_cred();
    3235                 :            :         int rc = 0;
    3236                 :            : 
    3237 [ #  # ][ #  # ]:          0 :         if (default_noexec &&
    3238 [ #  # ][ #  # ]:          0 :             (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
                 [ #  # ]
    3239                 :            :                 /*
    3240                 :            :                  * We are making executable an anonymous mapping or a
    3241                 :            :                  * private file mapping that will also be writable.
    3242                 :            :                  * This has an additional check.
    3243                 :            :                  */
    3244                 :            :                 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
    3245         [ #  # ]:          0 :                 if (rc)
    3246                 :            :                         goto error;
    3247                 :            :         }
    3248                 :            : 
    3249         [ #  # ]:          0 :         if (file) {
    3250                 :            :                 /* read access is always possible with a mapping */
    3251                 :            :                 u32 av = FILE__READ;
    3252                 :            : 
    3253                 :            :                 /* write access only matters if the mapping is shared */
    3254 [ #  # ][ #  # ]:          0 :                 if (shared && (prot & PROT_WRITE))
    3255                 :            :                         av |= FILE__WRITE;
    3256                 :            : 
    3257         [ #  # ]:          0 :                 if (prot & PROT_EXEC)
    3258                 :          0 :                         av |= FILE__EXECUTE;
    3259                 :            : 
    3260                 :          0 :                 return file_has_perm(cred, file, av);
    3261                 :            :         }
    3262                 :            : 
    3263                 :            : error:
    3264                 :          0 :         return rc;
    3265                 :            : }
    3266                 :            : 
    3267                 :          0 : static int selinux_mmap_addr(unsigned long addr)
    3268                 :            : {
    3269                 :            :         int rc = 0;
    3270                 :            :         u32 sid = current_sid();
    3271                 :            : 
    3272                 :            :         /*
    3273                 :            :          * notice that we are intentionally putting the SELinux check before
    3274                 :            :          * the secondary cap_file_mmap check.  This is such a likely attempt
    3275                 :            :          * at bad behaviour/exploit that we always want to get the AVC, even
    3276                 :            :          * if DAC would have also denied the operation.
    3277                 :            :          */
    3278                 :            :         if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
    3279                 :            :                 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
    3280                 :            :                                   MEMPROTECT__MMAP_ZERO, NULL);
    3281                 :            :                 if (rc)
    3282                 :            :                         return rc;
    3283                 :            :         }
    3284                 :            : 
    3285                 :            :         /* do DAC check on address space usage */
    3286                 :          0 :         return cap_mmap_addr(addr);
    3287                 :            : }
    3288                 :            : 
    3289                 :          0 : static int selinux_mmap_file(struct file *file, unsigned long reqprot,
    3290                 :            :                              unsigned long prot, unsigned long flags)
    3291                 :            : {
    3292         [ #  # ]:          0 :         if (selinux_checkreqprot)
    3293                 :            :                 prot = reqprot;
    3294                 :            : 
    3295                 :          0 :         return file_map_prot_check(file, prot,
    3296                 :          0 :                                    (flags & MAP_TYPE) == MAP_SHARED);
    3297                 :            : }
    3298                 :            : 
    3299                 :          0 : static int selinux_file_mprotect(struct vm_area_struct *vma,
    3300                 :            :                                  unsigned long reqprot,
    3301                 :            :                                  unsigned long prot)
    3302                 :            : {
    3303                 :          0 :         const struct cred *cred = current_cred();
    3304                 :            : 
    3305         [ #  # ]:          0 :         if (selinux_checkreqprot)
    3306                 :            :                 prot = reqprot;
    3307                 :            : 
    3308 [ #  # ][ #  # ]:          0 :         if (default_noexec &&
    3309         [ #  # ]:          0 :             (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
    3310                 :            :                 int rc = 0;
    3311 [ #  # ][ #  # ]:          0 :                 if (vma->vm_start >= vma->vm_mm->start_brk &&
    3312                 :          0 :                     vma->vm_end <= vma->vm_mm->brk) {
    3313                 :          0 :                         rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
    3314 [ #  # ][ #  # ]:          0 :                 } else if (!vma->vm_file &&
    3315         [ #  # ]:          0 :                            vma->vm_start <= vma->vm_mm->start_stack &&
    3316                 :          0 :                            vma->vm_end >= vma->vm_mm->start_stack) {
    3317                 :          0 :                         rc = current_has_perm(current, PROCESS__EXECSTACK);
    3318 [ #  # ][ #  # ]:          0 :                 } else if (vma->vm_file && vma->anon_vma) {
    3319                 :            :                         /*
    3320                 :            :                          * We are making executable a file mapping that has
    3321                 :            :                          * had some COW done. Since pages might have been
    3322                 :            :                          * written, check ability to execute the possibly
    3323                 :            :                          * modified content.  This typically should only
    3324                 :            :                          * occur for text relocations.
    3325                 :            :                          */
    3326                 :          0 :                         rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
    3327                 :            :                 }
    3328         [ #  # ]:          0 :                 if (rc)
    3329                 :            :                         return rc;
    3330                 :            :         }
    3331                 :            : 
    3332                 :          0 :         return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
    3333                 :            : }
    3334                 :            : 
    3335                 :          0 : static int selinux_file_lock(struct file *file, unsigned int cmd)
    3336                 :            : {
    3337                 :          0 :         const struct cred *cred = current_cred();
    3338                 :            : 
    3339                 :          0 :         return file_has_perm(cred, file, FILE__LOCK);
    3340                 :            : }
    3341                 :            : 
    3342                 :          0 : static int selinux_file_fcntl(struct file *file, unsigned int cmd,
    3343                 :            :                               unsigned long arg)
    3344                 :            : {
    3345 [ #  # ][ #  # ]:          0 :         const struct cred *cred = current_cred();
         [ #  # ][ #  # ]
    3346                 :            :         int err = 0;
    3347                 :            : 
    3348                 :            :         switch (cmd) {
    3349                 :            :         case F_SETFL:
    3350 [ #  # ][ #  # ]:          0 :                 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
    3351                 :          0 :                         err = file_has_perm(cred, file, FILE__WRITE);
    3352                 :          0 :                         break;
    3353                 :            :                 }
    3354                 :            :                 /* fall through */
    3355                 :            :         case F_SETOWN:
    3356                 :            :         case F_SETSIG:
    3357                 :            :         case F_GETFL:
    3358                 :            :         case F_GETOWN:
    3359                 :            :         case F_GETSIG:
    3360                 :            :         case F_GETOWNER_UIDS:
    3361                 :            :                 /* Just check FD__USE permission */
    3362                 :          0 :                 err = file_has_perm(cred, file, 0);
    3363                 :          0 :                 break;
    3364                 :            :         case F_GETLK:
    3365                 :            :         case F_SETLK:
    3366                 :            :         case F_SETLKW:
    3367                 :            : #if BITS_PER_LONG == 32
    3368                 :            :         case F_GETLK64:
    3369                 :            :         case F_SETLK64:
    3370                 :            :         case F_SETLKW64:
    3371                 :            : #endif
    3372                 :          0 :                 err = file_has_perm(cred, file, FILE__LOCK);
    3373                 :          0 :                 break;
    3374                 :            :         }
    3375                 :            : 
    3376                 :          0 :         return err;
    3377                 :            : }
    3378                 :            : 
    3379                 :          0 : static int selinux_file_set_fowner(struct file *file)
    3380                 :            : {
    3381                 :            :         struct file_security_struct *fsec;
    3382                 :            : 
    3383                 :          0 :         fsec = file->f_security;
    3384                 :          0 :         fsec->fown_sid = current_sid();
    3385                 :            : 
    3386                 :          0 :         return 0;
    3387                 :            : }
    3388                 :            : 
    3389                 :          0 : static int selinux_file_send_sigiotask(struct task_struct *tsk,
    3390                 :            :                                        struct fown_struct *fown, int signum)
    3391                 :            : {
    3392                 :            :         struct file *file;
    3393                 :            :         u32 sid = task_sid(tsk);
    3394                 :            :         u32 perm;
    3395                 :            :         struct file_security_struct *fsec;
    3396                 :            : 
    3397                 :            :         /* struct fown_struct is never outside the context of a struct file */
    3398                 :            :         file = container_of(fown, struct file, f_owner);
    3399                 :            : 
    3400                 :          0 :         fsec = file->f_security;
    3401                 :            : 
    3402         [ #  # ]:          0 :         if (!signum)
    3403                 :            :                 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
    3404                 :            :         else
    3405                 :            :                 perm = signal_to_av(signum);
    3406                 :            : 
    3407                 :          0 :         return avc_has_perm(fsec->fown_sid, sid,
    3408                 :            :                             SECCLASS_PROCESS, perm, NULL);
    3409                 :            : }
    3410                 :            : 
    3411                 :          0 : static int selinux_file_receive(struct file *file)
    3412                 :            : {
    3413                 :          0 :         const struct cred *cred = current_cred();
    3414                 :            : 
    3415                 :          0 :         return file_has_perm(cred, file, file_to_av(file));
    3416                 :            : }
    3417                 :            : 
    3418                 :          0 : static int selinux_file_open(struct file *file, const struct cred *cred)
    3419                 :            : {
    3420                 :            :         struct file_security_struct *fsec;
    3421                 :            :         struct inode_security_struct *isec;
    3422                 :            : 
    3423                 :          0 :         fsec = file->f_security;
    3424                 :          0 :         isec = file_inode(file)->i_security;
    3425                 :            :         /*
    3426                 :            :          * Save inode label and policy sequence number
    3427                 :            :          * at open-time so that selinux_file_permission
    3428                 :            :          * can determine whether revalidation is necessary.
    3429                 :            :          * Task label is already saved in the file security
    3430                 :            :          * struct as its SID.
    3431                 :            :          */
    3432                 :          0 :         fsec->isid = isec->sid;
    3433                 :          0 :         fsec->pseqno = avc_policy_seqno();
    3434                 :            :         /*
    3435                 :            :          * Since the inode label or policy seqno may have changed
    3436                 :            :          * between the selinux_inode_permission check and the saving
    3437                 :            :          * of state above, recheck that access is still permitted.
    3438                 :            :          * Otherwise, access might never be revalidated against the
    3439                 :            :          * new inode label or new policy.
    3440                 :            :          * This check is not redundant - do not remove.
    3441                 :            :          */
    3442                 :          0 :         return file_path_has_perm(cred, file, open_file_to_av(file));
    3443                 :            : }
    3444                 :            : 
    3445                 :            : /* task security operations */
    3446                 :            : 
    3447                 :          0 : static int selinux_task_create(unsigned long clone_flags)
    3448                 :            : {
    3449                 :          0 :         return current_has_perm(current, PROCESS__FORK);
    3450                 :            : }
    3451                 :            : 
    3452                 :            : /*
    3453                 :            :  * allocate the SELinux part of blank credentials
    3454                 :            :  */
    3455                 :          0 : static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
    3456                 :            : {
    3457                 :            :         struct task_security_struct *tsec;
    3458                 :            : 
    3459                 :            :         tsec = kzalloc(sizeof(struct task_security_struct), gfp);
    3460         [ #  # ]:          0 :         if (!tsec)
    3461                 :            :                 return -ENOMEM;
    3462                 :            : 
    3463                 :          0 :         cred->security = tsec;
    3464                 :          0 :         return 0;
    3465                 :            : }
    3466                 :            : 
    3467                 :            : /*
    3468                 :            :  * detach and free the LSM part of a set of credentials
    3469                 :            :  */
    3470                 :          0 : static void selinux_cred_free(struct cred *cred)
    3471                 :            : {
    3472                 :          0 :         struct task_security_struct *tsec = cred->security;
    3473                 :            : 
    3474                 :            :         /*
    3475                 :            :          * cred->security == NULL if security_cred_alloc_blank() or
    3476                 :            :          * security_prepare_creds() returned an error.
    3477                 :            :          */
    3478 [ #  # ][ #  # ]:          0 :         BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
    3479                 :          0 :         cred->security = (void *) 0x7UL;
    3480                 :          0 :         kfree(tsec);
    3481                 :          0 : }
    3482                 :            : 
    3483                 :            : /*
    3484                 :            :  * prepare a new set of credentials for modification
    3485                 :            :  */
    3486                 :          0 : static int selinux_cred_prepare(struct cred *new, const struct cred *old,
    3487                 :            :                                 gfp_t gfp)
    3488                 :            : {
    3489                 :            :         const struct task_security_struct *old_tsec;
    3490                 :            :         struct task_security_struct *tsec;
    3491                 :            : 
    3492                 :          0 :         old_tsec = old->security;
    3493                 :            : 
    3494                 :          0 :         tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
    3495         [ #  # ]:          0 :         if (!tsec)
    3496                 :            :                 return -ENOMEM;
    3497                 :            : 
    3498                 :          0 :         new->security = tsec;
    3499                 :          0 :         return 0;
    3500                 :            : }
    3501                 :            : 
    3502                 :            : /*
    3503                 :            :  * transfer the SELinux data to a blank set of creds
    3504                 :            :  */
    3505                 :          0 : static void selinux_cred_transfer(struct cred *new, const struct cred *old)
    3506                 :            : {
    3507                 :          0 :         const struct task_security_struct *old_tsec = old->security;
    3508                 :          0 :         struct task_security_struct *tsec = new->security;
    3509                 :            : 
    3510                 :          0 :         *tsec = *old_tsec;
    3511                 :          0 : }
    3512                 :            : 
    3513                 :            : /*
    3514                 :            :  * set the security data for a kernel service
    3515                 :            :  * - all the creation contexts are set to unlabelled
    3516                 :            :  */
    3517                 :          0 : static int selinux_kernel_act_as(struct cred *new, u32 secid)
    3518                 :            : {
    3519                 :          0 :         struct task_security_struct *tsec = new->security;
    3520                 :            :         u32 sid = current_sid();
    3521                 :            :         int ret;
    3522                 :            : 
    3523                 :          0 :         ret = avc_has_perm(sid, secid,
    3524                 :            :                            SECCLASS_KERNEL_SERVICE,
    3525                 :            :                            KERNEL_SERVICE__USE_AS_OVERRIDE,
    3526                 :            :                            NULL);
    3527         [ #  # ]:          0 :         if (ret == 0) {
    3528                 :          0 :                 tsec->sid = secid;
    3529                 :          0 :                 tsec->create_sid = 0;
    3530                 :          0 :                 tsec->keycreate_sid = 0;
    3531                 :          0 :                 tsec->sockcreate_sid = 0;
    3532                 :            :         }
    3533                 :          0 :         return ret;
    3534                 :            : }
    3535                 :            : 
    3536                 :            : /*
    3537                 :            :  * set the file creation context in a security record to the same as the
    3538                 :            :  * objective context of the specified inode
    3539                 :            :  */
    3540                 :          0 : static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
    3541                 :            : {
    3542                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3543                 :          0 :         struct task_security_struct *tsec = new->security;
    3544                 :            :         u32 sid = current_sid();
    3545                 :            :         int ret;
    3546                 :            : 
    3547                 :          0 :         ret = avc_has_perm(sid, isec->sid,
    3548                 :            :                            SECCLASS_KERNEL_SERVICE,
    3549                 :            :                            KERNEL_SERVICE__CREATE_FILES_AS,
    3550                 :            :                            NULL);
    3551                 :            : 
    3552         [ #  # ]:          0 :         if (ret == 0)
    3553                 :          0 :                 tsec->create_sid = isec->sid;
    3554                 :          0 :         return ret;
    3555                 :            : }
    3556                 :            : 
    3557                 :          0 : static int selinux_kernel_module_request(char *kmod_name)
    3558                 :            : {
    3559                 :            :         u32 sid;
    3560                 :            :         struct common_audit_data ad;
    3561                 :            : 
    3562                 :          0 :         sid = task_sid(current);
    3563                 :            : 
    3564                 :          0 :         ad.type = LSM_AUDIT_DATA_KMOD;
    3565                 :          0 :         ad.u.kmod_name = kmod_name;
    3566                 :            : 
    3567                 :          0 :         return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
    3568                 :            :                             SYSTEM__MODULE_REQUEST, &ad);
    3569                 :            : }
    3570                 :            : 
    3571                 :          0 : static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
    3572                 :            : {
    3573                 :          0 :         return current_has_perm(p, PROCESS__SETPGID);
    3574                 :            : }
    3575                 :            : 
    3576                 :          0 : static int selinux_task_getpgid(struct task_struct *p)
    3577                 :            : {
    3578                 :          0 :         return current_has_perm(p, PROCESS__GETPGID);
    3579                 :            : }
    3580                 :            : 
    3581                 :          0 : static int selinux_task_getsid(struct task_struct *p)
    3582                 :            : {
    3583                 :          0 :         return current_has_perm(p, PROCESS__GETSESSION);
    3584                 :            : }
    3585                 :            : 
    3586                 :          0 : static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
    3587                 :            : {
    3588                 :          0 :         *secid = task_sid(p);
    3589                 :          0 : }
    3590                 :            : 
    3591                 :          0 : static int selinux_task_setnice(struct task_struct *p, int nice)
    3592                 :            : {
    3593                 :            :         int rc;
    3594                 :            : 
    3595                 :          0 :         rc = cap_task_setnice(p, nice);
    3596         [ #  # ]:          0 :         if (rc)
    3597                 :            :                 return rc;
    3598                 :            : 
    3599                 :          0 :         return current_has_perm(p, PROCESS__SETSCHED);
    3600                 :            : }
    3601                 :            : 
    3602                 :          0 : static int selinux_task_setioprio(struct task_struct *p, int ioprio)
    3603                 :            : {
    3604                 :            :         int rc;
    3605                 :            : 
    3606                 :          0 :         rc = cap_task_setioprio(p, ioprio);
    3607         [ #  # ]:          0 :         if (rc)
    3608                 :            :                 return rc;
    3609                 :            : 
    3610                 :          0 :         return current_has_perm(p, PROCESS__SETSCHED);
    3611                 :            : }
    3612                 :            : 
    3613                 :          0 : static int selinux_task_getioprio(struct task_struct *p)
    3614                 :            : {
    3615                 :          0 :         return current_has_perm(p, PROCESS__GETSCHED);
    3616                 :            : }
    3617                 :            : 
    3618                 :          0 : static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
    3619                 :            :                 struct rlimit *new_rlim)
    3620                 :            : {
    3621                 :          0 :         struct rlimit *old_rlim = p->signal->rlim + resource;
    3622                 :            : 
    3623                 :            :         /* Control the ability to change the hard limit (whether
    3624                 :            :            lowering or raising it), so that the hard limit can
    3625                 :            :            later be used as a safe reset point for the soft limit
    3626                 :            :            upon context transitions.  See selinux_bprm_committing_creds. */
    3627         [ #  # ]:          0 :         if (old_rlim->rlim_max != new_rlim->rlim_max)
    3628                 :          0 :                 return current_has_perm(p, PROCESS__SETRLIMIT);
    3629                 :            : 
    3630                 :            :         return 0;
    3631                 :            : }
    3632                 :            : 
    3633                 :          0 : static int selinux_task_setscheduler(struct task_struct *p)
    3634                 :            : {
    3635                 :            :         int rc;
    3636                 :            : 
    3637                 :          0 :         rc = cap_task_setscheduler(p);
    3638         [ #  # ]:          0 :         if (rc)
    3639                 :            :                 return rc;
    3640                 :            : 
    3641                 :          0 :         return current_has_perm(p, PROCESS__SETSCHED);
    3642                 :            : }
    3643                 :            : 
    3644                 :          0 : static int selinux_task_getscheduler(struct task_struct *p)
    3645                 :            : {
    3646                 :          0 :         return current_has_perm(p, PROCESS__GETSCHED);
    3647                 :            : }
    3648                 :            : 
    3649                 :          0 : static int selinux_task_movememory(struct task_struct *p)
    3650                 :            : {
    3651                 :          0 :         return current_has_perm(p, PROCESS__SETSCHED);
    3652                 :            : }
    3653                 :            : 
    3654                 :          0 : static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
    3655                 :            :                                 int sig, u32 secid)
    3656                 :            : {
    3657                 :            :         u32 perm;
    3658                 :            :         int rc;
    3659                 :            : 
    3660         [ #  # ]:          0 :         if (!sig)
    3661                 :            :                 perm = PROCESS__SIGNULL; /* null signal; existence test */
    3662                 :            :         else
    3663                 :            :                 perm = signal_to_av(sig);
    3664         [ #  # ]:          0 :         if (secid)
    3665                 :          0 :                 rc = avc_has_perm(secid, task_sid(p),
    3666                 :            :                                   SECCLASS_PROCESS, perm, NULL);
    3667                 :            :         else
    3668                 :          0 :                 rc = current_has_perm(p, perm);
    3669                 :          0 :         return rc;
    3670                 :            : }
    3671                 :            : 
    3672                 :          0 : static int selinux_task_wait(struct task_struct *p)
    3673                 :            : {
    3674                 :          0 :         return task_has_perm(p, current, PROCESS__SIGCHLD);
    3675                 :            : }
    3676                 :            : 
    3677                 :          0 : static void selinux_task_to_inode(struct task_struct *p,
    3678                 :            :                                   struct inode *inode)
    3679                 :            : {
    3680                 :          0 :         struct inode_security_struct *isec = inode->i_security;
    3681                 :            :         u32 sid = task_sid(p);
    3682                 :            : 
    3683                 :          0 :         isec->sid = sid;
    3684                 :          0 :         isec->initialized = 1;
    3685                 :          0 : }
    3686                 :            : 
    3687                 :            : /* Returns error only if unable to parse addresses */
    3688                 :          0 : static int selinux_parse_skb_ipv4(struct sk_buff *skb,
    3689                 :            :                         struct common_audit_data *ad, u8 *proto)
    3690                 :            : {
    3691                 :            :         int offset, ihlen, ret = -EINVAL;
    3692                 :            :         struct iphdr _iph, *ih;
    3693                 :            : 
    3694                 :            :         offset = skb_network_offset(skb);
    3695                 :            :         ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
    3696         [ #  # ]:          0 :         if (ih == NULL)
    3697                 :            :                 goto out;
    3698                 :            : 
    3699                 :          0 :         ihlen = ih->ihl * 4;
    3700         [ #  # ]:          0 :         if (ihlen < sizeof(_iph))
    3701                 :            :                 goto out;
    3702                 :            : 
    3703                 :          0 :         ad->u.net->v4info.saddr = ih->saddr;
    3704                 :          0 :         ad->u.net->v4info.daddr = ih->daddr;
    3705                 :            :         ret = 0;
    3706                 :            : 
    3707         [ #  # ]:          0 :         if (proto)
    3708                 :          0 :                 *proto = ih->protocol;
    3709                 :            : 
    3710   [ #  #  #  # ]:          0 :         switch (ih->protocol) {
    3711                 :            :         case IPPROTO_TCP: {
    3712                 :            :                 struct tcphdr _tcph, *th;
    3713                 :            : 
    3714 [ #  # ][ #  # ]:          0 :                 if (ntohs(ih->frag_off) & IP_OFFSET)
    3715                 :            :                         break;
    3716                 :            : 
    3717                 :          0 :                 offset += ihlen;
    3718                 :            :                 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
    3719         [ #  # ]:          0 :                 if (th == NULL)
    3720                 :            :                         break;
    3721                 :            : 
    3722                 :          0 :                 ad->u.net->sport = th->source;
    3723                 :          0 :                 ad->u.net->dport = th->dest;
    3724                 :            :                 break;
    3725                 :            :         }
    3726                 :            : 
    3727                 :            :         case IPPROTO_UDP: {
    3728                 :            :                 struct udphdr _udph, *uh;
    3729                 :            : 
    3730 [ #  # ][ #  # ]:          0 :                 if (ntohs(ih->frag_off) & IP_OFFSET)
    3731                 :            :                         break;
    3732                 :            : 
    3733                 :          0 :                 offset += ihlen;
    3734                 :            :                 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
    3735         [ #  # ]:          0 :                 if (uh == NULL)
    3736                 :            :                         break;
    3737                 :            : 
    3738                 :          0 :                 ad->u.net->sport = uh->source;
    3739                 :          0 :                 ad->u.net->dport = uh->dest;
    3740                 :            :                 break;
    3741                 :            :         }
    3742                 :            : 
    3743                 :            :         case IPPROTO_DCCP: {
    3744                 :            :                 struct dccp_hdr _dccph, *dh;
    3745                 :            : 
    3746 [ #  # ][ #  # ]:          0 :                 if (ntohs(ih->frag_off) & IP_OFFSET)
    3747                 :            :                         break;
    3748                 :            : 
    3749                 :          0 :                 offset += ihlen;
    3750                 :            :                 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
    3751         [ #  # ]:          0 :                 if (dh == NULL)
    3752                 :            :                         break;
    3753                 :            : 
    3754                 :          0 :                 ad->u.net->sport = dh->dccph_sport;
    3755                 :          0 :                 ad->u.net->dport = dh->dccph_dport;
    3756                 :            :                 break;
    3757                 :            :         }
    3758                 :            : 
    3759                 :            :         default:
    3760                 :            :                 break;
    3761                 :            :         }
    3762                 :            : out:
    3763                 :          0 :         return ret;
    3764                 :            : }
    3765                 :            : 
    3766                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    3767                 :            : 
    3768                 :            : /* Returns error only if unable to parse addresses */
    3769                 :          0 : static int selinux_parse_skb_ipv6(struct sk_buff *skb,
    3770                 :            :                         struct common_audit_data *ad, u8 *proto)
    3771                 :            : {
    3772                 :            :         u8 nexthdr;
    3773                 :            :         int ret = -EINVAL, offset;
    3774                 :            :         struct ipv6hdr _ipv6h, *ip6;
    3775                 :            :         __be16 frag_off;
    3776                 :            : 
    3777                 :            :         offset = skb_network_offset(skb);
    3778                 :            :         ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
    3779         [ #  # ]:          0 :         if (ip6 == NULL)
    3780                 :            :                 goto out;
    3781                 :            : 
    3782                 :          0 :         ad->u.net->v6info.saddr = ip6->saddr;
    3783                 :          0 :         ad->u.net->v6info.daddr = ip6->daddr;
    3784                 :            :         ret = 0;
    3785                 :            : 
    3786                 :          0 :         nexthdr = ip6->nexthdr;
    3787                 :          0 :         offset += sizeof(_ipv6h);
    3788                 :          0 :         offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
    3789         [ #  # ]:          0 :         if (offset < 0)
    3790                 :            :                 goto out;
    3791                 :            : 
    3792         [ #  # ]:          0 :         if (proto)
    3793                 :          0 :                 *proto = nexthdr;
    3794                 :            : 
    3795   [ #  #  #  # ]:          0 :         switch (nexthdr) {
    3796                 :            :         case IPPROTO_TCP: {
    3797                 :            :                 struct tcphdr _tcph, *th;
    3798                 :            : 
    3799                 :            :                 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
    3800         [ #  # ]:          0 :                 if (th == NULL)
    3801                 :            :                         break;
    3802                 :            : 
    3803                 :          0 :                 ad->u.net->sport = th->source;
    3804                 :          0 :                 ad->u.net->dport = th->dest;
    3805                 :            :                 break;
    3806                 :            :         }
    3807                 :            : 
    3808                 :            :         case IPPROTO_UDP: {
    3809                 :            :                 struct udphdr _udph, *uh;
    3810                 :            : 
    3811                 :            :                 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
    3812         [ #  # ]:          0 :                 if (uh == NULL)
    3813                 :            :                         break;
    3814                 :            : 
    3815                 :          0 :                 ad->u.net->sport = uh->source;
    3816                 :          0 :                 ad->u.net->dport = uh->dest;
    3817                 :            :                 break;
    3818                 :            :         }
    3819                 :            : 
    3820                 :            :         case IPPROTO_DCCP: {
    3821                 :            :                 struct dccp_hdr _dccph, *dh;
    3822                 :            : 
    3823                 :            :                 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
    3824         [ #  # ]:          0 :                 if (dh == NULL)
    3825                 :            :                         break;
    3826                 :            : 
    3827                 :          0 :                 ad->u.net->sport = dh->dccph_sport;
    3828                 :          0 :                 ad->u.net->dport = dh->dccph_dport;
    3829                 :            :                 break;
    3830                 :            :         }
    3831                 :            : 
    3832                 :            :         /* includes fragments */
    3833                 :            :         default:
    3834                 :            :                 break;
    3835                 :            :         }
    3836                 :            : out:
    3837                 :          0 :         return ret;
    3838                 :            : }
    3839                 :            : 
    3840                 :            : #endif /* IPV6 */
    3841                 :            : 
    3842                 :          0 : static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
    3843                 :            :                              char **_addrp, int src, u8 *proto)
    3844                 :            : {
    3845                 :            :         char *addrp;
    3846                 :            :         int ret;
    3847                 :            : 
    3848      [ #  #  # ]:          0 :         switch (ad->u.net->family) {
    3849                 :            :         case PF_INET:
    3850                 :          0 :                 ret = selinux_parse_skb_ipv4(skb, ad, proto);
    3851         [ #  # ]:          0 :                 if (ret)
    3852                 :            :                         goto parse_error;
    3853         [ #  # ]:          0 :                 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
    3854                 :          0 :                                        &ad->u.net->v4info.daddr);
    3855                 :          0 :                 goto okay;
    3856                 :            : 
    3857                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    3858                 :            :         case PF_INET6:
    3859                 :          0 :                 ret = selinux_parse_skb_ipv6(skb, ad, proto);
    3860         [ #  # ]:          0 :                 if (ret)
    3861                 :            :                         goto parse_error;
    3862         [ #  # ]:          0 :                 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
    3863                 :          0 :                                        &ad->u.net->v6info.daddr);
    3864                 :          0 :                 goto okay;
    3865                 :            : #endif  /* IPV6 */
    3866                 :            :         default:
    3867                 :            :                 addrp = NULL;
    3868                 :            :                 goto okay;
    3869                 :            :         }
    3870                 :            : 
    3871                 :            : parse_error:
    3872                 :          0 :         printk(KERN_WARNING
    3873                 :            :                "SELinux: failure in selinux_parse_skb(),"
    3874                 :            :                " unable to parse packet\n");
    3875                 :          0 :         return ret;
    3876                 :            : 
    3877                 :            : okay:
    3878         [ #  # ]:          0 :         if (_addrp)
    3879                 :          0 :                 *_addrp = addrp;
    3880                 :            :         return 0;
    3881                 :            : }
    3882                 :            : 
    3883                 :            : /**
    3884                 :            :  * selinux_skb_peerlbl_sid - Determine the peer label of a packet
    3885                 :            :  * @skb: the packet
    3886                 :            :  * @family: protocol family
    3887                 :            :  * @sid: the packet's peer label SID
    3888                 :            :  *
    3889                 :            :  * Description:
    3890                 :            :  * Check the various different forms of network peer labeling and determine
    3891                 :            :  * the peer label/SID for the packet; most of the magic actually occurs in
    3892                 :            :  * the security server function security_net_peersid_cmp().  The function
    3893                 :            :  * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
    3894                 :            :  * or -EACCES if @sid is invalid due to inconsistencies with the different
    3895                 :            :  * peer labels.
    3896                 :            :  *
    3897                 :            :  */
    3898                 :          0 : static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
    3899                 :            : {
    3900                 :            :         int err;
    3901                 :            :         u32 xfrm_sid;
    3902                 :            :         u32 nlbl_sid;
    3903                 :            :         u32 nlbl_type;
    3904                 :            : 
    3905                 :            :         err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
    3906                 :            :         if (unlikely(err))
    3907                 :            :                 return -EACCES;
    3908                 :          0 :         err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
    3909         [ #  # ]:          0 :         if (unlikely(err))
    3910                 :            :                 return -EACCES;
    3911                 :            : 
    3912                 :          0 :         err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
    3913         [ #  # ]:          0 :         if (unlikely(err)) {
    3914                 :          0 :                 printk(KERN_WARNING
    3915                 :            :                        "SELinux: failure in selinux_skb_peerlbl_sid(),"
    3916                 :            :                        " unable to determine packet's peer label\n");
    3917                 :          0 :                 return -EACCES;
    3918                 :            :         }
    3919                 :            : 
    3920                 :            :         return 0;
    3921                 :            : }
    3922                 :            : 
    3923                 :            : /**
    3924                 :            :  * selinux_conn_sid - Determine the child socket label for a connection
    3925                 :            :  * @sk_sid: the parent socket's SID
    3926                 :            :  * @skb_sid: the packet's SID
    3927                 :            :  * @conn_sid: the resulting connection SID
    3928                 :            :  *
    3929                 :            :  * If @skb_sid is valid then the user:role:type information from @sk_sid is
    3930                 :            :  * combined with the MLS information from @skb_sid in order to create
    3931                 :            :  * @conn_sid.  If @skb_sid is not valid then then @conn_sid is simply a copy
    3932                 :            :  * of @sk_sid.  Returns zero on success, negative values on failure.
    3933                 :            :  *
    3934                 :            :  */
    3935                 :            : static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
    3936                 :            : {
    3937                 :            :         int err = 0;
    3938                 :            : 
    3939 [ #  # ][ #  # ]:          0 :         if (skb_sid != SECSID_NULL)
    3940                 :          0 :                 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
    3941                 :            :         else
    3942                 :          0 :                 *conn_sid = sk_sid;
    3943                 :            : 
    3944                 :            :         return err;
    3945                 :            : }
    3946                 :            : 
    3947                 :            : /* socket security operations */
    3948                 :            : 
    3949                 :            : static int socket_sockcreate_sid(const struct task_security_struct *tsec,
    3950                 :            :                                  u16 secclass, u32 *socksid)
    3951                 :            : {
    3952 [ #  # ][ #  # ]:          0 :         if (tsec->sockcreate_sid > SECSID_NULL) {
    3953                 :          0 :                 *socksid = tsec->sockcreate_sid;
    3954                 :            :                 return 0;
    3955                 :            :         }
    3956                 :            : 
    3957                 :          0 :         return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
    3958                 :            :                                        socksid);
    3959                 :            : }
    3960                 :            : 
    3961                 :          0 : static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
    3962                 :            : {
    3963                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    3964                 :            :         struct common_audit_data ad;
    3965                 :          0 :         struct lsm_network_audit net = {0,};
    3966                 :            :         u32 tsid = task_sid(task);
    3967                 :            : 
    3968         [ #  # ]:          0 :         if (sksec->sid == SECINITSID_KERNEL)
    3969                 :            :                 return 0;
    3970                 :            : 
    3971                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    3972                 :          0 :         ad.u.net = &net;
    3973                 :          0 :         ad.u.net->sk = sk;
    3974                 :            : 
    3975                 :          0 :         return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
    3976                 :            : }
    3977                 :            : 
    3978                 :          0 : static int selinux_socket_create(int family, int type,
    3979                 :            :                                  int protocol, int kern)
    3980                 :            : {
    3981                 :          0 :         const struct task_security_struct *tsec = current_security();
    3982                 :            :         u32 newsid;
    3983                 :            :         u16 secclass;
    3984                 :            :         int rc;
    3985                 :            : 
    3986         [ #  # ]:          0 :         if (kern)
    3987                 :            :                 return 0;
    3988                 :            : 
    3989                 :            :         secclass = socket_type_to_security_class(family, type, protocol);
    3990                 :            :         rc = socket_sockcreate_sid(tsec, secclass, &newsid);
    3991         [ #  # ]:          0 :         if (rc)
    3992                 :            :                 return rc;
    3993                 :            : 
    3994                 :          0 :         return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
    3995                 :            : }
    3996                 :            : 
    3997                 :          0 : static int selinux_socket_post_create(struct socket *sock, int family,
    3998                 :            :                                       int type, int protocol, int kern)
    3999                 :            : {
    4000                 :          0 :         const struct task_security_struct *tsec = current_security();
    4001                 :          0 :         struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
    4002                 :            :         struct sk_security_struct *sksec;
    4003                 :            :         int err = 0;
    4004                 :            : 
    4005                 :          0 :         isec->sclass = socket_type_to_security_class(family, type, protocol);
    4006                 :            : 
    4007         [ #  # ]:          0 :         if (kern)
    4008                 :          0 :                 isec->sid = SECINITSID_KERNEL;
    4009                 :            :         else {
    4010                 :          0 :                 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
    4011         [ #  # ]:          0 :                 if (err)
    4012                 :            :                         return err;
    4013                 :            :         }
    4014                 :            : 
    4015                 :          0 :         isec->initialized = 1;
    4016                 :            : 
    4017         [ #  # ]:          0 :         if (sock->sk) {
    4018                 :          0 :                 sksec = sock->sk->sk_security;
    4019                 :          0 :                 sksec->sid = isec->sid;
    4020                 :          0 :                 sksec->sclass = isec->sclass;
    4021                 :          0 :                 err = selinux_netlbl_socket_post_create(sock->sk, family);
    4022                 :            :         }
    4023                 :            : 
    4024                 :          0 :         return err;
    4025                 :            : }
    4026                 :            : 
    4027                 :            : /* Range of port numbers used to automatically bind.
    4028                 :            :    Need to determine whether we should perform a name_bind
    4029                 :            :    permission check between the socket and the port number. */
    4030                 :            : 
    4031                 :          0 : static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
    4032                 :            : {
    4033                 :          0 :         struct sock *sk = sock->sk;
    4034                 :            :         u16 family;
    4035                 :            :         int err;
    4036                 :            : 
    4037                 :          0 :         err = sock_has_perm(current, sk, SOCKET__BIND);
    4038         [ #  # ]:          0 :         if (err)
    4039                 :            :                 goto out;
    4040                 :            : 
    4041                 :            :         /*
    4042                 :            :          * If PF_INET or PF_INET6, check name_bind permission for the port.
    4043                 :            :          * Multiple address binding for SCTP is not supported yet: we just
    4044                 :            :          * check the first address now.
    4045                 :            :          */
    4046                 :          0 :         family = sk->sk_family;
    4047         [ #  # ]:          0 :         if (family == PF_INET || family == PF_INET6) {
    4048                 :            :                 char *addrp;
    4049                 :          0 :                 struct sk_security_struct *sksec = sk->sk_security;
    4050                 :            :                 struct common_audit_data ad;
    4051                 :          0 :                 struct lsm_network_audit net = {0,};
    4052                 :            :                 struct sockaddr_in *addr4 = NULL;
    4053                 :            :                 struct sockaddr_in6 *addr6 = NULL;
    4054                 :            :                 unsigned short snum;
    4055                 :            :                 u32 sid, node_perm;
    4056                 :            : 
    4057         [ #  # ]:          0 :                 if (family == PF_INET) {
    4058                 :            :                         addr4 = (struct sockaddr_in *)address;
    4059         [ #  # ]:          0 :                         snum = ntohs(addr4->sin_port);
    4060                 :          0 :                         addrp = (char *)&addr4->sin_addr.s_addr;
    4061                 :            :                 } else {
    4062                 :            :                         addr6 = (struct sockaddr_in6 *)address;
    4063         [ #  # ]:          0 :                         snum = ntohs(addr6->sin6_port);
    4064                 :          0 :                         addrp = (char *)&addr6->sin6_addr.s6_addr;
    4065                 :            :                 }
    4066                 :            : 
    4067         [ #  # ]:          0 :                 if (snum) {
    4068                 :            :                         int low, high;
    4069                 :            : 
    4070                 :          0 :                         inet_get_local_port_range(sock_net(sk), &low, &high);
    4071                 :            : 
    4072 [ #  # ][ #  # ]:          0 :                         if (snum < max(PROT_SOCK, low) || snum > high) {
    4073                 :          0 :                                 err = sel_netport_sid(sk->sk_protocol,
    4074                 :            :                                                       snum, &sid);
    4075         [ #  # ]:          0 :                                 if (err)
    4076                 :            :                                         goto out;
    4077                 :          0 :                                 ad.type = LSM_AUDIT_DATA_NET;
    4078                 :          0 :                                 ad.u.net = &net;
    4079         [ #  # ]:          0 :                                 ad.u.net->sport = htons(snum);
    4080                 :          0 :                                 ad.u.net->family = family;
    4081                 :          0 :                                 err = avc_has_perm(sksec->sid, sid,
    4082                 :            :                                                    sksec->sclass,
    4083                 :            :                                                    SOCKET__NAME_BIND, &ad);
    4084         [ #  # ]:          0 :                                 if (err)
    4085                 :            :                                         goto out;
    4086                 :            :                         }
    4087                 :            :                 }
    4088                 :            : 
    4089   [ #  #  #  # ]:          0 :                 switch (sksec->sclass) {
    4090                 :            :                 case SECCLASS_TCP_SOCKET:
    4091                 :            :                         node_perm = TCP_SOCKET__NODE_BIND;
    4092                 :            :                         break;
    4093                 :            : 
    4094                 :            :                 case SECCLASS_UDP_SOCKET:
    4095                 :            :                         node_perm = UDP_SOCKET__NODE_BIND;
    4096                 :          0 :                         break;
    4097                 :            : 
    4098                 :            :                 case SECCLASS_DCCP_SOCKET:
    4099                 :            :                         node_perm = DCCP_SOCKET__NODE_BIND;
    4100                 :          0 :                         break;
    4101                 :            : 
    4102                 :            :                 default:
    4103                 :            :                         node_perm = RAWIP_SOCKET__NODE_BIND;
    4104                 :          0 :                         break;
    4105                 :            :                 }
    4106                 :            : 
    4107                 :          0 :                 err = sel_netnode_sid(addrp, family, &sid);
    4108         [ #  # ]:          0 :                 if (err)
    4109                 :            :                         goto out;
    4110                 :            : 
    4111                 :          0 :                 ad.type = LSM_AUDIT_DATA_NET;
    4112                 :          0 :                 ad.u.net = &net;
    4113         [ #  # ]:          0 :                 ad.u.net->sport = htons(snum);
    4114                 :          0 :                 ad.u.net->family = family;
    4115                 :            : 
    4116         [ #  # ]:          0 :                 if (family == PF_INET)
    4117                 :          0 :                         ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
    4118                 :            :                 else
    4119                 :          0 :                         ad.u.net->v6info.saddr = addr6->sin6_addr;
    4120                 :            : 
    4121                 :          0 :                 err = avc_has_perm(sksec->sid, sid,
    4122                 :            :                                    sksec->sclass, node_perm, &ad);
    4123         [ #  # ]:          0 :                 if (err)
    4124                 :            :                         goto out;
    4125                 :            :         }
    4126                 :            : out:
    4127                 :          0 :         return err;
    4128                 :            : }
    4129                 :            : 
    4130                 :          0 : static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
    4131                 :            : {
    4132                 :          0 :         struct sock *sk = sock->sk;
    4133                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4134                 :            :         int err;
    4135                 :            : 
    4136                 :          0 :         err = sock_has_perm(current, sk, SOCKET__CONNECT);
    4137         [ #  # ]:          0 :         if (err)
    4138                 :            :                 return err;
    4139                 :            : 
    4140                 :            :         /*
    4141                 :            :          * If a TCP or DCCP socket, check name_connect permission for the port.
    4142                 :            :          */
    4143         [ #  # ]:          0 :         if (sksec->sclass == SECCLASS_TCP_SOCKET ||
    4144                 :            :             sksec->sclass == SECCLASS_DCCP_SOCKET) {
    4145                 :            :                 struct common_audit_data ad;
    4146                 :          0 :                 struct lsm_network_audit net = {0,};
    4147                 :            :                 struct sockaddr_in *addr4 = NULL;
    4148                 :            :                 struct sockaddr_in6 *addr6 = NULL;
    4149                 :            :                 unsigned short snum;
    4150                 :            :                 u32 sid, perm;
    4151                 :            : 
    4152         [ #  # ]:          0 :                 if (sk->sk_family == PF_INET) {
    4153                 :            :                         addr4 = (struct sockaddr_in *)address;
    4154         [ #  # ]:          0 :                         if (addrlen < sizeof(struct sockaddr_in))
    4155                 :          0 :                                 return -EINVAL;
    4156         [ #  # ]:          0 :                         snum = ntohs(addr4->sin_port);
    4157                 :            :                 } else {
    4158                 :            :                         addr6 = (struct sockaddr_in6 *)address;
    4159         [ #  # ]:          0 :                         if (addrlen < SIN6_LEN_RFC2133)
    4160                 :            :                                 return -EINVAL;
    4161         [ #  # ]:          0 :                         snum = ntohs(addr6->sin6_port);
    4162                 :            :                 }
    4163                 :            : 
    4164                 :          0 :                 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
    4165         [ #  # ]:          0 :                 if (err)
    4166                 :            :                         goto out;
    4167                 :            : 
    4168         [ #  # ]:          0 :                 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
    4169                 :            :                        TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
    4170                 :            : 
    4171                 :          0 :                 ad.type = LSM_AUDIT_DATA_NET;
    4172                 :          0 :                 ad.u.net = &net;
    4173         [ #  # ]:          0 :                 ad.u.net->dport = htons(snum);
    4174                 :          0 :                 ad.u.net->family = sk->sk_family;
    4175                 :          0 :                 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
    4176         [ #  # ]:          0 :                 if (err)
    4177                 :            :                         goto out;
    4178                 :            :         }
    4179                 :            : 
    4180                 :          0 :         err = selinux_netlbl_socket_connect(sk, address);
    4181                 :            : 
    4182                 :            : out:
    4183                 :          0 :         return err;
    4184                 :            : }
    4185                 :            : 
    4186                 :          0 : static int selinux_socket_listen(struct socket *sock, int backlog)
    4187                 :            : {
    4188                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
    4189                 :            : }
    4190                 :            : 
    4191                 :          0 : static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
    4192                 :            : {
    4193                 :            :         int err;
    4194                 :            :         struct inode_security_struct *isec;
    4195                 :            :         struct inode_security_struct *newisec;
    4196                 :            : 
    4197                 :          0 :         err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
    4198         [ #  # ]:          0 :         if (err)
    4199                 :            :                 return err;
    4200                 :            : 
    4201                 :          0 :         newisec = SOCK_INODE(newsock)->i_security;
    4202                 :            : 
    4203                 :          0 :         isec = SOCK_INODE(sock)->i_security;
    4204                 :          0 :         newisec->sclass = isec->sclass;
    4205                 :          0 :         newisec->sid = isec->sid;
    4206                 :          0 :         newisec->initialized = 1;
    4207                 :            : 
    4208                 :          0 :         return 0;
    4209                 :            : }
    4210                 :            : 
    4211                 :          0 : static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
    4212                 :            :                                   int size)
    4213                 :            : {
    4214                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__WRITE);
    4215                 :            : }
    4216                 :            : 
    4217                 :          0 : static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
    4218                 :            :                                   int size, int flags)
    4219                 :            : {
    4220                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__READ);
    4221                 :            : }
    4222                 :            : 
    4223                 :          0 : static int selinux_socket_getsockname(struct socket *sock)
    4224                 :            : {
    4225                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
    4226                 :            : }
    4227                 :            : 
    4228                 :          0 : static int selinux_socket_getpeername(struct socket *sock)
    4229                 :            : {
    4230                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
    4231                 :            : }
    4232                 :            : 
    4233                 :          0 : static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
    4234                 :            : {
    4235                 :            :         int err;
    4236                 :            : 
    4237                 :          0 :         err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
    4238         [ #  # ]:          0 :         if (err)
    4239                 :            :                 return err;
    4240                 :            : 
    4241                 :          0 :         return selinux_netlbl_socket_setsockopt(sock, level, optname);
    4242                 :            : }
    4243                 :            : 
    4244                 :          0 : static int selinux_socket_getsockopt(struct socket *sock, int level,
    4245                 :            :                                      int optname)
    4246                 :            : {
    4247                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
    4248                 :            : }
    4249                 :            : 
    4250                 :          0 : static int selinux_socket_shutdown(struct socket *sock, int how)
    4251                 :            : {
    4252                 :          0 :         return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
    4253                 :            : }
    4254                 :            : 
    4255                 :          0 : static int selinux_socket_unix_stream_connect(struct sock *sock,
    4256                 :            :                                               struct sock *other,
    4257                 :            :                                               struct sock *newsk)
    4258                 :            : {
    4259                 :          0 :         struct sk_security_struct *sksec_sock = sock->sk_security;
    4260                 :          0 :         struct sk_security_struct *sksec_other = other->sk_security;
    4261                 :          0 :         struct sk_security_struct *sksec_new = newsk->sk_security;
    4262                 :            :         struct common_audit_data ad;
    4263                 :          0 :         struct lsm_network_audit net = {0,};
    4264                 :            :         int err;
    4265                 :            : 
    4266                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    4267                 :          0 :         ad.u.net = &net;
    4268                 :          0 :         ad.u.net->sk = other;
    4269                 :            : 
    4270                 :          0 :         err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
    4271                 :            :                            sksec_other->sclass,
    4272                 :            :                            UNIX_STREAM_SOCKET__CONNECTTO, &ad);
    4273         [ #  # ]:          0 :         if (err)
    4274                 :            :                 return err;
    4275                 :            : 
    4276                 :            :         /* server child socket */
    4277                 :          0 :         sksec_new->peer_sid = sksec_sock->sid;
    4278                 :          0 :         err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
    4279                 :            :                                     &sksec_new->sid);
    4280         [ #  # ]:          0 :         if (err)
    4281                 :            :                 return err;
    4282                 :            : 
    4283                 :            :         /* connecting socket */
    4284                 :          0 :         sksec_sock->peer_sid = sksec_new->sid;
    4285                 :            : 
    4286                 :          0 :         return 0;
    4287                 :            : }
    4288                 :            : 
    4289                 :          0 : static int selinux_socket_unix_may_send(struct socket *sock,
    4290                 :            :                                         struct socket *other)
    4291                 :            : {
    4292                 :          0 :         struct sk_security_struct *ssec = sock->sk->sk_security;
    4293                 :          0 :         struct sk_security_struct *osec = other->sk->sk_security;
    4294                 :            :         struct common_audit_data ad;
    4295                 :          0 :         struct lsm_network_audit net = {0,};
    4296                 :            : 
    4297                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    4298                 :          0 :         ad.u.net = &net;
    4299                 :          0 :         ad.u.net->sk = other->sk;
    4300                 :            : 
    4301                 :          0 :         return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
    4302                 :            :                             &ad);
    4303                 :            : }
    4304                 :            : 
    4305                 :          0 : static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
    4306                 :            :                                     u32 peer_sid,
    4307                 :            :                                     struct common_audit_data *ad)
    4308                 :            : {
    4309                 :            :         int err;
    4310                 :            :         u32 if_sid;
    4311                 :            :         u32 node_sid;
    4312                 :            : 
    4313                 :          0 :         err = sel_netif_sid(ifindex, &if_sid);
    4314         [ #  # ]:          0 :         if (err)
    4315                 :            :                 return err;
    4316                 :          0 :         err = avc_has_perm(peer_sid, if_sid,
    4317                 :            :                            SECCLASS_NETIF, NETIF__INGRESS, ad);
    4318         [ #  # ]:          0 :         if (err)
    4319                 :            :                 return err;
    4320                 :            : 
    4321                 :          0 :         err = sel_netnode_sid(addrp, family, &node_sid);
    4322         [ #  # ]:          0 :         if (err)
    4323                 :            :                 return err;
    4324                 :          0 :         return avc_has_perm(peer_sid, node_sid,
    4325                 :            :                             SECCLASS_NODE, NODE__RECVFROM, ad);
    4326                 :            : }
    4327                 :            : 
    4328                 :          0 : static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
    4329                 :            :                                        u16 family)
    4330                 :            : {
    4331                 :            :         int err = 0;
    4332                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4333                 :          0 :         u32 sk_sid = sksec->sid;
    4334                 :            :         struct common_audit_data ad;
    4335                 :          0 :         struct lsm_network_audit net = {0,};
    4336                 :            :         char *addrp;
    4337                 :            : 
    4338                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    4339                 :          0 :         ad.u.net = &net;
    4340                 :          0 :         ad.u.net->netif = skb->skb_iif;
    4341                 :          0 :         ad.u.net->family = family;
    4342                 :          0 :         err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
    4343         [ #  # ]:          0 :         if (err)
    4344                 :            :                 return err;
    4345                 :            : 
    4346         [ #  # ]:          0 :         if (selinux_secmark_enabled()) {
    4347                 :          0 :                 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
    4348                 :            :                                    PACKET__RECV, &ad);
    4349         [ #  # ]:          0 :                 if (err)
    4350                 :            :                         return err;
    4351                 :            :         }
    4352                 :            : 
    4353                 :          0 :         err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
    4354         [ #  # ]:          0 :         if (err)
    4355                 :          0 :                 return err;
    4356                 :            :         err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
    4357                 :            : 
    4358                 :            :         return err;
    4359                 :            : }
    4360                 :            : 
    4361                 :          0 : static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
    4362                 :            : {
    4363                 :            :         int err;
    4364                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4365                 :          0 :         u16 family = sk->sk_family;
    4366                 :          0 :         u32 sk_sid = sksec->sid;
    4367                 :            :         struct common_audit_data ad;
    4368                 :          0 :         struct lsm_network_audit net = {0,};
    4369                 :            :         char *addrp;
    4370                 :            :         u8 secmark_active;
    4371                 :            :         u8 peerlbl_active;
    4372                 :            : 
    4373         [ #  # ]:          0 :         if (family != PF_INET && family != PF_INET6)
    4374                 :            :                 return 0;
    4375                 :            : 
    4376                 :            :         /* Handle mapped IPv4 packets arriving via IPv6 sockets */
    4377 [ #  # ][ #  # ]:          0 :         if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
    4378                 :            :                 family = PF_INET;
    4379                 :            : 
    4380                 :            :         /* If any sort of compatibility mode is enabled then handoff processing
    4381                 :            :          * to the selinux_sock_rcv_skb_compat() function to deal with the
    4382                 :            :          * special handling.  We do this in an attempt to keep this function
    4383                 :            :          * as fast and as clean as possible. */
    4384         [ #  # ]:          0 :         if (!selinux_policycap_netpeer)
    4385                 :          0 :                 return selinux_sock_rcv_skb_compat(sk, skb, family);
    4386                 :            : 
    4387                 :          0 :         secmark_active = selinux_secmark_enabled();
    4388                 :          0 :         peerlbl_active = selinux_peerlbl_enabled();
    4389         [ #  # ]:          0 :         if (!secmark_active && !peerlbl_active)
    4390                 :            :                 return 0;
    4391                 :            : 
    4392                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    4393                 :          0 :         ad.u.net = &net;
    4394                 :          0 :         ad.u.net->netif = skb->skb_iif;
    4395                 :          0 :         ad.u.net->family = family;
    4396                 :          0 :         err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
    4397         [ #  # ]:          0 :         if (err)
    4398                 :            :                 return err;
    4399                 :            : 
    4400         [ #  # ]:          0 :         if (peerlbl_active) {
    4401                 :            :                 u32 peer_sid;
    4402                 :            : 
    4403                 :          0 :                 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
    4404         [ #  # ]:          0 :                 if (err)
    4405                 :          0 :                         return err;
    4406                 :          0 :                 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
    4407                 :            :                                                peer_sid, &ad);
    4408         [ #  # ]:          0 :                 if (err) {
    4409                 :          0 :                         selinux_netlbl_err(skb, err, 0);
    4410                 :          0 :                         return err;
    4411                 :            :                 }
    4412                 :          0 :                 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
    4413                 :            :                                    PEER__RECV, &ad);
    4414         [ #  # ]:          0 :                 if (err) {
    4415                 :          0 :                         selinux_netlbl_err(skb, err, 0);
    4416                 :          0 :                         return err;
    4417                 :            :                 }
    4418                 :            :         }
    4419                 :            : 
    4420         [ #  # ]:          0 :         if (secmark_active) {
    4421                 :          0 :                 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
    4422                 :            :                                    PACKET__RECV, &ad);
    4423         [ #  # ]:          0 :                 if (err)
    4424                 :            :                         return err;
    4425                 :            :         }
    4426                 :            : 
    4427                 :          0 :         return err;
    4428                 :            : }
    4429                 :            : 
    4430                 :          0 : static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
    4431                 :            :                                             int __user *optlen, unsigned len)
    4432                 :            : {
    4433                 :            :         int err = 0;
    4434                 :            :         char *scontext;
    4435                 :            :         u32 scontext_len;
    4436                 :          0 :         struct sk_security_struct *sksec = sock->sk->sk_security;
    4437                 :            :         u32 peer_sid = SECSID_NULL;
    4438                 :            : 
    4439         [ #  # ]:          0 :         if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
    4440                 :            :             sksec->sclass == SECCLASS_TCP_SOCKET)
    4441                 :          0 :                 peer_sid = sksec->peer_sid;
    4442         [ #  # ]:          0 :         if (peer_sid == SECSID_NULL)
    4443                 :            :                 return -ENOPROTOOPT;
    4444                 :            : 
    4445                 :          0 :         err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
    4446         [ #  # ]:          0 :         if (err)
    4447                 :            :                 return err;
    4448                 :            : 
    4449         [ #  # ]:          0 :         if (scontext_len > len) {
    4450                 :            :                 err = -ERANGE;
    4451                 :            :                 goto out_len;
    4452                 :            :         }
    4453                 :            : 
    4454         [ #  # ]:          0 :         if (copy_to_user(optval, scontext, scontext_len))
    4455                 :            :                 err = -EFAULT;
    4456                 :            : 
    4457                 :            : out_len:
    4458         [ #  # ]:          0 :         if (put_user(scontext_len, optlen))
    4459                 :            :                 err = -EFAULT;
    4460                 :          0 :         kfree(scontext);
    4461                 :          0 :         return err;
    4462                 :            : }
    4463                 :            : 
    4464                 :          0 : static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
    4465                 :            : {
    4466                 :          0 :         u32 peer_secid = SECSID_NULL;
    4467                 :            :         u16 family;
    4468                 :            : 
    4469 [ #  # ][ #  # ]:          0 :         if (skb && skb->protocol == htons(ETH_P_IP))
    4470                 :            :                 family = PF_INET;
    4471 [ #  # ][ #  # ]:          0 :         else if (skb && skb->protocol == htons(ETH_P_IPV6))
    4472                 :            :                 family = PF_INET6;
    4473         [ #  # ]:          0 :         else if (sock)
    4474                 :          0 :                 family = sock->sk->sk_family;
    4475                 :            :         else
    4476                 :            :                 goto out;
    4477                 :            : 
    4478         [ #  # ]:          0 :         if (sock && family == PF_UNIX)
    4479                 :            :                 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
    4480         [ #  # ]:          0 :         else if (skb)
    4481                 :          0 :                 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
    4482                 :            : 
    4483                 :            : out:
    4484                 :          0 :         *secid = peer_secid;
    4485         [ #  # ]:          0 :         if (peer_secid == SECSID_NULL)
    4486                 :            :                 return -EINVAL;
    4487                 :          0 :         return 0;
    4488                 :            : }
    4489                 :            : 
    4490                 :          0 : static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
    4491                 :            : {
    4492                 :            :         struct sk_security_struct *sksec;
    4493                 :            : 
    4494                 :            :         sksec = kzalloc(sizeof(*sksec), priority);
    4495         [ #  # ]:          0 :         if (!sksec)
    4496                 :            :                 return -ENOMEM;
    4497                 :            : 
    4498                 :          0 :         sksec->peer_sid = SECINITSID_UNLABELED;
    4499                 :          0 :         sksec->sid = SECINITSID_UNLABELED;
    4500                 :          0 :         selinux_netlbl_sk_security_reset(sksec);
    4501                 :          0 :         sk->sk_security = sksec;
    4502                 :            : 
    4503                 :          0 :         return 0;
    4504                 :            : }
    4505                 :            : 
    4506                 :          0 : static void selinux_sk_free_security(struct sock *sk)
    4507                 :            : {
    4508                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4509                 :            : 
    4510                 :          0 :         sk->sk_security = NULL;
    4511                 :          0 :         selinux_netlbl_sk_security_free(sksec);
    4512                 :          0 :         kfree(sksec);
    4513                 :          0 : }
    4514                 :            : 
    4515                 :          0 : static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
    4516                 :            : {
    4517                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4518                 :          0 :         struct sk_security_struct *newsksec = newsk->sk_security;
    4519                 :            : 
    4520                 :          0 :         newsksec->sid = sksec->sid;
    4521                 :          0 :         newsksec->peer_sid = sksec->peer_sid;
    4522                 :          0 :         newsksec->sclass = sksec->sclass;
    4523                 :            : 
    4524                 :          0 :         selinux_netlbl_sk_security_reset(newsksec);
    4525                 :          0 : }
    4526                 :            : 
    4527                 :          0 : static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
    4528                 :            : {
    4529         [ #  # ]:          0 :         if (!sk)
    4530                 :          0 :                 *secid = SECINITSID_ANY_SOCKET;
    4531                 :            :         else {
    4532                 :          0 :                 struct sk_security_struct *sksec = sk->sk_security;
    4533                 :            : 
    4534                 :          0 :                 *secid = sksec->sid;
    4535                 :            :         }
    4536                 :          0 : }
    4537                 :            : 
    4538                 :          0 : static void selinux_sock_graft(struct sock *sk, struct socket *parent)
    4539                 :            : {
    4540                 :          0 :         struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
    4541                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4542                 :            : 
    4543 [ #  # ][ #  # ]:          0 :         if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
    4544                 :            :             sk->sk_family == PF_UNIX)
    4545                 :          0 :                 isec->sid = sksec->sid;
    4546                 :          0 :         sksec->sclass = isec->sclass;
    4547                 :          0 : }
    4548                 :            : 
    4549                 :          0 : static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
    4550                 :            :                                      struct request_sock *req)
    4551                 :            : {
    4552                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4553                 :            :         int err;
    4554                 :          0 :         u16 family = sk->sk_family;
    4555                 :            :         u32 connsid;
    4556                 :            :         u32 peersid;
    4557                 :            : 
    4558                 :            :         /* handle mapped IPv4 packets arriving via IPv6 sockets */
    4559 [ #  # ][ #  # ]:          0 :         if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
    4560                 :            :                 family = PF_INET;
    4561                 :            : 
    4562                 :          0 :         err = selinux_skb_peerlbl_sid(skb, family, &peersid);
    4563         [ #  # ]:          0 :         if (err)
    4564                 :            :                 return err;
    4565                 :          0 :         err = selinux_conn_sid(sksec->sid, peersid, &connsid);
    4566         [ #  # ]:          0 :         if (err)
    4567                 :            :                 return err;
    4568                 :          0 :         req->secid = connsid;
    4569                 :          0 :         req->peer_secid = peersid;
    4570                 :            : 
    4571                 :          0 :         return selinux_netlbl_inet_conn_request(req, family);
    4572                 :            : }
    4573                 :            : 
    4574                 :          0 : static void selinux_inet_csk_clone(struct sock *newsk,
    4575                 :            :                                    const struct request_sock *req)
    4576                 :            : {
    4577                 :          0 :         struct sk_security_struct *newsksec = newsk->sk_security;
    4578                 :            : 
    4579                 :          0 :         newsksec->sid = req->secid;
    4580                 :          0 :         newsksec->peer_sid = req->peer_secid;
    4581                 :            :         /* NOTE: Ideally, we should also get the isec->sid for the
    4582                 :            :            new socket in sync, but we don't have the isec available yet.
    4583                 :            :            So we will wait until sock_graft to do it, by which
    4584                 :            :            time it will have been created and available. */
    4585                 :            : 
    4586                 :            :         /* We don't need to take any sort of lock here as we are the only
    4587                 :            :          * thread with access to newsksec */
    4588                 :          0 :         selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
    4589                 :          0 : }
    4590                 :            : 
    4591                 :          0 : static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
    4592                 :            : {
    4593                 :          0 :         u16 family = sk->sk_family;
    4594                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4595                 :            : 
    4596                 :            :         /* handle mapped IPv4 packets arriving via IPv6 sockets */
    4597 [ #  # ][ #  # ]:          0 :         if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
    4598                 :            :                 family = PF_INET;
    4599                 :            : 
    4600                 :          0 :         selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
    4601                 :          0 : }
    4602                 :            : 
    4603                 :          0 : static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
    4604                 :            : {
    4605                 :            :         skb_set_owner_w(skb, sk);
    4606                 :          0 : }
    4607                 :            : 
    4608                 :          0 : static int selinux_secmark_relabel_packet(u32 sid)
    4609                 :            : {
    4610                 :            :         const struct task_security_struct *__tsec;
    4611                 :            :         u32 tsid;
    4612                 :            : 
    4613                 :          0 :         __tsec = current_security();
    4614                 :          0 :         tsid = __tsec->sid;
    4615                 :            : 
    4616                 :          0 :         return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
    4617                 :            : }
    4618                 :            : 
    4619                 :          0 : static void selinux_secmark_refcount_inc(void)
    4620                 :            : {
    4621                 :            :         atomic_inc(&selinux_secmark_refcount);
    4622                 :          0 : }
    4623                 :            : 
    4624                 :          0 : static void selinux_secmark_refcount_dec(void)
    4625                 :            : {
    4626                 :            :         atomic_dec(&selinux_secmark_refcount);
    4627                 :          0 : }
    4628                 :            : 
    4629                 :          0 : static void selinux_req_classify_flow(const struct request_sock *req,
    4630                 :            :                                       struct flowi *fl)
    4631                 :            : {
    4632                 :          0 :         fl->flowi_secid = req->secid;
    4633                 :          0 : }
    4634                 :            : 
    4635                 :          0 : static int selinux_tun_dev_alloc_security(void **security)
    4636                 :            : {
    4637                 :            :         struct tun_security_struct *tunsec;
    4638                 :            : 
    4639                 :            :         tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
    4640         [ #  # ]:          0 :         if (!tunsec)
    4641                 :            :                 return -ENOMEM;
    4642                 :          0 :         tunsec->sid = current_sid();
    4643                 :            : 
    4644                 :          0 :         *security = tunsec;
    4645                 :          0 :         return 0;
    4646                 :            : }
    4647                 :            : 
    4648                 :          0 : static void selinux_tun_dev_free_security(void *security)
    4649                 :            : {
    4650                 :          0 :         kfree(security);
    4651                 :          0 : }
    4652                 :            : 
    4653                 :          0 : static int selinux_tun_dev_create(void)
    4654                 :            : {
    4655                 :            :         u32 sid = current_sid();
    4656                 :            : 
    4657                 :            :         /* we aren't taking into account the "sockcreate" SID since the socket
    4658                 :            :          * that is being created here is not a socket in the traditional sense,
    4659                 :            :          * instead it is a private sock, accessible only to the kernel, and
    4660                 :            :          * representing a wide range of network traffic spanning multiple
    4661                 :            :          * connections unlike traditional sockets - check the TUN driver to
    4662                 :            :          * get a better understanding of why this socket is special */
    4663                 :            : 
    4664                 :          0 :         return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
    4665                 :            :                             NULL);
    4666                 :            : }
    4667                 :            : 
    4668                 :          0 : static int selinux_tun_dev_attach_queue(void *security)
    4669                 :            : {
    4670                 :            :         struct tun_security_struct *tunsec = security;
    4671                 :            : 
    4672                 :          0 :         return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
    4673                 :            :                             TUN_SOCKET__ATTACH_QUEUE, NULL);
    4674                 :            : }
    4675                 :            : 
    4676                 :          0 : static int selinux_tun_dev_attach(struct sock *sk, void *security)
    4677                 :            : {
    4678                 :            :         struct tun_security_struct *tunsec = security;
    4679                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4680                 :            : 
    4681                 :            :         /* we don't currently perform any NetLabel based labeling here and it
    4682                 :            :          * isn't clear that we would want to do so anyway; while we could apply
    4683                 :            :          * labeling without the support of the TUN user the resulting labeled
    4684                 :            :          * traffic from the other end of the connection would almost certainly
    4685                 :            :          * cause confusion to the TUN user that had no idea network labeling
    4686                 :            :          * protocols were being used */
    4687                 :            : 
    4688                 :          0 :         sksec->sid = tunsec->sid;
    4689                 :          0 :         sksec->sclass = SECCLASS_TUN_SOCKET;
    4690                 :            : 
    4691                 :          0 :         return 0;
    4692                 :            : }
    4693                 :            : 
    4694                 :          0 : static int selinux_tun_dev_open(void *security)
    4695                 :            : {
    4696                 :            :         struct tun_security_struct *tunsec = security;
    4697                 :            :         u32 sid = current_sid();
    4698                 :            :         int err;
    4699                 :            : 
    4700                 :          0 :         err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
    4701                 :            :                            TUN_SOCKET__RELABELFROM, NULL);
    4702         [ #  # ]:          0 :         if (err)
    4703                 :            :                 return err;
    4704                 :          0 :         err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
    4705                 :            :                            TUN_SOCKET__RELABELTO, NULL);
    4706         [ #  # ]:          0 :         if (err)
    4707                 :            :                 return err;
    4708                 :          0 :         tunsec->sid = sid;
    4709                 :            : 
    4710                 :          0 :         return 0;
    4711                 :            : }
    4712                 :            : 
    4713                 :          0 : static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
    4714                 :            : {
    4715                 :            :         int err = 0;
    4716                 :            :         u32 perm;
    4717                 :            :         struct nlmsghdr *nlh;
    4718                 :          0 :         struct sk_security_struct *sksec = sk->sk_security;
    4719                 :            : 
    4720         [ #  # ]:          0 :         if (skb->len < NLMSG_HDRLEN) {
    4721                 :            :                 err = -EINVAL;
    4722                 :            :                 goto out;
    4723                 :            :         }
    4724                 :            :         nlh = nlmsg_hdr(skb);
    4725                 :            : 
    4726                 :          0 :         err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
    4727         [ #  # ]:          0 :         if (err) {
    4728         [ #  # ]:          0 :                 if (err == -EINVAL) {
    4729                 :          0 :                         audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
    4730                 :            :                                   "SELinux:  unrecognized netlink message"
    4731                 :            :                                   " type=%hu for sclass=%hu\n",
    4732                 :          0 :                                   nlh->nlmsg_type, sksec->sclass);
    4733 [ #  # ][ #  # ]:          0 :                         if (!selinux_enforcing || security_get_allow_unknown())
    4734                 :            :                                 err = 0;
    4735                 :            :                 }
    4736                 :            : 
    4737                 :            :                 /* Ignore */
    4738         [ #  # ]:          0 :                 if (err == -ENOENT)
    4739                 :            :                         err = 0;
    4740                 :            :                 goto out;
    4741                 :            :         }
    4742                 :            : 
    4743                 :          0 :         err = sock_has_perm(current, sk, perm);
    4744                 :            : out:
    4745                 :          0 :         return err;
    4746                 :            : }
    4747                 :            : 
    4748                 :            : #ifdef CONFIG_NETFILTER
    4749                 :            : 
    4750                 :          0 : static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
    4751                 :            :                                        u16 family)
    4752                 :            : {
    4753                 :            :         int err;
    4754                 :            :         char *addrp;
    4755                 :            :         u32 peer_sid;
    4756                 :            :         struct common_audit_data ad;
    4757                 :          0 :         struct lsm_network_audit net = {0,};
    4758                 :            :         u8 secmark_active;
    4759                 :            :         u8 netlbl_active;
    4760                 :            :         u8 peerlbl_active;
    4761                 :            : 
    4762         [ #  # ]:          0 :         if (!selinux_policycap_netpeer)
    4763                 :            :                 return NF_ACCEPT;
    4764                 :            : 
    4765                 :          0 :         secmark_active = selinux_secmark_enabled();
    4766                 :          0 :         netlbl_active = netlbl_enabled();
    4767                 :          0 :         peerlbl_active = selinux_peerlbl_enabled();
    4768         [ #  # ]:          0 :         if (!secmark_active && !peerlbl_active)
    4769                 :            :                 return NF_ACCEPT;
    4770                 :            : 
    4771         [ #  # ]:          0 :         if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
    4772                 :            :                 return NF_DROP;
    4773                 :            : 
    4774                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    4775                 :          0 :         ad.u.net = &net;
    4776                 :          0 :         ad.u.net->netif = ifindex;
    4777                 :          0 :         ad.u.net->family = family;
    4778         [ #  # ]:          0 :         if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
    4779                 :            :                 return NF_DROP;
    4780                 :            : 
    4781         [ #  # ]:          0 :         if (peerlbl_active) {
    4782                 :          0 :                 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
    4783                 :            :                                                peer_sid, &ad);
    4784         [ #  # ]:          0 :                 if (err) {
    4785                 :          0 :                         selinux_netlbl_err(skb, err, 1);
    4786                 :          0 :                         return NF_DROP;
    4787                 :            :                 }
    4788                 :            :         }
    4789                 :            : 
    4790         [ #  # ]:          0 :         if (secmark_active)
    4791         [ #  # ]:          0 :                 if (avc_has_perm(peer_sid, skb->secmark,
    4792                 :            :                                  SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
    4793                 :            :                         return NF_DROP;
    4794                 :            : 
    4795         [ #  # ]:          0 :         if (netlbl_active)
    4796                 :            :                 /* we do this in the FORWARD path and not the POST_ROUTING
    4797                 :            :                  * path because we want to make sure we apply the necessary
    4798                 :            :                  * labeling before IPsec is applied so we can leverage AH
    4799                 :            :                  * protection */
    4800         [ #  # ]:          0 :                 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
    4801                 :            :                         return NF_DROP;
    4802                 :            : 
    4803                 :            :         return NF_ACCEPT;
    4804                 :            : }
    4805                 :            : 
    4806                 :          0 : static unsigned int selinux_ipv4_forward(const struct nf_hook_ops *ops,
    4807                 :            :                                          struct sk_buff *skb,
    4808                 :            :                                          const struct net_device *in,
    4809                 :            :                                          const struct net_device *out,
    4810                 :            :                                          int (*okfn)(struct sk_buff *))
    4811                 :            : {
    4812                 :          0 :         return selinux_ip_forward(skb, in->ifindex, PF_INET);
    4813                 :            : }
    4814                 :            : 
    4815                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    4816                 :          0 : static unsigned int selinux_ipv6_forward(const struct nf_hook_ops *ops,
    4817                 :            :                                          struct sk_buff *skb,
    4818                 :            :                                          const struct net_device *in,
    4819                 :            :                                          const struct net_device *out,
    4820                 :            :                                          int (*okfn)(struct sk_buff *))
    4821                 :            : {
    4822                 :          0 :         return selinux_ip_forward(skb, in->ifindex, PF_INET6);
    4823                 :            : }
    4824                 :            : #endif  /* IPV6 */
    4825                 :            : 
    4826                 :          0 : static unsigned int selinux_ip_output(struct sk_buff *skb,
    4827                 :            :                                       u16 family)
    4828                 :            : {
    4829                 :            :         struct sock *sk;
    4830                 :            :         u32 sid;
    4831                 :            : 
    4832         [ #  # ]:          0 :         if (!netlbl_enabled())
    4833                 :            :                 return NF_ACCEPT;
    4834                 :            : 
    4835                 :            :         /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
    4836                 :            :          * because we want to make sure we apply the necessary labeling
    4837                 :            :          * before IPsec is applied so we can leverage AH protection */
    4838                 :          0 :         sk = skb->sk;
    4839         [ #  # ]:          0 :         if (sk) {
    4840                 :            :                 struct sk_security_struct *sksec;
    4841                 :            : 
    4842         [ #  # ]:          0 :                 if (sk->sk_state == TCP_LISTEN)
    4843                 :            :                         /* if the socket is the listening state then this
    4844                 :            :                          * packet is a SYN-ACK packet which means it needs to
    4845                 :            :                          * be labeled based on the connection/request_sock and
    4846                 :            :                          * not the parent socket.  unfortunately, we can't
    4847                 :            :                          * lookup the request_sock yet as it isn't queued on
    4848                 :            :                          * the parent socket until after the SYN-ACK is sent.
    4849                 :            :                          * the "solution" is to simply pass the packet as-is
    4850                 :            :                          * as any IP option based labeling should be copied
    4851                 :            :                          * from the initial connection request (in the IP
    4852                 :            :                          * layer).  it is far from ideal, but until we get a
    4853                 :            :                          * security label in the packet itself this is the
    4854                 :            :                          * best we can do. */
    4855                 :            :                         return NF_ACCEPT;
    4856                 :            : 
    4857                 :            :                 /* standard practice, label using the parent socket */
    4858                 :          0 :                 sksec = sk->sk_security;
    4859                 :          0 :                 sid = sksec->sid;
    4860                 :            :         } else
    4861                 :            :                 sid = SECINITSID_KERNEL;
    4862         [ #  # ]:          0 :         if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
    4863                 :            :                 return NF_DROP;
    4864                 :            : 
    4865                 :          0 :         return NF_ACCEPT;
    4866                 :            : }
    4867                 :            : 
    4868                 :          0 : static unsigned int selinux_ipv4_output(const struct nf_hook_ops *ops,
    4869                 :            :                                         struct sk_buff *skb,
    4870                 :            :                                         const struct net_device *in,
    4871                 :            :                                         const struct net_device *out,
    4872                 :            :                                         int (*okfn)(struct sk_buff *))
    4873                 :            : {
    4874                 :          0 :         return selinux_ip_output(skb, PF_INET);
    4875                 :            : }
    4876                 :            : 
    4877                 :          0 : static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
    4878                 :            :                                                 int ifindex,
    4879                 :            :                                                 u16 family)
    4880                 :            : {
    4881                 :          0 :         struct sock *sk = skb->sk;
    4882                 :            :         struct sk_security_struct *sksec;
    4883                 :            :         struct common_audit_data ad;
    4884                 :          0 :         struct lsm_network_audit net = {0,};
    4885                 :            :         char *addrp;
    4886                 :            :         u8 proto;
    4887                 :            : 
    4888         [ #  # ]:          0 :         if (sk == NULL)
    4889                 :            :                 return NF_ACCEPT;
    4890                 :          0 :         sksec = sk->sk_security;
    4891                 :            : 
    4892                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    4893                 :          0 :         ad.u.net = &net;
    4894                 :          0 :         ad.u.net->netif = ifindex;
    4895                 :          0 :         ad.u.net->family = family;
    4896         [ #  # ]:          0 :         if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
    4897                 :            :                 return NF_DROP;
    4898                 :            : 
    4899         [ #  # ]:          0 :         if (selinux_secmark_enabled())
    4900         [ #  # ]:          0 :                 if (avc_has_perm(sksec->sid, skb->secmark,
    4901                 :            :                                  SECCLASS_PACKET, PACKET__SEND, &ad))
    4902                 :            :                         return NF_DROP_ERR(-ECONNREFUSED);
    4903                 :            : 
    4904                 :            :         if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
    4905                 :            :                 return NF_DROP_ERR(-ECONNREFUSED);
    4906                 :            : 
    4907                 :            :         return NF_ACCEPT;
    4908                 :            : }
    4909                 :            : 
    4910                 :          0 : static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
    4911                 :            :                                          u16 family)
    4912                 :            : {
    4913                 :            :         u32 secmark_perm;
    4914                 :            :         u32 peer_sid;
    4915                 :            :         struct sock *sk;
    4916                 :            :         struct common_audit_data ad;
    4917                 :          0 :         struct lsm_network_audit net = {0,};
    4918                 :            :         char *addrp;
    4919                 :            :         u8 secmark_active;
    4920                 :            :         u8 peerlbl_active;
    4921                 :            : 
    4922                 :            :         /* If any sort of compatibility mode is enabled then handoff processing
    4923                 :            :          * to the selinux_ip_postroute_compat() function to deal with the
    4924                 :            :          * special handling.  We do this in an attempt to keep this function
    4925                 :            :          * as fast and as clean as possible. */
    4926         [ #  # ]:          0 :         if (!selinux_policycap_netpeer)
    4927                 :          0 :                 return selinux_ip_postroute_compat(skb, ifindex, family);
    4928                 :            : 
    4929                 :          0 :         secmark_active = selinux_secmark_enabled();
    4930                 :          0 :         peerlbl_active = selinux_peerlbl_enabled();
    4931         [ #  # ]:          0 :         if (!secmark_active && !peerlbl_active)
    4932                 :            :                 return NF_ACCEPT;
    4933                 :            : 
    4934                 :          0 :         sk = skb->sk;
    4935                 :            : 
    4936                 :            : #ifdef CONFIG_XFRM
    4937                 :            :         /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
    4938                 :            :          * packet transformation so allow the packet to pass without any checks
    4939                 :            :          * since we'll have another chance to perform access control checks
    4940                 :            :          * when the packet is on it's final way out.
    4941                 :            :          * NOTE: there appear to be some IPv6 multicast cases where skb->dst
    4942                 :            :          *       is NULL, in this case go ahead and apply access control.
    4943                 :            :          * NOTE: if this is a local socket (skb->sk != NULL) that is in the
    4944                 :            :          *       TCP listening state we cannot wait until the XFRM processing
    4945                 :            :          *       is done as we will miss out on the SA label if we do;
    4946                 :            :          *       unfortunately, this means more work, but it is only once per
    4947                 :            :          *       connection. */
    4948 [ #  # ][ #  # ]:          0 :         if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
                 [ #  # ]
    4949         [ #  # ]:          0 :             !(sk != NULL && sk->sk_state == TCP_LISTEN))
    4950                 :            :                 return NF_ACCEPT;
    4951                 :            : #endif
    4952                 :            : 
    4953         [ #  # ]:          0 :         if (sk == NULL) {
    4954                 :            :                 /* Without an associated socket the packet is either coming
    4955                 :            :                  * from the kernel or it is being forwarded; check the packet
    4956                 :            :                  * to determine which and if the packet is being forwarded
    4957                 :            :                  * query the packet directly to determine the security label. */
    4958         [ #  # ]:          0 :                 if (skb->skb_iif) {
    4959                 :            :                         secmark_perm = PACKET__FORWARD_OUT;
    4960         [ #  # ]:          0 :                         if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
    4961                 :            :                                 return NF_DROP;
    4962                 :            :                 } else {
    4963                 :            :                         secmark_perm = PACKET__SEND;
    4964                 :          0 :                         peer_sid = SECINITSID_KERNEL;
    4965                 :            :                 }
    4966         [ #  # ]:          0 :         } else if (sk->sk_state == TCP_LISTEN) {
    4967                 :            :                 /* Locally generated packet but the associated socket is in the
    4968                 :            :                  * listening state which means this is a SYN-ACK packet.  In
    4969                 :            :                  * this particular case the correct security label is assigned
    4970                 :            :                  * to the connection/request_sock but unfortunately we can't
    4971                 :            :                  * query the request_sock as it isn't queued on the parent
    4972                 :            :                  * socket until after the SYN-ACK packet is sent; the only
    4973                 :            :                  * viable choice is to regenerate the label like we do in
    4974                 :            :                  * selinux_inet_conn_request().  See also selinux_ip_output()
    4975                 :            :                  * for similar problems. */
    4976                 :            :                 u32 skb_sid;
    4977                 :          0 :                 struct sk_security_struct *sksec = sk->sk_security;
    4978         [ #  # ]:          0 :                 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
    4979                 :          0 :                         return NF_DROP;
    4980                 :            :                 /* At this point, if the returned skb peerlbl is SECSID_NULL
    4981                 :            :                  * and the packet has been through at least one XFRM
    4982                 :            :                  * transformation then we must be dealing with the "final"
    4983                 :            :                  * form of labeled IPsec packet; since we've already applied
    4984                 :            :                  * all of our access controls on this packet we can safely
    4985                 :            :                  * pass the packet. */
    4986         [ #  # ]:          0 :                 if (skb_sid == SECSID_NULL) {
    4987      [ #  #  # ]:          0 :                         switch (family) {
    4988                 :            :                         case PF_INET:
    4989         [ #  # ]:          0 :                                 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
    4990                 :            :                                         return NF_ACCEPT;
    4991                 :            :                                 break;
    4992                 :            :                         case PF_INET6:
    4993         [ #  # ]:          0 :                                 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
    4994                 :            :                                         return NF_ACCEPT;
    4995                 :            :                         default:
    4996                 :          0 :                                 return NF_DROP_ERR(-ECONNREFUSED);
    4997                 :            :                         }
    4998                 :            :                 }
    4999         [ #  # ]:          0 :                 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
    5000                 :            :                         return NF_DROP;
    5001                 :            :                 secmark_perm = PACKET__SEND;
    5002                 :            :         } else {
    5003                 :            :                 /* Locally generated packet, fetch the security label from the
    5004                 :            :                  * associated socket. */
    5005                 :          0 :                 struct sk_security_struct *sksec = sk->sk_security;
    5006                 :          0 :                 peer_sid = sksec->sid;
    5007                 :            :                 secmark_perm = PACKET__SEND;
    5008                 :            :         }
    5009                 :            : 
    5010                 :          0 :         ad.type = LSM_AUDIT_DATA_NET;
    5011                 :          0 :         ad.u.net = &net;
    5012                 :          0 :         ad.u.net->netif = ifindex;
    5013                 :          0 :         ad.u.net->family = family;
    5014         [ #  # ]:          0 :         if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
    5015                 :            :                 return NF_DROP;
    5016                 :            : 
    5017         [ #  # ]:          0 :         if (secmark_active)
    5018         [ #  # ]:          0 :                 if (avc_has_perm(peer_sid, skb->secmark,
    5019                 :            :                                  SECCLASS_PACKET, secmark_perm, &ad))
    5020                 :            :                         return NF_DROP_ERR(-ECONNREFUSED);
    5021                 :            : 
    5022         [ #  # ]:          0 :         if (peerlbl_active) {
    5023                 :            :                 u32 if_sid;
    5024                 :            :                 u32 node_sid;
    5025                 :            : 
    5026         [ #  # ]:          0 :                 if (sel_netif_sid(ifindex, &if_sid))
    5027                 :          0 :                         return NF_DROP;
    5028         [ #  # ]:          0 :                 if (avc_has_perm(peer_sid, if_sid,
    5029                 :            :                                  SECCLASS_NETIF, NETIF__EGRESS, &ad))
    5030                 :            :                         return NF_DROP_ERR(-ECONNREFUSED);
    5031                 :            : 
    5032         [ #  # ]:          0 :                 if (sel_netnode_sid(addrp, family, &node_sid))
    5033                 :            :                         return NF_DROP;
    5034         [ #  # ]:          0 :                 if (avc_has_perm(peer_sid, node_sid,
    5035                 :            :                                  SECCLASS_NODE, NODE__SENDTO, &ad))
    5036                 :            :                         return NF_DROP_ERR(-ECONNREFUSED);
    5037                 :            :         }
    5038                 :            : 
    5039                 :            :         return NF_ACCEPT;
    5040                 :            : }
    5041                 :            : 
    5042                 :          0 : static unsigned int selinux_ipv4_postroute(const struct nf_hook_ops *ops,
    5043                 :            :                                            struct sk_buff *skb,
    5044                 :            :                                            const struct net_device *in,
    5045                 :            :                                            const struct net_device *out,
    5046                 :            :                                            int (*okfn)(struct sk_buff *))
    5047                 :            : {
    5048                 :          0 :         return selinux_ip_postroute(skb, out->ifindex, PF_INET);
    5049                 :            : }
    5050                 :            : 
    5051                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    5052                 :          0 : static unsigned int selinux_ipv6_postroute(const struct nf_hook_ops *ops,
    5053                 :            :                                            struct sk_buff *skb,
    5054                 :            :                                            const struct net_device *in,
    5055                 :            :                                            const struct net_device *out,
    5056                 :            :                                            int (*okfn)(struct sk_buff *))
    5057                 :            : {
    5058                 :          0 :         return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
    5059                 :            : }
    5060                 :            : #endif  /* IPV6 */
    5061                 :            : 
    5062                 :            : #endif  /* CONFIG_NETFILTER */
    5063                 :            : 
    5064                 :          0 : static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
    5065                 :            : {
    5066                 :            :         int err;
    5067                 :            : 
    5068                 :          0 :         err = cap_netlink_send(sk, skb);
    5069         [ #  # ]:          0 :         if (err)
    5070                 :            :                 return err;
    5071                 :            : 
    5072                 :          0 :         return selinux_nlmsg_perm(sk, skb);
    5073                 :            : }
    5074                 :            : 
    5075                 :          0 : static int ipc_alloc_security(struct task_struct *task,
    5076                 :            :                               struct kern_ipc_perm *perm,
    5077                 :            :                               u16 sclass)
    5078                 :            : {
    5079                 :            :         struct ipc_security_struct *isec;
    5080                 :            :         u32 sid;
    5081                 :            : 
    5082                 :            :         isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
    5083         [ #  # ]:          0 :         if (!isec)
    5084                 :            :                 return -ENOMEM;
    5085                 :            : 
    5086                 :            :         sid = task_sid(task);
    5087                 :          0 :         isec->sclass = sclass;
    5088                 :          0 :         isec->sid = sid;
    5089                 :          0 :         perm->security = isec;
    5090                 :            : 
    5091                 :            :         return 0;
    5092                 :            : }
    5093                 :            : 
    5094                 :            : static void ipc_free_security(struct kern_ipc_perm *perm)
    5095                 :            : {
    5096                 :          0 :         struct ipc_security_struct *isec = perm->security;
    5097                 :          0 :         perm->security = NULL;
    5098                 :          0 :         kfree(isec);
    5099                 :            : }
    5100                 :            : 
    5101                 :          0 : static int msg_msg_alloc_security(struct msg_msg *msg)
    5102                 :            : {
    5103                 :            :         struct msg_security_struct *msec;
    5104                 :            : 
    5105                 :            :         msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
    5106         [ #  # ]:          0 :         if (!msec)
    5107                 :            :                 return -ENOMEM;
    5108                 :            : 
    5109                 :          0 :         msec->sid = SECINITSID_UNLABELED;
    5110                 :          0 :         msg->security = msec;
    5111                 :            : 
    5112                 :            :         return 0;
    5113                 :            : }
    5114                 :            : 
    5115                 :            : static void msg_msg_free_security(struct msg_msg *msg)
    5116                 :            : {
    5117                 :          0 :         struct msg_security_struct *msec = msg->security;
    5118                 :            : 
    5119                 :          0 :         msg->security = NULL;
    5120                 :          0 :         kfree(msec);
    5121                 :            : }
    5122                 :            : 
    5123                 :          0 : static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
    5124                 :            :                         u32 perms)
    5125                 :            : {
    5126                 :            :         struct ipc_security_struct *isec;
    5127                 :            :         struct common_audit_data ad;
    5128                 :            :         u32 sid = current_sid();
    5129                 :            : 
    5130                 :          0 :         isec = ipc_perms->security;
    5131                 :            : 
    5132                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5133                 :          0 :         ad.u.ipc_id = ipc_perms->key;
    5134                 :            : 
    5135                 :          0 :         return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
    5136                 :            : }
    5137                 :            : 
    5138                 :          0 : static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
    5139                 :            : {
    5140                 :          0 :         return msg_msg_alloc_security(msg);
    5141                 :            : }
    5142                 :            : 
    5143                 :          0 : static void selinux_msg_msg_free_security(struct msg_msg *msg)
    5144                 :            : {
    5145                 :            :         msg_msg_free_security(msg);
    5146                 :          0 : }
    5147                 :            : 
    5148                 :            : /* message queue security operations */
    5149                 :          0 : static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
    5150                 :            : {
    5151                 :            :         struct ipc_security_struct *isec;
    5152                 :            :         struct common_audit_data ad;
    5153                 :            :         u32 sid = current_sid();
    5154                 :            :         int rc;
    5155                 :            : 
    5156                 :          0 :         rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
    5157         [ #  # ]:          0 :         if (rc)
    5158                 :            :                 return rc;
    5159                 :            : 
    5160                 :          0 :         isec = msq->q_perm.security;
    5161                 :            : 
    5162                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5163                 :          0 :         ad.u.ipc_id = msq->q_perm.key;
    5164                 :            : 
    5165                 :          0 :         rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
    5166                 :            :                           MSGQ__CREATE, &ad);
    5167         [ #  # ]:          0 :         if (rc) {
    5168                 :            :                 ipc_free_security(&msq->q_perm);
    5169                 :          0 :                 return rc;
    5170                 :            :         }
    5171                 :            :         return 0;
    5172                 :            : }
    5173                 :            : 
    5174                 :          0 : static void selinux_msg_queue_free_security(struct msg_queue *msq)
    5175                 :            : {
    5176                 :            :         ipc_free_security(&msq->q_perm);
    5177                 :          0 : }
    5178                 :            : 
    5179                 :          0 : static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
    5180                 :            : {
    5181                 :            :         struct ipc_security_struct *isec;
    5182                 :            :         struct common_audit_data ad;
    5183                 :            :         u32 sid = current_sid();
    5184                 :            : 
    5185                 :          0 :         isec = msq->q_perm.security;
    5186                 :            : 
    5187                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5188                 :          0 :         ad.u.ipc_id = msq->q_perm.key;
    5189                 :            : 
    5190                 :          0 :         return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
    5191                 :            :                             MSGQ__ASSOCIATE, &ad);
    5192                 :            : }
    5193                 :            : 
    5194                 :          0 : static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
    5195                 :            : {
    5196                 :            :         int err;
    5197                 :            :         int perms;
    5198                 :            : 
    5199   [ #  #  #  #  :          0 :         switch (cmd) {
                      # ]
    5200                 :            :         case IPC_INFO:
    5201                 :            :         case MSG_INFO:
    5202                 :            :                 /* No specific object, just general system-wide information. */
    5203                 :          0 :                 return task_has_system(current, SYSTEM__IPC_INFO);
    5204                 :            :         case IPC_STAT:
    5205                 :            :         case MSG_STAT:
    5206                 :            :                 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
    5207                 :            :                 break;
    5208                 :            :         case IPC_SET:
    5209                 :            :                 perms = MSGQ__SETATTR;
    5210                 :          0 :                 break;
    5211                 :            :         case IPC_RMID:
    5212                 :            :                 perms = MSGQ__DESTROY;
    5213                 :          0 :                 break;
    5214                 :            :         default:
    5215                 :            :                 return 0;
    5216                 :            :         }
    5217                 :            : 
    5218                 :          0 :         err = ipc_has_perm(&msq->q_perm, perms);
    5219                 :          0 :         return err;
    5220                 :            : }
    5221                 :            : 
    5222                 :          0 : static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
    5223                 :            : {
    5224                 :            :         struct ipc_security_struct *isec;
    5225                 :            :         struct msg_security_struct *msec;
    5226                 :            :         struct common_audit_data ad;
    5227                 :            :         u32 sid = current_sid();
    5228                 :            :         int rc;
    5229                 :            : 
    5230                 :          0 :         isec = msq->q_perm.security;
    5231                 :          0 :         msec = msg->security;
    5232                 :            : 
    5233                 :            :         /*
    5234                 :            :          * First time through, need to assign label to the message
    5235                 :            :          */
    5236         [ #  # ]:          0 :         if (msec->sid == SECINITSID_UNLABELED) {
    5237                 :            :                 /*
    5238                 :            :                  * Compute new sid based on current process and
    5239                 :            :                  * message queue this message will be stored in
    5240                 :            :                  */
    5241                 :          0 :                 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
    5242                 :            :                                              NULL, &msec->sid);
    5243         [ #  # ]:          0 :                 if (rc)
    5244                 :            :                         return rc;
    5245                 :            :         }
    5246                 :            : 
    5247                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5248                 :          0 :         ad.u.ipc_id = msq->q_perm.key;
    5249                 :            : 
    5250                 :            :         /* Can this process write to the queue? */
    5251                 :          0 :         rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
    5252                 :            :                           MSGQ__WRITE, &ad);
    5253         [ #  # ]:          0 :         if (!rc)
    5254                 :            :                 /* Can this process send the message */
    5255                 :          0 :                 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
    5256                 :            :                                   MSG__SEND, &ad);
    5257         [ #  # ]:          0 :         if (!rc)
    5258                 :            :                 /* Can the message be put in the queue? */
    5259                 :          0 :                 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
    5260                 :            :                                   MSGQ__ENQUEUE, &ad);
    5261                 :            : 
    5262                 :          0 :         return rc;
    5263                 :            : }
    5264                 :            : 
    5265                 :          0 : static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
    5266                 :            :                                     struct task_struct *target,
    5267                 :            :                                     long type, int mode)
    5268                 :            : {
    5269                 :            :         struct ipc_security_struct *isec;
    5270                 :            :         struct msg_security_struct *msec;
    5271                 :            :         struct common_audit_data ad;
    5272                 :            :         u32 sid = task_sid(target);
    5273                 :            :         int rc;
    5274                 :            : 
    5275                 :          0 :         isec = msq->q_perm.security;
    5276                 :          0 :         msec = msg->security;
    5277                 :            : 
    5278                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5279                 :          0 :         ad.u.ipc_id = msq->q_perm.key;
    5280                 :            : 
    5281                 :          0 :         rc = avc_has_perm(sid, isec->sid,
    5282                 :            :                           SECCLASS_MSGQ, MSGQ__READ, &ad);
    5283         [ #  # ]:          0 :         if (!rc)
    5284                 :          0 :                 rc = avc_has_perm(sid, msec->sid,
    5285                 :            :                                   SECCLASS_MSG, MSG__RECEIVE, &ad);
    5286                 :          0 :         return rc;
    5287                 :            : }
    5288                 :            : 
    5289                 :            : /* Shared Memory security operations */
    5290                 :          0 : static int selinux_shm_alloc_security(struct shmid_kernel *shp)
    5291                 :            : {
    5292                 :            :         struct ipc_security_struct *isec;
    5293                 :            :         struct common_audit_data ad;
    5294                 :            :         u32 sid = current_sid();
    5295                 :            :         int rc;
    5296                 :            : 
    5297                 :          0 :         rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
    5298         [ #  # ]:          0 :         if (rc)
    5299                 :            :                 return rc;
    5300                 :            : 
    5301                 :          0 :         isec = shp->shm_perm.security;
    5302                 :            : 
    5303                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5304                 :          0 :         ad.u.ipc_id = shp->shm_perm.key;
    5305                 :            : 
    5306                 :          0 :         rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
    5307                 :            :                           SHM__CREATE, &ad);
    5308         [ #  # ]:          0 :         if (rc) {
    5309                 :            :                 ipc_free_security(&shp->shm_perm);
    5310                 :          0 :                 return rc;
    5311                 :            :         }
    5312                 :            :         return 0;
    5313                 :            : }
    5314                 :            : 
    5315                 :          0 : static void selinux_shm_free_security(struct shmid_kernel *shp)
    5316                 :            : {
    5317                 :            :         ipc_free_security(&shp->shm_perm);
    5318                 :          0 : }
    5319                 :            : 
    5320                 :          0 : static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
    5321                 :            : {
    5322                 :            :         struct ipc_security_struct *isec;
    5323                 :            :         struct common_audit_data ad;
    5324                 :            :         u32 sid = current_sid();
    5325                 :            : 
    5326                 :          0 :         isec = shp->shm_perm.security;
    5327                 :            : 
    5328                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5329                 :          0 :         ad.u.ipc_id = shp->shm_perm.key;
    5330                 :            : 
    5331                 :          0 :         return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
    5332                 :            :                             SHM__ASSOCIATE, &ad);
    5333                 :            : }
    5334                 :            : 
    5335                 :            : /* Note, at this point, shp is locked down */
    5336                 :          0 : static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
    5337                 :            : {
    5338                 :            :         int perms;
    5339                 :            :         int err;
    5340                 :            : 
    5341   [ #  #  #  #  :          0 :         switch (cmd) {
                   #  # ]
    5342                 :            :         case IPC_INFO:
    5343                 :            :         case SHM_INFO:
    5344                 :            :                 /* No specific object, just general system-wide information. */
    5345                 :          0 :                 return task_has_system(current, SYSTEM__IPC_INFO);
    5346                 :            :         case IPC_STAT:
    5347                 :            :         case SHM_STAT:
    5348                 :            :                 perms = SHM__GETATTR | SHM__ASSOCIATE;
    5349                 :            :                 break;
    5350                 :            :         case IPC_SET:
    5351                 :            :                 perms = SHM__SETATTR;
    5352                 :          0 :                 break;
    5353                 :            :         case SHM_LOCK:
    5354                 :            :         case SHM_UNLOCK:
    5355                 :            :                 perms = SHM__LOCK;
    5356                 :          0 :                 break;
    5357                 :            :         case IPC_RMID:
    5358                 :            :                 perms = SHM__DESTROY;
    5359                 :          0 :                 break;
    5360                 :            :         default:
    5361                 :            :                 return 0;
    5362                 :            :         }
    5363                 :            : 
    5364                 :          0 :         err = ipc_has_perm(&shp->shm_perm, perms);
    5365                 :          0 :         return err;
    5366                 :            : }
    5367                 :            : 
    5368                 :          0 : static int selinux_shm_shmat(struct shmid_kernel *shp,
    5369                 :            :                              char __user *shmaddr, int shmflg)
    5370                 :            : {
    5371                 :            :         u32 perms;
    5372                 :            : 
    5373         [ #  # ]:          0 :         if (shmflg & SHM_RDONLY)
    5374                 :            :                 perms = SHM__READ;
    5375                 :            :         else
    5376                 :            :                 perms = SHM__READ | SHM__WRITE;
    5377                 :            : 
    5378                 :          0 :         return ipc_has_perm(&shp->shm_perm, perms);
    5379                 :            : }
    5380                 :            : 
    5381                 :            : /* Semaphore security operations */
    5382                 :          0 : static int selinux_sem_alloc_security(struct sem_array *sma)
    5383                 :            : {
    5384                 :            :         struct ipc_security_struct *isec;
    5385                 :            :         struct common_audit_data ad;
    5386                 :            :         u32 sid = current_sid();
    5387                 :            :         int rc;
    5388                 :            : 
    5389                 :          0 :         rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
    5390         [ #  # ]:          0 :         if (rc)
    5391                 :            :                 return rc;
    5392                 :            : 
    5393                 :          0 :         isec = sma->sem_perm.security;
    5394                 :            : 
    5395                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5396                 :          0 :         ad.u.ipc_id = sma->sem_perm.key;
    5397                 :            : 
    5398                 :          0 :         rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
    5399                 :            :                           SEM__CREATE, &ad);
    5400         [ #  # ]:          0 :         if (rc) {
    5401                 :            :                 ipc_free_security(&sma->sem_perm);
    5402                 :          0 :                 return rc;
    5403                 :            :         }
    5404                 :            :         return 0;
    5405                 :            : }
    5406                 :            : 
    5407                 :          0 : static void selinux_sem_free_security(struct sem_array *sma)
    5408                 :            : {
    5409                 :            :         ipc_free_security(&sma->sem_perm);
    5410                 :          0 : }
    5411                 :            : 
    5412                 :          0 : static int selinux_sem_associate(struct sem_array *sma, int semflg)
    5413                 :            : {
    5414                 :            :         struct ipc_security_struct *isec;
    5415                 :            :         struct common_audit_data ad;
    5416                 :            :         u32 sid = current_sid();
    5417                 :            : 
    5418                 :          0 :         isec = sma->sem_perm.security;
    5419                 :            : 
    5420                 :          0 :         ad.type = LSM_AUDIT_DATA_IPC;
    5421                 :          0 :         ad.u.ipc_id = sma->sem_perm.key;
    5422                 :            : 
    5423                 :          0 :         return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
    5424                 :            :                             SEM__ASSOCIATE, &ad);
    5425                 :            : }
    5426                 :            : 
    5427                 :            : /* Note, at this point, sma is locked down */
    5428                 :          0 : static int selinux_sem_semctl(struct sem_array *sma, int cmd)
    5429                 :            : {
    5430                 :            :         int err;
    5431                 :            :         u32 perms;
    5432                 :            : 
    5433   [ #  #  #  #  :          0 :         switch (cmd) {
             #  #  #  # ]
    5434                 :            :         case IPC_INFO:
    5435                 :            :         case SEM_INFO:
    5436                 :            :                 /* No specific object, just general system-wide information. */
    5437                 :          0 :                 return task_has_system(current, SYSTEM__IPC_INFO);
    5438                 :            :         case GETPID:
    5439                 :            :         case GETNCNT:
    5440                 :            :         case GETZCNT:
    5441                 :            :                 perms = SEM__GETATTR;
    5442                 :            :                 break;
    5443                 :            :         case GETVAL:
    5444                 :            :         case GETALL:
    5445                 :            :                 perms = SEM__READ;
    5446                 :          0 :                 break;
    5447                 :            :         case SETVAL:
    5448                 :            :         case SETALL:
    5449                 :            :                 perms = SEM__WRITE;
    5450                 :          0 :                 break;
    5451                 :            :         case IPC_RMID:
    5452                 :            :                 perms = SEM__DESTROY;
    5453                 :          0 :                 break;
    5454                 :            :         case IPC_SET:
    5455                 :            :                 perms = SEM__SETATTR;
    5456                 :          0 :                 break;
    5457                 :            :         case IPC_STAT:
    5458                 :            :         case SEM_STAT:
    5459                 :            :                 perms = SEM__GETATTR | SEM__ASSOCIATE;
    5460                 :          0 :                 break;
    5461                 :            :         default:
    5462                 :            :                 return 0;
    5463                 :            :         }
    5464                 :            : 
    5465                 :          0 :         err = ipc_has_perm(&sma->sem_perm, perms);
    5466                 :          0 :         return err;
    5467                 :            : }
    5468                 :            : 
    5469                 :          0 : static int selinux_sem_semop(struct sem_array *sma,
    5470                 :            :                              struct sembuf *sops, unsigned nsops, int alter)
    5471                 :            : {
    5472                 :            :         u32 perms;
    5473                 :            : 
    5474         [ #  # ]:          0 :         if (alter)
    5475                 :            :                 perms = SEM__READ | SEM__WRITE;
    5476                 :            :         else
    5477                 :            :                 perms = SEM__READ;
    5478                 :            : 
    5479                 :          0 :         return ipc_has_perm(&sma->sem_perm, perms);
    5480                 :            : }
    5481                 :            : 
    5482                 :          0 : static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
    5483                 :            : {
    5484                 :            :         u32 av = 0;
    5485                 :            : 
    5486                 :            :         av = 0;
    5487         [ #  # ]:          0 :         if (flag & S_IRUGO)
    5488                 :            :                 av |= IPC__UNIX_READ;
    5489         [ #  # ]:          0 :         if (flag & S_IWUGO)
    5490                 :          0 :                 av |= IPC__UNIX_WRITE;
    5491                 :            : 
    5492         [ #  # ]:          0 :         if (av == 0)
    5493                 :            :                 return 0;
    5494                 :            : 
    5495                 :          0 :         return ipc_has_perm(ipcp, av);
    5496                 :            : }
    5497                 :            : 
    5498                 :          0 : static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
    5499                 :            : {
    5500                 :          0 :         struct ipc_security_struct *isec = ipcp->security;
    5501                 :          0 :         *secid = isec->sid;
    5502                 :          0 : }
    5503                 :            : 
    5504                 :          0 : static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
    5505                 :            : {
    5506         [ #  # ]:          0 :         if (inode)
    5507                 :          0 :                 inode_doinit_with_dentry(inode, dentry);
    5508                 :          0 : }
    5509                 :            : 
    5510                 :          0 : static int selinux_getprocattr(struct task_struct *p,
    5511                 :            :                                char *name, char **value)
    5512                 :            : {
    5513                 :            :         const struct task_security_struct *__tsec;
    5514                 :            :         u32 sid;
    5515                 :            :         int error;
    5516                 :            :         unsigned len;
    5517                 :            : 
    5518         [ #  # ]:          0 :         if (current != p) {
    5519                 :          0 :                 error = current_has_perm(p, PROCESS__GETATTR);
    5520         [ #  # ]:          0 :                 if (error)
    5521                 :            :                         return error;
    5522                 :            :         }
    5523                 :            : 
    5524                 :            :         rcu_read_lock();
    5525                 :          0 :         __tsec = __task_cred(p)->security;
    5526                 :            : 
    5527         [ #  # ]:          0 :         if (!strcmp(name, "current"))
    5528                 :          0 :                 sid = __tsec->sid;
    5529         [ #  # ]:          0 :         else if (!strcmp(name, "prev"))
    5530                 :          0 :                 sid = __tsec->osid;
    5531         [ #  # ]:          0 :         else if (!strcmp(name, "exec"))
    5532                 :          0 :                 sid = __tsec->exec_sid;
    5533         [ #  # ]:          0 :         else if (!strcmp(name, "fscreate"))
    5534                 :          0 :                 sid = __tsec->create_sid;
    5535         [ #  # ]:          0 :         else if (!strcmp(name, "keycreate"))
    5536                 :          0 :                 sid = __tsec->keycreate_sid;
    5537         [ #  # ]:          0 :         else if (!strcmp(name, "sockcreate"))
    5538                 :          0 :                 sid = __tsec->sockcreate_sid;
    5539                 :            :         else
    5540                 :            :                 goto invalid;
    5541                 :            :         rcu_read_unlock();
    5542                 :            : 
    5543         [ #  # ]:          0 :         if (!sid)
    5544                 :            :                 return 0;
    5545                 :            : 
    5546                 :          0 :         error = security_sid_to_context(sid, value, &len);
    5547         [ #  # ]:          0 :         if (error)
    5548                 :            :                 return error;
    5549                 :          0 :         return len;
    5550                 :            : 
    5551                 :            : invalid:
    5552                 :            :         rcu_read_unlock();
    5553                 :          0 :         return -EINVAL;
    5554                 :            : }
    5555                 :            : 
    5556                 :          0 : static int selinux_setprocattr(struct task_struct *p,
    5557                 :            :                                char *name, void *value, size_t size)
    5558                 :            : {
    5559                 :            :         struct task_security_struct *tsec;
    5560                 :            :         struct task_struct *tracer;
    5561                 :            :         struct cred *new;
    5562                 :          0 :         u32 sid = 0, ptsid;
    5563                 :            :         int error;
    5564                 :            :         char *str = value;
    5565                 :            : 
    5566         [ #  # ]:          0 :         if (current != p) {
    5567                 :            :                 /* SELinux only allows a process to change its own
    5568                 :            :                    security attributes. */
    5569                 :            :                 return -EACCES;
    5570                 :            :         }
    5571                 :            : 
    5572                 :            :         /*
    5573                 :            :          * Basic control over ability to set these attributes at all.
    5574                 :            :          * current == p, but we'll pass them separately in case the
    5575                 :            :          * above restriction is ever removed.
    5576                 :            :          */
    5577         [ #  # ]:          0 :         if (!strcmp(name, "exec"))
    5578                 :          0 :                 error = current_has_perm(p, PROCESS__SETEXEC);
    5579         [ #  # ]:          0 :         else if (!strcmp(name, "fscreate"))
    5580                 :          0 :                 error = current_has_perm(p, PROCESS__SETFSCREATE);
    5581         [ #  # ]:          0 :         else if (!strcmp(name, "keycreate"))
    5582                 :          0 :                 error = current_has_perm(p, PROCESS__SETKEYCREATE);
    5583         [ #  # ]:          0 :         else if (!strcmp(name, "sockcreate"))
    5584                 :          0 :                 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
    5585         [ #  # ]:          0 :         else if (!strcmp(name, "current"))
    5586                 :          0 :                 error = current_has_perm(p, PROCESS__SETCURRENT);
    5587                 :            :         else
    5588                 :            :                 error = -EINVAL;
    5589         [ #  # ]:          0 :         if (error)
    5590                 :            :                 return error;
    5591                 :            : 
    5592                 :            :         /* Obtain a SID for the context, if one was specified. */
    5593 [ #  # ][ #  # ]:          0 :         if (size && str[1] && str[1] != '\n') {
                 [ #  # ]
    5594         [ #  # ]:          0 :                 if (str[size-1] == '\n') {
    5595                 :          0 :                         str[size-1] = 0;
    5596                 :            :                         size--;
    5597                 :            :                 }
    5598                 :          0 :                 error = security_context_to_sid(value, size, &sid);
    5599 [ #  # ][ #  # ]:          0 :                 if (error == -EINVAL && !strcmp(name, "fscreate")) {
    5600         [ #  # ]:          0 :                         if (!capable(CAP_MAC_ADMIN)) {
    5601                 :            :                                 struct audit_buffer *ab;
    5602                 :            :                                 size_t audit_size;
    5603                 :            : 
    5604                 :            :                                 /* We strip a nul only if it is at the end, otherwise the
    5605                 :            :                                  * context contains a nul and we should audit that */
    5606         [ #  # ]:          0 :                                 if (str[size - 1] == '\0')
    5607                 :            :                                         audit_size = size - 1;
    5608                 :            :                                 else
    5609                 :            :                                         audit_size = size;
    5610                 :          0 :                                 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
    5611                 :          0 :                                 audit_log_format(ab, "op=fscreate invalid_context=");
    5612                 :          0 :                                 audit_log_n_untrustedstring(ab, value, audit_size);
    5613                 :          0 :                                 audit_log_end(ab);
    5614                 :            : 
    5615                 :          0 :                                 return error;
    5616                 :            :                         }
    5617                 :          0 :                         error = security_context_to_sid_force(value, size,
    5618                 :            :                                                               &sid);
    5619                 :            :                 }
    5620         [ #  # ]:          0 :                 if (error)
    5621                 :            :                         return error;
    5622                 :            :         }
    5623                 :            : 
    5624                 :          0 :         new = prepare_creds();
    5625         [ #  # ]:          0 :         if (!new)
    5626                 :            :                 return -ENOMEM;
    5627                 :            : 
    5628                 :            :         /* Permission checking based on the specified context is
    5629                 :            :            performed during the actual operation (execve,
    5630                 :            :            open/mkdir/...), when we know the full context of the
    5631                 :            :            operation.  See selinux_bprm_set_creds for the execve
    5632                 :            :            checks and may_create for the file creation checks. The
    5633                 :            :            operation will then fail if the context is not permitted. */
    5634                 :          0 :         tsec = new->security;
    5635         [ #  # ]:          0 :         if (!strcmp(name, "exec")) {
    5636                 :          0 :                 tsec->exec_sid = sid;
    5637         [ #  # ]:          0 :         } else if (!strcmp(name, "fscreate")) {
    5638                 :          0 :                 tsec->create_sid = sid;
    5639         [ #  # ]:          0 :         } else if (!strcmp(name, "keycreate")) {
    5640                 :          0 :                 error = may_create_key(sid, p);
    5641         [ #  # ]:          0 :                 if (error)
    5642                 :            :                         goto abort_change;
    5643                 :          0 :                 tsec->keycreate_sid = sid;
    5644         [ #  # ]:          0 :         } else if (!strcmp(name, "sockcreate")) {
    5645                 :          0 :                 tsec->sockcreate_sid = sid;
    5646         [ #  # ]:          0 :         } else if (!strcmp(name, "current")) {
    5647                 :            :                 error = -EINVAL;
    5648         [ #  # ]:          0 :                 if (sid == 0)
    5649                 :            :                         goto abort_change;
    5650                 :            : 
    5651                 :            :                 /* Only allow single threaded processes to change context */
    5652                 :            :                 error = -EPERM;
    5653         [ #  # ]:          0 :                 if (!current_is_single_threaded()) {
    5654                 :          0 :                         error = security_bounded_transition(tsec->sid, sid);
    5655         [ #  # ]:          0 :                         if (error)
    5656                 :            :                                 goto abort_change;
    5657                 :            :                 }
    5658                 :            : 
    5659                 :            :                 /* Check permissions for the transition. */
    5660                 :          0 :                 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
    5661                 :            :                                      PROCESS__DYNTRANSITION, NULL);
    5662         [ #  # ]:          0 :                 if (error)
    5663                 :            :                         goto abort_change;
    5664                 :            : 
    5665                 :            :                 /* Check for ptracing, and update the task SID if ok.
    5666                 :            :                    Otherwise, leave SID unchanged and fail. */
    5667                 :            :                 ptsid = 0;
    5668                 :            :                 rcu_read_lock();
    5669                 :            :                 tracer = ptrace_parent(p);
    5670         [ #  # ]:          0 :                 if (tracer)
    5671                 :            :                         ptsid = task_sid(tracer);
    5672                 :            :                 rcu_read_unlock();
    5673                 :            : 
    5674         [ #  # ]:          0 :                 if (tracer) {
    5675                 :          0 :                         error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
    5676                 :            :                                              PROCESS__PTRACE, NULL);
    5677         [ #  # ]:          0 :                         if (error)
    5678                 :            :                                 goto abort_change;
    5679                 :            :                 }
    5680                 :            : 
    5681                 :          0 :                 tsec->sid = sid;
    5682                 :            :         } else {
    5683                 :            :                 error = -EINVAL;
    5684                 :            :                 goto abort_change;
    5685                 :            :         }
    5686                 :            : 
    5687                 :          0 :         commit_creds(new);
    5688                 :          0 :         return size;
    5689                 :            : 
    5690                 :            : abort_change:
    5691                 :          0 :         abort_creds(new);
    5692                 :          0 :         return error;
    5693                 :            : }
    5694                 :            : 
    5695                 :          0 : static int selinux_ismaclabel(const char *name)
    5696                 :            : {
    5697                 :          0 :         return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
    5698                 :            : }
    5699                 :            : 
    5700                 :          0 : static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
    5701                 :            : {
    5702                 :          0 :         return security_sid_to_context(secid, secdata, seclen);
    5703                 :            : }
    5704                 :            : 
    5705                 :          0 : static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
    5706                 :            : {
    5707                 :          0 :         return security_context_to_sid(secdata, seclen, secid);
    5708                 :            : }
    5709                 :            : 
    5710                 :          0 : static void selinux_release_secctx(char *secdata, u32 seclen)
    5711                 :            : {
    5712                 :          0 :         kfree(secdata);
    5713                 :          0 : }
    5714                 :            : 
    5715                 :            : /*
    5716                 :            :  *      called with inode->i_mutex locked
    5717                 :            :  */
    5718                 :          0 : static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
    5719                 :            : {
    5720                 :          0 :         return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
    5721                 :            : }
    5722                 :            : 
    5723                 :            : /*
    5724                 :            :  *      called with inode->i_mutex locked
    5725                 :            :  */
    5726                 :          0 : static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
    5727                 :            : {
    5728                 :          0 :         return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
    5729                 :            : }
    5730                 :            : 
    5731                 :          0 : static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
    5732                 :            : {
    5733                 :            :         int len = 0;
    5734                 :          0 :         len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
    5735                 :            :                                                 ctx, true);
    5736         [ #  # ]:          0 :         if (len < 0)
    5737                 :            :                 return len;
    5738                 :          0 :         *ctxlen = len;
    5739                 :          0 :         return 0;
    5740                 :            : }
    5741                 :            : #ifdef CONFIG_KEYS
    5742                 :            : 
    5743                 :          0 : static int selinux_key_alloc(struct key *k, const struct cred *cred,
    5744                 :            :                              unsigned long flags)
    5745                 :            : {
    5746                 :            :         const struct task_security_struct *tsec;
    5747                 :            :         struct key_security_struct *ksec;
    5748                 :            : 
    5749                 :            :         ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
    5750         [ #  # ]:          0 :         if (!ksec)
    5751                 :            :                 return -ENOMEM;
    5752                 :            : 
    5753                 :          0 :         tsec = cred->security;
    5754         [ #  # ]:          0 :         if (tsec->keycreate_sid)
    5755                 :          0 :                 ksec->sid = tsec->keycreate_sid;
    5756                 :            :         else
    5757                 :          0 :                 ksec->sid = tsec->sid;
    5758                 :            : 
    5759                 :          0 :         k->security = ksec;
    5760                 :          0 :         return 0;
    5761                 :            : }
    5762                 :            : 
    5763                 :          0 : static void selinux_key_free(struct key *k)
    5764                 :            : {
    5765                 :          0 :         struct key_security_struct *ksec = k->security;
    5766                 :            : 
    5767                 :          0 :         k->security = NULL;
    5768                 :          0 :         kfree(ksec);
    5769                 :          0 : }
    5770                 :            : 
    5771                 :          0 : static int selinux_key_permission(key_ref_t key_ref,
    5772                 :          0 :                                   const struct cred *cred,
    5773                 :            :                                   key_perm_t perm)
    5774                 :            : {
    5775                 :            :         struct key *key;
    5776                 :            :         struct key_security_struct *ksec;
    5777                 :            :         u32 sid;
    5778                 :            : 
    5779                 :            :         /* if no specific permissions are requested, we skip the
    5780                 :            :            permission check. No serious, additional covert channels
    5781                 :            :            appear to be created. */
    5782         [ #  # ]:          0 :         if (perm == 0)
    5783                 :            :                 return 0;
    5784                 :            : 
    5785                 :            :         sid = cred_sid(cred);
    5786                 :            : 
    5787                 :            :         key = key_ref_to_ptr(key_ref);
    5788                 :          0 :         ksec = key->security;
    5789                 :            : 
    5790                 :          0 :         return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
    5791                 :            : }
    5792                 :            : 
    5793                 :          0 : static int selinux_key_getsecurity(struct key *key, char **_buffer)
    5794                 :            : {
    5795                 :          0 :         struct key_security_struct *ksec = key->security;
    5796                 :          0 :         char *context = NULL;
    5797                 :            :         unsigned len;
    5798                 :            :         int rc;
    5799                 :            : 
    5800                 :          0 :         rc = security_sid_to_context(ksec->sid, &context, &len);
    5801         [ #  # ]:          0 :         if (!rc)
    5802                 :          0 :                 rc = len;
    5803                 :          0 :         *_buffer = context;
    5804                 :          0 :         return rc;
    5805                 :            : }
    5806                 :            : 
    5807                 :            : #endif
    5808                 :            : 
    5809                 :            : static struct security_operations selinux_ops = {
    5810                 :            :         .name =                         "selinux",
    5811                 :            : 
    5812                 :            :         .binder_set_context_mgr =       selinux_binder_set_context_mgr,
    5813                 :            :         .binder_transaction =           selinux_binder_transaction,
    5814                 :            :         .binder_transfer_binder =       selinux_binder_transfer_binder,
    5815                 :            :         .binder_transfer_file =         selinux_binder_transfer_file,
    5816                 :            : 
    5817                 :            :         .ptrace_access_check =          selinux_ptrace_access_check,
    5818                 :            :         .ptrace_traceme =               selinux_ptrace_traceme,
    5819                 :            :         .capget =                       selinux_capget,
    5820                 :            :         .capset =                       selinux_capset,
    5821                 :            :         .capable =                      selinux_capable,
    5822                 :            :         .quotactl =                     selinux_quotactl,
    5823                 :            :         .quota_on =                     selinux_quota_on,
    5824                 :            :         .syslog =                       selinux_syslog,
    5825                 :            :         .vm_enough_memory =             selinux_vm_enough_memory,
    5826                 :            : 
    5827                 :            :         .netlink_send =                 selinux_netlink_send,
    5828                 :            : 
    5829                 :            :         .bprm_set_creds =               selinux_bprm_set_creds,
    5830                 :            :         .bprm_committing_creds =        selinux_bprm_committing_creds,
    5831                 :            :         .bprm_committed_creds =         selinux_bprm_committed_creds,
    5832                 :            :         .bprm_secureexec =              selinux_bprm_secureexec,
    5833                 :            : 
    5834                 :            :         .sb_alloc_security =            selinux_sb_alloc_security,
    5835                 :            :         .sb_free_security =             selinux_sb_free_security,
    5836                 :            :         .sb_copy_data =                 selinux_sb_copy_data,
    5837                 :            :         .sb_remount =                   selinux_sb_remount,
    5838                 :            :         .sb_kern_mount =                selinux_sb_kern_mount,
    5839                 :            :         .sb_show_options =              selinux_sb_show_options,
    5840                 :            :         .sb_statfs =                    selinux_sb_statfs,
    5841                 :            :         .sb_mount =                     selinux_mount,
    5842                 :            :         .sb_umount =                    selinux_umount,
    5843                 :            :         .sb_set_mnt_opts =              selinux_set_mnt_opts,
    5844                 :            :         .sb_clone_mnt_opts =            selinux_sb_clone_mnt_opts,
    5845                 :            :         .sb_parse_opts_str =            selinux_parse_opts_str,
    5846                 :            : 
    5847                 :            :         .dentry_init_security =         selinux_dentry_init_security,
    5848                 :            : 
    5849                 :            :         .inode_alloc_security =         selinux_inode_alloc_security,
    5850                 :            :         .inode_free_security =          selinux_inode_free_security,
    5851                 :            :         .inode_init_security =          selinux_inode_init_security,
    5852                 :            :         .inode_create =                 selinux_inode_create,
    5853                 :            :         .inode_link =                   selinux_inode_link,
    5854                 :            :         .inode_unlink =                 selinux_inode_unlink,
    5855                 :            :         .inode_symlink =                selinux_inode_symlink,
    5856                 :            :         .inode_mkdir =                  selinux_inode_mkdir,
    5857                 :            :         .inode_rmdir =                  selinux_inode_rmdir,
    5858                 :            :         .inode_mknod =                  selinux_inode_mknod,
    5859                 :            :         .inode_rename =                 selinux_inode_rename,
    5860                 :            :         .inode_readlink =               selinux_inode_readlink,
    5861                 :            :         .inode_follow_link =            selinux_inode_follow_link,
    5862                 :            :         .inode_permission =             selinux_inode_permission,
    5863                 :            :         .inode_setattr =                selinux_inode_setattr,
    5864                 :            :         .inode_getattr =                selinux_inode_getattr,
    5865                 :            :         .inode_setxattr =               selinux_inode_setxattr,
    5866                 :            :         .inode_post_setxattr =          selinux_inode_post_setxattr,
    5867                 :            :         .inode_getxattr =               selinux_inode_getxattr,
    5868                 :            :         .inode_listxattr =              selinux_inode_listxattr,
    5869                 :            :         .inode_removexattr =            selinux_inode_removexattr,
    5870                 :            :         .inode_getsecurity =            selinux_inode_getsecurity,
    5871                 :            :         .inode_setsecurity =            selinux_inode_setsecurity,
    5872                 :            :         .inode_listsecurity =           selinux_inode_listsecurity,
    5873                 :            :         .inode_getsecid =               selinux_inode_getsecid,
    5874                 :            : 
    5875                 :            :         .file_permission =              selinux_file_permission,
    5876                 :            :         .file_alloc_security =          selinux_file_alloc_security,
    5877                 :            :         .file_free_security =           selinux_file_free_security,
    5878                 :            :         .file_ioctl =                   selinux_file_ioctl,
    5879                 :            :         .mmap_file =                    selinux_mmap_file,
    5880                 :            :         .mmap_addr =                    selinux_mmap_addr,
    5881                 :            :         .file_mprotect =                selinux_file_mprotect,
    5882                 :            :         .file_lock =                    selinux_file_lock,
    5883                 :            :         .file_fcntl =                   selinux_file_fcntl,
    5884                 :            :         .file_set_fowner =              selinux_file_set_fowner,
    5885                 :            :         .file_send_sigiotask =          selinux_file_send_sigiotask,
    5886                 :            :         .file_receive =                 selinux_file_receive,
    5887                 :            : 
    5888                 :            :         .file_open =                    selinux_file_open,
    5889                 :            : 
    5890                 :            :         .task_create =                  selinux_task_create,
    5891                 :            :         .cred_alloc_blank =             selinux_cred_alloc_blank,
    5892                 :            :         .cred_free =                    selinux_cred_free,
    5893                 :            :         .cred_prepare =                 selinux_cred_prepare,
    5894                 :            :         .cred_transfer =                selinux_cred_transfer,
    5895                 :            :         .kernel_act_as =                selinux_kernel_act_as,
    5896                 :            :         .kernel_create_files_as =       selinux_kernel_create_files_as,
    5897                 :            :         .kernel_module_request =        selinux_kernel_module_request,
    5898                 :            :         .task_setpgid =                 selinux_task_setpgid,
    5899                 :            :         .task_getpgid =                 selinux_task_getpgid,
    5900                 :            :         .task_getsid =                  selinux_task_getsid,
    5901                 :            :         .task_getsecid =                selinux_task_getsecid,
    5902                 :            :         .task_setnice =                 selinux_task_setnice,
    5903                 :            :         .task_setioprio =               selinux_task_setioprio,
    5904                 :            :         .task_getioprio =               selinux_task_getioprio,
    5905                 :            :         .task_setrlimit =               selinux_task_setrlimit,
    5906                 :            :         .task_setscheduler =            selinux_task_setscheduler,
    5907                 :            :         .task_getscheduler =            selinux_task_getscheduler,
    5908                 :            :         .task_movememory =              selinux_task_movememory,
    5909                 :            :         .task_kill =                    selinux_task_kill,
    5910                 :            :         .task_wait =                    selinux_task_wait,
    5911                 :            :         .task_to_inode =                selinux_task_to_inode,
    5912                 :            : 
    5913                 :            :         .ipc_permission =               selinux_ipc_permission,
    5914                 :            :         .ipc_getsecid =                 selinux_ipc_getsecid,
    5915                 :            : 
    5916                 :            :         .msg_msg_alloc_security =       selinux_msg_msg_alloc_security,
    5917                 :            :         .msg_msg_free_security =        selinux_msg_msg_free_security,
    5918                 :            : 
    5919                 :            :         .msg_queue_alloc_security =     selinux_msg_queue_alloc_security,
    5920                 :            :         .msg_queue_free_security =      selinux_msg_queue_free_security,
    5921                 :            :         .msg_queue_associate =          selinux_msg_queue_associate,
    5922                 :            :         .msg_queue_msgctl =             selinux_msg_queue_msgctl,
    5923                 :            :         .msg_queue_msgsnd =             selinux_msg_queue_msgsnd,
    5924                 :            :         .msg_queue_msgrcv =             selinux_msg_queue_msgrcv,
    5925                 :            : 
    5926                 :            :         .shm_alloc_security =           selinux_shm_alloc_security,
    5927                 :            :         .shm_free_security =            selinux_shm_free_security,
    5928                 :            :         .shm_associate =                selinux_shm_associate,
    5929                 :            :         .shm_shmctl =                   selinux_shm_shmctl,
    5930                 :            :         .shm_shmat =                    selinux_shm_shmat,
    5931                 :            : 
    5932                 :            :         .sem_alloc_security =           selinux_sem_alloc_security,
    5933                 :            :         .sem_free_security =            selinux_sem_free_security,
    5934                 :            :         .sem_associate =                selinux_sem_associate,
    5935                 :            :         .sem_semctl =                   selinux_sem_semctl,
    5936                 :            :         .sem_semop =                    selinux_sem_semop,
    5937                 :            : 
    5938                 :            :         .d_instantiate =                selinux_d_instantiate,
    5939                 :            : 
    5940                 :            :         .getprocattr =                  selinux_getprocattr,
    5941                 :            :         .setprocattr =                  selinux_setprocattr,
    5942                 :            : 
    5943                 :            :         .ismaclabel =                   selinux_ismaclabel,
    5944                 :            :         .secid_to_secctx =              selinux_secid_to_secctx,
    5945                 :            :         .secctx_to_secid =              selinux_secctx_to_secid,
    5946                 :            :         .release_secctx =               selinux_release_secctx,
    5947                 :            :         .inode_notifysecctx =           selinux_inode_notifysecctx,
    5948                 :            :         .inode_setsecctx =              selinux_inode_setsecctx,
    5949                 :            :         .inode_getsecctx =              selinux_inode_getsecctx,
    5950                 :            : 
    5951                 :            :         .unix_stream_connect =          selinux_socket_unix_stream_connect,
    5952                 :            :         .unix_may_send =                selinux_socket_unix_may_send,
    5953                 :            : 
    5954                 :            :         .socket_create =                selinux_socket_create,
    5955                 :            :         .socket_post_create =           selinux_socket_post_create,
    5956                 :            :         .socket_bind =                  selinux_socket_bind,
    5957                 :            :         .socket_connect =               selinux_socket_connect,
    5958                 :            :         .socket_listen =                selinux_socket_listen,
    5959                 :            :         .socket_accept =                selinux_socket_accept,
    5960                 :            :         .socket_sendmsg =               selinux_socket_sendmsg,
    5961                 :            :         .socket_recvmsg =               selinux_socket_recvmsg,
    5962                 :            :         .socket_getsockname =           selinux_socket_getsockname,
    5963                 :            :         .socket_getpeername =           selinux_socket_getpeername,
    5964                 :            :         .socket_getsockopt =            selinux_socket_getsockopt,
    5965                 :            :         .socket_setsockopt =            selinux_socket_setsockopt,
    5966                 :            :         .socket_shutdown =              selinux_socket_shutdown,
    5967                 :            :         .socket_sock_rcv_skb =          selinux_socket_sock_rcv_skb,
    5968                 :            :         .socket_getpeersec_stream =     selinux_socket_getpeersec_stream,
    5969                 :            :         .socket_getpeersec_dgram =      selinux_socket_getpeersec_dgram,
    5970                 :            :         .sk_alloc_security =            selinux_sk_alloc_security,
    5971                 :            :         .sk_free_security =             selinux_sk_free_security,
    5972                 :            :         .sk_clone_security =            selinux_sk_clone_security,
    5973                 :            :         .sk_getsecid =                  selinux_sk_getsecid,
    5974                 :            :         .sock_graft =                   selinux_sock_graft,
    5975                 :            :         .inet_conn_request =            selinux_inet_conn_request,
    5976                 :            :         .inet_csk_clone =               selinux_inet_csk_clone,
    5977                 :            :         .inet_conn_established =        selinux_inet_conn_established,
    5978                 :            :         .secmark_relabel_packet =       selinux_secmark_relabel_packet,
    5979                 :            :         .secmark_refcount_inc =         selinux_secmark_refcount_inc,
    5980                 :            :         .secmark_refcount_dec =         selinux_secmark_refcount_dec,
    5981                 :            :         .req_classify_flow =            selinux_req_classify_flow,
    5982                 :            :         .tun_dev_alloc_security =       selinux_tun_dev_alloc_security,
    5983                 :            :         .tun_dev_free_security =        selinux_tun_dev_free_security,
    5984                 :            :         .tun_dev_create =               selinux_tun_dev_create,
    5985                 :            :         .tun_dev_attach_queue =         selinux_tun_dev_attach_queue,
    5986                 :            :         .tun_dev_attach =               selinux_tun_dev_attach,
    5987                 :            :         .tun_dev_open =                 selinux_tun_dev_open,
    5988                 :            :         .skb_owned_by =                 selinux_skb_owned_by,
    5989                 :            : 
    5990                 :            : #ifdef CONFIG_SECURITY_NETWORK_XFRM
    5991                 :            :         .xfrm_policy_alloc_security =   selinux_xfrm_policy_alloc,
    5992                 :            :         .xfrm_policy_clone_security =   selinux_xfrm_policy_clone,
    5993                 :            :         .xfrm_policy_free_security =    selinux_xfrm_policy_free,
    5994                 :            :         .xfrm_policy_delete_security =  selinux_xfrm_policy_delete,
    5995                 :            :         .xfrm_state_alloc =             selinux_xfrm_state_alloc,
    5996                 :            :         .xfrm_state_alloc_acquire =     selinux_xfrm_state_alloc_acquire,
    5997                 :            :         .xfrm_state_free_security =     selinux_xfrm_state_free,
    5998                 :            :         .xfrm_state_delete_security =   selinux_xfrm_state_delete,
    5999                 :            :         .xfrm_policy_lookup =           selinux_xfrm_policy_lookup,
    6000                 :            :         .xfrm_state_pol_flow_match =    selinux_xfrm_state_pol_flow_match,
    6001                 :            :         .xfrm_decode_session =          selinux_xfrm_decode_session,
    6002                 :            : #endif
    6003                 :            : 
    6004                 :            : #ifdef CONFIG_KEYS
    6005                 :            :         .key_alloc =                    selinux_key_alloc,
    6006                 :            :         .key_free =                     selinux_key_free,
    6007                 :            :         .key_permission =               selinux_key_permission,
    6008                 :            :         .key_getsecurity =              selinux_key_getsecurity,
    6009                 :            : #endif
    6010                 :            : 
    6011                 :            : #ifdef CONFIG_AUDIT
    6012                 :            :         .audit_rule_init =              selinux_audit_rule_init,
    6013                 :            :         .audit_rule_known =             selinux_audit_rule_known,
    6014                 :            :         .audit_rule_match =             selinux_audit_rule_match,
    6015                 :            :         .audit_rule_free =              selinux_audit_rule_free,
    6016                 :            : #endif
    6017                 :            : };
    6018                 :            : 
    6019                 :          0 : static __init int selinux_init(void)
    6020                 :            : {
    6021         [ #  # ]:          0 :         if (!security_module_enable(&selinux_ops)) {
    6022                 :          0 :                 selinux_enabled = 0;
    6023                 :          0 :                 return 0;
    6024                 :            :         }
    6025                 :            : 
    6026         [ #  # ]:          0 :         if (!selinux_enabled) {
    6027                 :          0 :                 printk(KERN_INFO "SELinux:  Disabled at boot.\n");
    6028                 :          0 :                 return 0;
    6029                 :            :         }
    6030                 :            : 
    6031                 :          0 :         printk(KERN_INFO "SELinux:  Initializing.\n");
    6032                 :            : 
    6033                 :            :         /* Set the security state for the initial task. */
    6034                 :          0 :         cred_init_security();
    6035                 :            : 
    6036                 :          0 :         default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
    6037                 :            : 
    6038                 :          0 :         sel_inode_cache = kmem_cache_create("selinux_inode_security",
    6039                 :            :                                             sizeof(struct inode_security_struct),
    6040                 :            :                                             0, SLAB_PANIC, NULL);
    6041                 :          0 :         avc_init();
    6042                 :            : 
    6043         [ #  # ]:          0 :         if (register_security(&selinux_ops))
    6044                 :          0 :                 panic("SELinux: Unable to register with kernel.\n");
    6045                 :            : 
    6046         [ #  # ]:          0 :         if (selinux_enforcing)
    6047                 :          0 :                 printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
    6048                 :            :         else
    6049                 :          0 :                 printk(KERN_DEBUG "SELinux:  Starting in permissive mode\n");
    6050                 :            : 
    6051                 :            :         return 0;
    6052                 :            : }
    6053                 :            : 
    6054                 :          0 : static void delayed_superblock_init(struct super_block *sb, void *unused)
    6055                 :            : {
    6056                 :          0 :         superblock_doinit(sb, NULL);
    6057                 :          0 : }
    6058                 :            : 
    6059                 :          0 : void selinux_complete_init(void)
    6060                 :            : {
    6061                 :          0 :         printk(KERN_DEBUG "SELinux:  Completing initialization.\n");
    6062                 :            : 
    6063                 :            :         /* Set up any superblocks initialized prior to the policy load. */
    6064                 :          0 :         printk(KERN_DEBUG "SELinux:  Setting up existing superblocks.\n");
    6065                 :          0 :         iterate_supers(delayed_superblock_init, NULL);
    6066                 :          0 : }
    6067                 :            : 
    6068                 :            : /* SELinux requires early initialization in order to label
    6069                 :            :    all processes and objects when they are created. */
    6070                 :            : security_initcall(selinux_init);
    6071                 :            : 
    6072                 :            : #if defined(CONFIG_NETFILTER)
    6073                 :            : 
    6074                 :            : static struct nf_hook_ops selinux_ipv4_ops[] = {
    6075                 :            :         {
    6076                 :            :                 .hook =         selinux_ipv4_postroute,
    6077                 :            :                 .owner =        THIS_MODULE,
    6078                 :            :                 .pf =           NFPROTO_IPV4,
    6079                 :            :                 .hooknum =      NF_INET_POST_ROUTING,
    6080                 :            :                 .priority =     NF_IP_PRI_SELINUX_LAST,
    6081                 :            :         },
    6082                 :            :         {
    6083                 :            :                 .hook =         selinux_ipv4_forward,
    6084                 :            :                 .owner =        THIS_MODULE,
    6085                 :            :                 .pf =           NFPROTO_IPV4,
    6086                 :            :                 .hooknum =      NF_INET_FORWARD,
    6087                 :            :                 .priority =     NF_IP_PRI_SELINUX_FIRST,
    6088                 :            :         },
    6089                 :            :         {
    6090                 :            :                 .hook =         selinux_ipv4_output,
    6091                 :            :                 .owner =        THIS_MODULE,
    6092                 :            :                 .pf =           NFPROTO_IPV4,
    6093                 :            :                 .hooknum =      NF_INET_LOCAL_OUT,
    6094                 :            :                 .priority =     NF_IP_PRI_SELINUX_FIRST,
    6095                 :            :         }
    6096                 :            : };
    6097                 :            : 
    6098                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    6099                 :            : 
    6100                 :            : static struct nf_hook_ops selinux_ipv6_ops[] = {
    6101                 :            :         {
    6102                 :            :                 .hook =         selinux_ipv6_postroute,
    6103                 :            :                 .owner =        THIS_MODULE,
    6104                 :            :                 .pf =           NFPROTO_IPV6,
    6105                 :            :                 .hooknum =      NF_INET_POST_ROUTING,
    6106                 :            :                 .priority =     NF_IP6_PRI_SELINUX_LAST,
    6107                 :            :         },
    6108                 :            :         {
    6109                 :            :                 .hook =         selinux_ipv6_forward,
    6110                 :            :                 .owner =        THIS_MODULE,
    6111                 :            :                 .pf =           NFPROTO_IPV6,
    6112                 :            :                 .hooknum =      NF_INET_FORWARD,
    6113                 :            :                 .priority =     NF_IP6_PRI_SELINUX_FIRST,
    6114                 :            :         }
    6115                 :            : };
    6116                 :            : 
    6117                 :            : #endif  /* IPV6 */
    6118                 :            : 
    6119                 :          0 : static int __init selinux_nf_ip_init(void)
    6120                 :            : {
    6121                 :            :         int err = 0;
    6122                 :            : 
    6123         [ #  # ]:          0 :         if (!selinux_enabled)
    6124                 :            :                 goto out;
    6125                 :            : 
    6126                 :          0 :         printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
    6127                 :            : 
    6128                 :          0 :         err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
    6129         [ #  # ]:          0 :         if (err)
    6130                 :          0 :                 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
    6131                 :            : 
    6132                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    6133                 :          0 :         err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
    6134         [ #  # ]:          0 :         if (err)
    6135                 :          0 :                 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
    6136                 :            : #endif  /* IPV6 */
    6137                 :            : 
    6138                 :            : out:
    6139                 :          0 :         return err;
    6140                 :            : }
    6141                 :            : 
    6142                 :            : __initcall(selinux_nf_ip_init);
    6143                 :            : 
    6144                 :            : #ifdef CONFIG_SECURITY_SELINUX_DISABLE
    6145                 :            : static void selinux_nf_ip_exit(void)
    6146                 :            : {
    6147                 :            :         printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
    6148                 :            : 
    6149                 :            :         nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
    6150                 :            : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
    6151                 :            :         nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
    6152                 :            : #endif  /* IPV6 */
    6153                 :            : }
    6154                 :            : #endif
    6155                 :            : 
    6156                 :            : #else /* CONFIG_NETFILTER */
    6157                 :            : 
    6158                 :            : #ifdef CONFIG_SECURITY_SELINUX_DISABLE
    6159                 :            : #define selinux_nf_ip_exit()
    6160                 :            : #endif
    6161                 :            : 
    6162                 :            : #endif /* CONFIG_NETFILTER */
    6163                 :            : 
    6164                 :            : #ifdef CONFIG_SECURITY_SELINUX_DISABLE
    6165                 :            : static int selinux_disabled;
    6166                 :            : 
    6167                 :            : int selinux_disable(void)
    6168                 :            : {
    6169                 :            :         if (ss_initialized) {
    6170                 :            :                 /* Not permitted after initial policy load. */
    6171                 :            :                 return -EINVAL;
    6172                 :            :         }
    6173                 :            : 
    6174                 :            :         if (selinux_disabled) {
    6175                 :            :                 /* Only do this once. */
    6176                 :            :                 return -EINVAL;
    6177                 :            :         }
    6178                 :            : 
    6179                 :            :         printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
    6180                 :            : 
    6181                 :            :         selinux_disabled = 1;
    6182                 :            :         selinux_enabled = 0;
    6183                 :            : 
    6184                 :            :         reset_security_ops();
    6185                 :            : 
    6186                 :            :         /* Try to destroy the avc node cache */
    6187                 :            :         avc_disable();
    6188                 :            : 
    6189                 :            :         /* Unregister netfilter hooks. */
    6190                 :            :         selinux_nf_ip_exit();
    6191                 :            : 
    6192                 :            :         /* Unregister selinuxfs. */
    6193                 :            :         exit_sel_fs();
    6194                 :            : 
    6195                 :            :         return 0;
    6196                 :            : }
    6197                 :            : #endif

Generated by: LCOV version 1.9