Merge tag 'selinux-pr-20171113' of git://git.kernel.org/pub/scm/linux/kernel/git...
authorLinus Torvalds <torvalds@linux-foundation.org>
Wed, 15 Nov 2017 21:32:56 +0000 (13:32 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 15 Nov 2017 21:32:56 +0000 (13:32 -0800)
Pull SELinux updates from Paul Moore:
 "Seven SELinux patches for v4.15, although five of the seven are small
  build fixes and cleanups.

  Of the remaining two patches, the only one worth really calling out is
  Eric's fix for the SELinux filesystem xattr set/remove code; the other
  patch simply converts the SELinux hash table implementation to use
  kmem_cache.

  Eric's setxattr/removexattr tweak converts SELinux back to calling the
  commoncap implementations when the xattr is not SELinux related. The
  immediate win is to fixup filesystem capabilities in user namespaces,
  but it makes things a bit saner overall; more information in the
  commit description"

* tag 'selinux-pr-20171113' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux:
  selinux: remove extraneous initialization of slots_used and max_chain_len
  selinux: remove redundant assignment to len
  selinux: remove redundant assignment to str
  selinux: fix build warning
  selinux: fix build warning by removing the unused sid variable
  selinux: Perform both commoncap and selinux xattr checks
  selinux: Use kmem_cache for hashtab_node

1  2 
security/selinux/hooks.c
security/selinux/ss/hashtab.c
security/selinux/ss/hashtab.h

diff --combined security/selinux/hooks.c
@@@ -85,7 -85,6 +85,7 @@@
  #include <linux/export.h>
  #include <linux/msg.h>
  #include <linux/shm.h>
 +#include <linux/bpf.h>
  
  #include "avc.h"
  #include "objsec.h"
@@@ -1815,10 -1814,6 +1815,10 @@@ static inline int file_path_has_perm(co
        return inode_has_perm(cred, file_inode(file), av, &ad);
  }
  
 +#ifdef CONFIG_BPF_SYSCALL
 +static int bpf_fd_pass(struct file *file, u32 sid);
 +#endif
 +
  /* Check whether a task can use an open file descriptor to
     access an inode in a given way.  Check access to the
     descriptor itself, and then use dentry_has_perm to
@@@ -1849,12 -1844,6 +1849,12 @@@ static int file_has_perm(const struct c
                        goto out;
        }
  
 +#ifdef CONFIG_BPF_SYSCALL
 +      rc = bpf_fd_pass(file, cred_sid(cred));
 +      if (rc)
 +              return rc;
 +#endif
 +
        /* av is zero if only checking access to the descriptor. */
        rc = 0;
        if (av)
@@@ -2175,12 -2164,6 +2175,12 @@@ static int selinux_binder_transfer_file
                        return rc;
        }
  
 +#ifdef CONFIG_BPF_SYSCALL
 +      rc = bpf_fd_pass(file, sid);
 +      if (rc)
 +              return rc;
 +#endif
 +
        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
                return 0;
  
