Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 7 | * Chris Vance, <cvance@nai.com> |
| 8 | * Wayne Salamon, <wsalamon@nai.com> |
| 9 | * James Morris <jmorris@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2001,2002 Networks Associates Technology, Inc. |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 12 | * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 13 | * Eric Paris <eparis@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 15 | * <dgoeddel@trustedcs.com> |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 16 | * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 17 | * Paul Moore <paul@paul-moore.com> |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 18 | * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 19 | * Yuichi Nakamura <ynakam@hitachisoft.jp> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 23 | * as published by the Free Software Foundation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 27 | #include <linux/kd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/kernel.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 29 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/syscalls.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 44 | #include <linux/dcache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 46 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/namei.h> |
| 48 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/netfilter_ipv4.h> |
| 50 | #include <linux/netfilter_ipv6.h> |
| 51 | #include <linux/tty.h> |
| 52 | #include <net/icmp.h> |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 53 | #include <net/ip.h> /* for local_port_range[] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 55 | #include <net/net_namespace.h> |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 56 | #include <net/netlabel.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 57 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <asm/ioctls.h> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 59 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/bitops.h> |
| 61 | #include <linux/interrupt.h> |
| 62 | #include <linux/netdevice.h> /* for network interface checks */ |
| 63 | #include <linux/netlink.h> |
| 64 | #include <linux/tcp.h> |
| 65 | #include <linux/udp.h> |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 66 | #include <linux/dccp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/quota.h> |
| 68 | #include <linux/un.h> /* for Unix socket types */ |
| 69 | #include <net/af_unix.h> /* for Unix socket types */ |
| 70 | #include <linux/parser.h> |
| 71 | #include <linux/nfs_mount.h> |
| 72 | #include <net/ipv6.h> |
| 73 | #include <linux/hugetlb.h> |
| 74 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #include <linux/audit.h> |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 76 | #include <linux/string.h> |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 77 | #include <linux/selinux.h> |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 78 | #include <linux/mutex.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 79 | #include <linux/posix-timers.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 80 | #include <linux/syslog.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 81 | #include <linux/user_namespace.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 82 | #include <linux/export.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 83 | #include <linux/msg.h> |
| 84 | #include <linux/shm.h> |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 85 | #include <linux/pft.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | #include "avc.h" |
| 88 | #include "objsec.h" |
| 89 | #include "netif.h" |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 90 | #include "netnode.h" |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 91 | #include "netport.h" |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 92 | #include "xfrm.h" |
Paul Moore | c60475b | 2007-02-28 15:14:23 -0500 | [diff] [blame] | 93 | #include "netlabel.h" |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 94 | #include "audit.h" |
James Morris | 7b98a58 | 2011-08-30 12:52:32 +1000 | [diff] [blame] | 95 | #include "avc_ss.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 97 | #define NUM_SEL_MNT_OPTS 5 |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 98 | |
James Morris | 20510f2 | 2007-10-16 23:31:32 -0700 | [diff] [blame] | 99 | extern struct security_operations *security_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 101 | /* SECMARK reference count */ |
James Morris | 56a4ca9 | 2011-08-17 11:08:43 +1000 | [diff] [blame] | 102 | static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 105 | int selinux_enforcing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | static int __init enforcing_setup(char *str) |
| 108 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 109 | unsigned long enforcing; |
| 110 | if (!strict_strtoul(str, 0, &enforcing)) |
| 111 | selinux_enforcing = enforcing ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | 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 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 122 | unsigned long enabled; |
| 123 | if (!strict_strtoul(str, 0, &enabled)) |
| 124 | selinux_enabled = enabled ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | __setup("selinux=", selinux_enabled_setup); |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 128 | #else |
| 129 | int selinux_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | #endif |
| 131 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 132 | static struct kmem_cache *sel_inode_cache; |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 133 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 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. |
| 142 | * |
| 143 | */ |
| 144 | static int selinux_secmark_enabled(void) |
| 145 | { |
| 146 | return (atomic_read(&selinux_secmark_refcount) > 0); |
| 147 | } |
| 148 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 149 | /* |
| 150 | * initialise the security for the init task |
| 151 | */ |
| 152 | static void cred_init_security(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 154 | struct cred *cred = (struct cred *) current->real_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | struct task_security_struct *tsec; |
| 156 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 157 | tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (!tsec) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 159 | panic("SELinux: Failed to initialize initial task.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 161 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 162 | cred->security = tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 165 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 166 | * get the security ID of a set of credentials |
| 167 | */ |
| 168 | static inline u32 cred_sid(const struct cred *cred) |
| 169 | { |
| 170 | const struct task_security_struct *tsec; |
| 171 | |
| 172 | tsec = cred->security; |
| 173 | return tsec->sid; |
| 174 | } |
| 175 | |
| 176 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 177 | * get the objective security ID of a task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 178 | */ |
| 179 | static inline u32 task_sid(const struct task_struct *task) |
| 180 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 181 | u32 sid; |
| 182 | |
| 183 | rcu_read_lock(); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 184 | sid = cred_sid(__task_cred(task)); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 185 | rcu_read_unlock(); |
| 186 | return sid; |
| 187 | } |
| 188 | |
| 189 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 190 | * get the subjective security ID of the current task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 191 | */ |
| 192 | static inline u32 current_sid(void) |
| 193 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 194 | const struct task_security_struct *tsec = current_security(); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 195 | |
| 196 | return tsec->sid; |
| 197 | } |
| 198 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 199 | /* Allocate and free functions for each kind of security blob. */ |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | static int inode_alloc_security(struct inode *inode) |
| 202 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 204 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Josef Bacik | a02fe13 | 2008-04-04 09:35:05 +1100 | [diff] [blame] | 206 | isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (!isec) |
| 208 | return -ENOMEM; |
| 209 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 210 | mutex_init(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | INIT_LIST_HEAD(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | isec->inode = inode; |
| 213 | isec->sid = SECINITSID_UNLABELED; |
| 214 | isec->sclass = SECCLASS_FILE; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 215 | isec->task_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | inode->i_security = isec; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static void inode_free_security(struct inode *inode) |
| 222 | { |
| 223 | struct inode_security_struct *isec = inode->i_security; |
| 224 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | spin_lock(&sbsec->isec_lock); |
| 227 | if (!list_empty(&isec->list)) |
| 228 | list_del_init(&isec->list); |
| 229 | spin_unlock(&sbsec->isec_lock); |
| 230 | |
| 231 | inode->i_security = NULL; |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 232 | kmem_cache_free(sel_inode_cache, isec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int file_alloc_security(struct file *file) |
| 236 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | struct file_security_struct *fsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 238 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Stephen Smalley | 26d2a4b | 2006-02-01 03:05:55 -0800 | [diff] [blame] | 240 | fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (!fsec) |
| 242 | return -ENOMEM; |
| 243 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 244 | fsec->sid = sid; |
| 245 | fsec->fown_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | file->f_security = fsec; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static void file_free_security(struct file *file) |
| 252 | { |
| 253 | struct file_security_struct *fsec = file->f_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | file->f_security = NULL; |
| 255 | kfree(fsec); |
| 256 | } |
| 257 | |
| 258 | static int superblock_alloc_security(struct super_block *sb) |
| 259 | { |
| 260 | struct superblock_security_struct *sbsec; |
| 261 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 262 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!sbsec) |
| 264 | return -ENOMEM; |
| 265 | |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 266 | mutex_init(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | INIT_LIST_HEAD(&sbsec->isec_head); |
| 268 | spin_lock_init(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | sbsec->sb = sb; |
| 270 | sbsec->sid = SECINITSID_UNLABELED; |
| 271 | sbsec->def_sid = SECINITSID_FILE; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 272 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | sb->s_security = sbsec; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static void superblock_free_security(struct super_block *sb) |
| 279 | { |
| 280 | struct superblock_security_struct *sbsec = sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | sb->s_security = NULL; |
| 282 | kfree(sbsec); |
| 283 | } |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | /* The file system's label must be initialized prior to use. */ |
| 286 | |
Stephen Hemminger | 634a539 | 2010-03-04 21:59:03 -0800 | [diff] [blame] | 287 | static const char *labeling_behaviors[6] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | "uses xattr", |
| 289 | "uses transition SIDs", |
| 290 | "uses task SIDs", |
| 291 | "uses genfs_contexts", |
| 292 | "not configured for labeling", |
| 293 | "uses mountpoint labeling", |
| 294 | }; |
| 295 | |
| 296 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); |
| 297 | |
| 298 | static inline int inode_doinit(struct inode *inode) |
| 299 | { |
| 300 | return inode_doinit_with_dentry(inode, NULL); |
| 301 | } |
| 302 | |
| 303 | enum { |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 304 | Opt_error = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | Opt_context = 1, |
| 306 | Opt_fscontext = 2, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 307 | Opt_defcontext = 3, |
| 308 | Opt_rootcontext = 4, |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 309 | Opt_labelsupport = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 312 | static const match_table_t tokens = { |
Eric Paris | 832cbd9 | 2008-04-01 13:24:09 -0400 | [diff] [blame] | 313 | {Opt_context, CONTEXT_STR "%s"}, |
| 314 | {Opt_fscontext, FSCONTEXT_STR "%s"}, |
| 315 | {Opt_defcontext, DEFCONTEXT_STR "%s"}, |
| 316 | {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 317 | {Opt_labelsupport, LABELSUPP_STR}, |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 318 | {Opt_error, NULL}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" |
| 322 | |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 323 | static int may_context_mount_sb_relabel(u32 sid, |
| 324 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 325 | const struct cred *cred) |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 326 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 327 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 328 | int rc; |
| 329 | |
| 330 | rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| 331 | FILESYSTEM__RELABELFROM, NULL); |
| 332 | if (rc) |
| 333 | return rc; |
| 334 | |
| 335 | rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, |
| 336 | FILESYSTEM__RELABELTO, NULL); |
| 337 | return rc; |
| 338 | } |
| 339 | |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 340 | static int may_context_mount_inode_relabel(u32 sid, |
| 341 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 342 | const struct cred *cred) |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 343 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 344 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 345 | int rc; |
| 346 | rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| 347 | FILESYSTEM__RELABELFROM, NULL); |
| 348 | if (rc) |
| 349 | return rc; |
| 350 | |
| 351 | rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| 352 | FILESYSTEM__ASSOCIATE, NULL); |
| 353 | return rc; |
| 354 | } |
| 355 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 356 | static int sb_finish_set_opts(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | struct superblock_security_struct *sbsec = sb->s_security; |
| 359 | struct dentry *root = sb->s_root; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 360 | struct inode *root_inode = root->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | int rc = 0; |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 364 | /* Make sure that the xattr handler exists and that no |
| 365 | error other than -ENODATA is returned by getxattr on |
| 366 | the root directory. -ENODATA is ok, as this may be |
| 367 | the first boot of the SELinux kernel before we have |
| 368 | assigned xattr values to the filesystem. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 369 | if (!root_inode->i_op->getxattr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " |
| 371 | "xattr support\n", sb->s_id, sb->s_type->name); |
| 372 | rc = -EOPNOTSUPP; |
| 373 | goto out; |
| 374 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 375 | rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (rc < 0 && rc != -ENODATA) { |
| 377 | if (rc == -EOPNOTSUPP) |
| 378 | printk(KERN_WARNING "SELinux: (dev %s, type " |
| 379 | "%s) has no security xattr handler\n", |
| 380 | sb->s_id, sb->s_type->name); |
| 381 | else |
| 382 | printk(KERN_WARNING "SELinux: (dev %s, type " |
| 383 | "%s) getxattr errno %d\n", sb->s_id, |
| 384 | sb->s_type->name, -rc); |
| 385 | goto out; |
| 386 | } |
| 387 | } |
| 388 | |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 389 | sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 391 | if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 392 | printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | sb->s_id, sb->s_type->name); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 394 | else |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 395 | printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | sb->s_id, sb->s_type->name, |
| 397 | labeling_behaviors[sbsec->behavior-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 399 | if (sbsec->behavior == SECURITY_FS_USE_GENFS || |
| 400 | sbsec->behavior == SECURITY_FS_USE_MNTPOINT || |
| 401 | sbsec->behavior == SECURITY_FS_USE_NONE || |
| 402 | sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) |
| 403 | sbsec->flags &= ~SE_SBLABELSUPP; |
| 404 | |
Mark Salyzyn | 2c08805 | 2015-01-07 09:27:15 -0800 | [diff] [blame] | 405 | /* Special handling. Is genfs but also has in-core setxattr handler*/ |
| 406 | if (!strcmp(sb->s_type->name, "sysfs") || |
| 407 | !strcmp(sb->s_type->name, "pstore") || |
| 408 | !strcmp(sb->s_type->name, "debugfs") || |
| 409 | !strcmp(sb->s_type->name, "rootfs")) |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 410 | sbsec->flags |= SE_SBLABELSUPP; |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* Initialize the root inode. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 413 | rc = inode_doinit_with_dentry(root_inode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | /* Initialize any other inodes associated with the superblock, e.g. |
| 416 | inodes created prior to initial policy load or inodes created |
| 417 | during get_sb by a pseudo filesystem that directly |
| 418 | populates itself. */ |
| 419 | spin_lock(&sbsec->isec_lock); |
| 420 | next_inode: |
| 421 | if (!list_empty(&sbsec->isec_head)) { |
| 422 | struct inode_security_struct *isec = |
| 423 | list_entry(sbsec->isec_head.next, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 424 | struct inode_security_struct, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | struct inode *inode = isec->inode; |
| 426 | spin_unlock(&sbsec->isec_lock); |
| 427 | inode = igrab(inode); |
| 428 | if (inode) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 429 | if (!IS_PRIVATE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | inode_doinit(inode); |
| 431 | iput(inode); |
| 432 | } |
| 433 | spin_lock(&sbsec->isec_lock); |
| 434 | list_del_init(&isec->list); |
| 435 | goto next_inode; |
| 436 | } |
| 437 | spin_unlock(&sbsec->isec_lock); |
| 438 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 439 | return rc; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * This function should allow an FS to ask what it's mount security |
| 444 | * options were so it can use those later for submounts, displaying |
| 445 | * mount options, or whatever. |
| 446 | */ |
| 447 | static int selinux_get_mnt_opts(const struct super_block *sb, |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 448 | struct security_mnt_opts *opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 449 | { |
| 450 | int rc = 0, i; |
| 451 | struct superblock_security_struct *sbsec = sb->s_security; |
| 452 | char *context = NULL; |
| 453 | u32 len; |
| 454 | char tmp; |
| 455 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 456 | security_init_mnt_opts(opts); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 457 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 458 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 459 | return -EINVAL; |
| 460 | |
| 461 | if (!ss_initialized) |
| 462 | return -EINVAL; |
| 463 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 464 | tmp = sbsec->flags & SE_MNTMASK; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 465 | /* count the number of mount options for this sb */ |
| 466 | for (i = 0; i < 8; i++) { |
| 467 | if (tmp & 0x01) |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 468 | opts->num_mnt_opts++; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 469 | tmp >>= 1; |
| 470 | } |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 471 | /* Check if the Label support flag is set */ |
| 472 | if (sbsec->flags & SE_SBLABELSUPP) |
| 473 | opts->num_mnt_opts++; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 474 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 475 | opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); |
| 476 | if (!opts->mnt_opts) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 477 | rc = -ENOMEM; |
| 478 | goto out_free; |
| 479 | } |
| 480 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 481 | opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); |
| 482 | if (!opts->mnt_opts_flags) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 483 | rc = -ENOMEM; |
| 484 | goto out_free; |
| 485 | } |
| 486 | |
| 487 | i = 0; |
| 488 | if (sbsec->flags & FSCONTEXT_MNT) { |
| 489 | rc = security_sid_to_context(sbsec->sid, &context, &len); |
| 490 | if (rc) |
| 491 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 492 | opts->mnt_opts[i] = context; |
| 493 | opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 494 | } |
| 495 | if (sbsec->flags & CONTEXT_MNT) { |
| 496 | rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); |
| 497 | if (rc) |
| 498 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 499 | opts->mnt_opts[i] = context; |
| 500 | opts->mnt_opts_flags[i++] = CONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 501 | } |
| 502 | if (sbsec->flags & DEFCONTEXT_MNT) { |
| 503 | rc = security_sid_to_context(sbsec->def_sid, &context, &len); |
| 504 | if (rc) |
| 505 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 506 | opts->mnt_opts[i] = context; |
| 507 | opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 508 | } |
| 509 | if (sbsec->flags & ROOTCONTEXT_MNT) { |
| 510 | struct inode *root = sbsec->sb->s_root->d_inode; |
| 511 | struct inode_security_struct *isec = root->i_security; |
| 512 | |
| 513 | rc = security_sid_to_context(isec->sid, &context, &len); |
| 514 | if (rc) |
| 515 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 516 | opts->mnt_opts[i] = context; |
| 517 | opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 518 | } |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 519 | if (sbsec->flags & SE_SBLABELSUPP) { |
| 520 | opts->mnt_opts[i] = NULL; |
| 521 | opts->mnt_opts_flags[i++] = SE_SBLABELSUPP; |
| 522 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 523 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 524 | BUG_ON(i != opts->num_mnt_opts); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 525 | |
| 526 | return 0; |
| 527 | |
| 528 | out_free: |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 529 | security_free_mnt_opts(opts); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 530 | return rc; |
| 531 | } |
| 532 | |
| 533 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
| 534 | u32 old_sid, u32 new_sid) |
| 535 | { |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 536 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
| 537 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 538 | /* check if the old mount command had the same options */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 539 | if (sbsec->flags & SE_SBINITIALIZED) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 540 | if (!(sbsec->flags & flag) || |
| 541 | (old_sid != new_sid)) |
| 542 | return 1; |
| 543 | |
| 544 | /* check if we were passed the same options twice, |
| 545 | * aka someone passed context=a,context=b |
| 546 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 547 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 548 | if (mnt_flags & flag) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 549 | return 1; |
| 550 | return 0; |
| 551 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 552 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 553 | /* |
| 554 | * Allow filesystems with binary mount data to explicitly set mount point |
| 555 | * labeling information. |
| 556 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 557 | static int selinux_set_mnt_opts(struct super_block *sb, |
| 558 | struct security_mnt_opts *opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 559 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 560 | const struct cred *cred = current_cred(); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 561 | int rc = 0, i; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 562 | struct superblock_security_struct *sbsec = sb->s_security; |
| 563 | const char *name = sb->s_type->name; |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 564 | struct inode *inode = sbsec->sb->s_root->d_inode; |
| 565 | struct inode_security_struct *root_isec = inode->i_security; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 566 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
| 567 | u32 defcontext_sid = 0; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 568 | char **mount_options = opts->mnt_opts; |
| 569 | int *flags = opts->mnt_opts_flags; |
| 570 | int num_opts = opts->num_mnt_opts; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 571 | |
| 572 | mutex_lock(&sbsec->lock); |
| 573 | |
| 574 | if (!ss_initialized) { |
| 575 | if (!num_opts) { |
| 576 | /* Defer initialization until selinux_complete_init, |
| 577 | after the initial policy is loaded and the security |
| 578 | server is ready to handle calls. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 579 | goto out; |
| 580 | } |
| 581 | rc = -EINVAL; |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 582 | printk(KERN_WARNING "SELinux: Unable to set superblock options " |
| 583 | "before the security server is initialized\n"); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 584 | goto out; |
| 585 | } |
| 586 | |
| 587 | /* |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 588 | * Binary mount data FS will come through this function twice. Once |
| 589 | * from an explicit call and once from the generic calls from the vfs. |
| 590 | * Since the generic VFS calls will not contain any security mount data |
| 591 | * we need to skip the double mount verification. |
| 592 | * |
| 593 | * This does open a hole in which we will not notice if the first |
| 594 | * mount using this sb set explict options and a second mount using |
| 595 | * this sb does not set any security options. (The first options |
| 596 | * will be used for both mounts) |
| 597 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 598 | if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 599 | && (num_opts == 0)) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 600 | goto out; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 601 | |
| 602 | /* |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 603 | * parse the mount options, check if they are valid sids. |
| 604 | * also check if someone is trying to mount the same sb more |
| 605 | * than once with different security options. |
| 606 | */ |
| 607 | for (i = 0; i < num_opts; i++) { |
| 608 | u32 sid; |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 609 | |
| 610 | if (flags[i] == SE_SBLABELSUPP) |
| 611 | continue; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 612 | rc = security_context_to_sid(mount_options[i], |
| 613 | strlen(mount_options[i]), &sid); |
| 614 | if (rc) { |
| 615 | printk(KERN_WARNING "SELinux: security_context_to_sid" |
| 616 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 617 | mount_options[i], sb->s_id, name, rc); |
| 618 | goto out; |
| 619 | } |
| 620 | switch (flags[i]) { |
| 621 | case FSCONTEXT_MNT: |
| 622 | fscontext_sid = sid; |
| 623 | |
| 624 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, |
| 625 | fscontext_sid)) |
| 626 | goto out_double_mount; |
| 627 | |
| 628 | sbsec->flags |= FSCONTEXT_MNT; |
| 629 | break; |
| 630 | case CONTEXT_MNT: |
| 631 | context_sid = sid; |
| 632 | |
| 633 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, |
| 634 | context_sid)) |
| 635 | goto out_double_mount; |
| 636 | |
| 637 | sbsec->flags |= CONTEXT_MNT; |
| 638 | break; |
| 639 | case ROOTCONTEXT_MNT: |
| 640 | rootcontext_sid = sid; |
| 641 | |
| 642 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, |
| 643 | rootcontext_sid)) |
| 644 | goto out_double_mount; |
| 645 | |
| 646 | sbsec->flags |= ROOTCONTEXT_MNT; |
| 647 | |
| 648 | break; |
| 649 | case DEFCONTEXT_MNT: |
| 650 | defcontext_sid = sid; |
| 651 | |
| 652 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, |
| 653 | defcontext_sid)) |
| 654 | goto out_double_mount; |
| 655 | |
| 656 | sbsec->flags |= DEFCONTEXT_MNT; |
| 657 | |
| 658 | break; |
| 659 | default: |
| 660 | rc = -EINVAL; |
| 661 | goto out; |
| 662 | } |
| 663 | } |
| 664 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 665 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 666 | /* previously mounted with options, but not on this attempt? */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 667 | if ((sbsec->flags & SE_MNTMASK) && !num_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 668 | goto out_double_mount; |
| 669 | rc = 0; |
| 670 | goto out; |
| 671 | } |
| 672 | |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 673 | if (strcmp(sb->s_type->name, "proc") == 0) |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 674 | sbsec->flags |= SE_SBPROC; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 675 | |
| 676 | /* Determine the labeling behavior to use for this filesystem type. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 677 | rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 678 | if (rc) { |
| 679 | printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 680 | __func__, sb->s_type->name, rc); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 681 | goto out; |
| 682 | } |
| 683 | |
| 684 | /* sets the context of the superblock for the fs being mounted. */ |
| 685 | if (fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 686 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 687 | if (rc) |
| 688 | goto out; |
| 689 | |
| 690 | sbsec->sid = fscontext_sid; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Switch to using mount point labeling behavior. |
| 695 | * sets the label used on all file below the mountpoint, and will set |
| 696 | * the superblock context if not already set. |
| 697 | */ |
| 698 | if (context_sid) { |
| 699 | if (!fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 700 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
| 701 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 702 | if (rc) |
| 703 | goto out; |
| 704 | sbsec->sid = context_sid; |
| 705 | } else { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 706 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
| 707 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 708 | if (rc) |
| 709 | goto out; |
| 710 | } |
| 711 | if (!rootcontext_sid) |
| 712 | rootcontext_sid = context_sid; |
| 713 | |
| 714 | sbsec->mntpoint_sid = context_sid; |
| 715 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
| 716 | } |
| 717 | |
| 718 | if (rootcontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 719 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
| 720 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 721 | if (rc) |
| 722 | goto out; |
| 723 | |
| 724 | root_isec->sid = rootcontext_sid; |
| 725 | root_isec->initialized = 1; |
| 726 | } |
| 727 | |
| 728 | if (defcontext_sid) { |
| 729 | if (sbsec->behavior != SECURITY_FS_USE_XATTR) { |
| 730 | rc = -EINVAL; |
| 731 | printk(KERN_WARNING "SELinux: defcontext option is " |
| 732 | "invalid for this filesystem type\n"); |
| 733 | goto out; |
| 734 | } |
| 735 | |
| 736 | if (defcontext_sid != sbsec->def_sid) { |
| 737 | rc = may_context_mount_inode_relabel(defcontext_sid, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 738 | sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 739 | if (rc) |
| 740 | goto out; |
| 741 | } |
| 742 | |
| 743 | sbsec->def_sid = defcontext_sid; |
| 744 | } |
| 745 | |
| 746 | rc = sb_finish_set_opts(sb); |
| 747 | out: |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 748 | mutex_unlock(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 750 | out_double_mount: |
| 751 | rc = -EINVAL; |
| 752 | printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " |
| 753 | "security settings for (dev %s, type %s)\n", sb->s_id, name); |
| 754 | goto out; |
| 755 | } |
| 756 | |
| 757 | static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, |
| 758 | struct super_block *newsb) |
| 759 | { |
| 760 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
| 761 | struct superblock_security_struct *newsbsec = newsb->s_security; |
| 762 | |
| 763 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 764 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| 765 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); |
| 766 | |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 767 | /* |
| 768 | * if the parent was able to be mounted it clearly had no special lsm |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 769 | * mount options. thus we can safely deal with this superblock later |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 770 | */ |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 771 | if (!ss_initialized) |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 772 | return; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 773 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 774 | /* how can we clone if the old one wasn't set up?? */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 775 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 776 | |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 777 | /* if fs is reusing a sb, just let its options stand... */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 778 | if (newsbsec->flags & SE_SBINITIALIZED) |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 779 | return; |
| 780 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 781 | mutex_lock(&newsbsec->lock); |
| 782 | |
| 783 | newsbsec->flags = oldsbsec->flags; |
| 784 | |
| 785 | newsbsec->sid = oldsbsec->sid; |
| 786 | newsbsec->def_sid = oldsbsec->def_sid; |
| 787 | newsbsec->behavior = oldsbsec->behavior; |
| 788 | |
| 789 | if (set_context) { |
| 790 | u32 sid = oldsbsec->mntpoint_sid; |
| 791 | |
| 792 | if (!set_fscontext) |
| 793 | newsbsec->sid = sid; |
| 794 | if (!set_rootcontext) { |
| 795 | struct inode *newinode = newsb->s_root->d_inode; |
| 796 | struct inode_security_struct *newisec = newinode->i_security; |
| 797 | newisec->sid = sid; |
| 798 | } |
| 799 | newsbsec->mntpoint_sid = sid; |
| 800 | } |
| 801 | if (set_rootcontext) { |
| 802 | const struct inode *oldinode = oldsb->s_root->d_inode; |
| 803 | const struct inode_security_struct *oldisec = oldinode->i_security; |
| 804 | struct inode *newinode = newsb->s_root->d_inode; |
| 805 | struct inode_security_struct *newisec = newinode->i_security; |
| 806 | |
| 807 | newisec->sid = oldisec->sid; |
| 808 | } |
| 809 | |
| 810 | sb_finish_set_opts(newsb); |
| 811 | mutex_unlock(&newsbsec->lock); |
| 812 | } |
| 813 | |
Adrian Bunk | 2e1479d | 2008-03-17 22:29:23 +0200 | [diff] [blame] | 814 | static int selinux_parse_opts_str(char *options, |
| 815 | struct security_mnt_opts *opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 816 | { |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 817 | char *p; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 818 | char *context = NULL, *defcontext = NULL; |
| 819 | char *fscontext = NULL, *rootcontext = NULL; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 820 | int rc, num_mnt_opts = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 821 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 822 | opts->num_mnt_opts = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 823 | |
| 824 | /* Standard string-based options. */ |
| 825 | while ((p = strsep(&options, "|")) != NULL) { |
| 826 | int token; |
| 827 | substring_t args[MAX_OPT_ARGS]; |
| 828 | |
| 829 | if (!*p) |
| 830 | continue; |
| 831 | |
| 832 | token = match_token(p, tokens, args); |
| 833 | |
| 834 | switch (token) { |
| 835 | case Opt_context: |
| 836 | if (context || defcontext) { |
| 837 | rc = -EINVAL; |
| 838 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 839 | goto out_err; |
| 840 | } |
| 841 | context = match_strdup(&args[0]); |
| 842 | if (!context) { |
| 843 | rc = -ENOMEM; |
| 844 | goto out_err; |
| 845 | } |
| 846 | break; |
| 847 | |
| 848 | case Opt_fscontext: |
| 849 | if (fscontext) { |
| 850 | rc = -EINVAL; |
| 851 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 852 | goto out_err; |
| 853 | } |
| 854 | fscontext = match_strdup(&args[0]); |
| 855 | if (!fscontext) { |
| 856 | rc = -ENOMEM; |
| 857 | goto out_err; |
| 858 | } |
| 859 | break; |
| 860 | |
| 861 | case Opt_rootcontext: |
| 862 | if (rootcontext) { |
| 863 | rc = -EINVAL; |
| 864 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 865 | goto out_err; |
| 866 | } |
| 867 | rootcontext = match_strdup(&args[0]); |
| 868 | if (!rootcontext) { |
| 869 | rc = -ENOMEM; |
| 870 | goto out_err; |
| 871 | } |
| 872 | break; |
| 873 | |
| 874 | case Opt_defcontext: |
| 875 | if (context || defcontext) { |
| 876 | rc = -EINVAL; |
| 877 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 878 | goto out_err; |
| 879 | } |
| 880 | defcontext = match_strdup(&args[0]); |
| 881 | if (!defcontext) { |
| 882 | rc = -ENOMEM; |
| 883 | goto out_err; |
| 884 | } |
| 885 | break; |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 886 | case Opt_labelsupport: |
| 887 | break; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 888 | default: |
| 889 | rc = -EINVAL; |
| 890 | printk(KERN_WARNING "SELinux: unknown mount option\n"); |
| 891 | goto out_err; |
| 892 | |
| 893 | } |
| 894 | } |
| 895 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 896 | rc = -ENOMEM; |
| 897 | opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); |
| 898 | if (!opts->mnt_opts) |
| 899 | goto out_err; |
| 900 | |
| 901 | opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); |
| 902 | if (!opts->mnt_opts_flags) { |
| 903 | kfree(opts->mnt_opts); |
| 904 | goto out_err; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 905 | } |
| 906 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 907 | if (fscontext) { |
| 908 | opts->mnt_opts[num_mnt_opts] = fscontext; |
| 909 | opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; |
| 910 | } |
| 911 | if (context) { |
| 912 | opts->mnt_opts[num_mnt_opts] = context; |
| 913 | opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; |
| 914 | } |
| 915 | if (rootcontext) { |
| 916 | opts->mnt_opts[num_mnt_opts] = rootcontext; |
| 917 | opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; |
| 918 | } |
| 919 | if (defcontext) { |
| 920 | opts->mnt_opts[num_mnt_opts] = defcontext; |
| 921 | opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; |
| 922 | } |
| 923 | |
| 924 | opts->num_mnt_opts = num_mnt_opts; |
| 925 | return 0; |
| 926 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 927 | out_err: |
| 928 | kfree(context); |
| 929 | kfree(defcontext); |
| 930 | kfree(fscontext); |
| 931 | kfree(rootcontext); |
| 932 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 934 | /* |
| 935 | * string mount options parsing and call set the sbsec |
| 936 | */ |
| 937 | static int superblock_doinit(struct super_block *sb, void *data) |
| 938 | { |
| 939 | int rc = 0; |
| 940 | char *options = data; |
| 941 | struct security_mnt_opts opts; |
| 942 | |
| 943 | security_init_mnt_opts(&opts); |
| 944 | |
| 945 | if (!data) |
| 946 | goto out; |
| 947 | |
| 948 | BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); |
| 949 | |
| 950 | rc = selinux_parse_opts_str(options, &opts); |
| 951 | if (rc) |
| 952 | goto out_err; |
| 953 | |
| 954 | out: |
| 955 | rc = selinux_set_mnt_opts(sb, &opts); |
| 956 | |
| 957 | out_err: |
| 958 | security_free_mnt_opts(&opts); |
| 959 | return rc; |
| 960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Adrian Bunk | 3583a71 | 2008-07-22 20:21:23 +0300 | [diff] [blame] | 962 | static void selinux_write_opts(struct seq_file *m, |
| 963 | struct security_mnt_opts *opts) |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 964 | { |
| 965 | int i; |
| 966 | char *prefix; |
| 967 | |
| 968 | for (i = 0; i < opts->num_mnt_opts; i++) { |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 969 | char *has_comma; |
| 970 | |
| 971 | if (opts->mnt_opts[i]) |
| 972 | has_comma = strchr(opts->mnt_opts[i], ','); |
| 973 | else |
| 974 | has_comma = NULL; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 975 | |
| 976 | switch (opts->mnt_opts_flags[i]) { |
| 977 | case CONTEXT_MNT: |
| 978 | prefix = CONTEXT_STR; |
| 979 | break; |
| 980 | case FSCONTEXT_MNT: |
| 981 | prefix = FSCONTEXT_STR; |
| 982 | break; |
| 983 | case ROOTCONTEXT_MNT: |
| 984 | prefix = ROOTCONTEXT_STR; |
| 985 | break; |
| 986 | case DEFCONTEXT_MNT: |
| 987 | prefix = DEFCONTEXT_STR; |
| 988 | break; |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 989 | case SE_SBLABELSUPP: |
| 990 | seq_putc(m, ','); |
| 991 | seq_puts(m, LABELSUPP_STR); |
| 992 | continue; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 993 | default: |
| 994 | BUG(); |
Eric Paris | a35c6c8 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 995 | return; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 996 | }; |
| 997 | /* we need a comma before each option */ |
| 998 | seq_putc(m, ','); |
| 999 | seq_puts(m, prefix); |
| 1000 | if (has_comma) |
| 1001 | seq_putc(m, '\"'); |
| 1002 | seq_puts(m, opts->mnt_opts[i]); |
| 1003 | if (has_comma) |
| 1004 | seq_putc(m, '\"'); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1009 | { |
| 1010 | struct security_mnt_opts opts; |
| 1011 | int rc; |
| 1012 | |
| 1013 | rc = selinux_get_mnt_opts(sb, &opts); |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1014 | if (rc) { |
| 1015 | /* before policy load we may get EINVAL, don't show anything */ |
| 1016 | if (rc == -EINVAL) |
| 1017 | rc = 0; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1018 | return rc; |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1019 | } |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1020 | |
| 1021 | selinux_write_opts(m, &opts); |
| 1022 | |
| 1023 | security_free_mnt_opts(&opts); |
| 1024 | |
| 1025 | return rc; |
| 1026 | } |
| 1027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | static inline u16 inode_mode_to_security_class(umode_t mode) |
| 1029 | { |
| 1030 | switch (mode & S_IFMT) { |
| 1031 | case S_IFSOCK: |
| 1032 | return SECCLASS_SOCK_FILE; |
| 1033 | case S_IFLNK: |
| 1034 | return SECCLASS_LNK_FILE; |
| 1035 | case S_IFREG: |
| 1036 | return SECCLASS_FILE; |
| 1037 | case S_IFBLK: |
| 1038 | return SECCLASS_BLK_FILE; |
| 1039 | case S_IFDIR: |
| 1040 | return SECCLASS_DIR; |
| 1041 | case S_IFCHR: |
| 1042 | return SECCLASS_CHR_FILE; |
| 1043 | case S_IFIFO: |
| 1044 | return SECCLASS_FIFO_FILE; |
| 1045 | |
| 1046 | } |
| 1047 | |
| 1048 | return SECCLASS_FILE; |
| 1049 | } |
| 1050 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1051 | static inline int default_protocol_stream(int protocol) |
| 1052 | { |
| 1053 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); |
| 1054 | } |
| 1055 | |
| 1056 | static inline int default_protocol_dgram(int protocol) |
| 1057 | { |
| 1058 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); |
| 1059 | } |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
| 1062 | { |
| 1063 | switch (family) { |
| 1064 | case PF_UNIX: |
| 1065 | switch (type) { |
| 1066 | case SOCK_STREAM: |
| 1067 | case SOCK_SEQPACKET: |
| 1068 | return SECCLASS_UNIX_STREAM_SOCKET; |
| 1069 | case SOCK_DGRAM: |
| 1070 | return SECCLASS_UNIX_DGRAM_SOCKET; |
| 1071 | } |
| 1072 | break; |
| 1073 | case PF_INET: |
| 1074 | case PF_INET6: |
| 1075 | switch (type) { |
| 1076 | case SOCK_STREAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1077 | if (default_protocol_stream(protocol)) |
| 1078 | return SECCLASS_TCP_SOCKET; |
| 1079 | else |
| 1080 | return SECCLASS_RAWIP_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | case SOCK_DGRAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1082 | if (default_protocol_dgram(protocol)) |
| 1083 | return SECCLASS_UDP_SOCKET; |
| 1084 | else |
| 1085 | return SECCLASS_RAWIP_SOCKET; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 1086 | case SOCK_DCCP: |
| 1087 | return SECCLASS_DCCP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1088 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | return SECCLASS_RAWIP_SOCKET; |
| 1090 | } |
| 1091 | break; |
| 1092 | case PF_NETLINK: |
| 1093 | switch (protocol) { |
| 1094 | case NETLINK_ROUTE: |
| 1095 | return SECCLASS_NETLINK_ROUTE_SOCKET; |
| 1096 | case NETLINK_FIREWALL: |
| 1097 | return SECCLASS_NETLINK_FIREWALL_SOCKET; |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1098 | case NETLINK_SOCK_DIAG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
| 1100 | case NETLINK_NFLOG: |
| 1101 | return SECCLASS_NETLINK_NFLOG_SOCKET; |
| 1102 | case NETLINK_XFRM: |
| 1103 | return SECCLASS_NETLINK_XFRM_SOCKET; |
| 1104 | case NETLINK_SELINUX: |
| 1105 | return SECCLASS_NETLINK_SELINUX_SOCKET; |
| 1106 | case NETLINK_AUDIT: |
| 1107 | return SECCLASS_NETLINK_AUDIT_SOCKET; |
| 1108 | case NETLINK_IP6_FW: |
| 1109 | return SECCLASS_NETLINK_IP6FW_SOCKET; |
| 1110 | case NETLINK_DNRTMSG: |
| 1111 | return SECCLASS_NETLINK_DNRT_SOCKET; |
James Morris | 0c9b794 | 2005-04-16 15:24:13 -0700 | [diff] [blame] | 1112 | case NETLINK_KOBJECT_UEVENT: |
| 1113 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | default: |
| 1115 | return SECCLASS_NETLINK_SOCKET; |
| 1116 | } |
| 1117 | case PF_PACKET: |
| 1118 | return SECCLASS_PACKET_SOCKET; |
| 1119 | case PF_KEY: |
| 1120 | return SECCLASS_KEY_SOCKET; |
Christopher J. PeBenito | 3e3ff15 | 2006-06-09 00:25:03 -0700 | [diff] [blame] | 1121 | case PF_APPLETALK: |
| 1122 | return SECCLASS_APPLETALK_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | return SECCLASS_SOCKET; |
| 1126 | } |
| 1127 | |
| 1128 | #ifdef CONFIG_PROC_FS |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1129 | static int selinux_proc_get_sid(struct dentry *dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | u16 tclass, |
| 1131 | u32 *sid) |
| 1132 | { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1133 | int rc; |
| 1134 | char *buffer, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1136 | buffer = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | if (!buffer) |
| 1138 | return -ENOMEM; |
| 1139 | |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1140 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
| 1141 | if (IS_ERR(path)) |
| 1142 | rc = PTR_ERR(path); |
| 1143 | else { |
| 1144 | /* each process gets a /proc/PID/ entry. Strip off the |
| 1145 | * PID part to get a valid selinux labeling. |
| 1146 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ |
| 1147 | while (path[1] >= '0' && path[1] <= '9') { |
| 1148 | path[1] = '/'; |
| 1149 | path++; |
| 1150 | } |
| 1151 | rc = security_genfs_sid("proc", path, tclass, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | free_page((unsigned long)buffer); |
| 1154 | return rc; |
| 1155 | } |
| 1156 | #else |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1157 | static int selinux_proc_get_sid(struct dentry *dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | u16 tclass, |
| 1159 | u32 *sid) |
| 1160 | { |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | #endif |
| 1164 | |
| 1165 | /* The inode's security attributes must be initialized before first use. */ |
| 1166 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1167 | { |
| 1168 | struct superblock_security_struct *sbsec = NULL; |
| 1169 | struct inode_security_struct *isec = inode->i_security; |
| 1170 | u32 sid; |
| 1171 | struct dentry *dentry; |
| 1172 | #define INITCONTEXTLEN 255 |
| 1173 | char *context = NULL; |
| 1174 | unsigned len = 0; |
| 1175 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
| 1177 | if (isec->initialized) |
| 1178 | goto out; |
| 1179 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1180 | mutex_lock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | if (isec->initialized) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1182 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
| 1184 | sbsec = inode->i_sb->s_security; |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1185 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | /* Defer initialization until selinux_complete_init, |
| 1187 | after the initial policy is loaded and the security |
| 1188 | server is ready to handle calls. */ |
| 1189 | spin_lock(&sbsec->isec_lock); |
| 1190 | if (list_empty(&isec->list)) |
| 1191 | list_add(&isec->list, &sbsec->isec_head); |
| 1192 | spin_unlock(&sbsec->isec_lock); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1193 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | switch (sbsec->behavior) { |
| 1197 | case SECURITY_FS_USE_XATTR: |
| 1198 | if (!inode->i_op->getxattr) { |
| 1199 | isec->sid = sbsec->def_sid; |
| 1200 | break; |
| 1201 | } |
| 1202 | |
| 1203 | /* Need a dentry, since the xattr API requires one. |
| 1204 | Life would be simpler if we could just pass the inode. */ |
| 1205 | if (opt_dentry) { |
| 1206 | /* Called from d_instantiate or d_splice_alias. */ |
| 1207 | dentry = dget(opt_dentry); |
| 1208 | } else { |
| 1209 | /* Called from selinux_complete_init, try to find a dentry. */ |
| 1210 | dentry = d_find_alias(inode); |
| 1211 | } |
| 1212 | if (!dentry) { |
Eric Paris | df7f54c | 2009-03-09 14:35:58 -0400 | [diff] [blame] | 1213 | /* |
| 1214 | * this is can be hit on boot when a file is accessed |
| 1215 | * before the policy is loaded. When we load policy we |
| 1216 | * may find inodes that have no dentry on the |
| 1217 | * sbsec->isec_head list. No reason to complain as these |
| 1218 | * will get fixed up the next time we go through |
| 1219 | * inode_doinit with a dentry, before these inodes could |
| 1220 | * be used again by userspace. |
| 1221 | */ |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1222 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | len = INITCONTEXTLEN; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1226 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | if (!context) { |
| 1228 | rc = -ENOMEM; |
| 1229 | dput(dentry); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1230 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1232 | context[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, |
| 1234 | context, len); |
| 1235 | if (rc == -ERANGE) { |
James Morris | 314dabb | 2009-08-10 22:00:13 +1000 | [diff] [blame] | 1236 | kfree(context); |
| 1237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | /* Need a larger buffer. Query for the right size. */ |
| 1239 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, |
| 1240 | NULL, 0); |
| 1241 | if (rc < 0) { |
| 1242 | dput(dentry); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1243 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | len = rc; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1246 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | if (!context) { |
| 1248 | rc = -ENOMEM; |
| 1249 | dput(dentry); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1250 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1252 | context[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | rc = inode->i_op->getxattr(dentry, |
| 1254 | XATTR_NAME_SELINUX, |
| 1255 | context, len); |
| 1256 | } |
| 1257 | dput(dentry); |
| 1258 | if (rc < 0) { |
| 1259 | if (rc != -ENODATA) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1260 | printk(KERN_WARNING "SELinux: %s: getxattr returned " |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 1261 | "%d for dev=%s ino=%ld\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | -rc, inode->i_sb->s_id, inode->i_ino); |
| 1263 | kfree(context); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1264 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | /* Map ENODATA to the default file SID */ |
| 1267 | sid = sbsec->def_sid; |
| 1268 | rc = 0; |
| 1269 | } else { |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1270 | rc = security_context_to_sid_default(context, rc, &sid, |
Stephen Smalley | 869ab51 | 2008-04-04 08:46:05 -0400 | [diff] [blame] | 1271 | sbsec->def_sid, |
| 1272 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | if (rc) { |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1274 | char *dev = inode->i_sb->s_id; |
| 1275 | unsigned long ino = inode->i_ino; |
| 1276 | |
| 1277 | if (rc == -EINVAL) { |
| 1278 | if (printk_ratelimit()) |
| 1279 | printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " |
| 1280 | "context=%s. This indicates you may need to relabel the inode or the " |
| 1281 | "filesystem in question.\n", ino, dev, context); |
| 1282 | } else { |
| 1283 | printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " |
| 1284 | "returned %d for dev=%s ino=%ld\n", |
| 1285 | __func__, context, -rc, dev, ino); |
| 1286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | kfree(context); |
| 1288 | /* Leave with the unlabeled SID */ |
| 1289 | rc = 0; |
| 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | kfree(context); |
| 1294 | isec->sid = sid; |
| 1295 | break; |
| 1296 | case SECURITY_FS_USE_TASK: |
| 1297 | isec->sid = isec->task_sid; |
| 1298 | break; |
| 1299 | case SECURITY_FS_USE_TRANS: |
| 1300 | /* Default to the fs SID. */ |
| 1301 | isec->sid = sbsec->sid; |
| 1302 | |
| 1303 | /* Try to obtain a transition SID. */ |
| 1304 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 1305 | rc = security_transition_sid(isec->task_sid, sbsec->sid, |
| 1306 | isec->sclass, NULL, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | if (rc) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1308 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | isec->sid = sid; |
| 1310 | break; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1311 | case SECURITY_FS_USE_MNTPOINT: |
| 1312 | isec->sid = sbsec->mntpoint_sid; |
| 1313 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | default: |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1315 | /* Default to the fs superblock SID. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | isec->sid = sbsec->sid; |
| 1317 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1318 | if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1319 | if (opt_dentry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1321 | rc = selinux_proc_get_sid(opt_dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | isec->sclass, |
| 1323 | &sid); |
| 1324 | if (rc) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1325 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | isec->sid = sid; |
| 1327 | } |
| 1328 | } |
| 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | isec->initialized = 1; |
| 1333 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1334 | out_unlock: |
| 1335 | mutex_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | out: |
| 1337 | if (isec->sclass == SECCLASS_FILE) |
| 1338 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | return rc; |
| 1340 | } |
| 1341 | |
| 1342 | /* Convert a Linux signal to an access vector. */ |
| 1343 | static inline u32 signal_to_av(int sig) |
| 1344 | { |
| 1345 | u32 perm = 0; |
| 1346 | |
| 1347 | switch (sig) { |
| 1348 | case SIGCHLD: |
| 1349 | /* Commonly granted from child to parent. */ |
| 1350 | perm = PROCESS__SIGCHLD; |
| 1351 | break; |
| 1352 | case SIGKILL: |
| 1353 | /* Cannot be caught or ignored */ |
| 1354 | perm = PROCESS__SIGKILL; |
| 1355 | break; |
| 1356 | case SIGSTOP: |
| 1357 | /* Cannot be caught or ignored */ |
| 1358 | perm = PROCESS__SIGSTOP; |
| 1359 | break; |
| 1360 | default: |
| 1361 | /* All other signals. */ |
| 1362 | perm = PROCESS__SIGNAL; |
| 1363 | break; |
| 1364 | } |
| 1365 | |
| 1366 | return perm; |
| 1367 | } |
| 1368 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1369 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1370 | * Check permission between a pair of credentials |
| 1371 | * fork check, ptrace check, etc. |
| 1372 | */ |
| 1373 | static int cred_has_perm(const struct cred *actor, |
| 1374 | const struct cred *target, |
| 1375 | u32 perms) |
| 1376 | { |
| 1377 | u32 asid = cred_sid(actor), tsid = cred_sid(target); |
| 1378 | |
| 1379 | return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); |
| 1380 | } |
| 1381 | |
| 1382 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1383 | * Check permission between a pair of tasks, e.g. signal checks, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1384 | * fork check, ptrace check, etc. |
| 1385 | * tsk1 is the actor and tsk2 is the target |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1386 | * - this uses the default subjective creds of tsk1 |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1387 | */ |
| 1388 | static int task_has_perm(const struct task_struct *tsk1, |
| 1389 | const struct task_struct *tsk2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | u32 perms) |
| 1391 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1392 | const struct task_security_struct *__tsec1, *__tsec2; |
| 1393 | u32 sid1, sid2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1395 | rcu_read_lock(); |
| 1396 | __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; |
| 1397 | __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; |
| 1398 | rcu_read_unlock(); |
| 1399 | return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1402 | /* |
| 1403 | * Check permission between current and another task, e.g. signal checks, |
| 1404 | * fork check, ptrace check, etc. |
| 1405 | * current is the actor and tsk2 is the target |
| 1406 | * - this uses current's subjective creds |
| 1407 | */ |
| 1408 | static int current_has_perm(const struct task_struct *tsk, |
| 1409 | u32 perms) |
| 1410 | { |
| 1411 | u32 sid, tsid; |
| 1412 | |
| 1413 | sid = current_sid(); |
| 1414 | tsid = task_sid(tsk); |
| 1415 | return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); |
| 1416 | } |
| 1417 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1418 | #if CAP_LAST_CAP > 63 |
| 1419 | #error Fix SELinux to handle capabilities > 63. |
| 1420 | #endif |
| 1421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | /* Check whether a task is allowed to use a capability. */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1423 | static int cred_has_capability(const struct cred *cred, |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1424 | int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1426 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1427 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1428 | struct av_decision avd; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1429 | u16 sclass; |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 1430 | u32 sid = cred_sid(cred); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1431 | u32 av = CAP_TO_MASK(cap); |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1432 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1434 | COMMON_AUDIT_DATA_INIT(&ad, CAP); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1435 | ad.selinux_audit_data = &sad; |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1436 | ad.tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | ad.u.cap = cap; |
| 1438 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1439 | switch (CAP_TO_INDEX(cap)) { |
| 1440 | case 0: |
| 1441 | sclass = SECCLASS_CAPABILITY; |
| 1442 | break; |
| 1443 | case 1: |
| 1444 | sclass = SECCLASS_CAPABILITY2; |
| 1445 | break; |
| 1446 | default: |
| 1447 | printk(KERN_ERR |
| 1448 | "SELinux: out of range capability %d\n", cap); |
| 1449 | BUG(); |
Eric Paris | a35c6c8 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1450 | return -EINVAL; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1451 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1452 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1453 | rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1454 | if (audit == SECURITY_CAP_AUDIT) { |
| 1455 | int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0); |
| 1456 | if (rc2) |
| 1457 | return rc2; |
| 1458 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1459 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* Check whether a task is allowed to use a system operation. */ |
| 1463 | static int task_has_system(struct task_struct *tsk, |
| 1464 | u32 perms) |
| 1465 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1466 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1468 | return avc_has_perm(sid, SECINITSID_KERNEL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | SECCLASS_SYSTEM, perms, NULL); |
| 1470 | } |
| 1471 | |
| 1472 | /* Check whether a task has a particular permission to an inode. |
| 1473 | The 'adp' parameter is optional and allows other audit |
| 1474 | data to be passed (e.g. the dentry). */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1475 | static int inode_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | struct inode *inode, |
| 1477 | u32 perms, |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1478 | struct common_audit_data *adp, |
| 1479 | unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1482 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 1484 | validate_creds(cred); |
| 1485 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1486 | if (unlikely(IS_PRIVATE(inode))) |
Stephen Smalley | bbaca6c | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 1487 | return 0; |
| 1488 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1489 | sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | isec = inode->i_security; |
| 1491 | |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1492 | return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 1495 | static int inode_has_perm_noadp(const struct cred *cred, |
| 1496 | struct inode *inode, |
| 1497 | u32 perms, |
| 1498 | unsigned flags) |
| 1499 | { |
| 1500 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1501 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 1502 | |
| 1503 | COMMON_AUDIT_DATA_INIT(&ad, INODE); |
| 1504 | ad.u.inode = inode; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1505 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 1506 | return inode_has_perm(cred, inode, perms, &ad, flags); |
| 1507 | } |
| 1508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1510 | the dentry to help the auditing code to more easily generate the |
| 1511 | pathname if needed. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1512 | static inline int dentry_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | struct dentry *dentry, |
| 1514 | u32 av) |
| 1515 | { |
| 1516 | struct inode *inode = dentry->d_inode; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1517 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1518 | struct selinux_audit_data sad = {0,}; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1519 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1520 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
| 1521 | ad.u.dentry = dentry; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1522 | ad.selinux_audit_data = &sad; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1523 | return inode_has_perm(cred, inode, av, &ad, 0); |
| 1524 | } |
| 1525 | |
| 1526 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1527 | the path to help the auditing code to more easily generate the |
| 1528 | pathname if needed. */ |
| 1529 | static inline int path_has_perm(const struct cred *cred, |
| 1530 | struct path *path, |
| 1531 | u32 av) |
| 1532 | { |
| 1533 | struct inode *inode = path->dentry->d_inode; |
| 1534 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1535 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1536 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1537 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1538 | ad.u.path = *path; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1539 | ad.selinux_audit_data = &sad; |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1540 | return inode_has_perm(cred, inode, av, &ad, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /* Check whether a task can use an open file descriptor to |
| 1544 | access an inode in a given way. Check access to the |
| 1545 | descriptor itself, and then use dentry_has_perm to |
| 1546 | check a particular permission to the file. |
| 1547 | Access to the descriptor is implicitly granted if it |
| 1548 | has the same SID as the process. If av is zero, then |
| 1549 | access to the file is not checked, e.g. for cases |
| 1550 | where only the descriptor is affected like seek. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1551 | static int file_has_perm(const struct cred *cred, |
| 1552 | struct file *file, |
| 1553 | u32 av) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | struct file_security_struct *fsec = file->f_security; |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1556 | struct inode *inode = file->f_path.dentry->d_inode; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1557 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1558 | struct selinux_audit_data sad = {0,}; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1559 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | int rc; |
| 1561 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1562 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
| 1563 | ad.u.path = file->f_path; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1564 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1566 | if (sid != fsec->sid) { |
| 1567 | rc = avc_has_perm(sid, fsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | SECCLASS_FD, |
| 1569 | FD__USE, |
| 1570 | &ad); |
| 1571 | if (rc) |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1572 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* av is zero if only checking access to the descriptor. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1576 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | if (av) |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1578 | rc = inode_has_perm(cred, inode, av, &ad, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1580 | out: |
| 1581 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* Check whether a task can create a file. */ |
| 1585 | static int may_create(struct inode *dir, |
| 1586 | struct dentry *dentry, |
| 1587 | u16 tclass) |
| 1588 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 1589 | const struct task_security_struct *tsec = current_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | struct inode_security_struct *dsec; |
| 1591 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1592 | u32 sid, newsid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1593 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1594 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | int rc; |
| 1596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | dsec = dir->i_security; |
| 1598 | sbsec = dir->i_sb->s_security; |
| 1599 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1600 | sid = tsec->sid; |
| 1601 | newsid = tsec->create_sid; |
| 1602 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1603 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
| 1604 | ad.u.dentry = dentry; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1605 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1607 | rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | DIR__ADD_NAME | DIR__SEARCH, |
| 1609 | &ad); |
| 1610 | if (rc) |
| 1611 | return rc; |
| 1612 | |
David P. Quigley | cd89596 | 2009-01-16 09:22:04 -0500 | [diff] [blame] | 1613 | if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { |
Eric Paris | cb1e922 | 2011-04-28 15:11:21 -0400 | [diff] [blame] | 1614 | rc = security_transition_sid(sid, dsec->sid, tclass, |
| 1615 | &dentry->d_name, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | if (rc) |
| 1617 | return rc; |
| 1618 | } |
| 1619 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1620 | rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | if (rc) |
| 1622 | return rc; |
| 1623 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 1624 | rc = avc_has_perm(newsid, sbsec->sid, |
| 1625 | SECCLASS_FILESYSTEM, |
| 1626 | FILESYSTEM__ASSOCIATE, &ad); |
| 1627 | if (rc) |
| 1628 | return rc; |
| 1629 | |
| 1630 | rc = pft_inode_mknod(dir, dentry, 0, 0); |
| 1631 | |
| 1632 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1635 | /* Check whether a task can create a key. */ |
| 1636 | static int may_create_key(u32 ksid, |
| 1637 | struct task_struct *ctx) |
| 1638 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1639 | u32 sid = task_sid(ctx); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1640 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1641 | return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1644 | #define MAY_LINK 0 |
| 1645 | #define MAY_UNLINK 1 |
| 1646 | #define MAY_RMDIR 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
| 1648 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ |
| 1649 | static int may_link(struct inode *dir, |
| 1650 | struct dentry *dentry, |
| 1651 | int kind) |
| 1652 | |
| 1653 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | struct inode_security_struct *dsec, *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1655 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1656 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1657 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | u32 av; |
| 1659 | int rc; |
| 1660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | dsec = dir->i_security; |
| 1662 | isec = dentry->d_inode->i_security; |
| 1663 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1664 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
| 1665 | ad.u.dentry = dentry; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1666 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | |
| 1668 | av = DIR__SEARCH; |
| 1669 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1670 | rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | if (rc) |
| 1672 | return rc; |
| 1673 | |
| 1674 | switch (kind) { |
| 1675 | case MAY_LINK: |
| 1676 | av = FILE__LINK; |
| 1677 | break; |
| 1678 | case MAY_UNLINK: |
| 1679 | av = FILE__UNLINK; |
| 1680 | break; |
| 1681 | case MAY_RMDIR: |
| 1682 | av = DIR__RMDIR; |
| 1683 | break; |
| 1684 | default: |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1685 | printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", |
| 1686 | __func__, kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
| 1689 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1690 | rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 1691 | if (rc) |
| 1692 | return rc; |
| 1693 | |
| 1694 | if (kind == MAY_UNLINK) |
| 1695 | rc = pft_inode_unlink(dir, dentry); |
| 1696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | return rc; |
| 1698 | } |
| 1699 | |
| 1700 | static inline int may_rename(struct inode *old_dir, |
| 1701 | struct dentry *old_dentry, |
| 1702 | struct inode *new_dir, |
| 1703 | struct dentry *new_dentry) |
| 1704 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1706 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1707 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1708 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | u32 av; |
| 1710 | int old_is_dir, new_is_dir; |
| 1711 | int rc; |
| 1712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | old_dsec = old_dir->i_security; |
| 1714 | old_isec = old_dentry->d_inode->i_security; |
| 1715 | old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); |
| 1716 | new_dsec = new_dir->i_security; |
| 1717 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1718 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1719 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1721 | ad.u.dentry = old_dentry; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1722 | rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
| 1724 | if (rc) |
| 1725 | return rc; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1726 | rc = avc_has_perm(sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | old_isec->sclass, FILE__RENAME, &ad); |
| 1728 | if (rc) |
| 1729 | return rc; |
| 1730 | if (old_is_dir && new_dir != old_dir) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1731 | rc = avc_has_perm(sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | old_isec->sclass, DIR__REPARENT, &ad); |
| 1733 | if (rc) |
| 1734 | return rc; |
| 1735 | } |
| 1736 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1737 | ad.u.dentry = new_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | av = DIR__ADD_NAME | DIR__SEARCH; |
| 1739 | if (new_dentry->d_inode) |
| 1740 | av |= DIR__REMOVE_NAME; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1741 | rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | if (rc) |
| 1743 | return rc; |
| 1744 | if (new_dentry->d_inode) { |
| 1745 | new_isec = new_dentry->d_inode->i_security; |
| 1746 | new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1747 | rc = avc_has_perm(sid, new_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | new_isec->sclass, |
| 1749 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); |
| 1750 | if (rc) |
| 1751 | return rc; |
| 1752 | } |
| 1753 | |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | /* Check whether a task can perform a filesystem operation. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1758 | static int superblock_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | struct super_block *sb, |
| 1760 | u32 perms, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1761 | struct common_audit_data *ad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | struct superblock_security_struct *sbsec; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1764 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | sbsec = sb->s_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1767 | return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /* Convert a Linux mode and permission mask to an access vector. */ |
| 1771 | static inline u32 file_mask_to_av(int mode, int mask) |
| 1772 | { |
| 1773 | u32 av = 0; |
| 1774 | |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 1775 | if (!S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | if (mask & MAY_EXEC) |
| 1777 | av |= FILE__EXECUTE; |
| 1778 | if (mask & MAY_READ) |
| 1779 | av |= FILE__READ; |
| 1780 | |
| 1781 | if (mask & MAY_APPEND) |
| 1782 | av |= FILE__APPEND; |
| 1783 | else if (mask & MAY_WRITE) |
| 1784 | av |= FILE__WRITE; |
| 1785 | |
| 1786 | } else { |
| 1787 | if (mask & MAY_EXEC) |
| 1788 | av |= DIR__SEARCH; |
| 1789 | if (mask & MAY_WRITE) |
| 1790 | av |= DIR__WRITE; |
| 1791 | if (mask & MAY_READ) |
| 1792 | av |= DIR__READ; |
| 1793 | } |
| 1794 | |
| 1795 | return av; |
| 1796 | } |
| 1797 | |
| 1798 | /* Convert a Linux file to an access vector. */ |
| 1799 | static inline u32 file_to_av(struct file *file) |
| 1800 | { |
| 1801 | u32 av = 0; |
| 1802 | |
| 1803 | if (file->f_mode & FMODE_READ) |
| 1804 | av |= FILE__READ; |
| 1805 | if (file->f_mode & FMODE_WRITE) { |
| 1806 | if (file->f_flags & O_APPEND) |
| 1807 | av |= FILE__APPEND; |
| 1808 | else |
| 1809 | av |= FILE__WRITE; |
| 1810 | } |
Stephen Smalley | 0794c66 | 2008-03-17 08:55:18 -0400 | [diff] [blame] | 1811 | if (!av) { |
| 1812 | /* |
| 1813 | * Special file opened with flags 3 for ioctl-only use. |
| 1814 | */ |
| 1815 | av = FILE__IOCTL; |
| 1816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | |
| 1818 | return av; |
| 1819 | } |
| 1820 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1821 | /* |
| 1822 | * Convert a file to an access vector and include the correct open |
| 1823 | * open permission. |
| 1824 | */ |
| 1825 | static inline u32 open_file_to_av(struct file *file) |
| 1826 | { |
| 1827 | u32 av = file_to_av(file); |
| 1828 | |
Eric Paris | 49b7b8d | 2010-07-23 11:44:09 -0400 | [diff] [blame] | 1829 | if (selinux_policycap_openperm) |
| 1830 | av |= FILE__OPEN; |
| 1831 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1832 | return av; |
| 1833 | } |
| 1834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | /* Hook functions begin here. */ |
| 1836 | |
Stephen Smalley | 48a2370 | 2012-11-05 08:15:34 -0500 | [diff] [blame] | 1837 | static int selinux_binder_set_context_mgr(struct task_struct *mgr) |
| 1838 | { |
| 1839 | u32 mysid = current_sid(); |
| 1840 | u32 mgrsid = task_sid(mgr); |
| 1841 | |
| 1842 | return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL); |
| 1843 | } |
| 1844 | |
| 1845 | static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to) |
| 1846 | { |
| 1847 | u32 mysid = current_sid(); |
| 1848 | u32 fromsid = task_sid(from); |
| 1849 | u32 tosid = task_sid(to); |
| 1850 | int rc; |
| 1851 | |
| 1852 | if (mysid != fromsid) { |
| 1853 | rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL); |
| 1854 | if (rc) |
| 1855 | return rc; |
| 1856 | } |
| 1857 | |
| 1858 | return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL); |
| 1859 | } |
| 1860 | |
| 1861 | static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to) |
| 1862 | { |
| 1863 | u32 fromsid = task_sid(from); |
| 1864 | u32 tosid = task_sid(to); |
| 1865 | return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL); |
| 1866 | } |
| 1867 | |
| 1868 | static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file) |
| 1869 | { |
| 1870 | u32 sid = task_sid(to); |
| 1871 | struct file_security_struct *fsec = file->f_security; |
| 1872 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1873 | struct inode_security_struct *isec = inode->i_security; |
| 1874 | struct common_audit_data ad; |
| 1875 | struct selinux_audit_data sad = {0,}; |
| 1876 | int rc; |
| 1877 | |
| 1878 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
| 1879 | ad.u.path = file->f_path; |
| 1880 | ad.selinux_audit_data = &sad; |
| 1881 | |
| 1882 | if (sid != fsec->sid) { |
| 1883 | rc = avc_has_perm(sid, fsec->sid, |
| 1884 | SECCLASS_FD, |
| 1885 | FD__USE, |
| 1886 | &ad); |
| 1887 | if (rc) |
| 1888 | return rc; |
| 1889 | } |
| 1890 | |
| 1891 | if (unlikely(IS_PRIVATE(inode))) |
| 1892 | return 0; |
| 1893 | |
| 1894 | return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file), |
| 1895 | &ad); |
| 1896 | } |
| 1897 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 1898 | static int selinux_ptrace_access_check(struct task_struct *child, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1899 | unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | int rc; |
| 1902 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 1903 | rc = cap_ptrace_access_check(child, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | if (rc) |
| 1905 | return rc; |
| 1906 | |
Eric Paris | 69f594a | 2012-01-03 12:25:15 -0500 | [diff] [blame] | 1907 | if (mode & PTRACE_MODE_READ) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1908 | u32 sid = current_sid(); |
| 1909 | u32 csid = task_sid(child); |
| 1910 | return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 1911 | } |
| 1912 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1913 | return current_has_perm(child, PROCESS__PTRACE); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static int selinux_ptrace_traceme(struct task_struct *parent) |
| 1917 | { |
| 1918 | int rc; |
| 1919 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 1920 | rc = cap_ptrace_traceme(parent); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1921 | if (rc) |
| 1922 | return rc; |
| 1923 | |
| 1924 | return task_has_perm(parent, current, PROCESS__PTRACE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1928 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | { |
| 1930 | int error; |
| 1931 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1932 | error = current_has_perm(target, PROCESS__GETCAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | if (error) |
| 1934 | return error; |
| 1935 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 1936 | return cap_capget(target, effective, inheritable, permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1939 | static int selinux_capset(struct cred *new, const struct cred *old, |
| 1940 | const kernel_cap_t *effective, |
| 1941 | const kernel_cap_t *inheritable, |
| 1942 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | { |
| 1944 | int error; |
| 1945 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 1946 | error = cap_capset(new, old, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1947 | effective, inheritable, permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | if (error) |
| 1949 | return error; |
| 1950 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1951 | return cred_has_perm(old, new, PROCESS__SETCAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
James Morris | 5626d3e | 2009-01-30 10:05:06 +1100 | [diff] [blame] | 1954 | /* |
| 1955 | * (This comment used to live with the selinux_task_setuid hook, |
| 1956 | * which was removed). |
| 1957 | * |
| 1958 | * Since setuid only affects the current process, and since the SELinux |
| 1959 | * controls are not based on the Linux identity attributes, SELinux does not |
| 1960 | * need to control this operation. However, SELinux does control the use of |
| 1961 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. |
| 1962 | */ |
| 1963 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1964 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
| 1965 | int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | { |
| 1967 | int rc; |
| 1968 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1969 | rc = cap_capable(cred, ns, cap, audit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | if (rc) |
| 1971 | return rc; |
| 1972 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1973 | return cred_has_capability(cred, cap, audit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| 1977 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1978 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | int rc = 0; |
| 1980 | |
| 1981 | if (!sb) |
| 1982 | return 0; |
| 1983 | |
| 1984 | switch (cmds) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1985 | case Q_SYNC: |
| 1986 | case Q_QUOTAON: |
| 1987 | case Q_QUOTAOFF: |
| 1988 | case Q_SETINFO: |
| 1989 | case Q_SETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1990 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1991 | break; |
| 1992 | case Q_GETFMT: |
| 1993 | case Q_GETINFO: |
| 1994 | case Q_GETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1995 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1996 | break; |
| 1997 | default: |
| 1998 | rc = 0; /* let the kernel handle invalid cmds */ |
| 1999 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | } |
| 2001 | return rc; |
| 2002 | } |
| 2003 | |
| 2004 | static int selinux_quota_on(struct dentry *dentry) |
| 2005 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2006 | const struct cred *cred = current_cred(); |
| 2007 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2008 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 2011 | static int selinux_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | { |
| 2013 | int rc; |
| 2014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2016 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
| 2017 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2018 | rc = task_has_system(current, SYSTEM__SYSLOG_READ); |
| 2019 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2020 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
| 2021 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ |
| 2022 | /* Set level of messages printed to console */ |
| 2023 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2024 | rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); |
| 2025 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2026 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
| 2027 | case SYSLOG_ACTION_OPEN: /* Open log */ |
| 2028 | case SYSLOG_ACTION_READ: /* Read from log */ |
| 2029 | case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */ |
| 2030 | case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2031 | default: |
| 2032 | rc = task_has_system(current, SYSTEM__SYSLOG_MOD); |
| 2033 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | } |
| 2035 | return rc; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * Check that a process has enough memory to allocate a new virtual |
| 2040 | * mapping. 0 means there is enough memory for the allocation to |
| 2041 | * succeed and -ENOMEM implies there is not. |
| 2042 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | * Do not audit the selinux permission check, as this is applied to all |
| 2044 | * processes that allocate mappings. |
| 2045 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2046 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | { |
| 2048 | int rc, cap_sys_admin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2050 | rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 2051 | SECURITY_CAP_NOAUDIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | if (rc == 0) |
| 2053 | cap_sys_admin = 1; |
| 2054 | |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2055 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /* binprm security operations */ |
| 2059 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2060 | static int selinux_bprm_set_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2062 | const struct task_security_struct *old_tsec; |
| 2063 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | struct inode_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2065 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2066 | struct selinux_audit_data sad = {0,}; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2067 | struct inode *inode = bprm->file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | int rc; |
| 2069 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 2070 | rc = cap_bprm_set_creds(bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | if (rc) |
| 2072 | return rc; |
| 2073 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2074 | /* SELinux context only depends on initial program or script and not |
| 2075 | * the script interpreter */ |
| 2076 | if (bprm->cred_prepared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | return 0; |
| 2078 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2079 | old_tsec = current_security(); |
| 2080 | new_tsec = bprm->cred->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | isec = inode->i_security; |
| 2082 | |
| 2083 | /* Default to the current task SID. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2084 | new_tsec->sid = old_tsec->sid; |
| 2085 | new_tsec->osid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | |
Michael LeMay | 28eba5b | 2006-06-27 02:53:42 -0700 | [diff] [blame] | 2087 | /* Reset fs, key, and sock SIDs on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2088 | new_tsec->create_sid = 0; |
| 2089 | new_tsec->keycreate_sid = 0; |
| 2090 | new_tsec->sockcreate_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2092 | if (old_tsec->exec_sid) { |
| 2093 | new_tsec->sid = old_tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | /* Reset exec SID on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2095 | new_tsec->exec_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } else { |
| 2097 | /* Check for a default transition on this program. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2098 | rc = security_transition_sid(old_tsec->sid, isec->sid, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2099 | SECCLASS_PROCESS, NULL, |
| 2100 | &new_tsec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | if (rc) |
| 2102 | return rc; |
| 2103 | } |
| 2104 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2105 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2106 | ad.selinux_audit_data = &sad; |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2107 | ad.u.path = bprm->file->f_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 2109 | if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2110 | new_tsec->sid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2112 | if (new_tsec->sid == old_tsec->sid) { |
| 2113 | rc = avc_has_perm(old_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
| 2115 | if (rc) |
| 2116 | return rc; |
| 2117 | } else { |
| 2118 | /* Check permissions for the transition. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2119 | rc = avc_has_perm(old_tsec->sid, new_tsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
| 2121 | if (rc) |
| 2122 | return rc; |
| 2123 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2124 | rc = avc_has_perm(new_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
| 2126 | if (rc) |
| 2127 | return rc; |
| 2128 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2129 | /* Check for shared state */ |
| 2130 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
| 2131 | rc = avc_has_perm(old_tsec->sid, new_tsec->sid, |
| 2132 | SECCLASS_PROCESS, PROCESS__SHARE, |
| 2133 | NULL); |
| 2134 | if (rc) |
| 2135 | return -EPERM; |
| 2136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2138 | /* Make sure that anyone attempting to ptrace over a task that |
| 2139 | * changes its SID has the appropriate permit */ |
| 2140 | if (bprm->unsafe & |
| 2141 | (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { |
| 2142 | struct task_struct *tracer; |
| 2143 | struct task_security_struct *sec; |
| 2144 | u32 ptsid = 0; |
| 2145 | |
| 2146 | rcu_read_lock(); |
Tejun Heo | 06d9847 | 2011-06-17 16:50:40 +0200 | [diff] [blame] | 2147 | tracer = ptrace_parent(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2148 | if (likely(tracer != NULL)) { |
| 2149 | sec = __task_cred(tracer)->security; |
| 2150 | ptsid = sec->sid; |
| 2151 | } |
| 2152 | rcu_read_unlock(); |
| 2153 | |
| 2154 | if (ptsid != 0) { |
| 2155 | rc = avc_has_perm(ptsid, new_tsec->sid, |
| 2156 | SECCLASS_PROCESS, |
| 2157 | PROCESS__PTRACE, NULL); |
| 2158 | if (rc) |
| 2159 | return -EPERM; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | /* Clear any possibly unsafe personality bits on exec: */ |
| 2164 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | return 0; |
| 2168 | } |
| 2169 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2170 | static int selinux_bprm_secureexec(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 2172 | const struct task_security_struct *tsec = current_security(); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2173 | u32 sid, osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | int atsecure = 0; |
| 2175 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2176 | sid = tsec->sid; |
| 2177 | osid = tsec->osid; |
| 2178 | |
| 2179 | if (osid != sid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | /* Enable secure mode for SIDs transitions unless |
| 2181 | the noatsecure permission is granted between |
| 2182 | the two SIDs, i.e. ahp returns 0. */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2183 | atsecure = avc_has_perm(osid, sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2184 | SECCLASS_PROCESS, |
| 2185 | PROCESS__NOATSECURE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 2188 | return (atsecure || cap_bprm_secureexec(bprm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | /* Derived from fs/exec.c:flush_old_files. */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2192 | static inline void flush_unauthorized_files(const struct cred *cred, |
| 2193 | struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2195 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2196 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | struct file *file, *devnull = NULL; |
Stephen Smalley | b20c812 | 2006-09-25 23:32:03 -0700 | [diff] [blame] | 2198 | struct tty_struct *tty; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 2199 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | long j = -1; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2201 | int drop_tty = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2203 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | if (tty) { |
Nick Piggin | ee2ffa0 | 2010-08-18 04:37:35 +1000 | [diff] [blame] | 2205 | spin_lock(&tty_files_lock); |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2206 | if (!list_empty(&tty->tty_files)) { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2207 | struct tty_file_private *file_priv; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2208 | struct inode *inode; |
| 2209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | /* Revalidate access to controlling tty. |
| 2211 | Use inode_has_perm on the tty inode directly rather |
| 2212 | than using file_has_perm, as this particular open |
| 2213 | file may belong to another process and we are only |
| 2214 | interested in the inode-based check here. */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2215 | file_priv = list_first_entry(&tty->tty_files, |
| 2216 | struct tty_file_private, list); |
| 2217 | file = file_priv->file; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2218 | inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 2219 | if (inode_has_perm_noadp(cred, inode, |
| 2220 | FILE__READ | FILE__WRITE, 0)) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2221 | drop_tty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | } |
| 2223 | } |
Nick Piggin | ee2ffa0 | 2010-08-18 04:37:35 +1000 | [diff] [blame] | 2224 | spin_unlock(&tty_files_lock); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 2225 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | } |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 2227 | /* Reset controlling tty. */ |
| 2228 | if (drop_tty) |
| 2229 | no_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | |
| 2231 | /* Revalidate access to inherited open files. */ |
| 2232 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2233 | COMMON_AUDIT_DATA_INIT(&ad, INODE); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2234 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | |
| 2236 | spin_lock(&files->file_lock); |
| 2237 | for (;;) { |
| 2238 | unsigned long set, i; |
| 2239 | int fd; |
| 2240 | |
| 2241 | j++; |
| 2242 | i = j * __NFDBITS; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 2243 | fdt = files_fdtable(files); |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 2244 | if (i >= fdt->max_fds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | break; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 2246 | set = fdt->open_fds[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | if (!set) |
| 2248 | continue; |
| 2249 | spin_unlock(&files->file_lock); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2250 | for ( ; set ; i++, set >>= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | if (set & 1) { |
| 2252 | file = fget(i); |
| 2253 | if (!file) |
| 2254 | continue; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2255 | if (file_has_perm(cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | file, |
| 2257 | file_to_av(file))) { |
| 2258 | sys_close(i); |
| 2259 | fd = get_unused_fd(); |
| 2260 | if (fd != i) { |
| 2261 | if (fd >= 0) |
| 2262 | put_unused_fd(fd); |
| 2263 | fput(file); |
| 2264 | continue; |
| 2265 | } |
| 2266 | if (devnull) { |
Nick Piggin | 095975d | 2006-01-08 01:02:19 -0800 | [diff] [blame] | 2267 | get_file(devnull); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | } else { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2269 | devnull = dentry_open( |
| 2270 | dget(selinux_null), |
| 2271 | mntget(selinuxfs_mount), |
| 2272 | O_RDWR, cred); |
Akinobu Mita | fc5d81e | 2006-11-27 15:16:48 +0900 | [diff] [blame] | 2273 | if (IS_ERR(devnull)) { |
| 2274 | devnull = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | put_unused_fd(fd); |
| 2276 | fput(file); |
| 2277 | continue; |
| 2278 | } |
| 2279 | } |
| 2280 | fd_install(fd, devnull); |
| 2281 | } |
| 2282 | fput(file); |
| 2283 | } |
| 2284 | } |
| 2285 | spin_lock(&files->file_lock); |
| 2286 | |
| 2287 | } |
| 2288 | spin_unlock(&files->file_lock); |
| 2289 | } |
| 2290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2292 | * Prepare a process for imminent new credential changes due to exec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2294 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2296 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | struct rlimit *rlim, *initrlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | int rc, i; |
| 2299 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2300 | new_tsec = bprm->cred->security; |
| 2301 | if (new_tsec->sid == new_tsec->osid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | return; |
| 2303 | |
| 2304 | /* Close files for which the new task SID is not authorized. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2305 | flush_unauthorized_files(bprm->cred, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2307 | /* Always clear parent death signal on SID transitions. */ |
| 2308 | current->pdeath_signal = 0; |
| 2309 | |
| 2310 | /* Check whether the new SID can inherit resource limits from the old |
| 2311 | * SID. If not, reset all soft limits to the lower of the current |
| 2312 | * task's hard limit and the init task's soft limit. |
| 2313 | * |
| 2314 | * Note that the setting of hard limits (even to lower them) can be |
| 2315 | * controlled by the setrlimit check. The inclusion of the init task's |
| 2316 | * soft limit into the computation is to avoid resetting soft limits |
| 2317 | * higher than the default soft limit for cases where the default is |
| 2318 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. |
| 2319 | */ |
| 2320 | rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, |
| 2321 | PROCESS__RLIMITINH, NULL); |
| 2322 | if (rc) { |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2323 | /* protect against do_prlimit() */ |
| 2324 | task_lock(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2325 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 2326 | rlim = current->signal->rlim + i; |
| 2327 | initrlim = init_task.signal->rlim + i; |
| 2328 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); |
| 2329 | } |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2330 | task_unlock(current); |
| 2331 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Clean up the process immediately after the installation of new credentials |
| 2337 | * due to exec |
| 2338 | */ |
| 2339 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2340 | { |
| 2341 | const struct task_security_struct *tsec = current_security(); |
| 2342 | struct itimerval itimer; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2343 | u32 osid, sid; |
| 2344 | int rc, i; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2345 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2346 | osid = tsec->osid; |
| 2347 | sid = tsec->sid; |
| 2348 | |
| 2349 | if (sid == osid) |
| 2350 | return; |
| 2351 | |
| 2352 | /* Check whether the new SID can inherit signal state from the old SID. |
| 2353 | * If not, clear itimers to avoid subsequent signal generation and |
| 2354 | * flush and unblock signals. |
| 2355 | * |
| 2356 | * This must occur _after_ the task SID has been updated so that any |
| 2357 | * kill done after the flush will be checked against the new SID. |
| 2358 | */ |
| 2359 | rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | if (rc) { |
| 2361 | memset(&itimer, 0, sizeof itimer); |
| 2362 | for (i = 0; i < 3; i++) |
| 2363 | do_setitimer(i, &itimer, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | spin_lock_irq(¤t->sighand->siglock); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2365 | if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { |
| 2366 | __flush_signals(current); |
| 2367 | flush_signal_handlers(current, 1); |
| 2368 | sigemptyset(¤t->blocked); |
| 2369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | spin_unlock_irq(¤t->sighand->siglock); |
| 2371 | } |
| 2372 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2373 | /* Wake up the parent if it is waiting so that it can recheck |
| 2374 | * wait permission to the new task SID. */ |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2375 | read_lock(&tasklist_lock); |
Oleg Nesterov | 0b7570e | 2009-09-23 15:56:46 -0700 | [diff] [blame] | 2376 | __wake_up_parent(current, current->real_parent); |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2377 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /* superblock security operations */ |
| 2381 | |
| 2382 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2383 | { |
| 2384 | return superblock_alloc_security(sb); |
| 2385 | } |
| 2386 | |
| 2387 | static void selinux_sb_free_security(struct super_block *sb) |
| 2388 | { |
| 2389 | superblock_free_security(sb); |
| 2390 | } |
| 2391 | |
| 2392 | static inline int match_prefix(char *prefix, int plen, char *option, int olen) |
| 2393 | { |
| 2394 | if (plen > olen) |
| 2395 | return 0; |
| 2396 | |
| 2397 | return !memcmp(prefix, option, plen); |
| 2398 | } |
| 2399 | |
| 2400 | static inline int selinux_option(char *option, int len) |
| 2401 | { |
Eric Paris | 832cbd9 | 2008-04-01 13:24:09 -0400 | [diff] [blame] | 2402 | return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || |
| 2403 | match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || |
| 2404 | match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 2405 | match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || |
| 2406 | match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | static inline void take_option(char **to, char *from, int *first, int len) |
| 2410 | { |
| 2411 | if (!*first) { |
| 2412 | **to = ','; |
| 2413 | *to += 1; |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2414 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | *first = 0; |
| 2416 | memcpy(*to, from, len); |
| 2417 | *to += len; |
| 2418 | } |
| 2419 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2420 | static inline void take_selinux_option(char **to, char *from, int *first, |
| 2421 | int len) |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2422 | { |
| 2423 | int current_size = 0; |
| 2424 | |
| 2425 | if (!*first) { |
| 2426 | **to = '|'; |
| 2427 | *to += 1; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2428 | } else |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2429 | *first = 0; |
| 2430 | |
| 2431 | while (current_size < len) { |
| 2432 | if (*from != '"') { |
| 2433 | **to = *from; |
| 2434 | *to += 1; |
| 2435 | } |
| 2436 | from += 1; |
| 2437 | current_size += 1; |
| 2438 | } |
| 2439 | } |
| 2440 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 2441 | static int selinux_sb_copy_data(char *orig, char *copy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | { |
| 2443 | int fnosec, fsec, rc = 0; |
| 2444 | char *in_save, *in_curr, *in_end; |
| 2445 | char *sec_curr, *nosec_save, *nosec; |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2446 | int open_quote = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | |
| 2448 | in_curr = orig; |
| 2449 | sec_curr = copy; |
| 2450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | nosec = (char *)get_zeroed_page(GFP_KERNEL); |
| 2452 | if (!nosec) { |
| 2453 | rc = -ENOMEM; |
| 2454 | goto out; |
| 2455 | } |
| 2456 | |
| 2457 | nosec_save = nosec; |
| 2458 | fnosec = fsec = 1; |
| 2459 | in_save = in_end = orig; |
| 2460 | |
| 2461 | do { |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2462 | if (*in_end == '"') |
| 2463 | open_quote = !open_quote; |
| 2464 | if ((*in_end == ',' && open_quote == 0) || |
| 2465 | *in_end == '\0') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | int len = in_end - in_curr; |
| 2467 | |
| 2468 | if (selinux_option(in_curr, len)) |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2469 | take_selinux_option(&sec_curr, in_curr, &fsec, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | else |
| 2471 | take_option(&nosec, in_curr, &fnosec, len); |
| 2472 | |
| 2473 | in_curr = in_end + 1; |
| 2474 | } |
| 2475 | } while (*in_end++); |
| 2476 | |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 2477 | strcpy(in_save, nosec_save); |
Gerald Schaefer | da3caa2 | 2005-06-21 17:15:18 -0700 | [diff] [blame] | 2478 | free_page((unsigned long)nosec_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | out: |
| 2480 | return rc; |
| 2481 | } |
| 2482 | |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2483 | static int selinux_sb_remount(struct super_block *sb, void *data) |
| 2484 | { |
| 2485 | int rc, i, *flags; |
| 2486 | struct security_mnt_opts opts; |
| 2487 | char *secdata, **mount_options; |
| 2488 | struct superblock_security_struct *sbsec = sb->s_security; |
| 2489 | |
| 2490 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 2491 | return 0; |
| 2492 | |
| 2493 | if (!data) |
| 2494 | return 0; |
| 2495 | |
| 2496 | if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) |
| 2497 | return 0; |
| 2498 | |
| 2499 | security_init_mnt_opts(&opts); |
| 2500 | secdata = alloc_secdata(); |
| 2501 | if (!secdata) |
| 2502 | return -ENOMEM; |
| 2503 | rc = selinux_sb_copy_data(data, secdata); |
| 2504 | if (rc) |
| 2505 | goto out_free_secdata; |
| 2506 | |
| 2507 | rc = selinux_parse_opts_str(secdata, &opts); |
| 2508 | if (rc) |
| 2509 | goto out_free_secdata; |
| 2510 | |
| 2511 | mount_options = opts.mnt_opts; |
| 2512 | flags = opts.mnt_opts_flags; |
| 2513 | |
| 2514 | for (i = 0; i < opts.num_mnt_opts; i++) { |
| 2515 | u32 sid; |
| 2516 | size_t len; |
| 2517 | |
| 2518 | if (flags[i] == SE_SBLABELSUPP) |
| 2519 | continue; |
| 2520 | len = strlen(mount_options[i]); |
| 2521 | rc = security_context_to_sid(mount_options[i], len, &sid); |
| 2522 | if (rc) { |
| 2523 | printk(KERN_WARNING "SELinux: security_context_to_sid" |
| 2524 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 2525 | mount_options[i], sb->s_id, sb->s_type->name, rc); |
| 2526 | goto out_free_opts; |
| 2527 | } |
| 2528 | rc = -EINVAL; |
| 2529 | switch (flags[i]) { |
| 2530 | case FSCONTEXT_MNT: |
| 2531 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) |
| 2532 | goto out_bad_option; |
| 2533 | break; |
| 2534 | case CONTEXT_MNT: |
| 2535 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) |
| 2536 | goto out_bad_option; |
| 2537 | break; |
| 2538 | case ROOTCONTEXT_MNT: { |
| 2539 | struct inode_security_struct *root_isec; |
| 2540 | root_isec = sb->s_root->d_inode->i_security; |
| 2541 | |
| 2542 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) |
| 2543 | goto out_bad_option; |
| 2544 | break; |
| 2545 | } |
| 2546 | case DEFCONTEXT_MNT: |
| 2547 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) |
| 2548 | goto out_bad_option; |
| 2549 | break; |
| 2550 | default: |
| 2551 | goto out_free_opts; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | rc = 0; |
| 2556 | out_free_opts: |
| 2557 | security_free_mnt_opts(&opts); |
| 2558 | out_free_secdata: |
| 2559 | free_secdata(secdata); |
| 2560 | return rc; |
| 2561 | out_bad_option: |
| 2562 | printk(KERN_WARNING "SELinux: unable to change security options " |
| 2563 | "during remount (dev %s, type=%s)\n", sb->s_id, |
| 2564 | sb->s_type->name); |
| 2565 | goto out_free_opts; |
| 2566 | } |
| 2567 | |
James Morris | 12204e2 | 2008-12-19 10:44:42 +1100 | [diff] [blame] | 2568 | static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2570 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2571 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2572 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | int rc; |
| 2574 | |
| 2575 | rc = superblock_doinit(sb, data); |
| 2576 | if (rc) |
| 2577 | return rc; |
| 2578 | |
James Morris | 7419224 | 2008-12-19 11:41:10 +1100 | [diff] [blame] | 2579 | /* Allow all mounts performed by the kernel */ |
| 2580 | if (flags & MS_KERNMOUNT) |
| 2581 | return 0; |
| 2582 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2583 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2584 | ad.selinux_audit_data = &sad; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2585 | ad.u.dentry = sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2586 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2589 | static int selinux_sb_statfs(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2591 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2592 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2593 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2595 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2596 | ad.selinux_audit_data = &sad; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2597 | ad.u.dentry = dentry->d_sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2598 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2601 | static int selinux_mount(char *dev_name, |
Al Viro | b5266eb | 2008-03-22 17:48:24 -0400 | [diff] [blame] | 2602 | struct path *path, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2603 | char *type, |
| 2604 | unsigned long flags, |
| 2605 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2607 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | |
| 2609 | if (flags & MS_REMOUNT) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 2610 | return superblock_has_perm(cred, path->dentry->d_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2611 | FILESYSTEM__REMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | else |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2613 | return path_has_perm(cred, path, FILE__MOUNTON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | static int selinux_umount(struct vfsmount *mnt, int flags) |
| 2617 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2618 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2620 | return superblock_has_perm(cred, mnt->mnt_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2621 | FILESYSTEM__UNMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | /* inode security operations */ |
| 2625 | |
| 2626 | static int selinux_inode_alloc_security(struct inode *inode) |
| 2627 | { |
| 2628 | return inode_alloc_security(inode); |
| 2629 | } |
| 2630 | |
| 2631 | static void selinux_inode_free_security(struct inode *inode) |
| 2632 | { |
| 2633 | inode_free_security(inode); |
| 2634 | } |
| 2635 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2636 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2637 | const struct qstr *qstr, char **name, |
| 2638 | void **value, size_t *len) |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2639 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 2640 | const struct task_security_struct *tsec = current_security(); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2641 | struct inode_security_struct *dsec; |
| 2642 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2643 | u32 sid, newsid, clen; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2644 | int rc; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2645 | char *namep = NULL, *context; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2646 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2647 | dsec = dir->i_security; |
| 2648 | sbsec = dir->i_sb->s_security; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2649 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2650 | sid = tsec->sid; |
| 2651 | newsid = tsec->create_sid; |
| 2652 | |
Eric Paris | 415103f | 2010-12-02 16:13:40 -0500 | [diff] [blame] | 2653 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 2654 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) |
| 2655 | newsid = sbsec->mntpoint_sid; |
| 2656 | else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2657 | rc = security_transition_sid(sid, dsec->sid, |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2658 | inode_mode_to_security_class(inode->i_mode), |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2659 | qstr, &newsid); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2660 | if (rc) { |
| 2661 | printk(KERN_WARNING "%s: " |
| 2662 | "security_transition_sid failed, rc=%d (dev=%s " |
| 2663 | "ino=%ld)\n", |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 2664 | __func__, |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2665 | -rc, inode->i_sb->s_id, inode->i_ino); |
| 2666 | return rc; |
| 2667 | } |
| 2668 | } |
| 2669 | |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2670 | /* Possibly defer initialization to selinux_complete_init. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 2671 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2672 | struct inode_security_struct *isec = inode->i_security; |
| 2673 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2674 | isec->sid = newsid; |
| 2675 | isec->initialized = 1; |
| 2676 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2677 | |
David P. Quigley | cd89596 | 2009-01-16 09:22:04 -0500 | [diff] [blame] | 2678 | if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP)) |
Stephen Smalley | 25a74f3 | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 2679 | return -EOPNOTSUPP; |
| 2680 | |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2681 | if (name) { |
Josef Bacik | a02fe13 | 2008-04-04 09:35:05 +1100 | [diff] [blame] | 2682 | namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2683 | if (!namep) |
| 2684 | return -ENOMEM; |
| 2685 | *name = namep; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2686 | } |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2687 | |
| 2688 | if (value && len) { |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2689 | rc = security_sid_to_context_force(newsid, &context, &clen); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2690 | if (rc) { |
| 2691 | kfree(namep); |
| 2692 | return rc; |
| 2693 | } |
| 2694 | *value = context; |
| 2695 | *len = clen; |
| 2696 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2697 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2698 | return 0; |
| 2699 | } |
| 2700 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2701 | static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | { |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2703 | int ret; |
| 2704 | |
| 2705 | ret = pft_inode_create(dir, dentry, mode); |
| 2706 | if (ret < 0) |
| 2707 | return ret; |
| 2708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | return may_create(dir, dentry, SECCLASS_FILE); |
| 2710 | } |
| 2711 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2712 | static int selinux_inode_post_create(struct inode *dir, struct dentry *dentry, |
| 2713 | umode_t mode) |
| 2714 | { |
| 2715 | int ret; |
| 2716 | |
| 2717 | ret = pft_inode_post_create(dir, dentry, mode); |
| 2718 | |
| 2719 | return ret; |
| 2720 | } |
| 2721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2723 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | return may_link(dir, old_dentry, MAY_LINK); |
| 2725 | } |
| 2726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 2728 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | return may_link(dir, dentry, MAY_UNLINK); |
| 2730 | } |
| 2731 | |
| 2732 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) |
| 2733 | { |
| 2734 | return may_create(dir, dentry, SECCLASS_LNK_FILE); |
| 2735 | } |
| 2736 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2737 | static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | { |
| 2739 | return may_create(dir, dentry, SECCLASS_DIR); |
| 2740 | } |
| 2741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 2743 | { |
| 2744 | return may_link(dir, dentry, MAY_RMDIR); |
| 2745 | } |
| 2746 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2747 | static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
| 2750 | } |
| 2751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2753 | struct inode *new_inode, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | { |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2755 | int rc; |
| 2756 | |
| 2757 | rc = pft_inode_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2758 | if (rc) |
| 2759 | return rc; |
| 2760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2762 | } |
| 2763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | static int selinux_inode_readlink(struct dentry *dentry) |
| 2765 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2766 | const struct cred *cred = current_cred(); |
| 2767 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2768 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) |
| 2772 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2773 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2775 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 2778 | static int selinux_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2780 | const struct cred *cred = current_cred(); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2781 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2782 | struct selinux_audit_data sad = {0,}; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2783 | u32 perms; |
| 2784 | bool from_access; |
Al Viro | cf1dd1d | 2011-06-20 19:44:08 -0400 | [diff] [blame] | 2785 | unsigned flags = mask & MAY_NOT_BLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2787 | from_access = mask & MAY_ACCESS; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 2788 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
| 2789 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2790 | /* No permission to check. Existence test. */ |
| 2791 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2794 | COMMON_AUDIT_DATA_INIT(&ad, INODE); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2795 | ad.selinux_audit_data = &sad; |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2796 | ad.u.inode = inode; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2797 | |
| 2798 | if (from_access) |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2799 | ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2800 | |
| 2801 | perms = file_mask_to_av(inode->i_mode, mask); |
| 2802 | |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 2803 | return inode_has_perm(cred, inode, perms, &ad, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 2807 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2808 | const struct cred *cred = current_cred(); |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 2809 | unsigned int ia_valid = iattr->ia_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 2811 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
| 2812 | if (ia_valid & ATTR_FORCE) { |
| 2813 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | |
| 2814 | ATTR_FORCE); |
| 2815 | if (!ia_valid) |
| 2816 | return 0; |
| 2817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 2819 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
| 2820 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2821 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2823 | return dentry_has_perm(cred, dentry, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) |
| 2827 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2828 | const struct cred *cred = current_cred(); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2829 | struct path path; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2830 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2831 | path.dentry = dentry; |
| 2832 | path.mnt = mnt; |
| 2833 | |
| 2834 | return path_has_perm(cred, &path, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2837 | static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2838 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2839 | const struct cred *cred = current_cred(); |
| 2840 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2841 | if (pft_inode_set_xattr(dentry, name) < 0) |
| 2842 | return -EACCES; |
| 2843 | |
| 2844 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2845 | if (!strncmp(name, XATTR_SECURITY_PREFIX, |
| 2846 | sizeof XATTR_SECURITY_PREFIX - 1)) { |
| 2847 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
| 2848 | if (!capable(CAP_SETFCAP)) |
| 2849 | return -EPERM; |
| 2850 | } else if (!capable(CAP_SYS_ADMIN)) { |
| 2851 | /* A different attribute in the security namespace. |
| 2852 | Restrict to administrator. */ |
| 2853 | return -EPERM; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | /* Not an attribute we recognize, so just check the |
| 2858 | ordinary setattr permission. */ |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2859 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2862 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
| 2863 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | struct inode *inode = dentry->d_inode; |
| 2866 | struct inode_security_struct *isec = inode->i_security; |
| 2867 | struct superblock_security_struct *sbsec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2868 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2869 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2870 | u32 newsid, sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | int rc = 0; |
| 2872 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2873 | if (strcmp(name, XATTR_NAME_SELINUX)) |
| 2874 | return selinux_inode_setotherxattr(dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | |
| 2876 | sbsec = inode->i_sb->s_security; |
David P. Quigley | cd89596 | 2009-01-16 09:22:04 -0500 | [diff] [blame] | 2877 | if (!(sbsec->flags & SE_SBLABELSUPP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | return -EOPNOTSUPP; |
| 2879 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2880 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | return -EPERM; |
| 2882 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2883 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2884 | ad.selinux_audit_data = &sad; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2885 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2887 | rc = avc_has_perm(sid, isec->sid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | FILE__RELABELFROM, &ad); |
| 2889 | if (rc) |
| 2890 | return rc; |
| 2891 | |
| 2892 | rc = security_context_to_sid(value, size, &newsid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2893 | if (rc == -EINVAL) { |
| 2894 | if (!capable(CAP_MAC_ADMIN)) |
| 2895 | return rc; |
| 2896 | rc = security_context_to_sid_force(value, size, &newsid); |
| 2897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | if (rc) |
| 2899 | return rc; |
| 2900 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2901 | rc = avc_has_perm(sid, newsid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | FILE__RELABELTO, &ad); |
| 2903 | if (rc) |
| 2904 | return rc; |
| 2905 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2906 | rc = security_validate_transition(isec->sid, newsid, sid, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2907 | isec->sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | if (rc) |
| 2909 | return rc; |
| 2910 | |
| 2911 | return avc_has_perm(newsid, |
| 2912 | sbsec->sid, |
| 2913 | SECCLASS_FILESYSTEM, |
| 2914 | FILESYSTEM__ASSOCIATE, |
| 2915 | &ad); |
| 2916 | } |
| 2917 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2918 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 2919 | const void *value, size_t size, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2920 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | { |
| 2922 | struct inode *inode = dentry->d_inode; |
| 2923 | struct inode_security_struct *isec = inode->i_security; |
| 2924 | u32 newsid; |
| 2925 | int rc; |
| 2926 | |
| 2927 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 2928 | /* Not an attribute we recognize, so nothing to do. */ |
| 2929 | return; |
| 2930 | } |
| 2931 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2932 | rc = security_context_to_sid_force(value, size, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | if (rc) { |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2934 | printk(KERN_ERR "SELinux: unable to map context to SID" |
| 2935 | "for (%s, %lu), rc=%d\n", |
| 2936 | inode->i_sb->s_id, inode->i_ino, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | isec->sid = newsid; |
| 2941 | return; |
| 2942 | } |
| 2943 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2944 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2946 | const struct cred *cred = current_cred(); |
| 2947 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2948 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2951 | static int selinux_inode_listxattr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2953 | const struct cred *cred = current_cred(); |
| 2954 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2955 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2958 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2960 | if (strcmp(name, XATTR_NAME_SELINUX)) |
| 2961 | return selinux_inode_setotherxattr(dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | |
| 2963 | /* No one is allowed to remove a SELinux security label. |
| 2964 | You can change the label, but all data must be labeled. */ |
| 2965 | return -EACCES; |
| 2966 | } |
| 2967 | |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 2968 | /* |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 2969 | * Copy the inode security context value to the user. |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 2970 | * |
| 2971 | * Permission check is handled by selinux_inode_getxattr hook. |
| 2972 | */ |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 2973 | static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | { |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 2975 | u32 size; |
| 2976 | int error; |
| 2977 | char *context = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | struct inode_security_struct *isec = inode->i_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 2980 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 2981 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 2983 | /* |
| 2984 | * If the caller has CAP_MAC_ADMIN, then get the raw context |
| 2985 | * value even if it is not defined by current policy; otherwise, |
| 2986 | * use the in-core value under current policy. |
| 2987 | * Use the non-auditing forms of the permission checks since |
| 2988 | * getxattr may be called by unprivileged processes commonly |
| 2989 | * and lack of permission just means that we fall back to the |
| 2990 | * in-core context value, not a denial. |
| 2991 | */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2992 | error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN, |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 2993 | SECURITY_CAP_NOAUDIT); |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 2994 | if (!error) |
| 2995 | error = security_sid_to_context_force(isec->sid, &context, |
| 2996 | &size); |
| 2997 | else |
| 2998 | error = security_sid_to_context(isec->sid, &context, &size); |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 2999 | if (error) |
| 3000 | return error; |
| 3001 | error = size; |
| 3002 | if (alloc) { |
| 3003 | *buffer = context; |
| 3004 | goto out_nofree; |
| 3005 | } |
| 3006 | kfree(context); |
| 3007 | out_nofree: |
| 3008 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3012 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | { |
| 3014 | struct inode_security_struct *isec = inode->i_security; |
| 3015 | u32 newsid; |
| 3016 | int rc; |
| 3017 | |
| 3018 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3019 | return -EOPNOTSUPP; |
| 3020 | |
| 3021 | if (!value || !size) |
| 3022 | return -EACCES; |
| 3023 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3024 | rc = security_context_to_sid((void *)value, size, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | if (rc) |
| 3026 | return rc; |
| 3027 | |
| 3028 | isec->sid = newsid; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 3029 | isec->initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | return 0; |
| 3031 | } |
| 3032 | |
| 3033 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) |
| 3034 | { |
| 3035 | const int len = sizeof(XATTR_NAME_SELINUX); |
| 3036 | if (buffer && len <= buffer_size) |
| 3037 | memcpy(buffer, XATTR_NAME_SELINUX, len); |
| 3038 | return len; |
| 3039 | } |
| 3040 | |
Ahmed S. Darwish | 713a04a | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3041 | static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) |
| 3042 | { |
| 3043 | struct inode_security_struct *isec = inode->i_security; |
| 3044 | *secid = isec->sid; |
| 3045 | } |
| 3046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | /* file security operations */ |
| 3048 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3049 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3051 | const struct cred *cred = current_cred(); |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 3052 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
| 3055 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) |
| 3056 | mask |= MAY_APPEND; |
| 3057 | |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3058 | return file_has_perm(cred, file, |
| 3059 | file_mask_to_av(inode->i_mode, mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3062 | static int selinux_file_permission(struct file *file, int mask) |
| 3063 | { |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3064 | struct inode *inode = file->f_path.dentry->d_inode; |
| 3065 | struct file_security_struct *fsec = file->f_security; |
| 3066 | struct inode_security_struct *isec = inode->i_security; |
| 3067 | u32 sid = current_sid(); |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3068 | int ret; |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3069 | |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3070 | if (!mask) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3071 | /* No permission to check. Existence test. */ |
| 3072 | return 0; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3073 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3074 | ret = pft_file_permission(file, mask); |
| 3075 | if (ret < 0) |
| 3076 | return ret; |
| 3077 | |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3078 | if (sid == fsec->sid && fsec->isid == isec->sid && |
| 3079 | fsec->pseqno == avc_policy_seqno()) |
| 3080 | /* No change since dentry_open check. */ |
| 3081 | return 0; |
| 3082 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3083 | return selinux_revalidate_file_permission(file, mask); |
| 3084 | } |
| 3085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | static int selinux_file_alloc_security(struct file *file) |
| 3087 | { |
| 3088 | return file_alloc_security(file); |
| 3089 | } |
| 3090 | |
| 3091 | static void selinux_file_free_security(struct file *file) |
| 3092 | { |
| 3093 | file_free_security(file); |
| 3094 | } |
| 3095 | |
| 3096 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, |
| 3097 | unsigned long arg) |
| 3098 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3099 | const struct cred *cred = current_cred(); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3100 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3102 | switch (cmd) { |
| 3103 | case FIONREAD: |
| 3104 | /* fall through */ |
| 3105 | case FIBMAP: |
| 3106 | /* fall through */ |
| 3107 | case FIGETBSZ: |
| 3108 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3109 | case FS_IOC_GETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3110 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3111 | case FS_IOC_GETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3112 | error = file_has_perm(cred, file, FILE__GETATTR); |
| 3113 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3115 | case FS_IOC_SETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3116 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3117 | case FS_IOC_SETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3118 | error = file_has_perm(cred, file, FILE__SETATTR); |
| 3119 | break; |
| 3120 | |
| 3121 | /* sys_ioctl() checks */ |
| 3122 | case FIONBIO: |
| 3123 | /* fall through */ |
| 3124 | case FIOASYNC: |
| 3125 | error = file_has_perm(cred, file, 0); |
| 3126 | break; |
| 3127 | |
| 3128 | case KDSKBENT: |
| 3129 | case KDSKBSENT: |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3130 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
| 3131 | SECURITY_CAP_AUDIT); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3132 | break; |
| 3133 | |
| 3134 | /* default case assumes that the command will go |
| 3135 | * to the file's ioctl() function. |
| 3136 | */ |
| 3137 | default: |
Jeff Vander Stoep | 1db0d29 | 2015-07-29 18:36:41 -0700 | [diff] [blame^] | 3138 | error = file_has_perm(cred, file, FILE__IOCTL); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3139 | } |
| 3140 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3143 | static int default_noexec; |
| 3144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
| 3146 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3147 | const struct cred *cred = current_cred(); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3148 | int rc = 0; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3149 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3150 | if (default_noexec && |
| 3151 | (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | /* |
| 3153 | * We are making executable an anonymous mapping or a |
| 3154 | * private file mapping that will also be writable. |
| 3155 | * This has an additional check. |
| 3156 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3157 | rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | if (rc) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3159 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | |
| 3162 | if (file) { |
| 3163 | /* read access is always possible with a mapping */ |
| 3164 | u32 av = FILE__READ; |
| 3165 | |
| 3166 | /* write access only matters if the mapping is shared */ |
| 3167 | if (shared && (prot & PROT_WRITE)) |
| 3168 | av |= FILE__WRITE; |
| 3169 | |
| 3170 | if (prot & PROT_EXEC) |
| 3171 | av |= FILE__EXECUTE; |
| 3172 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3173 | return file_has_perm(cred, file, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3175 | |
| 3176 | error: |
| 3177 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | static int selinux_file_mmap(struct file *file, unsigned long reqprot, |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3181 | unsigned long prot, unsigned long flags, |
| 3182 | unsigned long addr, unsigned long addr_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | { |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3184 | int rc = 0; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3185 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | |
Eric Paris | 84336d1a | 2009-07-31 12:54:05 -0400 | [diff] [blame] | 3187 | /* |
| 3188 | * notice that we are intentionally putting the SELinux check before |
| 3189 | * the secondary cap_file_mmap check. This is such a likely attempt |
| 3190 | * at bad behaviour/exploit that we always want to get the AVC, even |
| 3191 | * if DAC would have also denied the operation. |
| 3192 | */ |
Eric Paris | a2551df | 2009-07-31 12:54:11 -0400 | [diff] [blame] | 3193 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3194 | rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, |
| 3195 | MEMPROTECT__MMAP_ZERO, NULL); |
Eric Paris | 84336d1a | 2009-07-31 12:54:05 -0400 | [diff] [blame] | 3196 | if (rc) |
| 3197 | return rc; |
| 3198 | } |
| 3199 | |
| 3200 | /* do DAC check on address space usage */ |
| 3201 | rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only); |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3202 | if (rc || addr_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | return rc; |
| 3204 | |
| 3205 | if (selinux_checkreqprot) |
| 3206 | prot = reqprot; |
| 3207 | |
| 3208 | return file_map_prot_check(file, prot, |
| 3209 | (flags & MAP_TYPE) == MAP_SHARED); |
| 3210 | } |
| 3211 | |
| 3212 | static int selinux_file_mprotect(struct vm_area_struct *vma, |
| 3213 | unsigned long reqprot, |
| 3214 | unsigned long prot) |
| 3215 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3216 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
| 3218 | if (selinux_checkreqprot) |
| 3219 | prot = reqprot; |
| 3220 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3221 | if (default_noexec && |
| 3222 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { |
James Morris | d541bbe | 2009-01-29 12:19:51 +1100 | [diff] [blame] | 3223 | int rc = 0; |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3224 | if (vma->vm_start >= vma->vm_mm->start_brk && |
| 3225 | vma->vm_end <= vma->vm_mm->brk) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3226 | rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3227 | } else if (!vma->vm_file && |
| 3228 | vma->vm_start <= vma->vm_mm->start_stack && |
| 3229 | vma->vm_end >= vma->vm_mm->start_stack) { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3230 | rc = current_has_perm(current, PROCESS__EXECSTACK); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3231 | } else if (vma->vm_file && vma->anon_vma) { |
| 3232 | /* |
| 3233 | * We are making executable a file mapping that has |
| 3234 | * had some COW done. Since pages might have been |
| 3235 | * written, check ability to execute the possibly |
| 3236 | * modified content. This typically should only |
| 3237 | * occur for text relocations. |
| 3238 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3239 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3240 | } |
Lorenzo Hernandez GarcÃa-Hierro | 6b99219 | 2005-06-25 14:54:34 -0700 | [diff] [blame] | 3241 | if (rc) |
| 3242 | return rc; |
| 3243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | |
| 3245 | return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); |
| 3246 | } |
| 3247 | |
| 3248 | static int selinux_file_lock(struct file *file, unsigned int cmd) |
| 3249 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3250 | const struct cred *cred = current_cred(); |
| 3251 | |
| 3252 | return file_has_perm(cred, file, FILE__LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | static int selinux_file_fcntl(struct file *file, unsigned int cmd, |
| 3256 | unsigned long arg) |
| 3257 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3258 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | int err = 0; |
| 3260 | |
| 3261 | switch (cmd) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3262 | case F_SETFL: |
| 3263 | if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { |
| 3264 | err = -EINVAL; |
| 3265 | break; |
| 3266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3268 | if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3269 | err = file_has_perm(cred, file, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3271 | } |
| 3272 | /* fall through */ |
| 3273 | case F_SETOWN: |
| 3274 | case F_SETSIG: |
| 3275 | case F_GETFL: |
| 3276 | case F_GETOWN: |
| 3277 | case F_GETSIG: |
| 3278 | /* Just check FD__USE permission */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3279 | err = file_has_perm(cred, file, 0); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3280 | break; |
| 3281 | case F_GETLK: |
| 3282 | case F_SETLK: |
| 3283 | case F_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | #if BITS_PER_LONG == 32 |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3285 | case F_GETLK64: |
| 3286 | case F_SETLK64: |
| 3287 | case F_SETLKW64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | #endif |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3289 | if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { |
| 3290 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3292 | } |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3293 | err = file_has_perm(cred, file, FILE__LOCK); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3294 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | return err; |
| 3298 | } |
| 3299 | |
| 3300 | static int selinux_file_set_fowner(struct file *file) |
| 3301 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | struct file_security_struct *fsec; |
| 3303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | fsec = file->f_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3305 | fsec->fown_sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | |
| 3307 | return 0; |
| 3308 | } |
| 3309 | |
| 3310 | static int selinux_file_send_sigiotask(struct task_struct *tsk, |
| 3311 | struct fown_struct *fown, int signum) |
| 3312 | { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3313 | struct file *file; |
Stephen Smalley | 65c90bc | 2009-05-04 15:43:18 -0400 | [diff] [blame] | 3314 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | struct file_security_struct *fsec; |
| 3317 | |
| 3318 | /* struct fown_struct is never outside the context of a struct file */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3319 | file = container_of(fown, struct file, f_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | fsec = file->f_security; |
| 3322 | |
| 3323 | if (!signum) |
| 3324 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| 3325 | else |
| 3326 | perm = signal_to_av(signum); |
| 3327 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3328 | return avc_has_perm(fsec->fown_sid, sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | SECCLASS_PROCESS, perm, NULL); |
| 3330 | } |
| 3331 | |
| 3332 | static int selinux_file_receive(struct file *file) |
| 3333 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3334 | const struct cred *cred = current_cred(); |
| 3335 | |
| 3336 | return file_has_perm(cred, file, file_to_av(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | } |
| 3338 | |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 3339 | static int selinux_dentry_open(struct file *file, const struct cred *cred) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3340 | { |
| 3341 | struct file_security_struct *fsec; |
| 3342 | struct inode *inode; |
| 3343 | struct inode_security_struct *isec; |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3344 | int ret; |
| 3345 | |
| 3346 | ret = pft_file_open(file, cred); |
| 3347 | if (ret < 0) |
| 3348 | return ret; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3349 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3350 | inode = file->f_path.dentry->d_inode; |
| 3351 | fsec = file->f_security; |
| 3352 | isec = inode->i_security; |
| 3353 | /* |
| 3354 | * Save inode label and policy sequence number |
| 3355 | * at open-time so that selinux_file_permission |
| 3356 | * can determine whether revalidation is necessary. |
| 3357 | * Task label is already saved in the file security |
| 3358 | * struct as its SID. |
| 3359 | */ |
| 3360 | fsec->isid = isec->sid; |
| 3361 | fsec->pseqno = avc_policy_seqno(); |
| 3362 | /* |
| 3363 | * Since the inode label or policy seqno may have changed |
| 3364 | * between the selinux_inode_permission check and the saving |
| 3365 | * of state above, recheck that access is still permitted. |
| 3366 | * Otherwise, access might never be revalidated against the |
| 3367 | * new inode label or new policy. |
| 3368 | * This check is not redundant - do not remove. |
| 3369 | */ |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 3370 | return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3371 | } |
| 3372 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3373 | static int selinux_file_close(struct file *file) |
| 3374 | { |
| 3375 | return pft_file_close(file); |
| 3376 | } |
| 3377 | |
| 3378 | static bool selinux_allow_merge_bio(struct bio *bio1, struct bio *bio2) |
| 3379 | { |
| 3380 | return pft_allow_merge_bio(bio1, bio2); |
| 3381 | } |
| 3382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | /* task security operations */ |
| 3384 | |
| 3385 | static int selinux_task_create(unsigned long clone_flags) |
| 3386 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3387 | return current_has_perm(current, PROCESS__FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3390 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3391 | * allocate the SELinux part of blank credentials |
| 3392 | */ |
| 3393 | static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 3394 | { |
| 3395 | struct task_security_struct *tsec; |
| 3396 | |
| 3397 | tsec = kzalloc(sizeof(struct task_security_struct), gfp); |
| 3398 | if (!tsec) |
| 3399 | return -ENOMEM; |
| 3400 | |
| 3401 | cred->security = tsec; |
| 3402 | return 0; |
| 3403 | } |
| 3404 | |
| 3405 | /* |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3406 | * detach and free the LSM part of a set of credentials |
| 3407 | */ |
| 3408 | static void selinux_cred_free(struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | { |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3410 | struct task_security_struct *tsec = cred->security; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3411 | |
Tetsuo Handa | 2edeaa3 | 2011-02-07 13:36:10 +0000 | [diff] [blame] | 3412 | /* |
| 3413 | * cred->security == NULL if security_cred_alloc_blank() or |
| 3414 | * security_prepare_creds() returned an error. |
| 3415 | */ |
| 3416 | BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3417 | cred->security = (void *) 0x7UL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3418 | kfree(tsec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3421 | /* |
| 3422 | * prepare a new set of credentials for modification |
| 3423 | */ |
| 3424 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3425 | gfp_t gfp) |
| 3426 | { |
| 3427 | const struct task_security_struct *old_tsec; |
| 3428 | struct task_security_struct *tsec; |
| 3429 | |
| 3430 | old_tsec = old->security; |
| 3431 | |
| 3432 | tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); |
| 3433 | if (!tsec) |
| 3434 | return -ENOMEM; |
| 3435 | |
| 3436 | new->security = tsec; |
| 3437 | return 0; |
| 3438 | } |
| 3439 | |
| 3440 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3441 | * transfer the SELinux data to a blank set of creds |
| 3442 | */ |
| 3443 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3444 | { |
| 3445 | const struct task_security_struct *old_tsec = old->security; |
| 3446 | struct task_security_struct *tsec = new->security; |
| 3447 | |
| 3448 | *tsec = *old_tsec; |
| 3449 | } |
| 3450 | |
| 3451 | /* |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3452 | * set the security data for a kernel service |
| 3453 | * - all the creation contexts are set to unlabelled |
| 3454 | */ |
| 3455 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3456 | { |
| 3457 | struct task_security_struct *tsec = new->security; |
| 3458 | u32 sid = current_sid(); |
| 3459 | int ret; |
| 3460 | |
| 3461 | ret = avc_has_perm(sid, secid, |
| 3462 | SECCLASS_KERNEL_SERVICE, |
| 3463 | KERNEL_SERVICE__USE_AS_OVERRIDE, |
| 3464 | NULL); |
| 3465 | if (ret == 0) { |
| 3466 | tsec->sid = secid; |
| 3467 | tsec->create_sid = 0; |
| 3468 | tsec->keycreate_sid = 0; |
| 3469 | tsec->sockcreate_sid = 0; |
| 3470 | } |
| 3471 | return ret; |
| 3472 | } |
| 3473 | |
| 3474 | /* |
| 3475 | * set the file creation context in a security record to the same as the |
| 3476 | * objective context of the specified inode |
| 3477 | */ |
| 3478 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3479 | { |
| 3480 | struct inode_security_struct *isec = inode->i_security; |
| 3481 | struct task_security_struct *tsec = new->security; |
| 3482 | u32 sid = current_sid(); |
| 3483 | int ret; |
| 3484 | |
| 3485 | ret = avc_has_perm(sid, isec->sid, |
| 3486 | SECCLASS_KERNEL_SERVICE, |
| 3487 | KERNEL_SERVICE__CREATE_FILES_AS, |
| 3488 | NULL); |
| 3489 | |
| 3490 | if (ret == 0) |
| 3491 | tsec->create_sid = isec->sid; |
David Howells | ef57471 | 2010-02-26 01:56:16 +0000 | [diff] [blame] | 3492 | return ret; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3493 | } |
| 3494 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3495 | static int selinux_kernel_module_request(char *kmod_name) |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3496 | { |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3497 | u32 sid; |
| 3498 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3499 | struct selinux_audit_data sad = {0,}; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3500 | |
| 3501 | sid = task_sid(current); |
| 3502 | |
| 3503 | COMMON_AUDIT_DATA_INIT(&ad, KMOD); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3504 | ad.selinux_audit_data = &sad; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3505 | ad.u.kmod_name = kmod_name; |
| 3506 | |
| 3507 | return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM, |
| 3508 | SYSTEM__MODULE_REQUEST, &ad); |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3509 | } |
| 3510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) |
| 3512 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3513 | return current_has_perm(p, PROCESS__SETPGID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | static int selinux_task_getpgid(struct task_struct *p) |
| 3517 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3518 | return current_has_perm(p, PROCESS__GETPGID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | static int selinux_task_getsid(struct task_struct *p) |
| 3522 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3523 | return current_has_perm(p, PROCESS__GETSESSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | } |
| 3525 | |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3526 | static void selinux_task_getsecid(struct task_struct *p, u32 *secid) |
| 3527 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3528 | *secid = task_sid(p); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3529 | } |
| 3530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | static int selinux_task_setnice(struct task_struct *p, int nice) |
| 3532 | { |
| 3533 | int rc; |
| 3534 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 3535 | rc = cap_task_setnice(p, nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | if (rc) |
| 3537 | return rc; |
| 3538 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3539 | return current_has_perm(p, PROCESS__SETSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3542 | static int selinux_task_setioprio(struct task_struct *p, int ioprio) |
| 3543 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3544 | int rc; |
| 3545 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 3546 | rc = cap_task_setioprio(p, ioprio); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3547 | if (rc) |
| 3548 | return rc; |
| 3549 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3550 | return current_has_perm(p, PROCESS__SETSCHED); |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3551 | } |
| 3552 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3553 | static int selinux_task_getioprio(struct task_struct *p) |
| 3554 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3555 | return current_has_perm(p, PROCESS__GETSCHED); |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3558 | static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, |
| 3559 | struct rlimit *new_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | { |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3561 | struct rlimit *old_rlim = p->signal->rlim + resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | |
| 3563 | /* Control the ability to change the hard limit (whether |
| 3564 | lowering or raising it), so that the hard limit can |
| 3565 | later be used as a safe reset point for the soft limit |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3566 | upon context transitions. See selinux_bprm_committing_creds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | if (old_rlim->rlim_max != new_rlim->rlim_max) |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3568 | return current_has_perm(p, PROCESS__SETRLIMIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 3573 | static int selinux_task_setscheduler(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3575 | int rc; |
| 3576 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 3577 | rc = cap_task_setscheduler(p); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3578 | if (rc) |
| 3579 | return rc; |
| 3580 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3581 | return current_has_perm(p, PROCESS__SETSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | static int selinux_task_getscheduler(struct task_struct *p) |
| 3585 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3586 | return current_has_perm(p, PROCESS__GETSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 3589 | static int selinux_task_movememory(struct task_struct *p) |
| 3590 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3591 | return current_has_perm(p, PROCESS__SETSCHED); |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3594 | static int selinux_task_kill(struct task_struct *p, struct siginfo *info, |
| 3595 | int sig, u32 secid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | { |
| 3597 | u32 perm; |
| 3598 | int rc; |
| 3599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | if (!sig) |
| 3601 | perm = PROCESS__SIGNULL; /* null signal; existence test */ |
| 3602 | else |
| 3603 | perm = signal_to_av(sig); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3604 | if (secid) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3605 | rc = avc_has_perm(secid, task_sid(p), |
| 3606 | SECCLASS_PROCESS, perm, NULL); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3607 | else |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3608 | rc = current_has_perm(p, perm); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3609 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | static int selinux_task_wait(struct task_struct *p) |
| 3613 | { |
Eric Paris | 8a53514 | 2007-10-22 16:10:31 -0400 | [diff] [blame] | 3614 | return task_has_perm(p, current, PROCESS__SIGCHLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | static void selinux_task_to_inode(struct task_struct *p, |
| 3618 | struct inode *inode) |
| 3619 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | struct inode_security_struct *isec = inode->i_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3621 | u32 sid = task_sid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3623 | isec->sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | isec->initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | } |
| 3626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3628 | static int selinux_parse_skb_ipv4(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3629 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | { |
| 3631 | int offset, ihlen, ret = -EINVAL; |
| 3632 | struct iphdr _iph, *ih; |
| 3633 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 3634 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); |
| 3636 | if (ih == NULL) |
| 3637 | goto out; |
| 3638 | |
| 3639 | ihlen = ih->ihl * 4; |
| 3640 | if (ihlen < sizeof(_iph)) |
| 3641 | goto out; |
| 3642 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3643 | ad->u.net->v4info.saddr = ih->saddr; |
| 3644 | ad->u.net->v4info.daddr = ih->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | ret = 0; |
| 3646 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3647 | if (proto) |
| 3648 | *proto = ih->protocol; |
| 3649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | switch (ih->protocol) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3651 | case IPPROTO_TCP: { |
| 3652 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3654 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 3655 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | |
| 3657 | offset += ihlen; |
| 3658 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 3659 | if (th == NULL) |
| 3660 | break; |
| 3661 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3662 | ad->u.net->sport = th->source; |
| 3663 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3667 | case IPPROTO_UDP: { |
| 3668 | struct udphdr _udph, *uh; |
| 3669 | |
| 3670 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 3671 | break; |
| 3672 | |
| 3673 | offset += ihlen; |
| 3674 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 3675 | if (uh == NULL) |
| 3676 | break; |
| 3677 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3678 | ad->u.net->sport = uh->source; |
| 3679 | ad->u.net->dport = uh->dest; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3680 | break; |
| 3681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3683 | case IPPROTO_DCCP: { |
| 3684 | struct dccp_hdr _dccph, *dh; |
| 3685 | |
| 3686 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 3687 | break; |
| 3688 | |
| 3689 | offset += ihlen; |
| 3690 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 3691 | if (dh == NULL) |
| 3692 | break; |
| 3693 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3694 | ad->u.net->sport = dh->dccph_sport; |
| 3695 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3696 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3697 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3698 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3699 | default: |
| 3700 | break; |
| 3701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | out: |
| 3703 | return ret; |
| 3704 | } |
| 3705 | |
| 3706 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 3707 | |
| 3708 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3709 | static int selinux_parse_skb_ipv6(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3710 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | { |
| 3712 | u8 nexthdr; |
| 3713 | int ret = -EINVAL, offset; |
| 3714 | struct ipv6hdr _ipv6h, *ip6; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 3715 | __be16 frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 3717 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 3719 | if (ip6 == NULL) |
| 3720 | goto out; |
| 3721 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3722 | ad->u.net->v6info.saddr = ip6->saddr; |
| 3723 | ad->u.net->v6info.daddr = ip6->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | ret = 0; |
| 3725 | |
| 3726 | nexthdr = ip6->nexthdr; |
| 3727 | offset += sizeof(_ipv6h); |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 3728 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | if (offset < 0) |
| 3730 | goto out; |
| 3731 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3732 | if (proto) |
| 3733 | *proto = nexthdr; |
| 3734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | switch (nexthdr) { |
| 3736 | case IPPROTO_TCP: { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3737 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | |
| 3739 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 3740 | if (th == NULL) |
| 3741 | break; |
| 3742 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3743 | ad->u.net->sport = th->source; |
| 3744 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | break; |
| 3746 | } |
| 3747 | |
| 3748 | case IPPROTO_UDP: { |
| 3749 | struct udphdr _udph, *uh; |
| 3750 | |
| 3751 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 3752 | if (uh == NULL) |
| 3753 | break; |
| 3754 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3755 | ad->u.net->sport = uh->source; |
| 3756 | ad->u.net->dport = uh->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | break; |
| 3758 | } |
| 3759 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3760 | case IPPROTO_DCCP: { |
| 3761 | struct dccp_hdr _dccph, *dh; |
| 3762 | |
| 3763 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 3764 | if (dh == NULL) |
| 3765 | break; |
| 3766 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3767 | ad->u.net->sport = dh->dccph_sport; |
| 3768 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3769 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3770 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | /* includes fragments */ |
| 3773 | default: |
| 3774 | break; |
| 3775 | } |
| 3776 | out: |
| 3777 | return ret; |
| 3778 | } |
| 3779 | |
| 3780 | #endif /* IPV6 */ |
| 3781 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3782 | static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3783 | char **_addrp, int src, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | { |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3785 | char *addrp; |
| 3786 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3788 | switch (ad->u.net->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | case PF_INET: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3790 | ret = selinux_parse_skb_ipv4(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3791 | if (ret) |
| 3792 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3793 | addrp = (char *)(src ? &ad->u.net->v4info.saddr : |
| 3794 | &ad->u.net->v4info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3795 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | |
| 3797 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 3798 | case PF_INET6: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3799 | ret = selinux_parse_skb_ipv6(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3800 | if (ret) |
| 3801 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3802 | addrp = (char *)(src ? &ad->u.net->v6info.saddr : |
| 3803 | &ad->u.net->v6info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3804 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | #endif /* IPV6 */ |
| 3806 | default: |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3807 | addrp = NULL; |
| 3808 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | } |
| 3810 | |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3811 | parse_error: |
| 3812 | printk(KERN_WARNING |
| 3813 | "SELinux: failure in selinux_parse_skb()," |
| 3814 | " unable to parse packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | return ret; |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3816 | |
| 3817 | okay: |
| 3818 | if (_addrp) |
| 3819 | *_addrp = addrp; |
| 3820 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | } |
| 3822 | |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3823 | /** |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3824 | * selinux_skb_peerlbl_sid - Determine the peer label of a packet |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3825 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 3826 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3827 | * @sid: the packet's peer label SID |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3828 | * |
| 3829 | * Description: |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3830 | * Check the various different forms of network peer labeling and determine |
| 3831 | * the peer label/SID for the packet; most of the magic actually occurs in |
| 3832 | * the security server function security_net_peersid_cmp(). The function |
| 3833 | * returns zero if the value in @sid is valid (although it may be SECSID_NULL) |
| 3834 | * or -EACCES if @sid is invalid due to inconsistencies with the different |
| 3835 | * peer labels. |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3836 | * |
| 3837 | */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3838 | static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3839 | { |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 3840 | int err; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3841 | u32 xfrm_sid; |
| 3842 | u32 nlbl_sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3843 | u32 nlbl_type; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3844 | |
| 3845 | selinux_skb_xfrm_sid(skb, &xfrm_sid); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3846 | selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3847 | |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 3848 | err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); |
| 3849 | if (unlikely(err)) { |
| 3850 | printk(KERN_WARNING |
| 3851 | "SELinux: failure in selinux_skb_peerlbl_sid()," |
| 3852 | " unable to determine packet's peer label\n"); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3853 | return -EACCES; |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 3854 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3855 | |
| 3856 | return 0; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3857 | } |
| 3858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | /* socket security operations */ |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3860 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3861 | static int socket_sockcreate_sid(const struct task_security_struct *tsec, |
| 3862 | u16 secclass, u32 *socksid) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3863 | { |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3864 | if (tsec->sockcreate_sid > SECSID_NULL) { |
| 3865 | *socksid = tsec->sockcreate_sid; |
| 3866 | return 0; |
| 3867 | } |
| 3868 | |
| 3869 | return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, |
| 3870 | socksid); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3871 | } |
| 3872 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3873 | static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3875 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3876 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3877 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3878 | struct lsm_network_audit net = {0,}; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3879 | u32 tsid = task_sid(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | |
Satya Durga Srinivasu Prabhala | 9028065 | 2013-09-24 15:23:48 -0700 | [diff] [blame] | 3881 | if (unlikely(!sksec)) { |
| 3882 | pr_warn("SELinux: sksec is NULL, socket is already freed\n"); |
| 3883 | return -EINVAL; |
| 3884 | } |
| 3885 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3886 | if (sksec->sid == SECINITSID_KERNEL) |
| 3887 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3889 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3890 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3891 | ad.u.net = &net; |
| 3892 | ad.u.net->sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3894 | return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | static int selinux_socket_create(int family, int type, |
| 3898 | int protocol, int kern) |
| 3899 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3900 | const struct task_security_struct *tsec = current_security(); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3901 | u32 newsid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3902 | u16 secclass; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3903 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | |
| 3905 | if (kern) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3906 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3908 | secclass = socket_type_to_security_class(family, type, protocol); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3909 | rc = socket_sockcreate_sid(tsec, secclass, &newsid); |
| 3910 | if (rc) |
| 3911 | return rc; |
| 3912 | |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3913 | return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | } |
| 3915 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3916 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 3917 | int type, int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3919 | const struct task_security_struct *tsec = current_security(); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3920 | struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 3921 | struct sk_security_struct *sksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3922 | int err = 0; |
| 3923 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3924 | isec->sclass = socket_type_to_security_class(family, type, protocol); |
| 3925 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3926 | if (kern) |
| 3927 | isec->sid = SECINITSID_KERNEL; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3928 | else { |
| 3929 | err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid)); |
| 3930 | if (err) |
| 3931 | return err; |
| 3932 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | isec->initialized = 1; |
| 3935 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 3936 | if (sock->sk) { |
| 3937 | sksec = sock->sk->sk_security; |
| 3938 | sksec->sid = isec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3939 | sksec->sclass = isec->sclass; |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3940 | err = selinux_netlbl_socket_post_create(sock->sk, family); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3943 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | /* Range of port numbers used to automatically bind. |
| 3947 | Need to determine whether we should perform a name_bind |
| 3948 | permission check between the socket and the port number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | |
| 3950 | static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 3951 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3952 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | u16 family; |
| 3954 | int err; |
| 3955 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3956 | err = sock_has_perm(current, sk, SOCKET__BIND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | if (err) |
| 3958 | goto out; |
| 3959 | |
| 3960 | /* |
| 3961 | * If PF_INET or PF_INET6, check name_bind permission for the port. |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 3962 | * Multiple address binding for SCTP is not supported yet: we just |
| 3963 | * check the first address now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3965 | family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | if (family == PF_INET || family == PF_INET6) { |
| 3967 | char *addrp; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3968 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3969 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3970 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3971 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | struct sockaddr_in *addr4 = NULL; |
| 3973 | struct sockaddr_in6 *addr6 = NULL; |
| 3974 | unsigned short snum; |
James Morris | e399f98 | 2008-06-12 01:39:58 +1000 | [diff] [blame] | 3975 | u32 sid, node_perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | if (family == PF_INET) { |
| 3978 | addr4 = (struct sockaddr_in *)address; |
| 3979 | snum = ntohs(addr4->sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | addrp = (char *)&addr4->sin_addr.s_addr; |
| 3981 | } else { |
| 3982 | addr6 = (struct sockaddr_in6 *)address; |
| 3983 | snum = ntohs(addr6->sin6_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | addrp = (char *)&addr6->sin6_addr.s6_addr; |
| 3985 | } |
| 3986 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 3987 | if (snum) { |
| 3988 | int low, high; |
| 3989 | |
| 3990 | inet_get_local_port_range(&low, &high); |
| 3991 | |
| 3992 | if (snum < max(PROT_SOCK, low) || snum > high) { |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 3993 | err = sel_netport_sid(sk->sk_protocol, |
| 3994 | snum, &sid); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 3995 | if (err) |
| 3996 | goto out; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3997 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3998 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3999 | ad.u.net = &net; |
| 4000 | ad.u.net->sport = htons(snum); |
| 4001 | ad.u.net->family = family; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4002 | err = avc_has_perm(sksec->sid, sid, |
| 4003 | sksec->sclass, |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4004 | SOCKET__NAME_BIND, &ad); |
| 4005 | if (err) |
| 4006 | goto out; |
| 4007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4009 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4010 | switch (sksec->sclass) { |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4011 | case SECCLASS_TCP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | node_perm = TCP_SOCKET__NODE_BIND; |
| 4013 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4014 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4015 | case SECCLASS_UDP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | node_perm = UDP_SOCKET__NODE_BIND; |
| 4017 | break; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4018 | |
| 4019 | case SECCLASS_DCCP_SOCKET: |
| 4020 | node_perm = DCCP_SOCKET__NODE_BIND; |
| 4021 | break; |
| 4022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | default: |
| 4024 | node_perm = RAWIP_SOCKET__NODE_BIND; |
| 4025 | break; |
| 4026 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4027 | |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4028 | err = sel_netnode_sid(addrp, family, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | if (err) |
| 4030 | goto out; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4031 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4032 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4033 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4034 | ad.u.net = &net; |
| 4035 | ad.u.net->sport = htons(snum); |
| 4036 | ad.u.net->family = family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | |
| 4038 | if (family == PF_INET) |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4039 | ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | else |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4041 | ad.u.net->v6info.saddr = addr6->sin6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4043 | err = avc_has_perm(sksec->sid, sid, |
| 4044 | sksec->sclass, node_perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | if (err) |
| 4046 | goto out; |
| 4047 | } |
| 4048 | out: |
| 4049 | return err; |
| 4050 | } |
| 4051 | |
| 4052 | static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4053 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4054 | struct sock *sk = sock->sk; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4055 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | int err; |
| 4057 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4058 | err = sock_has_perm(current, sk, SOCKET__CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | if (err) |
| 4060 | return err; |
| 4061 | |
| 4062 | /* |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4063 | * If a TCP or DCCP socket, check name_connect permission for the port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4065 | if (sksec->sclass == SECCLASS_TCP_SOCKET || |
| 4066 | sksec->sclass == SECCLASS_DCCP_SOCKET) { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4067 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4068 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4069 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | struct sockaddr_in *addr4 = NULL; |
| 4071 | struct sockaddr_in6 *addr6 = NULL; |
| 4072 | unsigned short snum; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4073 | u32 sid, perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | |
| 4075 | if (sk->sk_family == PF_INET) { |
| 4076 | addr4 = (struct sockaddr_in *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4077 | if (addrlen < sizeof(struct sockaddr_in)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | return -EINVAL; |
| 4079 | snum = ntohs(addr4->sin_port); |
| 4080 | } else { |
| 4081 | addr6 = (struct sockaddr_in6 *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4082 | if (addrlen < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | return -EINVAL; |
| 4084 | snum = ntohs(addr6->sin6_port); |
| 4085 | } |
| 4086 | |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4087 | err = sel_netport_sid(sk->sk_protocol, snum, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | if (err) |
| 4089 | goto out; |
| 4090 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4091 | perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4092 | TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; |
| 4093 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4094 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4095 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4096 | ad.u.net = &net; |
| 4097 | ad.u.net->dport = htons(snum); |
| 4098 | ad.u.net->family = sk->sk_family; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4099 | err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | if (err) |
| 4101 | goto out; |
| 4102 | } |
| 4103 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4104 | err = selinux_netlbl_socket_connect(sk, address); |
| 4105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | out: |
| 4107 | return err; |
| 4108 | } |
| 4109 | |
| 4110 | static int selinux_socket_listen(struct socket *sock, int backlog) |
| 4111 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4112 | return sock_has_perm(current, sock->sk, SOCKET__LISTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | static int selinux_socket_accept(struct socket *sock, struct socket *newsock) |
| 4116 | { |
| 4117 | int err; |
| 4118 | struct inode_security_struct *isec; |
| 4119 | struct inode_security_struct *newisec; |
| 4120 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4121 | err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | if (err) |
| 4123 | return err; |
| 4124 | |
| 4125 | newisec = SOCK_INODE(newsock)->i_security; |
| 4126 | |
| 4127 | isec = SOCK_INODE(sock)->i_security; |
| 4128 | newisec->sclass = isec->sclass; |
| 4129 | newisec->sid = isec->sid; |
| 4130 | newisec->initialized = 1; |
| 4131 | |
| 4132 | return 0; |
| 4133 | } |
| 4134 | |
| 4135 | static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4136 | int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4138 | return sock_has_perm(current, sock->sk, SOCKET__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 4142 | int size, int flags) |
| 4143 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4144 | return sock_has_perm(current, sock->sk, SOCKET__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | static int selinux_socket_getsockname(struct socket *sock) |
| 4148 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4149 | return sock_has_perm(current, sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | static int selinux_socket_getpeername(struct socket *sock) |
| 4153 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4154 | return sock_has_perm(current, sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | } |
| 4156 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4157 | static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | { |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4159 | int err; |
| 4160 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4161 | err = sock_has_perm(current, sock->sk, SOCKET__SETOPT); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4162 | if (err) |
| 4163 | return err; |
| 4164 | |
| 4165 | return selinux_netlbl_socket_setsockopt(sock, level, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | static int selinux_socket_getsockopt(struct socket *sock, int level, |
| 4169 | int optname) |
| 4170 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4171 | return sock_has_perm(current, sock->sk, SOCKET__GETOPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | static int selinux_socket_shutdown(struct socket *sock, int how) |
| 4175 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4176 | return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4179 | static int selinux_socket_unix_stream_connect(struct sock *sock, |
| 4180 | struct sock *other, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | struct sock *newsk) |
| 4182 | { |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4183 | struct sk_security_struct *sksec_sock = sock->sk_security; |
| 4184 | struct sk_security_struct *sksec_other = other->sk_security; |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4185 | struct sk_security_struct *sksec_new = newsk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4186 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4187 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4188 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | int err; |
| 4190 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4191 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4192 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4193 | ad.u.net = &net; |
| 4194 | ad.u.net->sk = other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4196 | err = avc_has_perm(sksec_sock->sid, sksec_other->sid, |
| 4197 | sksec_other->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | UNIX_STREAM_SOCKET__CONNECTTO, &ad); |
| 4199 | if (err) |
| 4200 | return err; |
| 4201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | /* server child socket */ |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4203 | sksec_new->peer_sid = sksec_sock->sid; |
| 4204 | err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, |
| 4205 | &sksec_new->sid); |
| 4206 | if (err) |
| 4207 | return err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4208 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4209 | /* connecting socket */ |
| 4210 | sksec_sock->peer_sid = sksec_new->sid; |
| 4211 | |
| 4212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | static int selinux_socket_unix_may_send(struct socket *sock, |
| 4216 | struct socket *other) |
| 4217 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4218 | struct sk_security_struct *ssec = sock->sk->sk_security; |
| 4219 | struct sk_security_struct *osec = other->sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4220 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4221 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4222 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4224 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4225 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4226 | ad.u.net = &net; |
| 4227 | ad.u.net->sk = other->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4229 | return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, |
| 4230 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | } |
| 4232 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4233 | static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, |
| 4234 | u32 peer_sid, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4235 | struct common_audit_data *ad) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4236 | { |
| 4237 | int err; |
| 4238 | u32 if_sid; |
| 4239 | u32 node_sid; |
| 4240 | |
| 4241 | err = sel_netif_sid(ifindex, &if_sid); |
| 4242 | if (err) |
| 4243 | return err; |
| 4244 | err = avc_has_perm(peer_sid, if_sid, |
| 4245 | SECCLASS_NETIF, NETIF__INGRESS, ad); |
| 4246 | if (err) |
| 4247 | return err; |
| 4248 | |
| 4249 | err = sel_netnode_sid(addrp, family, &node_sid); |
| 4250 | if (err) |
| 4251 | return err; |
| 4252 | return avc_has_perm(peer_sid, node_sid, |
| 4253 | SECCLASS_NODE, NODE__RECVFROM, ad); |
| 4254 | } |
| 4255 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4256 | static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4257 | u16 family) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4258 | { |
Paul Moore | 277d342 | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 4259 | int err = 0; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4260 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4261 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4262 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4263 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4264 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4265 | char *addrp; |
| 4266 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4267 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4268 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4269 | ad.u.net = &net; |
| 4270 | ad.u.net->netif = skb->skb_iif; |
| 4271 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4272 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
| 4273 | if (err) |
| 4274 | return err; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4275 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4276 | if (selinux_secmark_enabled()) { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4277 | err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4278 | PACKET__RECV, &ad); |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4279 | if (err) |
| 4280 | return err; |
| 4281 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4282 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4283 | err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); |
| 4284 | if (err) |
| 4285 | return err; |
| 4286 | err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4287 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4288 | return err; |
| 4289 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4290 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4291 | static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 4292 | { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4293 | int err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4294 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4295 | u16 family = sk->sk_family; |
| 4296 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4297 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4298 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4299 | struct lsm_network_audit net = {0,}; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4300 | char *addrp; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4301 | u8 secmark_active; |
| 4302 | u8 peerlbl_active; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4303 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4304 | if (family != PF_INET && family != PF_INET6) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4305 | return 0; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4306 | |
| 4307 | /* Handle mapped IPv4 packets arriving via IPv6 sockets */ |
Al Viro | 87fcd70 | 2006-12-04 22:00:55 +0000 | [diff] [blame] | 4308 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4309 | family = PF_INET; |
| 4310 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4311 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4312 | * to the selinux_sock_rcv_skb_compat() function to deal with the |
| 4313 | * special handling. We do this in an attempt to keep this function |
| 4314 | * as fast and as clean as possible. */ |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4315 | if (!selinux_policycap_netpeer) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4316 | return selinux_sock_rcv_skb_compat(sk, skb, family); |
| 4317 | |
| 4318 | secmark_active = selinux_secmark_enabled(); |
| 4319 | peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); |
| 4320 | if (!secmark_active && !peerlbl_active) |
| 4321 | return 0; |
| 4322 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4323 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4324 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4325 | ad.u.net = &net; |
| 4326 | ad.u.net->netif = skb->skb_iif; |
| 4327 | ad.u.net->family = family; |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4328 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4329 | if (err) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4330 | return err; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4331 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4332 | if (peerlbl_active) { |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4333 | u32 peer_sid; |
| 4334 | |
| 4335 | err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); |
| 4336 | if (err) |
| 4337 | return err; |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 4338 | err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4339 | peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4340 | if (err) { |
| 4341 | selinux_netlbl_err(skb, err, 0); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4342 | return err; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4343 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4344 | err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, |
| 4345 | PEER__RECV, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4346 | if (err) |
| 4347 | selinux_netlbl_err(skb, err, 0); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4348 | } |
| 4349 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4350 | if (secmark_active) { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4351 | err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, |
| 4352 | PACKET__RECV, &ad); |
| 4353 | if (err) |
| 4354 | return err; |
| 4355 | } |
| 4356 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4357 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4360 | static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, |
| 4361 | int __user *optlen, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | { |
| 4363 | int err = 0; |
| 4364 | char *scontext; |
| 4365 | u32 scontext_len; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4366 | struct sk_security_struct *sksec = sock->sk->sk_security; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4367 | u32 peer_sid = SECSID_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4369 | if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || |
| 4370 | sksec->sclass == SECCLASS_TCP_SOCKET) |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4371 | peer_sid = sksec->peer_sid; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4372 | if (peer_sid == SECSID_NULL) |
| 4373 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4375 | err = security_sid_to_context(peer_sid, &scontext, &scontext_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | if (err) |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4377 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | |
| 4379 | if (scontext_len > len) { |
| 4380 | err = -ERANGE; |
| 4381 | goto out_len; |
| 4382 | } |
| 4383 | |
| 4384 | if (copy_to_user(optval, scontext, scontext_len)) |
| 4385 | err = -EFAULT; |
| 4386 | |
| 4387 | out_len: |
| 4388 | if (put_user(scontext_len, optlen)) |
| 4389 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | kfree(scontext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | return err; |
| 4392 | } |
| 4393 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4394 | static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4395 | { |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4396 | u32 peer_secid = SECSID_NULL; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4397 | u16 family; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 4398 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4399 | if (skb && skb->protocol == htons(ETH_P_IP)) |
| 4400 | family = PF_INET; |
| 4401 | else if (skb && skb->protocol == htons(ETH_P_IPV6)) |
| 4402 | family = PF_INET6; |
| 4403 | else if (sock) |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4404 | family = sock->sk->sk_family; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4405 | else |
| 4406 | goto out; |
| 4407 | |
| 4408 | if (sock && family == PF_UNIX) |
Ahmed S. Darwish | 713a04a | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 4409 | selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4410 | else if (skb) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4411 | selinux_skb_peerlbl_sid(skb, family, &peer_secid); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4412 | |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4413 | out: |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4414 | *secid = peer_secid; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4415 | if (peer_secid == SECSID_NULL) |
| 4416 | return -EINVAL; |
| 4417 | return 0; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4418 | } |
| 4419 | |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 4420 | static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4422 | struct sk_security_struct *sksec; |
| 4423 | |
| 4424 | sksec = kzalloc(sizeof(*sksec), priority); |
| 4425 | if (!sksec) |
| 4426 | return -ENOMEM; |
| 4427 | |
| 4428 | sksec->peer_sid = SECINITSID_UNLABELED; |
| 4429 | sksec->sid = SECINITSID_UNLABELED; |
| 4430 | selinux_netlbl_sk_security_reset(sksec); |
| 4431 | sk->sk_security = sksec; |
| 4432 | |
| 4433 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | static void selinux_sk_free_security(struct sock *sk) |
| 4437 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4438 | struct sk_security_struct *sksec = sk->sk_security; |
| 4439 | |
| 4440 | sk->sk_security = NULL; |
| 4441 | selinux_netlbl_sk_security_free(sksec); |
| 4442 | kfree(sksec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | } |
| 4444 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4445 | static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) |
| 4446 | { |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4447 | struct sk_security_struct *sksec = sk->sk_security; |
| 4448 | struct sk_security_struct *newsksec = newsk->sk_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4449 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4450 | newsksec->sid = sksec->sid; |
| 4451 | newsksec->peer_sid = sksec->peer_sid; |
| 4452 | newsksec->sclass = sksec->sclass; |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 4453 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4454 | selinux_netlbl_sk_security_reset(newsksec); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4455 | } |
| 4456 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4457 | static void selinux_sk_getsecid(struct sock *sk, u32 *secid) |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4458 | { |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4459 | if (!sk) |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4460 | *secid = SECINITSID_ANY_SOCKET; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4461 | else { |
| 4462 | struct sk_security_struct *sksec = sk->sk_security; |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4463 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4464 | *secid = sksec->sid; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4465 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4466 | } |
| 4467 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4468 | static void selinux_sock_graft(struct sock *sk, struct socket *parent) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4469 | { |
| 4470 | struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; |
| 4471 | struct sk_security_struct *sksec = sk->sk_security; |
| 4472 | |
David Woodhouse | 2148ccc | 2006-09-29 15:50:25 -0700 | [diff] [blame] | 4473 | if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || |
| 4474 | sk->sk_family == PF_UNIX) |
| 4475 | isec->sid = sksec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4476 | sksec->sclass = isec->sclass; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4477 | } |
| 4478 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 4479 | static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 4480 | struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4481 | { |
| 4482 | struct sk_security_struct *sksec = sk->sk_security; |
| 4483 | int err; |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4484 | u16 family = sk->sk_family; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4485 | u32 newsid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4486 | u32 peersid; |
| 4487 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4488 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 4489 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 4490 | family = PF_INET; |
| 4491 | |
| 4492 | err = selinux_skb_peerlbl_sid(skb, family, &peersid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4493 | if (err) |
| 4494 | return err; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 4495 | if (peersid == SECSID_NULL) { |
| 4496 | req->secid = sksec->sid; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4497 | req->peer_secid = SECSID_NULL; |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4498 | } else { |
| 4499 | err = security_sid_mls_copy(sksec->sid, peersid, &newsid); |
| 4500 | if (err) |
| 4501 | return err; |
| 4502 | req->secid = newsid; |
| 4503 | req->peer_secid = peersid; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 4504 | } |
| 4505 | |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4506 | return selinux_netlbl_inet_conn_request(req, family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 4509 | static void selinux_inet_csk_clone(struct sock *newsk, |
| 4510 | const struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4511 | { |
| 4512 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 4513 | |
| 4514 | newsksec->sid = req->secid; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4515 | newsksec->peer_sid = req->peer_secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4516 | /* NOTE: Ideally, we should also get the isec->sid for the |
| 4517 | new socket in sync, but we don't have the isec available yet. |
| 4518 | So we will wait until sock_graft to do it, by which |
| 4519 | time it will have been created and available. */ |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 4520 | |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 4521 | /* We don't need to take any sort of lock here as we are the only |
| 4522 | * thread with access to newsksec */ |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4523 | selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4526 | static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4527 | { |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4528 | u16 family = sk->sk_family; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4529 | struct sk_security_struct *sksec = sk->sk_security; |
| 4530 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4531 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 4532 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 4533 | family = PF_INET; |
| 4534 | |
| 4535 | selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4536 | } |
| 4537 | |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 4538 | static int selinux_secmark_relabel_packet(u32 sid) |
| 4539 | { |
| 4540 | const struct task_security_struct *__tsec; |
| 4541 | u32 tsid; |
| 4542 | |
| 4543 | __tsec = current_security(); |
| 4544 | tsid = __tsec->sid; |
| 4545 | |
| 4546 | return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); |
| 4547 | } |
| 4548 | |
| 4549 | static void selinux_secmark_refcount_inc(void) |
| 4550 | { |
| 4551 | atomic_inc(&selinux_secmark_refcount); |
| 4552 | } |
| 4553 | |
| 4554 | static void selinux_secmark_refcount_dec(void) |
| 4555 | { |
| 4556 | atomic_dec(&selinux_secmark_refcount); |
| 4557 | } |
| 4558 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 4559 | static void selinux_req_classify_flow(const struct request_sock *req, |
| 4560 | struct flowi *fl) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4561 | { |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 4562 | fl->flowi_secid = req->secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4563 | } |
| 4564 | |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 4565 | static int selinux_tun_dev_create(void) |
| 4566 | { |
| 4567 | u32 sid = current_sid(); |
| 4568 | |
| 4569 | /* we aren't taking into account the "sockcreate" SID since the socket |
| 4570 | * that is being created here is not a socket in the traditional sense, |
| 4571 | * instead it is a private sock, accessible only to the kernel, and |
| 4572 | * representing a wide range of network traffic spanning multiple |
| 4573 | * connections unlike traditional sockets - check the TUN driver to |
| 4574 | * get a better understanding of why this socket is special */ |
| 4575 | |
| 4576 | return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, |
| 4577 | NULL); |
| 4578 | } |
| 4579 | |
| 4580 | static void selinux_tun_dev_post_create(struct sock *sk) |
| 4581 | { |
| 4582 | struct sk_security_struct *sksec = sk->sk_security; |
| 4583 | |
| 4584 | /* we don't currently perform any NetLabel based labeling here and it |
| 4585 | * isn't clear that we would want to do so anyway; while we could apply |
| 4586 | * labeling without the support of the TUN user the resulting labeled |
| 4587 | * traffic from the other end of the connection would almost certainly |
| 4588 | * cause confusion to the TUN user that had no idea network labeling |
| 4589 | * protocols were being used */ |
| 4590 | |
| 4591 | /* see the comments in selinux_tun_dev_create() about why we don't use |
| 4592 | * the sockcreate SID here */ |
| 4593 | |
| 4594 | sksec->sid = current_sid(); |
| 4595 | sksec->sclass = SECCLASS_TUN_SOCKET; |
| 4596 | } |
| 4597 | |
| 4598 | static int selinux_tun_dev_attach(struct sock *sk) |
| 4599 | { |
| 4600 | struct sk_security_struct *sksec = sk->sk_security; |
| 4601 | u32 sid = current_sid(); |
| 4602 | int err; |
| 4603 | |
| 4604 | err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET, |
| 4605 | TUN_SOCKET__RELABELFROM, NULL); |
| 4606 | if (err) |
| 4607 | return err; |
| 4608 | err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, |
| 4609 | TUN_SOCKET__RELABELTO, NULL); |
| 4610 | if (err) |
| 4611 | return err; |
| 4612 | |
| 4613 | sksec->sid = sid; |
| 4614 | |
| 4615 | return 0; |
| 4616 | } |
| 4617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) |
| 4619 | { |
| 4620 | int err = 0; |
| 4621 | u32 perm; |
| 4622 | struct nlmsghdr *nlh; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4623 | struct sk_security_struct *sksec = sk->sk_security; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | if (skb->len < NLMSG_SPACE(0)) { |
| 4626 | err = -EINVAL; |
| 4627 | goto out; |
| 4628 | } |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 4629 | nlh = nlmsg_hdr(skb); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4630 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4631 | err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | if (err) { |
| 4633 | if (err == -EINVAL) { |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 4634 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | "SELinux: unrecognized netlink message" |
| 4636 | " type=%hu for sclass=%hu\n", |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4637 | nlh->nlmsg_type, sksec->sclass); |
Eric Paris | 39c9aed | 2008-11-05 09:34:42 -0500 | [diff] [blame] | 4638 | if (!selinux_enforcing || security_get_allow_unknown()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | err = 0; |
| 4640 | } |
| 4641 | |
| 4642 | /* Ignore */ |
| 4643 | if (err == -ENOENT) |
| 4644 | err = 0; |
| 4645 | goto out; |
| 4646 | } |
| 4647 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4648 | err = sock_has_perm(current, sk, perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | out: |
| 4650 | return err; |
| 4651 | } |
| 4652 | |
| 4653 | #ifdef CONFIG_NETFILTER |
| 4654 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4655 | static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, |
| 4656 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4658 | int err; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4659 | char *addrp; |
| 4660 | u32 peer_sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4661 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4662 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4663 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4664 | u8 secmark_active; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4665 | u8 netlbl_active; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4666 | u8 peerlbl_active; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4667 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4668 | if (!selinux_policycap_netpeer) |
| 4669 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4670 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4671 | secmark_active = selinux_secmark_enabled(); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4672 | netlbl_active = netlbl_enabled(); |
| 4673 | peerlbl_active = netlbl_active || selinux_xfrm_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4674 | if (!secmark_active && !peerlbl_active) |
| 4675 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4676 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4677 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) |
| 4678 | return NF_DROP; |
| 4679 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4680 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4681 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4682 | ad.u.net = &net; |
| 4683 | ad.u.net->netif = ifindex; |
| 4684 | ad.u.net->family = family; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4685 | if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) |
| 4686 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4688 | if (peerlbl_active) { |
| 4689 | err = selinux_inet_sys_rcv_skb(ifindex, addrp, family, |
| 4690 | peer_sid, &ad); |
| 4691 | if (err) { |
| 4692 | selinux_netlbl_err(skb, err, 1); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4693 | return NF_DROP; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4694 | } |
| 4695 | } |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4696 | |
| 4697 | if (secmark_active) |
| 4698 | if (avc_has_perm(peer_sid, skb->secmark, |
| 4699 | SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) |
| 4700 | return NF_DROP; |
| 4701 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4702 | if (netlbl_active) |
| 4703 | /* we do this in the FORWARD path and not the POST_ROUTING |
| 4704 | * path because we want to make sure we apply the necessary |
| 4705 | * labeling before IPsec is applied so we can leverage AH |
| 4706 | * protection */ |
| 4707 | if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) |
| 4708 | return NF_DROP; |
| 4709 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4710 | return NF_ACCEPT; |
| 4711 | } |
| 4712 | |
| 4713 | static unsigned int selinux_ipv4_forward(unsigned int hooknum, |
| 4714 | struct sk_buff *skb, |
| 4715 | const struct net_device *in, |
| 4716 | const struct net_device *out, |
| 4717 | int (*okfn)(struct sk_buff *)) |
| 4718 | { |
| 4719 | return selinux_ip_forward(skb, in->ifindex, PF_INET); |
| 4720 | } |
| 4721 | |
| 4722 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 4723 | static unsigned int selinux_ipv6_forward(unsigned int hooknum, |
| 4724 | struct sk_buff *skb, |
| 4725 | const struct net_device *in, |
| 4726 | const struct net_device *out, |
| 4727 | int (*okfn)(struct sk_buff *)) |
| 4728 | { |
| 4729 | return selinux_ip_forward(skb, in->ifindex, PF_INET6); |
| 4730 | } |
| 4731 | #endif /* IPV6 */ |
| 4732 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4733 | static unsigned int selinux_ip_output(struct sk_buff *skb, |
| 4734 | u16 family) |
| 4735 | { |
| 4736 | u32 sid; |
| 4737 | |
| 4738 | if (!netlbl_enabled()) |
| 4739 | return NF_ACCEPT; |
| 4740 | |
| 4741 | /* we do this in the LOCAL_OUT path and not the POST_ROUTING path |
| 4742 | * because we want to make sure we apply the necessary labeling |
| 4743 | * before IPsec is applied so we can leverage AH protection */ |
| 4744 | if (skb->sk) { |
| 4745 | struct sk_security_struct *sksec = skb->sk->sk_security; |
| 4746 | sid = sksec->sid; |
| 4747 | } else |
| 4748 | sid = SECINITSID_KERNEL; |
| 4749 | if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) |
| 4750 | return NF_DROP; |
| 4751 | |
| 4752 | return NF_ACCEPT; |
| 4753 | } |
| 4754 | |
| 4755 | static unsigned int selinux_ipv4_output(unsigned int hooknum, |
| 4756 | struct sk_buff *skb, |
| 4757 | const struct net_device *in, |
| 4758 | const struct net_device *out, |
| 4759 | int (*okfn)(struct sk_buff *)) |
| 4760 | { |
| 4761 | return selinux_ip_output(skb, PF_INET); |
| 4762 | } |
| 4763 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4764 | static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, |
| 4765 | int ifindex, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4766 | u16 family) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4767 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4768 | struct sock *sk = skb->sk; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4769 | struct sk_security_struct *sksec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4770 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4771 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4772 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4773 | char *addrp; |
| 4774 | u8 proto; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4775 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4776 | if (sk == NULL) |
| 4777 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4778 | sksec = sk->sk_security; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4779 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4780 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4781 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4782 | ad.u.net = &net; |
| 4783 | ad.u.net->netif = ifindex; |
| 4784 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4785 | if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) |
| 4786 | return NF_DROP; |
| 4787 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4788 | if (selinux_secmark_enabled()) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4789 | if (avc_has_perm(sksec->sid, skb->secmark, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4790 | SECCLASS_PACKET, PACKET__SEND, &ad)) |
Eric Paris | 2fe66ec | 2010-11-23 06:28:08 +0000 | [diff] [blame] | 4791 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4792 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4793 | if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) |
| 4794 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4795 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4796 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | } |
| 4798 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4799 | static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, |
| 4800 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4802 | u32 secmark_perm; |
| 4803 | u32 peer_sid; |
| 4804 | struct sock *sk; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4805 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4806 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4807 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4808 | char *addrp; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4809 | u8 secmark_active; |
| 4810 | u8 peerlbl_active; |
| 4811 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4812 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4813 | * to the selinux_ip_postroute_compat() function to deal with the |
| 4814 | * special handling. We do this in an attempt to keep this function |
| 4815 | * as fast and as clean as possible. */ |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4816 | if (!selinux_policycap_netpeer) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4817 | return selinux_ip_postroute_compat(skb, ifindex, family); |
Alexey Dobriyan | def8b4f | 2008-10-28 13:24:06 -0700 | [diff] [blame] | 4818 | #ifdef CONFIG_XFRM |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4819 | /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec |
| 4820 | * packet transformation so allow the packet to pass without any checks |
| 4821 | * since we'll have another chance to perform access control checks |
| 4822 | * when the packet is on it's final way out. |
| 4823 | * NOTE: there appear to be some IPv6 multicast cases where skb->dst |
| 4824 | * is NULL, in this case go ahead and apply access control. */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4825 | if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4826 | return NF_ACCEPT; |
Alexey Dobriyan | def8b4f | 2008-10-28 13:24:06 -0700 | [diff] [blame] | 4827 | #endif |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4828 | secmark_active = selinux_secmark_enabled(); |
| 4829 | peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); |
| 4830 | if (!secmark_active && !peerlbl_active) |
| 4831 | return NF_ACCEPT; |
| 4832 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4833 | /* if the packet is being forwarded then get the peer label from the |
| 4834 | * packet itself; otherwise check to see if it is from a local |
| 4835 | * application or the kernel, if from an application get the peer label |
| 4836 | * from the sending socket, otherwise use the kernel's sid */ |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4837 | sk = skb->sk; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4838 | if (sk == NULL) { |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 4839 | if (skb->skb_iif) { |
| 4840 | secmark_perm = PACKET__FORWARD_OUT; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4841 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 4842 | return NF_DROP; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 4843 | } else { |
| 4844 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4845 | peer_sid = SECINITSID_KERNEL; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 4846 | } |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4847 | } else { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4848 | struct sk_security_struct *sksec = sk->sk_security; |
| 4849 | peer_sid = sksec->sid; |
| 4850 | secmark_perm = PACKET__SEND; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4851 | } |
| 4852 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4853 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4854 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4855 | ad.u.net = &net; |
| 4856 | ad.u.net->netif = ifindex; |
| 4857 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4858 | if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 4859 | return NF_DROP; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4860 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4861 | if (secmark_active) |
| 4862 | if (avc_has_perm(peer_sid, skb->secmark, |
| 4863 | SECCLASS_PACKET, secmark_perm, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 4864 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4865 | |
| 4866 | if (peerlbl_active) { |
| 4867 | u32 if_sid; |
| 4868 | u32 node_sid; |
| 4869 | |
| 4870 | if (sel_netif_sid(ifindex, &if_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 4871 | return NF_DROP; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4872 | if (avc_has_perm(peer_sid, if_sid, |
| 4873 | SECCLASS_NETIF, NETIF__EGRESS, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 4874 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4875 | |
| 4876 | if (sel_netnode_sid(addrp, family, &node_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 4877 | return NF_DROP; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4878 | if (avc_has_perm(peer_sid, node_sid, |
| 4879 | SECCLASS_NODE, NODE__SENDTO, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 4880 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | return NF_ACCEPT; |
| 4884 | } |
| 4885 | |
| 4886 | static unsigned int selinux_ipv4_postroute(unsigned int hooknum, |
| 4887 | struct sk_buff *skb, |
| 4888 | const struct net_device *in, |
| 4889 | const struct net_device *out, |
| 4890 | int (*okfn)(struct sk_buff *)) |
| 4891 | { |
| 4892 | return selinux_ip_postroute(skb, out->ifindex, PF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4896 | static unsigned int selinux_ipv6_postroute(unsigned int hooknum, |
| 4897 | struct sk_buff *skb, |
| 4898 | const struct net_device *in, |
| 4899 | const struct net_device *out, |
| 4900 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4902 | return selinux_ip_postroute(skb, out->ifindex, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | #endif /* IPV6 */ |
| 4905 | |
| 4906 | #endif /* CONFIG_NETFILTER */ |
| 4907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 4909 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | int err; |
| 4911 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 4912 | err = cap_netlink_send(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | if (err) |
| 4914 | return err; |
| 4915 | |
Stephen Smalley | 941fc5b | 2009-10-01 14:48:23 -0400 | [diff] [blame] | 4916 | return selinux_nlmsg_perm(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | } |
| 4918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | static int ipc_alloc_security(struct task_struct *task, |
| 4920 | struct kern_ipc_perm *perm, |
| 4921 | u16 sclass) |
| 4922 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | struct ipc_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4924 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 4926 | isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | if (!isec) |
| 4928 | return -ENOMEM; |
| 4929 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4930 | sid = task_sid(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | isec->sclass = sclass; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4932 | isec->sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | perm->security = isec; |
| 4934 | |
| 4935 | return 0; |
| 4936 | } |
| 4937 | |
| 4938 | static void ipc_free_security(struct kern_ipc_perm *perm) |
| 4939 | { |
| 4940 | struct ipc_security_struct *isec = perm->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | perm->security = NULL; |
| 4942 | kfree(isec); |
| 4943 | } |
| 4944 | |
| 4945 | static int msg_msg_alloc_security(struct msg_msg *msg) |
| 4946 | { |
| 4947 | struct msg_security_struct *msec; |
| 4948 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 4949 | msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | if (!msec) |
| 4951 | return -ENOMEM; |
| 4952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | msec->sid = SECINITSID_UNLABELED; |
| 4954 | msg->security = msec; |
| 4955 | |
| 4956 | return 0; |
| 4957 | } |
| 4958 | |
| 4959 | static void msg_msg_free_security(struct msg_msg *msg) |
| 4960 | { |
| 4961 | struct msg_security_struct *msec = msg->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | |
| 4963 | msg->security = NULL; |
| 4964 | kfree(msec); |
| 4965 | } |
| 4966 | |
| 4967 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 4968 | u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4971 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4972 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4973 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | isec = ipc_perms->security; |
| 4976 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4977 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4978 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | ad.u.ipc_id = ipc_perms->key; |
| 4980 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4981 | return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | static int selinux_msg_msg_alloc_security(struct msg_msg *msg) |
| 4985 | { |
| 4986 | return msg_msg_alloc_security(msg); |
| 4987 | } |
| 4988 | |
| 4989 | static void selinux_msg_msg_free_security(struct msg_msg *msg) |
| 4990 | { |
| 4991 | msg_msg_free_security(msg); |
| 4992 | } |
| 4993 | |
| 4994 | /* message queue security operations */ |
| 4995 | static int selinux_msg_queue_alloc_security(struct msg_queue *msq) |
| 4996 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4998 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4999 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5000 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | int rc; |
| 5002 | |
| 5003 | rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); |
| 5004 | if (rc) |
| 5005 | return rc; |
| 5006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | isec = msq->q_perm.security; |
| 5008 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5009 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5010 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5011 | ad.u.ipc_id = msq->q_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5013 | rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | MSGQ__CREATE, &ad); |
| 5015 | if (rc) { |
| 5016 | ipc_free_security(&msq->q_perm); |
| 5017 | return rc; |
| 5018 | } |
| 5019 | return 0; |
| 5020 | } |
| 5021 | |
| 5022 | static void selinux_msg_queue_free_security(struct msg_queue *msq) |
| 5023 | { |
| 5024 | ipc_free_security(&msq->q_perm); |
| 5025 | } |
| 5026 | |
| 5027 | static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) |
| 5028 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5030 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5031 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5032 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | isec = msq->q_perm.security; |
| 5035 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5036 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5037 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | ad.u.ipc_id = msq->q_perm.key; |
| 5039 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5040 | return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | MSGQ__ASSOCIATE, &ad); |
| 5042 | } |
| 5043 | |
| 5044 | static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) |
| 5045 | { |
| 5046 | int err; |
| 5047 | int perms; |
| 5048 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5049 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | case IPC_INFO: |
| 5051 | case MSG_INFO: |
| 5052 | /* No specific object, just general system-wide information. */ |
| 5053 | return task_has_system(current, SYSTEM__IPC_INFO); |
| 5054 | case IPC_STAT: |
| 5055 | case MSG_STAT: |
| 5056 | perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; |
| 5057 | break; |
| 5058 | case IPC_SET: |
| 5059 | perms = MSGQ__SETATTR; |
| 5060 | break; |
| 5061 | case IPC_RMID: |
| 5062 | perms = MSGQ__DESTROY; |
| 5063 | break; |
| 5064 | default: |
| 5065 | return 0; |
| 5066 | } |
| 5067 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5068 | err = ipc_has_perm(&msq->q_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | return err; |
| 5070 | } |
| 5071 | |
| 5072 | static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) |
| 5073 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | struct ipc_security_struct *isec; |
| 5075 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5076 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5077 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5078 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | int rc; |
| 5080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | isec = msq->q_perm.security; |
| 5082 | msec = msg->security; |
| 5083 | |
| 5084 | /* |
| 5085 | * First time through, need to assign label to the message |
| 5086 | */ |
| 5087 | if (msec->sid == SECINITSID_UNLABELED) { |
| 5088 | /* |
| 5089 | * Compute new sid based on current process and |
| 5090 | * message queue this message will be stored in |
| 5091 | */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5092 | rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 5093 | NULL, &msec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | if (rc) |
| 5095 | return rc; |
| 5096 | } |
| 5097 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5098 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5099 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | ad.u.ipc_id = msq->q_perm.key; |
| 5101 | |
| 5102 | /* Can this process write to the queue? */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5103 | rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | MSGQ__WRITE, &ad); |
| 5105 | if (!rc) |
| 5106 | /* Can this process send the message */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5107 | rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, |
| 5108 | MSG__SEND, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | if (!rc) |
| 5110 | /* Can the message be put in the queue? */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5111 | rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, |
| 5112 | MSGQ__ENQUEUE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
| 5114 | return rc; |
| 5115 | } |
| 5116 | |
| 5117 | static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, |
| 5118 | struct task_struct *target, |
| 5119 | long type, int mode) |
| 5120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | struct ipc_security_struct *isec; |
| 5122 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5123 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5124 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5125 | u32 sid = task_sid(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | int rc; |
| 5127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | isec = msq->q_perm.security; |
| 5129 | msec = msg->security; |
| 5130 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5131 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5132 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5133 | ad.u.ipc_id = msq->q_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5135 | rc = avc_has_perm(sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | SECCLASS_MSGQ, MSGQ__READ, &ad); |
| 5137 | if (!rc) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5138 | rc = avc_has_perm(sid, msec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | SECCLASS_MSG, MSG__RECEIVE, &ad); |
| 5140 | return rc; |
| 5141 | } |
| 5142 | |
| 5143 | /* Shared Memory security operations */ |
| 5144 | static int selinux_shm_alloc_security(struct shmid_kernel *shp) |
| 5145 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5147 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5148 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5149 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | int rc; |
| 5151 | |
| 5152 | rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); |
| 5153 | if (rc) |
| 5154 | return rc; |
| 5155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | isec = shp->shm_perm.security; |
| 5157 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5158 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5159 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5160 | ad.u.ipc_id = shp->shm_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5162 | rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | SHM__CREATE, &ad); |
| 5164 | if (rc) { |
| 5165 | ipc_free_security(&shp->shm_perm); |
| 5166 | return rc; |
| 5167 | } |
| 5168 | return 0; |
| 5169 | } |
| 5170 | |
| 5171 | static void selinux_shm_free_security(struct shmid_kernel *shp) |
| 5172 | { |
| 5173 | ipc_free_security(&shp->shm_perm); |
| 5174 | } |
| 5175 | |
| 5176 | static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) |
| 5177 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5179 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5180 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5181 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | isec = shp->shm_perm.security; |
| 5184 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5185 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5186 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | ad.u.ipc_id = shp->shm_perm.key; |
| 5188 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5189 | return avc_has_perm(sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | SHM__ASSOCIATE, &ad); |
| 5191 | } |
| 5192 | |
| 5193 | /* Note, at this point, shp is locked down */ |
| 5194 | static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) |
| 5195 | { |
| 5196 | int perms; |
| 5197 | int err; |
| 5198 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5199 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | case IPC_INFO: |
| 5201 | case SHM_INFO: |
| 5202 | /* No specific object, just general system-wide information. */ |
| 5203 | return task_has_system(current, SYSTEM__IPC_INFO); |
| 5204 | case IPC_STAT: |
| 5205 | case SHM_STAT: |
| 5206 | perms = SHM__GETATTR | SHM__ASSOCIATE; |
| 5207 | break; |
| 5208 | case IPC_SET: |
| 5209 | perms = SHM__SETATTR; |
| 5210 | break; |
| 5211 | case SHM_LOCK: |
| 5212 | case SHM_UNLOCK: |
| 5213 | perms = SHM__LOCK; |
| 5214 | break; |
| 5215 | case IPC_RMID: |
| 5216 | perms = SHM__DESTROY; |
| 5217 | break; |
| 5218 | default: |
| 5219 | return 0; |
| 5220 | } |
| 5221 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5222 | err = ipc_has_perm(&shp->shm_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | return err; |
| 5224 | } |
| 5225 | |
| 5226 | static int selinux_shm_shmat(struct shmid_kernel *shp, |
| 5227 | char __user *shmaddr, int shmflg) |
| 5228 | { |
| 5229 | u32 perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | |
| 5231 | if (shmflg & SHM_RDONLY) |
| 5232 | perms = SHM__READ; |
| 5233 | else |
| 5234 | perms = SHM__READ | SHM__WRITE; |
| 5235 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5236 | return ipc_has_perm(&shp->shm_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /* Semaphore security operations */ |
| 5240 | static int selinux_sem_alloc_security(struct sem_array *sma) |
| 5241 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5243 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5244 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5245 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | int rc; |
| 5247 | |
| 5248 | rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); |
| 5249 | if (rc) |
| 5250 | return rc; |
| 5251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | isec = sma->sem_perm.security; |
| 5253 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5254 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5255 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5256 | ad.u.ipc_id = sma->sem_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5258 | rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | SEM__CREATE, &ad); |
| 5260 | if (rc) { |
| 5261 | ipc_free_security(&sma->sem_perm); |
| 5262 | return rc; |
| 5263 | } |
| 5264 | return 0; |
| 5265 | } |
| 5266 | |
| 5267 | static void selinux_sem_free_security(struct sem_array *sma) |
| 5268 | { |
| 5269 | ipc_free_security(&sma->sem_perm); |
| 5270 | } |
| 5271 | |
| 5272 | static int selinux_sem_associate(struct sem_array *sma, int semflg) |
| 5273 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5275 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5276 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5277 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | isec = sma->sem_perm.security; |
| 5280 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5281 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5282 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | ad.u.ipc_id = sma->sem_perm.key; |
| 5284 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5285 | return avc_has_perm(sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | SEM__ASSOCIATE, &ad); |
| 5287 | } |
| 5288 | |
| 5289 | /* Note, at this point, sma is locked down */ |
| 5290 | static int selinux_sem_semctl(struct sem_array *sma, int cmd) |
| 5291 | { |
| 5292 | int err; |
| 5293 | u32 perms; |
| 5294 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5295 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | case IPC_INFO: |
| 5297 | case SEM_INFO: |
| 5298 | /* No specific object, just general system-wide information. */ |
| 5299 | return task_has_system(current, SYSTEM__IPC_INFO); |
| 5300 | case GETPID: |
| 5301 | case GETNCNT: |
| 5302 | case GETZCNT: |
| 5303 | perms = SEM__GETATTR; |
| 5304 | break; |
| 5305 | case GETVAL: |
| 5306 | case GETALL: |
| 5307 | perms = SEM__READ; |
| 5308 | break; |
| 5309 | case SETVAL: |
| 5310 | case SETALL: |
| 5311 | perms = SEM__WRITE; |
| 5312 | break; |
| 5313 | case IPC_RMID: |
| 5314 | perms = SEM__DESTROY; |
| 5315 | break; |
| 5316 | case IPC_SET: |
| 5317 | perms = SEM__SETATTR; |
| 5318 | break; |
| 5319 | case IPC_STAT: |
| 5320 | case SEM_STAT: |
| 5321 | perms = SEM__GETATTR | SEM__ASSOCIATE; |
| 5322 | break; |
| 5323 | default: |
| 5324 | return 0; |
| 5325 | } |
| 5326 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5327 | err = ipc_has_perm(&sma->sem_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | return err; |
| 5329 | } |
| 5330 | |
| 5331 | static int selinux_sem_semop(struct sem_array *sma, |
| 5332 | struct sembuf *sops, unsigned nsops, int alter) |
| 5333 | { |
| 5334 | u32 perms; |
| 5335 | |
| 5336 | if (alter) |
| 5337 | perms = SEM__READ | SEM__WRITE; |
| 5338 | else |
| 5339 | perms = SEM__READ; |
| 5340 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5341 | return ipc_has_perm(&sma->sem_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) |
| 5345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | u32 av = 0; |
| 5347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | av = 0; |
| 5349 | if (flag & S_IRUGO) |
| 5350 | av |= IPC__UNIX_READ; |
| 5351 | if (flag & S_IWUGO) |
| 5352 | av |= IPC__UNIX_WRITE; |
| 5353 | |
| 5354 | if (av == 0) |
| 5355 | return 0; |
| 5356 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5357 | return ipc_has_perm(ipcp, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | } |
| 5359 | |
Ahmed S. Darwish | 713a04a | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 5360 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 5361 | { |
| 5362 | struct ipc_security_struct *isec = ipcp->security; |
| 5363 | *secid = isec->sid; |
| 5364 | } |
| 5365 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5366 | static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | { |
| 5368 | if (inode) |
| 5369 | inode_doinit_with_dentry(inode, dentry); |
| 5370 | } |
| 5371 | |
| 5372 | static int selinux_getprocattr(struct task_struct *p, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 5373 | char *name, char **value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5375 | const struct task_security_struct *__tsec; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 5376 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | int error; |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 5378 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | |
| 5380 | if (current != p) { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5381 | error = current_has_perm(p, PROCESS__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | if (error) |
| 5383 | return error; |
| 5384 | } |
| 5385 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5386 | rcu_read_lock(); |
| 5387 | __tsec = __task_cred(p)->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | |
| 5389 | if (!strcmp(name, "current")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5390 | sid = __tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | else if (!strcmp(name, "prev")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5392 | sid = __tsec->osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | else if (!strcmp(name, "exec")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5394 | sid = __tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | else if (!strcmp(name, "fscreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5396 | sid = __tsec->create_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5397 | else if (!strcmp(name, "keycreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5398 | sid = __tsec->keycreate_sid; |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 5399 | else if (!strcmp(name, "sockcreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5400 | sid = __tsec->sockcreate_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | else |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5402 | goto invalid; |
| 5403 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | |
| 5405 | if (!sid) |
| 5406 | return 0; |
| 5407 | |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 5408 | error = security_sid_to_context(sid, value, &len); |
| 5409 | if (error) |
| 5410 | return error; |
| 5411 | return len; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5412 | |
| 5413 | invalid: |
| 5414 | rcu_read_unlock(); |
| 5415 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | static int selinux_setprocattr(struct task_struct *p, |
| 5419 | char *name, void *value, size_t size) |
| 5420 | { |
| 5421 | struct task_security_struct *tsec; |
Roland McGrath | 0356357 | 2008-03-26 15:46:39 -0700 | [diff] [blame] | 5422 | struct task_struct *tracer; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5423 | struct cred *new; |
| 5424 | u32 sid = 0, ptsid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | int error; |
| 5426 | char *str = value; |
| 5427 | |
| 5428 | if (current != p) { |
| 5429 | /* SELinux only allows a process to change its own |
| 5430 | security attributes. */ |
| 5431 | return -EACCES; |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * Basic control over ability to set these attributes at all. |
| 5436 | * current == p, but we'll pass them separately in case the |
| 5437 | * above restriction is ever removed. |
| 5438 | */ |
| 5439 | if (!strcmp(name, "exec")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5440 | error = current_has_perm(p, PROCESS__SETEXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | else if (!strcmp(name, "fscreate")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5442 | error = current_has_perm(p, PROCESS__SETFSCREATE); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5443 | else if (!strcmp(name, "keycreate")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5444 | error = current_has_perm(p, PROCESS__SETKEYCREATE); |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 5445 | else if (!strcmp(name, "sockcreate")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5446 | error = current_has_perm(p, PROCESS__SETSOCKCREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | else if (!strcmp(name, "current")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5448 | error = current_has_perm(p, PROCESS__SETCURRENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | else |
| 5450 | error = -EINVAL; |
| 5451 | if (error) |
| 5452 | return error; |
| 5453 | |
| 5454 | /* Obtain a SID for the context, if one was specified. */ |
| 5455 | if (size && str[1] && str[1] != '\n') { |
| 5456 | if (str[size-1] == '\n') { |
| 5457 | str[size-1] = 0; |
| 5458 | size--; |
| 5459 | } |
| 5460 | error = security_context_to_sid(value, size, &sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 5461 | if (error == -EINVAL && !strcmp(name, "fscreate")) { |
| 5462 | if (!capable(CAP_MAC_ADMIN)) |
| 5463 | return error; |
| 5464 | error = security_context_to_sid_force(value, size, |
| 5465 | &sid); |
| 5466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | if (error) |
| 5468 | return error; |
| 5469 | } |
| 5470 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5471 | new = prepare_creds(); |
| 5472 | if (!new) |
| 5473 | return -ENOMEM; |
| 5474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | /* Permission checking based on the specified context is |
| 5476 | performed during the actual operation (execve, |
| 5477 | open/mkdir/...), when we know the full context of the |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5478 | operation. See selinux_bprm_set_creds for the execve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | checks and may_create for the file creation checks. The |
| 5480 | operation will then fail if the context is not permitted. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5481 | tsec = new->security; |
| 5482 | if (!strcmp(name, "exec")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | tsec->exec_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5484 | } else if (!strcmp(name, "fscreate")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | tsec->create_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5486 | } else if (!strcmp(name, "keycreate")) { |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5487 | error = may_create_key(sid, p); |
| 5488 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5489 | goto abort_change; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5490 | tsec->keycreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5491 | } else if (!strcmp(name, "sockcreate")) { |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 5492 | tsec->sockcreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5493 | } else if (!strcmp(name, "current")) { |
| 5494 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | if (sid == 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5496 | goto abort_change; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 5497 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5498 | /* Only allow single threaded processes to change context */ |
| 5499 | error = -EPERM; |
Oleg Nesterov | 5bb459b | 2009-07-10 03:48:23 +0200 | [diff] [blame] | 5500 | if (!current_is_single_threaded()) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5501 | error = security_bounded_transition(tsec->sid, sid); |
| 5502 | if (error) |
| 5503 | goto abort_change; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | |
| 5506 | /* Check permissions for the transition. */ |
| 5507 | error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5508 | PROCESS__DYNTRANSITION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5510 | goto abort_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | |
| 5512 | /* Check for ptracing, and update the task SID if ok. |
| 5513 | Otherwise, leave SID unchanged and fail. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5514 | ptsid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | task_lock(p); |
Tejun Heo | 06d9847 | 2011-06-17 16:50:40 +0200 | [diff] [blame] | 5516 | tracer = ptrace_parent(p); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5517 | if (tracer) |
| 5518 | ptsid = task_sid(tracer); |
| 5519 | task_unlock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5521 | if (tracer) { |
| 5522 | error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, |
| 5523 | PROCESS__PTRACE, NULL); |
| 5524 | if (error) |
| 5525 | goto abort_change; |
| 5526 | } |
| 5527 | |
| 5528 | tsec->sid = sid; |
| 5529 | } else { |
| 5530 | error = -EINVAL; |
| 5531 | goto abort_change; |
| 5532 | } |
| 5533 | |
| 5534 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | return size; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5536 | |
| 5537 | abort_change: |
| 5538 | abort_creds(new); |
| 5539 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | } |
| 5541 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5542 | static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 5543 | { |
| 5544 | return security_sid_to_context(secid, secdata, seclen); |
| 5545 | } |
| 5546 | |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 5547 | static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 5548 | { |
| 5549 | return security_context_to_sid(secdata, seclen, secid); |
| 5550 | } |
| 5551 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5552 | static void selinux_release_secctx(char *secdata, u32 seclen) |
| 5553 | { |
Paul Moore | 088999e | 2007-08-01 11:12:58 -0400 | [diff] [blame] | 5554 | kfree(secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5555 | } |
| 5556 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 5557 | /* |
| 5558 | * called with inode->i_mutex locked |
| 5559 | */ |
| 5560 | static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 5561 | { |
| 5562 | return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); |
| 5563 | } |
| 5564 | |
| 5565 | /* |
| 5566 | * called with inode->i_mutex locked |
| 5567 | */ |
| 5568 | static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 5569 | { |
| 5570 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); |
| 5571 | } |
| 5572 | |
| 5573 | static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 5574 | { |
| 5575 | int len = 0; |
| 5576 | len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 5577 | ctx, true); |
| 5578 | if (len < 0) |
| 5579 | return len; |
| 5580 | *ctxlen = len; |
| 5581 | return 0; |
| 5582 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5583 | #ifdef CONFIG_KEYS |
| 5584 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5585 | static int selinux_key_alloc(struct key *k, const struct cred *cred, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 5586 | unsigned long flags) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5587 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5588 | const struct task_security_struct *tsec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5589 | struct key_security_struct *ksec; |
| 5590 | |
| 5591 | ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); |
| 5592 | if (!ksec) |
| 5593 | return -ENOMEM; |
| 5594 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5595 | tsec = cred->security; |
| 5596 | if (tsec->keycreate_sid) |
| 5597 | ksec->sid = tsec->keycreate_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5598 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5599 | ksec->sid = tsec->sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5600 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5601 | k->security = ksec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5602 | return 0; |
| 5603 | } |
| 5604 | |
| 5605 | static void selinux_key_free(struct key *k) |
| 5606 | { |
| 5607 | struct key_security_struct *ksec = k->security; |
| 5608 | |
| 5609 | k->security = NULL; |
| 5610 | kfree(ksec); |
| 5611 | } |
| 5612 | |
| 5613 | static int selinux_key_permission(key_ref_t key_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5614 | const struct cred *cred, |
| 5615 | key_perm_t perm) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5616 | { |
| 5617 | struct key *key; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5618 | struct key_security_struct *ksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5619 | u32 sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5620 | |
| 5621 | /* if no specific permissions are requested, we skip the |
| 5622 | permission check. No serious, additional covert channels |
| 5623 | appear to be created. */ |
| 5624 | if (perm == 0) |
| 5625 | return 0; |
| 5626 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5627 | sid = cred_sid(cred); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5628 | |
| 5629 | key = key_ref_to_ptr(key_ref); |
| 5630 | ksec = key->security; |
| 5631 | |
| 5632 | return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5633 | } |
| 5634 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 5635 | static int selinux_key_getsecurity(struct key *key, char **_buffer) |
| 5636 | { |
| 5637 | struct key_security_struct *ksec = key->security; |
| 5638 | char *context = NULL; |
| 5639 | unsigned len; |
| 5640 | int rc; |
| 5641 | |
| 5642 | rc = security_sid_to_context(ksec->sid, &context, &len); |
| 5643 | if (!rc) |
| 5644 | rc = len; |
| 5645 | *_buffer = context; |
| 5646 | return rc; |
| 5647 | } |
| 5648 | |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5649 | #endif |
| 5650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | static struct security_operations selinux_ops = { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 5652 | .name = "selinux", |
| 5653 | |
Stephen Smalley | 48a2370 | 2012-11-05 08:15:34 -0500 | [diff] [blame] | 5654 | .binder_set_context_mgr = selinux_binder_set_context_mgr, |
| 5655 | .binder_transaction = selinux_binder_transaction, |
| 5656 | .binder_transfer_binder = selinux_binder_transfer_binder, |
| 5657 | .binder_transfer_file = selinux_binder_transfer_file, |
| 5658 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 5659 | .ptrace_access_check = selinux_ptrace_access_check, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 5660 | .ptrace_traceme = selinux_ptrace_traceme, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | .capget = selinux_capget, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5662 | .capset = selinux_capset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | .capable = selinux_capable, |
| 5664 | .quotactl = selinux_quotactl, |
| 5665 | .quota_on = selinux_quota_on, |
| 5666 | .syslog = selinux_syslog, |
| 5667 | .vm_enough_memory = selinux_vm_enough_memory, |
| 5668 | |
| 5669 | .netlink_send = selinux_netlink_send, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 5671 | .bprm_set_creds = selinux_bprm_set_creds, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 5672 | .bprm_committing_creds = selinux_bprm_committing_creds, |
| 5673 | .bprm_committed_creds = selinux_bprm_committed_creds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | .bprm_secureexec = selinux_bprm_secureexec, |
| 5675 | |
| 5676 | .sb_alloc_security = selinux_sb_alloc_security, |
| 5677 | .sb_free_security = selinux_sb_free_security, |
| 5678 | .sb_copy_data = selinux_sb_copy_data, |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 5679 | .sb_remount = selinux_sb_remount, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5680 | .sb_kern_mount = selinux_sb_kern_mount, |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 5681 | .sb_show_options = selinux_sb_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | .sb_statfs = selinux_sb_statfs, |
| 5683 | .sb_mount = selinux_mount, |
| 5684 | .sb_umount = selinux_umount, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 5685 | .sb_set_mnt_opts = selinux_set_mnt_opts, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5686 | .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 5687 | .sb_parse_opts_str = selinux_parse_opts_str, |
| 5688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | |
| 5690 | .inode_alloc_security = selinux_inode_alloc_security, |
| 5691 | .inode_free_security = selinux_inode_free_security, |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 5692 | .inode_init_security = selinux_inode_init_security, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | .inode_create = selinux_inode_create, |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 5694 | .inode_post_create = selinux_inode_post_create, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | .inode_link = selinux_inode_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | .inode_unlink = selinux_inode_unlink, |
| 5697 | .inode_symlink = selinux_inode_symlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | .inode_mkdir = selinux_inode_mkdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | .inode_rmdir = selinux_inode_rmdir, |
| 5700 | .inode_mknod = selinux_inode_mknod, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | .inode_rename = selinux_inode_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | .inode_readlink = selinux_inode_readlink, |
| 5703 | .inode_follow_link = selinux_inode_follow_link, |
| 5704 | .inode_permission = selinux_inode_permission, |
| 5705 | .inode_setattr = selinux_inode_setattr, |
| 5706 | .inode_getattr = selinux_inode_getattr, |
| 5707 | .inode_setxattr = selinux_inode_setxattr, |
| 5708 | .inode_post_setxattr = selinux_inode_post_setxattr, |
| 5709 | .inode_getxattr = selinux_inode_getxattr, |
| 5710 | .inode_listxattr = selinux_inode_listxattr, |
| 5711 | .inode_removexattr = selinux_inode_removexattr, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5712 | .inode_getsecurity = selinux_inode_getsecurity, |
| 5713 | .inode_setsecurity = selinux_inode_setsecurity, |
| 5714 | .inode_listsecurity = selinux_inode_listsecurity, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 5715 | .inode_getsecid = selinux_inode_getsecid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | |
| 5717 | .file_permission = selinux_file_permission, |
| 5718 | .file_alloc_security = selinux_file_alloc_security, |
| 5719 | .file_free_security = selinux_file_free_security, |
| 5720 | .file_ioctl = selinux_file_ioctl, |
| 5721 | .file_mmap = selinux_file_mmap, |
| 5722 | .file_mprotect = selinux_file_mprotect, |
| 5723 | .file_lock = selinux_file_lock, |
| 5724 | .file_fcntl = selinux_file_fcntl, |
| 5725 | .file_set_fowner = selinux_file_set_fowner, |
| 5726 | .file_send_sigiotask = selinux_file_send_sigiotask, |
| 5727 | .file_receive = selinux_file_receive, |
| 5728 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5729 | .dentry_open = selinux_dentry_open, |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 5730 | .file_close = selinux_file_close, |
| 5731 | .allow_merge_bio = selinux_allow_merge_bio, |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 5732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | .task_create = selinux_task_create, |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 5734 | .cred_alloc_blank = selinux_cred_alloc_blank, |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 5735 | .cred_free = selinux_cred_free, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5736 | .cred_prepare = selinux_cred_prepare, |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 5737 | .cred_transfer = selinux_cred_transfer, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 5738 | .kernel_act_as = selinux_kernel_act_as, |
| 5739 | .kernel_create_files_as = selinux_kernel_create_files_as, |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 5740 | .kernel_module_request = selinux_kernel_module_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | .task_setpgid = selinux_task_setpgid, |
| 5742 | .task_getpgid = selinux_task_getpgid, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5743 | .task_getsid = selinux_task_getsid, |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 5744 | .task_getsecid = selinux_task_getsecid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | .task_setnice = selinux_task_setnice, |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 5746 | .task_setioprio = selinux_task_setioprio, |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 5747 | .task_getioprio = selinux_task_getioprio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | .task_setrlimit = selinux_task_setrlimit, |
| 5749 | .task_setscheduler = selinux_task_setscheduler, |
| 5750 | .task_getscheduler = selinux_task_getscheduler, |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 5751 | .task_movememory = selinux_task_movememory, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | .task_kill = selinux_task_kill, |
| 5753 | .task_wait = selinux_task_wait, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5754 | .task_to_inode = selinux_task_to_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
| 5756 | .ipc_permission = selinux_ipc_permission, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 5757 | .ipc_getsecid = selinux_ipc_getsecid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | |
| 5759 | .msg_msg_alloc_security = selinux_msg_msg_alloc_security, |
| 5760 | .msg_msg_free_security = selinux_msg_msg_free_security, |
| 5761 | |
| 5762 | .msg_queue_alloc_security = selinux_msg_queue_alloc_security, |
| 5763 | .msg_queue_free_security = selinux_msg_queue_free_security, |
| 5764 | .msg_queue_associate = selinux_msg_queue_associate, |
| 5765 | .msg_queue_msgctl = selinux_msg_queue_msgctl, |
| 5766 | .msg_queue_msgsnd = selinux_msg_queue_msgsnd, |
| 5767 | .msg_queue_msgrcv = selinux_msg_queue_msgrcv, |
| 5768 | |
| 5769 | .shm_alloc_security = selinux_shm_alloc_security, |
| 5770 | .shm_free_security = selinux_shm_free_security, |
| 5771 | .shm_associate = selinux_shm_associate, |
| 5772 | .shm_shmctl = selinux_shm_shmctl, |
| 5773 | .shm_shmat = selinux_shm_shmat, |
| 5774 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5775 | .sem_alloc_security = selinux_sem_alloc_security, |
| 5776 | .sem_free_security = selinux_sem_free_security, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | .sem_associate = selinux_sem_associate, |
| 5778 | .sem_semctl = selinux_sem_semctl, |
| 5779 | .sem_semop = selinux_sem_semop, |
| 5780 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5781 | .d_instantiate = selinux_d_instantiate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5783 | .getprocattr = selinux_getprocattr, |
| 5784 | .setprocattr = selinux_setprocattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5786 | .secid_to_secctx = selinux_secid_to_secctx, |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 5787 | .secctx_to_secid = selinux_secctx_to_secid, |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5788 | .release_secctx = selinux_release_secctx, |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 5789 | .inode_notifysecctx = selinux_inode_notifysecctx, |
| 5790 | .inode_setsecctx = selinux_inode_setsecctx, |
| 5791 | .inode_getsecctx = selinux_inode_getsecctx, |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5792 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5793 | .unix_stream_connect = selinux_socket_unix_stream_connect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | .unix_may_send = selinux_socket_unix_may_send, |
| 5795 | |
| 5796 | .socket_create = selinux_socket_create, |
| 5797 | .socket_post_create = selinux_socket_post_create, |
| 5798 | .socket_bind = selinux_socket_bind, |
| 5799 | .socket_connect = selinux_socket_connect, |
| 5800 | .socket_listen = selinux_socket_listen, |
| 5801 | .socket_accept = selinux_socket_accept, |
| 5802 | .socket_sendmsg = selinux_socket_sendmsg, |
| 5803 | .socket_recvmsg = selinux_socket_recvmsg, |
| 5804 | .socket_getsockname = selinux_socket_getsockname, |
| 5805 | .socket_getpeername = selinux_socket_getpeername, |
| 5806 | .socket_getsockopt = selinux_socket_getsockopt, |
| 5807 | .socket_setsockopt = selinux_socket_setsockopt, |
| 5808 | .socket_shutdown = selinux_socket_shutdown, |
| 5809 | .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5810 | .socket_getpeersec_stream = selinux_socket_getpeersec_stream, |
| 5811 | .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | .sk_alloc_security = selinux_sk_alloc_security, |
| 5813 | .sk_free_security = selinux_sk_free_security, |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5814 | .sk_clone_security = selinux_sk_clone_security, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5815 | .sk_getsecid = selinux_sk_getsecid, |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5816 | .sock_graft = selinux_sock_graft, |
| 5817 | .inet_conn_request = selinux_inet_conn_request, |
| 5818 | .inet_csk_clone = selinux_inet_csk_clone, |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5819 | .inet_conn_established = selinux_inet_conn_established, |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5820 | .secmark_relabel_packet = selinux_secmark_relabel_packet, |
| 5821 | .secmark_refcount_inc = selinux_secmark_refcount_inc, |
| 5822 | .secmark_refcount_dec = selinux_secmark_refcount_dec, |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5823 | .req_classify_flow = selinux_req_classify_flow, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5824 | .tun_dev_create = selinux_tun_dev_create, |
| 5825 | .tun_dev_post_create = selinux_tun_dev_post_create, |
| 5826 | .tun_dev_attach = selinux_tun_dev_attach, |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5827 | |
| 5828 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 5829 | .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, |
| 5830 | .xfrm_policy_clone_security = selinux_xfrm_policy_clone, |
| 5831 | .xfrm_policy_free_security = selinux_xfrm_policy_free, |
Catherine Zhang | c8c05a8 | 2006-06-08 23:39:49 -0700 | [diff] [blame] | 5832 | .xfrm_policy_delete_security = selinux_xfrm_policy_delete, |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5833 | .xfrm_state_alloc_security = selinux_xfrm_state_alloc, |
| 5834 | .xfrm_state_free_security = selinux_xfrm_state_free, |
Catherine Zhang | c8c05a8 | 2006-06-08 23:39:49 -0700 | [diff] [blame] | 5835 | .xfrm_state_delete_security = selinux_xfrm_state_delete, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5836 | .xfrm_policy_lookup = selinux_xfrm_policy_lookup, |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 5837 | .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 5838 | .xfrm_decode_session = selinux_xfrm_decode_session, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5840 | |
| 5841 | #ifdef CONFIG_KEYS |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5842 | .key_alloc = selinux_key_alloc, |
| 5843 | .key_free = selinux_key_free, |
| 5844 | .key_permission = selinux_key_permission, |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 5845 | .key_getsecurity = selinux_key_getsecurity, |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5846 | #endif |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 5847 | |
| 5848 | #ifdef CONFIG_AUDIT |
| 5849 | .audit_rule_init = selinux_audit_rule_init, |
| 5850 | .audit_rule_known = selinux_audit_rule_known, |
| 5851 | .audit_rule_match = selinux_audit_rule_match, |
| 5852 | .audit_rule_free = selinux_audit_rule_free, |
| 5853 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | }; |
| 5855 | |
| 5856 | static __init int selinux_init(void) |
| 5857 | { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 5858 | if (!security_module_enable(&selinux_ops)) { |
| 5859 | selinux_enabled = 0; |
| 5860 | return 0; |
| 5861 | } |
| 5862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | if (!selinux_enabled) { |
| 5864 | printk(KERN_INFO "SELinux: Disabled at boot.\n"); |
| 5865 | return 0; |
| 5866 | } |
| 5867 | |
| 5868 | printk(KERN_INFO "SELinux: Initializing.\n"); |
| 5869 | |
| 5870 | /* Set the security state for the initial task. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5871 | cred_init_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 5873 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 5874 | |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 5875 | sel_inode_cache = kmem_cache_create("selinux_inode_security", |
| 5876 | sizeof(struct inode_security_struct), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 5877 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | avc_init(); |
| 5879 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5880 | if (register_security(&selinux_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | panic("SELinux: Unable to register with kernel.\n"); |
| 5882 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5883 | if (selinux_enforcing) |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 5884 | printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5885 | else |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 5886 | printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | return 0; |
| 5889 | } |
| 5890 | |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 5891 | static void delayed_superblock_init(struct super_block *sb, void *unused) |
| 5892 | { |
| 5893 | superblock_doinit(sb, NULL); |
| 5894 | } |
| 5895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | void selinux_complete_init(void) |
| 5897 | { |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 5898 | printk(KERN_DEBUG "SELinux: Completing initialization.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | |
| 5900 | /* Set up any superblocks initialized prior to the policy load. */ |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 5901 | printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 5902 | iterate_supers(delayed_superblock_init, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
| 5905 | /* SELinux requires early initialization in order to label |
| 5906 | all processes and objects when they are created. */ |
| 5907 | security_initcall(selinux_init); |
| 5908 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 5909 | #if defined(CONFIG_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5911 | static struct nf_hook_ops selinux_ipv4_ops[] = { |
| 5912 | { |
| 5913 | .hook = selinux_ipv4_postroute, |
| 5914 | .owner = THIS_MODULE, |
| 5915 | .pf = PF_INET, |
| 5916 | .hooknum = NF_INET_POST_ROUTING, |
| 5917 | .priority = NF_IP_PRI_SELINUX_LAST, |
| 5918 | }, |
| 5919 | { |
| 5920 | .hook = selinux_ipv4_forward, |
| 5921 | .owner = THIS_MODULE, |
| 5922 | .pf = PF_INET, |
| 5923 | .hooknum = NF_INET_FORWARD, |
| 5924 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5925 | }, |
| 5926 | { |
| 5927 | .hook = selinux_ipv4_output, |
| 5928 | .owner = THIS_MODULE, |
| 5929 | .pf = PF_INET, |
| 5930 | .hooknum = NF_INET_LOCAL_OUT, |
| 5931 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5932 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | }; |
| 5934 | |
| 5935 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 5936 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5937 | static struct nf_hook_ops selinux_ipv6_ops[] = { |
| 5938 | { |
| 5939 | .hook = selinux_ipv6_postroute, |
| 5940 | .owner = THIS_MODULE, |
| 5941 | .pf = PF_INET6, |
| 5942 | .hooknum = NF_INET_POST_ROUTING, |
| 5943 | .priority = NF_IP6_PRI_SELINUX_LAST, |
| 5944 | }, |
| 5945 | { |
| 5946 | .hook = selinux_ipv6_forward, |
| 5947 | .owner = THIS_MODULE, |
| 5948 | .pf = PF_INET6, |
| 5949 | .hooknum = NF_INET_FORWARD, |
| 5950 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 5951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | }; |
| 5953 | |
| 5954 | #endif /* IPV6 */ |
| 5955 | |
| 5956 | static int __init selinux_nf_ip_init(void) |
| 5957 | { |
| 5958 | int err = 0; |
| 5959 | |
| 5960 | if (!selinux_enabled) |
| 5961 | goto out; |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 5962 | |
| 5963 | printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); |
| 5964 | |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 5965 | err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); |
| 5966 | if (err) |
| 5967 | panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | |
| 5969 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 5970 | err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); |
| 5971 | if (err) |
| 5972 | panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | #endif /* IPV6 */ |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | out: |
| 5976 | return err; |
| 5977 | } |
| 5978 | |
| 5979 | __initcall(selinux_nf_ip_init); |
| 5980 | |
| 5981 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 5982 | static void selinux_nf_ip_exit(void) |
| 5983 | { |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 5984 | printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 5986 | nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 5988 | nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | #endif /* IPV6 */ |
| 5990 | } |
| 5991 | #endif |
| 5992 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 5993 | #else /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | |
| 5995 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 5996 | #define selinux_nf_ip_exit() |
| 5997 | #endif |
| 5998 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 5999 | #endif /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | |
| 6001 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6002 | static int selinux_disabled; |
| 6003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | int selinux_disable(void) |
| 6005 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | if (ss_initialized) { |
| 6007 | /* Not permitted after initial policy load. */ |
| 6008 | return -EINVAL; |
| 6009 | } |
| 6010 | |
| 6011 | if (selinux_disabled) { |
| 6012 | /* Only do this once. */ |
| 6013 | return -EINVAL; |
| 6014 | } |
| 6015 | |
| 6016 | printk(KERN_INFO "SELinux: Disabled at runtime.\n"); |
| 6017 | |
| 6018 | selinux_disabled = 1; |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 6019 | selinux_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
wzt.wzt@gmail.com | 189b3b1 | 2010-02-23 23:15:28 +0800 | [diff] [blame] | 6021 | reset_security_ops(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | |
Eric Paris | af8ff04 | 2009-09-20 21:23:01 -0400 | [diff] [blame] | 6023 | /* Try to destroy the avc node cache */ |
| 6024 | avc_disable(); |
| 6025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | /* Unregister netfilter hooks. */ |
| 6027 | selinux_nf_ip_exit(); |
| 6028 | |
| 6029 | /* Unregister selinuxfs. */ |
| 6030 | exit_sel_fs(); |
| 6031 | |
| 6032 | return 0; |
| 6033 | } |
| 6034 | #endif |