@@@ -2935,13 -2918,12 +2935,12 @@@ static int selinux_inode_init_security(
  {
        const struct task_security_struct *tsec = current_security();
        struct superblock_security_struct *sbsec;
-       u32 sid, newsid, clen;
+       u32 newsid, clen;
        int rc;
        char *context;
  
        sbsec = dir->i_sb->s_security;
  
-       sid = tsec->sid;
        newsid = tsec->create_sid;
  
        rc = selinux_determine_inode_label(current_security(),
@@@ -3141,27 -3123,6 +3140,6 @@@ static int selinux_inode_getattr(const 
        return path_has_perm(current_cred(), path, FILE__GETATTR);
  }
  
- static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
- {
-       const struct cred *cred = current_cred();
-       if (!strncmp(name, XATTR_SECURITY_PREFIX,
-                    sizeof XATTR_SECURITY_PREFIX - 1)) {
-               if (!strcmp(name, XATTR_NAME_CAPS)) {
-                       if (!capable(CAP_SETFCAP))
-                               return -EPERM;
-               } else if (!capable(CAP_SYS_ADMIN)) {
-                       /* A different attribute in the security namespace.
-                          Restrict to administrator. */
-                       return -EPERM;
-               }
-       }
-       /* Not an attribute we recognize, so just check the
-          ordinary setattr permission. */
-       return dentry_has_perm(cred, dentry, FILE__SETATTR);
- }
  static bool has_cap_mac_admin(bool audit)
  {
        const struct cred *cred = current_cred();
@@@ -3184,8 -3145,15 +3162,15 @@@ static int selinux_inode_setxattr(struc
        u32 newsid, sid = current_sid();
        int rc = 0;
  
-       if (strcmp(name, XATTR_NAME_SELINUX))
-               return selinux_inode_setotherxattr(dentry, name);
+       if (strcmp(name, XATTR_NAME_SELINUX)) {
+               rc = cap_inode_setxattr(dentry, name, value, size, flags);
+               if (rc)
+                       return rc;
+               /* Not an attribute we recognize, so just check the
+                  ordinary setattr permission. */
+               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+       }
  
        sbsec = inode->i_sb->s_security;
        if (!(sbsec->flags & SBLABEL_MNT))
                if (!has_cap_mac_admin(true)) {
                        struct audit_buffer *ab;
                        size_t audit_size;
-                       const char *str;
  
                        /* We strip a nul only if it is at the end, otherwise the
                         * context contains a nul and we should audit that */
                        if (value) {
-                               str = value;
+                               const char *str = value;
                                if (str[size - 1] == '\0')
                                        audit_size = size - 1;
                                else
                                        audit_size = size;
                        } else {
-                               str = "";
                                audit_size = 0;
                        }
                        ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
@@@ -3299,8 -3266,15 +3283,15 @@@ static int selinux_inode_listxattr(stru
  
  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
  {
-       if (strcmp(name, XATTR_NAME_SELINUX))
-               return selinux_inode_setotherxattr(dentry, name);
+       if (strcmp(name, XATTR_NAME_SELINUX)) {
+               int rc = cap_inode_removexattr(dentry, name);
+               if (rc)
+                       return rc;
+               /* Not an attribute we recognize, so just check the
+                  ordinary setattr permission. */
+               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+       }
  
        /* No one is allowed to remove a SELinux security label.
           You can change the label, but all data must be labeled. */
@@@ -3995,8 -3969,8 +3986,8 @@@ static int selinux_task_getioprio(struc
                            PROCESS__GETSCHED, NULL);
  }
  
- int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
-                        unsigned int flags)
static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
+                               unsigned int flags)
  {
        u32 av = 0;
  
@@@ -6269,139 -6243,6 +6260,139 @@@ static void selinux_ib_free_security(vo
  }
  #endif
  
 +#ifdef CONFIG_BPF_SYSCALL
 +static int selinux_bpf(int cmd, union bpf_attr *attr,
 +                                   unsigned int size)
 +{
 +      u32 sid = current_sid();
 +      int ret;
 +
 +      switch (cmd) {
 +      case BPF_MAP_CREATE:
 +              ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
 +                                 NULL);
 +              break;
 +      case BPF_PROG_LOAD:
 +              ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
 +                                 NULL);
 +              break;
 +      default:
 +              ret = 0;
 +              break;
 +      }
 +
 +      return ret;
 +}
 +
 +static u32 bpf_map_fmode_to_av(fmode_t fmode)
 +{
 +      u32 av = 0;
 +
 +      if (fmode & FMODE_READ)
 +              av |= BPF__MAP_READ;
 +      if (fmode & FMODE_WRITE)
 +              av |= BPF__MAP_WRITE;
 +      return av;
 +}
 +
 +/* This function will check the file pass through unix socket or binder to see
 + * if it is a bpf related object. And apply correspinding checks on the bpf
 + * object based on the type. The bpf maps and programs, not like other files and
 + * socket, are using a shared anonymous inode inside the kernel as their inode.
 + * So checking that inode cannot identify if the process have privilege to
 + * access the bpf object and that's why we have to add this additional check in
 + * selinux_file_receive and selinux_binder_transfer_files.
 + */
 +static int bpf_fd_pass(struct file *file, u32 sid)
 +{
 +      struct bpf_security_struct *bpfsec;
 +      struct bpf_prog *prog;
 +      struct bpf_map *map;
 +      int ret;
 +
 +      if (file->f_op == &bpf_map_fops) {
 +              map = file->private_data;
 +              bpfsec = map->security;
 +              ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
 +                                 bpf_map_fmode_to_av(file->f_mode), NULL);
 +              if (ret)
 +                      return ret;
 +      } else if (file->f_op == &bpf_prog_fops) {
 +              prog = file->private_data;
 +              bpfsec = prog->aux->security;
 +              ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
 +                                 BPF__PROG_RUN, NULL);
 +              if (ret)
 +                      return ret;
 +      }
 +      return 0;
 +}
 +
 +static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
 +{
 +      u32 sid = current_sid();
 +      struct bpf_security_struct *bpfsec;
 +
 +      bpfsec = map->security;
 +      return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
 +                          bpf_map_fmode_to_av(fmode), NULL);
 +}
 +
 +static int selinux_bpf_prog(struct bpf_prog *prog)
 +{
 +      u32 sid = current_sid();
 +      struct bpf_security_struct *bpfsec;
 +
 +      bpfsec = prog->aux->security;
 +      return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
 +                          BPF__PROG_RUN, NULL);
 +}
 +
 +static int selinux_bpf_map_alloc(struct bpf_map *map)
 +{
 +      struct bpf_security_struct *bpfsec;
 +
 +      bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
 +      if (!bpfsec)
 +              return -ENOMEM;
 +
 +      bpfsec->sid = current_sid();
 +      map->security = bpfsec;
 +
 +      return 0;
 +}
 +
 +static void selinux_bpf_map_free(struct bpf_map *map)
 +{
 +      struct bpf_security_struct *bpfsec = map->security;
 +
 +      map->security = NULL;
 +      kfree(bpfsec);
 +}
 +
 +static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
 +{
 +      struct bpf_security_struct *bpfsec;
 +
 +      bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
 +      if (!bpfsec)
 +              return -ENOMEM;
 +
 +      bpfsec->sid = current_sid();
 +      aux->security = bpfsec;
 +
 +      return 0;
 +}
 +
 +static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
 +{
 +      struct bpf_security_struct *bpfsec = aux->security;
 +
 +      aux->security = NULL;
 +      kfree(bpfsec);
 +}
 +#endif
 +
  static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
        LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
        LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
        LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
        LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
  #endif
 +
 +#ifdef CONFIG_BPF_SYSCALL
 +      LSM_HOOK_INIT(bpf, selinux_bpf),
 +      LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
 +      LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
 +      LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
 +      LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
 +      LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
 +      LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
 +#endif
  };
  
  static __init int selinux_init(void)
@@@ -1,4 -1,3 +1,4 @@@
 +// SPDX-License-Identifier: GPL-2.0
  /*
   * Implementation of the hash table type.
   *
@@@ -10,6 -9,8 +10,8 @@@
  #include <linux/sched.h>
  #include "hashtab.h"
  
+ static struct kmem_cache *hashtab_node_cachep;
  struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
                               int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
                               u32 size)
@@@ -58,7 -59,7 +60,7 @@@ int hashtab_insert(struct hashtab *h, v
        if (cur && (h->keycmp(h, key, cur->key) == 0))
                return -EEXIST;
  
-       newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
+       newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
        if (!newnode)
                return -ENOMEM;
        newnode->key = key;
@@@ -107,7 -108,7 +109,7 @@@ void hashtab_destroy(struct hashtab *h
                while (cur) {
                        temp = cur;
                        cur = cur->next;
-                       kfree(temp);
+                       kmem_cache_free(hashtab_node_cachep, temp);
                }
                h->htable[i] = NULL;
        }
@@@ -149,7 -150,7 +151,7 @@@ void hashtab_stat(struct hashtab *h, st
  
        slots_used = 0;
        max_chain_len = 0;
-       for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
+       for (i = 0; i < h->size; i++) {
                cur = h->htable[i];
                if (cur) {
                        slots_used++;
        info->slots_used = slots_used;
        info->max_chain_len = max_chain_len;
  }
+ void hashtab_cache_init(void)
+ {
+               hashtab_node_cachep = kmem_cache_create("hashtab_node",
+                       sizeof(struct hashtab_node),
+                       0, SLAB_PANIC, NULL);
+ }
+ void hashtab_cache_destroy(void)
+ {
+               kmem_cache_destroy(hashtab_node_cachep);
+ }
@@@ -1,4 -1,3 +1,4 @@@
 +/* SPDX-License-Identifier: GPL-2.0 */
  /*
   * A hash table (hashtab) maintains associations between
   * key values and datum values.  The type of the key values
@@@ -85,4 -84,8 +85,8 @@@ int hashtab_map(struct hashtab *h
  /* Fill info with some hash table statistics */
  void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
  
+ /* Use kmem_cache for hashtab_node */
+ void hashtab_cache_init(void);
+ void hashtab_cache_destroy(void);
  #endif        /* _SS_HASHTAB_H */