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 | 3ac7b8d | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 55 | #include <net/inet_connection_sock.h> |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 56 | #include <net/net_namespace.h> |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 57 | #include <net/netlabel.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 58 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <asm/ioctls.h> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 60 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/bitops.h> |
| 62 | #include <linux/interrupt.h> |
| 63 | #include <linux/netdevice.h> /* for network interface checks */ |
| 64 | #include <linux/netlink.h> |
| 65 | #include <linux/tcp.h> |
| 66 | #include <linux/udp.h> |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 67 | #include <linux/dccp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/quota.h> |
| 69 | #include <linux/un.h> /* for Unix socket types */ |
| 70 | #include <net/af_unix.h> /* for Unix socket types */ |
| 71 | #include <linux/parser.h> |
| 72 | #include <linux/nfs_mount.h> |
| 73 | #include <net/ipv6.h> |
| 74 | #include <linux/hugetlb.h> |
| 75 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/audit.h> |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 77 | #include <linux/string.h> |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 78 | #include <linux/selinux.h> |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 79 | #include <linux/mutex.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 80 | #include <linux/posix-timers.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 81 | #include <linux/syslog.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 82 | #include <linux/user_namespace.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 83 | #include <linux/export.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 84 | #include <linux/msg.h> |
| 85 | #include <linux/shm.h> |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 86 | #include <linux/pft.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | #include "avc.h" |
| 89 | #include "objsec.h" |
| 90 | #include "netif.h" |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 91 | #include "netnode.h" |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 92 | #include "netport.h" |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 93 | #include "xfrm.h" |
Paul Moore | c60475b | 2007-02-28 15:14:23 -0500 | [diff] [blame] | 94 | #include "netlabel.h" |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 95 | #include "audit.h" |
James Morris | 7b98a58 | 2011-08-30 12:52:32 +1000 | [diff] [blame] | 96 | #include "avc_ss.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 98 | #define NUM_SEL_MNT_OPTS 5 |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 99 | |
James Morris | 20510f2 | 2007-10-16 23:31:32 -0700 | [diff] [blame] | 100 | extern struct security_operations *security_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 102 | /* SECMARK reference count */ |
James Morris | 56a4ca9 | 2011-08-17 11:08:43 +1000 | [diff] [blame] | 103 | static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 106 | int selinux_enforcing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static int __init enforcing_setup(char *str) |
| 109 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 110 | unsigned long enforcing; |
| 111 | if (!strict_strtoul(str, 0, &enforcing)) |
| 112 | selinux_enforcing = enforcing ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return 1; |
| 114 | } |
| 115 | __setup("enforcing=", enforcing_setup); |
| 116 | #endif |
| 117 | |
| 118 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM |
| 119 | int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; |
| 120 | |
| 121 | static int __init selinux_enabled_setup(char *str) |
| 122 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 123 | unsigned long enabled; |
| 124 | if (!strict_strtoul(str, 0, &enabled)) |
| 125 | selinux_enabled = enabled ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return 1; |
| 127 | } |
| 128 | __setup("selinux=", selinux_enabled_setup); |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 129 | #else |
| 130 | int selinux_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #endif |
| 132 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 133 | static struct kmem_cache *sel_inode_cache; |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 134 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 135 | /** |
| 136 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled |
| 137 | * |
| 138 | * Description: |
| 139 | * This function checks the SECMARK reference counter to see if any SECMARK |
| 140 | * targets are currently configured, if the reference counter is greater than |
| 141 | * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is |
| 142 | * enabled, false (0) if SECMARK is disabled. |
| 143 | * |
| 144 | */ |
| 145 | static int selinux_secmark_enabled(void) |
| 146 | { |
| 147 | return (atomic_read(&selinux_secmark_refcount) > 0); |
| 148 | } |
| 149 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 150 | /* |
| 151 | * initialise the security for the init task |
| 152 | */ |
| 153 | static void cred_init_security(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 155 | struct cred *cred = (struct cred *) current->real_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | struct task_security_struct *tsec; |
| 157 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 158 | tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (!tsec) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 160 | panic("SELinux: Failed to initialize initial task.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 162 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 163 | cred->security = tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 166 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 167 | * get the security ID of a set of credentials |
| 168 | */ |
| 169 | static inline u32 cred_sid(const struct cred *cred) |
| 170 | { |
| 171 | const struct task_security_struct *tsec; |
| 172 | |
| 173 | tsec = cred->security; |
| 174 | return tsec->sid; |
| 175 | } |
| 176 | |
| 177 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 178 | * get the objective security ID of a task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 179 | */ |
| 180 | static inline u32 task_sid(const struct task_struct *task) |
| 181 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 182 | u32 sid; |
| 183 | |
| 184 | rcu_read_lock(); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 185 | sid = cred_sid(__task_cred(task)); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 186 | rcu_read_unlock(); |
| 187 | return sid; |
| 188 | } |
| 189 | |
| 190 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 191 | * get the subjective security ID of the current task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 192 | */ |
| 193 | static inline u32 current_sid(void) |
| 194 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 195 | const struct task_security_struct *tsec = current_security(); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 196 | |
| 197 | return tsec->sid; |
| 198 | } |
| 199 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 200 | /* Allocate and free functions for each kind of security blob. */ |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | static int inode_alloc_security(struct inode *inode) |
| 203 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 205 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Josef Bacik | a02fe13 | 2008-04-04 09:35:05 +1100 | [diff] [blame] | 207 | isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (!isec) |
| 209 | return -ENOMEM; |
| 210 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 211 | mutex_init(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | INIT_LIST_HEAD(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | isec->inode = inode; |
| 214 | isec->sid = SECINITSID_UNLABELED; |
| 215 | isec->sclass = SECCLASS_FILE; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 216 | isec->task_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | inode->i_security = isec; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Steven Rostedt | c7e9742 | 2014-01-09 21:46:34 -0500 | [diff] [blame^] | 222 | static void inode_free_rcu(struct rcu_head *head) |
| 223 | { |
| 224 | struct inode_security_struct *isec; |
| 225 | |
| 226 | isec = container_of(head, struct inode_security_struct, rcu); |
| 227 | kmem_cache_free(sel_inode_cache, isec); |
| 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | static void inode_free_security(struct inode *inode) |
| 231 | { |
| 232 | struct inode_security_struct *isec = inode->i_security; |
| 233 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | spin_lock(&sbsec->isec_lock); |
| 236 | if (!list_empty(&isec->list)) |
| 237 | list_del_init(&isec->list); |
| 238 | spin_unlock(&sbsec->isec_lock); |
| 239 | |
Steven Rostedt | c7e9742 | 2014-01-09 21:46:34 -0500 | [diff] [blame^] | 240 | /* |
| 241 | * The inode may still be referenced in a path walk and |
| 242 | * a call to selinux_inode_permission() can be made |
| 243 | * after inode_free_security() is called. Ideally, the VFS |
| 244 | * wouldn't do this, but fixing that is a much harder |
| 245 | * job. For now, simply free the i_security via RCU, and |
| 246 | * leave the current inode->i_security pointer intact. |
| 247 | * The inode will be freed after the RCU grace period too. |
| 248 | */ |
| 249 | call_rcu(&isec->rcu, inode_free_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int file_alloc_security(struct file *file) |
| 253 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | struct file_security_struct *fsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 255 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Stephen Smalley | 26d2a4b | 2006-02-01 03:05:55 -0800 | [diff] [blame] | 257 | fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (!fsec) |
| 259 | return -ENOMEM; |
| 260 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 261 | fsec->sid = sid; |
| 262 | fsec->fown_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | file->f_security = fsec; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static void file_free_security(struct file *file) |
| 269 | { |
| 270 | struct file_security_struct *fsec = file->f_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | file->f_security = NULL; |
| 272 | kfree(fsec); |
| 273 | } |
| 274 | |
| 275 | static int superblock_alloc_security(struct super_block *sb) |
| 276 | { |
| 277 | struct superblock_security_struct *sbsec; |
| 278 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 279 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (!sbsec) |
| 281 | return -ENOMEM; |
| 282 | |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 283 | mutex_init(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | INIT_LIST_HEAD(&sbsec->isec_head); |
| 285 | spin_lock_init(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | sbsec->sb = sb; |
| 287 | sbsec->sid = SECINITSID_UNLABELED; |
| 288 | sbsec->def_sid = SECINITSID_FILE; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 289 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | sb->s_security = sbsec; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static void superblock_free_security(struct super_block *sb) |
| 296 | { |
| 297 | struct superblock_security_struct *sbsec = sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | sb->s_security = NULL; |
| 299 | kfree(sbsec); |
| 300 | } |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | /* The file system's label must be initialized prior to use. */ |
| 303 | |
Stephen Hemminger | 634a539 | 2010-03-04 21:59:03 -0800 | [diff] [blame] | 304 | static const char *labeling_behaviors[6] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | "uses xattr", |
| 306 | "uses transition SIDs", |
| 307 | "uses task SIDs", |
| 308 | "uses genfs_contexts", |
| 309 | "not configured for labeling", |
| 310 | "uses mountpoint labeling", |
| 311 | }; |
| 312 | |
| 313 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); |
| 314 | |
| 315 | static inline int inode_doinit(struct inode *inode) |
| 316 | { |
| 317 | return inode_doinit_with_dentry(inode, NULL); |
| 318 | } |
| 319 | |
| 320 | enum { |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 321 | Opt_error = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | Opt_context = 1, |
| 323 | Opt_fscontext = 2, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 324 | Opt_defcontext = 3, |
| 325 | Opt_rootcontext = 4, |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 326 | Opt_labelsupport = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 329 | static const match_table_t tokens = { |
Eric Paris | 832cbd9 | 2008-04-01 13:24:09 -0400 | [diff] [blame] | 330 | {Opt_context, CONTEXT_STR "%s"}, |
| 331 | {Opt_fscontext, FSCONTEXT_STR "%s"}, |
| 332 | {Opt_defcontext, DEFCONTEXT_STR "%s"}, |
| 333 | {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 334 | {Opt_labelsupport, LABELSUPP_STR}, |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 335 | {Opt_error, NULL}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" |
| 339 | |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 340 | static int may_context_mount_sb_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 | c312feb | 2006-07-10 04:43:53 -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 | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 345 | int rc; |
| 346 | |
| 347 | rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| 348 | FILESYSTEM__RELABELFROM, NULL); |
| 349 | if (rc) |
| 350 | return rc; |
| 351 | |
| 352 | rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, |
| 353 | FILESYSTEM__RELABELTO, NULL); |
| 354 | return rc; |
| 355 | } |
| 356 | |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 357 | static int may_context_mount_inode_relabel(u32 sid, |
| 358 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 359 | const struct cred *cred) |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 360 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 361 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 362 | int rc; |
| 363 | rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| 364 | FILESYSTEM__RELABELFROM, NULL); |
| 365 | if (rc) |
| 366 | return rc; |
| 367 | |
| 368 | rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| 369 | FILESYSTEM__ASSOCIATE, NULL); |
| 370 | return rc; |
| 371 | } |
| 372 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 373 | static int sb_finish_set_opts(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | struct superblock_security_struct *sbsec = sb->s_security; |
| 376 | struct dentry *root = sb->s_root; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 377 | struct inode *root_inode = root->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | int rc = 0; |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 381 | /* Make sure that the xattr handler exists and that no |
| 382 | error other than -ENODATA is returned by getxattr on |
| 383 | the root directory. -ENODATA is ok, as this may be |
| 384 | the first boot of the SELinux kernel before we have |
| 385 | assigned xattr values to the filesystem. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 386 | if (!root_inode->i_op->getxattr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " |
| 388 | "xattr support\n", sb->s_id, sb->s_type->name); |
| 389 | rc = -EOPNOTSUPP; |
| 390 | goto out; |
| 391 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 392 | rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (rc < 0 && rc != -ENODATA) { |
| 394 | if (rc == -EOPNOTSUPP) |
| 395 | printk(KERN_WARNING "SELinux: (dev %s, type " |
| 396 | "%s) has no security xattr handler\n", |
| 397 | sb->s_id, sb->s_type->name); |
| 398 | else |
| 399 | printk(KERN_WARNING "SELinux: (dev %s, type " |
| 400 | "%s) getxattr errno %d\n", sb->s_id, |
| 401 | sb->s_type->name, -rc); |
| 402 | goto out; |
| 403 | } |
| 404 | } |
| 405 | |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 406 | sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 408 | if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 409 | printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | sb->s_id, sb->s_type->name); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 411 | else |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 412 | printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | sb->s_id, sb->s_type->name, |
| 414 | labeling_behaviors[sbsec->behavior-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 416 | if (sbsec->behavior == SECURITY_FS_USE_GENFS || |
| 417 | sbsec->behavior == SECURITY_FS_USE_MNTPOINT || |
| 418 | sbsec->behavior == SECURITY_FS_USE_NONE || |
| 419 | sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) |
| 420 | sbsec->flags &= ~SE_SBLABELSUPP; |
| 421 | |
Mark Salyzyn | 2c08805 | 2015-01-07 09:27:15 -0800 | [diff] [blame] | 422 | /* Special handling. Is genfs but also has in-core setxattr handler*/ |
| 423 | if (!strcmp(sb->s_type->name, "sysfs") || |
| 424 | !strcmp(sb->s_type->name, "pstore") || |
| 425 | !strcmp(sb->s_type->name, "debugfs") || |
| 426 | !strcmp(sb->s_type->name, "rootfs")) |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 427 | sbsec->flags |= SE_SBLABELSUPP; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | /* Initialize the root inode. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 430 | rc = inode_doinit_with_dentry(root_inode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* Initialize any other inodes associated with the superblock, e.g. |
| 433 | inodes created prior to initial policy load or inodes created |
| 434 | during get_sb by a pseudo filesystem that directly |
| 435 | populates itself. */ |
| 436 | spin_lock(&sbsec->isec_lock); |
| 437 | next_inode: |
| 438 | if (!list_empty(&sbsec->isec_head)) { |
| 439 | struct inode_security_struct *isec = |
| 440 | list_entry(sbsec->isec_head.next, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 441 | struct inode_security_struct, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | struct inode *inode = isec->inode; |
| 443 | spin_unlock(&sbsec->isec_lock); |
| 444 | inode = igrab(inode); |
| 445 | if (inode) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 446 | if (!IS_PRIVATE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | inode_doinit(inode); |
| 448 | iput(inode); |
| 449 | } |
| 450 | spin_lock(&sbsec->isec_lock); |
| 451 | list_del_init(&isec->list); |
| 452 | goto next_inode; |
| 453 | } |
| 454 | spin_unlock(&sbsec->isec_lock); |
| 455 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 456 | return rc; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * This function should allow an FS to ask what it's mount security |
| 461 | * options were so it can use those later for submounts, displaying |
| 462 | * mount options, or whatever. |
| 463 | */ |
| 464 | static int selinux_get_mnt_opts(const struct super_block *sb, |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 465 | struct security_mnt_opts *opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 466 | { |
| 467 | int rc = 0, i; |
| 468 | struct superblock_security_struct *sbsec = sb->s_security; |
| 469 | char *context = NULL; |
| 470 | u32 len; |
| 471 | char tmp; |
| 472 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 473 | security_init_mnt_opts(opts); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 474 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 475 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 476 | return -EINVAL; |
| 477 | |
| 478 | if (!ss_initialized) |
| 479 | return -EINVAL; |
| 480 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 481 | tmp = sbsec->flags & SE_MNTMASK; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 482 | /* count the number of mount options for this sb */ |
| 483 | for (i = 0; i < 8; i++) { |
| 484 | if (tmp & 0x01) |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 485 | opts->num_mnt_opts++; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 486 | tmp >>= 1; |
| 487 | } |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 488 | /* Check if the Label support flag is set */ |
| 489 | if (sbsec->flags & SE_SBLABELSUPP) |
| 490 | opts->num_mnt_opts++; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 491 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 492 | opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); |
| 493 | if (!opts->mnt_opts) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 494 | rc = -ENOMEM; |
| 495 | goto out_free; |
| 496 | } |
| 497 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 498 | opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); |
| 499 | if (!opts->mnt_opts_flags) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 500 | rc = -ENOMEM; |
| 501 | goto out_free; |
| 502 | } |
| 503 | |
| 504 | i = 0; |
| 505 | if (sbsec->flags & FSCONTEXT_MNT) { |
| 506 | rc = security_sid_to_context(sbsec->sid, &context, &len); |
| 507 | if (rc) |
| 508 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 509 | opts->mnt_opts[i] = context; |
| 510 | opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 511 | } |
| 512 | if (sbsec->flags & CONTEXT_MNT) { |
| 513 | rc = security_sid_to_context(sbsec->mntpoint_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++] = CONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 518 | } |
| 519 | if (sbsec->flags & DEFCONTEXT_MNT) { |
| 520 | rc = security_sid_to_context(sbsec->def_sid, &context, &len); |
| 521 | if (rc) |
| 522 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 523 | opts->mnt_opts[i] = context; |
| 524 | opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 525 | } |
| 526 | if (sbsec->flags & ROOTCONTEXT_MNT) { |
| 527 | struct inode *root = sbsec->sb->s_root->d_inode; |
| 528 | struct inode_security_struct *isec = root->i_security; |
| 529 | |
| 530 | rc = security_sid_to_context(isec->sid, &context, &len); |
| 531 | if (rc) |
| 532 | goto out_free; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 533 | opts->mnt_opts[i] = context; |
| 534 | opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 535 | } |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 536 | if (sbsec->flags & SE_SBLABELSUPP) { |
| 537 | opts->mnt_opts[i] = NULL; |
| 538 | opts->mnt_opts_flags[i++] = SE_SBLABELSUPP; |
| 539 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 540 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 541 | BUG_ON(i != opts->num_mnt_opts); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 542 | |
| 543 | return 0; |
| 544 | |
| 545 | out_free: |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 546 | security_free_mnt_opts(opts); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
| 551 | u32 old_sid, u32 new_sid) |
| 552 | { |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 553 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
| 554 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 555 | /* check if the old mount command had the same options */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 556 | if (sbsec->flags & SE_SBINITIALIZED) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 557 | if (!(sbsec->flags & flag) || |
| 558 | (old_sid != new_sid)) |
| 559 | return 1; |
| 560 | |
| 561 | /* check if we were passed the same options twice, |
| 562 | * aka someone passed context=a,context=b |
| 563 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 564 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 565 | if (mnt_flags & flag) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 566 | return 1; |
| 567 | return 0; |
| 568 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 569 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 570 | /* |
| 571 | * Allow filesystems with binary mount data to explicitly set mount point |
| 572 | * labeling information. |
| 573 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 574 | static int selinux_set_mnt_opts(struct super_block *sb, |
| 575 | struct security_mnt_opts *opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 576 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 577 | const struct cred *cred = current_cred(); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 578 | int rc = 0, i; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 579 | struct superblock_security_struct *sbsec = sb->s_security; |
| 580 | const char *name = sb->s_type->name; |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 581 | struct inode *inode = sbsec->sb->s_root->d_inode; |
| 582 | struct inode_security_struct *root_isec = inode->i_security; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 583 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
| 584 | u32 defcontext_sid = 0; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 585 | char **mount_options = opts->mnt_opts; |
| 586 | int *flags = opts->mnt_opts_flags; |
| 587 | int num_opts = opts->num_mnt_opts; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 588 | |
| 589 | mutex_lock(&sbsec->lock); |
| 590 | |
| 591 | if (!ss_initialized) { |
| 592 | if (!num_opts) { |
| 593 | /* Defer initialization until selinux_complete_init, |
| 594 | after the initial policy is loaded and the security |
| 595 | server is ready to handle calls. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 596 | goto out; |
| 597 | } |
| 598 | rc = -EINVAL; |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 599 | printk(KERN_WARNING "SELinux: Unable to set superblock options " |
| 600 | "before the security server is initialized\n"); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 601 | goto out; |
| 602 | } |
| 603 | |
| 604 | /* |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 605 | * Binary mount data FS will come through this function twice. Once |
| 606 | * from an explicit call and once from the generic calls from the vfs. |
| 607 | * Since the generic VFS calls will not contain any security mount data |
| 608 | * we need to skip the double mount verification. |
| 609 | * |
| 610 | * This does open a hole in which we will not notice if the first |
| 611 | * mount using this sb set explict options and a second mount using |
| 612 | * this sb does not set any security options. (The first options |
| 613 | * will be used for both mounts) |
| 614 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 615 | 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] | 616 | && (num_opts == 0)) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 617 | goto out; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 618 | |
| 619 | /* |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 620 | * parse the mount options, check if they are valid sids. |
| 621 | * also check if someone is trying to mount the same sb more |
| 622 | * than once with different security options. |
| 623 | */ |
| 624 | for (i = 0; i < num_opts; i++) { |
| 625 | u32 sid; |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 626 | |
| 627 | if (flags[i] == SE_SBLABELSUPP) |
| 628 | continue; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 629 | rc = security_context_to_sid(mount_options[i], |
| 630 | strlen(mount_options[i]), &sid); |
| 631 | if (rc) { |
| 632 | printk(KERN_WARNING "SELinux: security_context_to_sid" |
| 633 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 634 | mount_options[i], sb->s_id, name, rc); |
| 635 | goto out; |
| 636 | } |
| 637 | switch (flags[i]) { |
| 638 | case FSCONTEXT_MNT: |
| 639 | fscontext_sid = sid; |
| 640 | |
| 641 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, |
| 642 | fscontext_sid)) |
| 643 | goto out_double_mount; |
| 644 | |
| 645 | sbsec->flags |= FSCONTEXT_MNT; |
| 646 | break; |
| 647 | case CONTEXT_MNT: |
| 648 | context_sid = sid; |
| 649 | |
| 650 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, |
| 651 | context_sid)) |
| 652 | goto out_double_mount; |
| 653 | |
| 654 | sbsec->flags |= CONTEXT_MNT; |
| 655 | break; |
| 656 | case ROOTCONTEXT_MNT: |
| 657 | rootcontext_sid = sid; |
| 658 | |
| 659 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, |
| 660 | rootcontext_sid)) |
| 661 | goto out_double_mount; |
| 662 | |
| 663 | sbsec->flags |= ROOTCONTEXT_MNT; |
| 664 | |
| 665 | break; |
| 666 | case DEFCONTEXT_MNT: |
| 667 | defcontext_sid = sid; |
| 668 | |
| 669 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, |
| 670 | defcontext_sid)) |
| 671 | goto out_double_mount; |
| 672 | |
| 673 | sbsec->flags |= DEFCONTEXT_MNT; |
| 674 | |
| 675 | break; |
| 676 | default: |
| 677 | rc = -EINVAL; |
| 678 | goto out; |
| 679 | } |
| 680 | } |
| 681 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 682 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 683 | /* previously mounted with options, but not on this attempt? */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 684 | if ((sbsec->flags & SE_MNTMASK) && !num_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 685 | goto out_double_mount; |
| 686 | rc = 0; |
| 687 | goto out; |
| 688 | } |
| 689 | |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 690 | if (strcmp(sb->s_type->name, "proc") == 0) |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 691 | sbsec->flags |= SE_SBPROC | SE_SBGENFS; |
| 692 | |
Stephen Smalley | 043ab94 | 2015-05-20 12:33:16 -0400 | [diff] [blame] | 693 | if (!strcmp(sb->s_type->name, "debugfs") || |
| 694 | !strcmp(sb->s_type->name, "sysfs") || |
| 695 | !strcmp(sb->s_type->name, "pstore")) |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 696 | sbsec->flags |= SE_SBGENFS; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 697 | |
| 698 | /* Determine the labeling behavior to use for this filesystem type. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 699 | 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] | 700 | if (rc) { |
| 701 | printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 702 | __func__, sb->s_type->name, rc); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 703 | goto out; |
| 704 | } |
| 705 | |
| 706 | /* sets the context of the superblock for the fs being mounted. */ |
| 707 | if (fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 708 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 709 | if (rc) |
| 710 | goto out; |
| 711 | |
| 712 | sbsec->sid = fscontext_sid; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Switch to using mount point labeling behavior. |
| 717 | * sets the label used on all file below the mountpoint, and will set |
| 718 | * the superblock context if not already set. |
| 719 | */ |
| 720 | if (context_sid) { |
| 721 | if (!fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 722 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
| 723 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 724 | if (rc) |
| 725 | goto out; |
| 726 | sbsec->sid = context_sid; |
| 727 | } else { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 728 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
| 729 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 730 | if (rc) |
| 731 | goto out; |
| 732 | } |
| 733 | if (!rootcontext_sid) |
| 734 | rootcontext_sid = context_sid; |
| 735 | |
| 736 | sbsec->mntpoint_sid = context_sid; |
| 737 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
| 738 | } |
| 739 | |
| 740 | if (rootcontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 741 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
| 742 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 743 | if (rc) |
| 744 | goto out; |
| 745 | |
| 746 | root_isec->sid = rootcontext_sid; |
| 747 | root_isec->initialized = 1; |
| 748 | } |
| 749 | |
| 750 | if (defcontext_sid) { |
| 751 | if (sbsec->behavior != SECURITY_FS_USE_XATTR) { |
| 752 | rc = -EINVAL; |
| 753 | printk(KERN_WARNING "SELinux: defcontext option is " |
| 754 | "invalid for this filesystem type\n"); |
| 755 | goto out; |
| 756 | } |
| 757 | |
| 758 | if (defcontext_sid != sbsec->def_sid) { |
| 759 | rc = may_context_mount_inode_relabel(defcontext_sid, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 760 | sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 761 | if (rc) |
| 762 | goto out; |
| 763 | } |
| 764 | |
| 765 | sbsec->def_sid = defcontext_sid; |
| 766 | } |
| 767 | |
| 768 | rc = sb_finish_set_opts(sb); |
| 769 | out: |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 770 | mutex_unlock(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 772 | out_double_mount: |
| 773 | rc = -EINVAL; |
| 774 | printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " |
| 775 | "security settings for (dev %s, type %s)\n", sb->s_id, name); |
| 776 | goto out; |
| 777 | } |
| 778 | |
| 779 | static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, |
| 780 | struct super_block *newsb) |
| 781 | { |
| 782 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
| 783 | struct superblock_security_struct *newsbsec = newsb->s_security; |
| 784 | |
| 785 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 786 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| 787 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); |
| 788 | |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 789 | /* |
| 790 | * 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] | 791 | * mount options. thus we can safely deal with this superblock later |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 792 | */ |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 793 | if (!ss_initialized) |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 794 | return; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 795 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 796 | /* 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] | 797 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 798 | |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 799 | /* if fs is reusing a sb, just let its options stand... */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 800 | if (newsbsec->flags & SE_SBINITIALIZED) |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 801 | return; |
| 802 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 803 | mutex_lock(&newsbsec->lock); |
| 804 | |
| 805 | newsbsec->flags = oldsbsec->flags; |
| 806 | |
| 807 | newsbsec->sid = oldsbsec->sid; |
| 808 | newsbsec->def_sid = oldsbsec->def_sid; |
| 809 | newsbsec->behavior = oldsbsec->behavior; |
| 810 | |
| 811 | if (set_context) { |
| 812 | u32 sid = oldsbsec->mntpoint_sid; |
| 813 | |
| 814 | if (!set_fscontext) |
| 815 | newsbsec->sid = sid; |
| 816 | if (!set_rootcontext) { |
| 817 | struct inode *newinode = newsb->s_root->d_inode; |
| 818 | struct inode_security_struct *newisec = newinode->i_security; |
| 819 | newisec->sid = sid; |
| 820 | } |
| 821 | newsbsec->mntpoint_sid = sid; |
| 822 | } |
| 823 | if (set_rootcontext) { |
| 824 | const struct inode *oldinode = oldsb->s_root->d_inode; |
| 825 | const struct inode_security_struct *oldisec = oldinode->i_security; |
| 826 | struct inode *newinode = newsb->s_root->d_inode; |
| 827 | struct inode_security_struct *newisec = newinode->i_security; |
| 828 | |
| 829 | newisec->sid = oldisec->sid; |
| 830 | } |
| 831 | |
| 832 | sb_finish_set_opts(newsb); |
| 833 | mutex_unlock(&newsbsec->lock); |
| 834 | } |
| 835 | |
Adrian Bunk | 2e1479d | 2008-03-17 22:29:23 +0200 | [diff] [blame] | 836 | static int selinux_parse_opts_str(char *options, |
| 837 | struct security_mnt_opts *opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 838 | { |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 839 | char *p; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 840 | char *context = NULL, *defcontext = NULL; |
| 841 | char *fscontext = NULL, *rootcontext = NULL; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 842 | int rc, num_mnt_opts = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 843 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 844 | opts->num_mnt_opts = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 845 | |
| 846 | /* Standard string-based options. */ |
| 847 | while ((p = strsep(&options, "|")) != NULL) { |
| 848 | int token; |
| 849 | substring_t args[MAX_OPT_ARGS]; |
| 850 | |
| 851 | if (!*p) |
| 852 | continue; |
| 853 | |
| 854 | token = match_token(p, tokens, args); |
| 855 | |
| 856 | switch (token) { |
| 857 | case Opt_context: |
| 858 | if (context || defcontext) { |
| 859 | rc = -EINVAL; |
| 860 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 861 | goto out_err; |
| 862 | } |
| 863 | context = match_strdup(&args[0]); |
| 864 | if (!context) { |
| 865 | rc = -ENOMEM; |
| 866 | goto out_err; |
| 867 | } |
| 868 | break; |
| 869 | |
| 870 | case Opt_fscontext: |
| 871 | if (fscontext) { |
| 872 | rc = -EINVAL; |
| 873 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 874 | goto out_err; |
| 875 | } |
| 876 | fscontext = match_strdup(&args[0]); |
| 877 | if (!fscontext) { |
| 878 | rc = -ENOMEM; |
| 879 | goto out_err; |
| 880 | } |
| 881 | break; |
| 882 | |
| 883 | case Opt_rootcontext: |
| 884 | if (rootcontext) { |
| 885 | rc = -EINVAL; |
| 886 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 887 | goto out_err; |
| 888 | } |
| 889 | rootcontext = match_strdup(&args[0]); |
| 890 | if (!rootcontext) { |
| 891 | rc = -ENOMEM; |
| 892 | goto out_err; |
| 893 | } |
| 894 | break; |
| 895 | |
| 896 | case Opt_defcontext: |
| 897 | if (context || defcontext) { |
| 898 | rc = -EINVAL; |
| 899 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); |
| 900 | goto out_err; |
| 901 | } |
| 902 | defcontext = match_strdup(&args[0]); |
| 903 | if (!defcontext) { |
| 904 | rc = -ENOMEM; |
| 905 | goto out_err; |
| 906 | } |
| 907 | break; |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 908 | case Opt_labelsupport: |
| 909 | break; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 910 | default: |
| 911 | rc = -EINVAL; |
| 912 | printk(KERN_WARNING "SELinux: unknown mount option\n"); |
| 913 | goto out_err; |
| 914 | |
| 915 | } |
| 916 | } |
| 917 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 918 | rc = -ENOMEM; |
| 919 | opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); |
| 920 | if (!opts->mnt_opts) |
| 921 | goto out_err; |
| 922 | |
| 923 | opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); |
| 924 | if (!opts->mnt_opts_flags) { |
| 925 | kfree(opts->mnt_opts); |
| 926 | goto out_err; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 927 | } |
| 928 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 929 | if (fscontext) { |
| 930 | opts->mnt_opts[num_mnt_opts] = fscontext; |
| 931 | opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; |
| 932 | } |
| 933 | if (context) { |
| 934 | opts->mnt_opts[num_mnt_opts] = context; |
| 935 | opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; |
| 936 | } |
| 937 | if (rootcontext) { |
| 938 | opts->mnt_opts[num_mnt_opts] = rootcontext; |
| 939 | opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; |
| 940 | } |
| 941 | if (defcontext) { |
| 942 | opts->mnt_opts[num_mnt_opts] = defcontext; |
| 943 | opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; |
| 944 | } |
| 945 | |
| 946 | opts->num_mnt_opts = num_mnt_opts; |
| 947 | return 0; |
| 948 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 949 | out_err: |
| 950 | kfree(context); |
| 951 | kfree(defcontext); |
| 952 | kfree(fscontext); |
| 953 | kfree(rootcontext); |
| 954 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 956 | /* |
| 957 | * string mount options parsing and call set the sbsec |
| 958 | */ |
| 959 | static int superblock_doinit(struct super_block *sb, void *data) |
| 960 | { |
| 961 | int rc = 0; |
| 962 | char *options = data; |
| 963 | struct security_mnt_opts opts; |
| 964 | |
| 965 | security_init_mnt_opts(&opts); |
| 966 | |
| 967 | if (!data) |
| 968 | goto out; |
| 969 | |
| 970 | BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); |
| 971 | |
| 972 | rc = selinux_parse_opts_str(options, &opts); |
| 973 | if (rc) |
| 974 | goto out_err; |
| 975 | |
| 976 | out: |
| 977 | rc = selinux_set_mnt_opts(sb, &opts); |
| 978 | |
| 979 | out_err: |
| 980 | security_free_mnt_opts(&opts); |
| 981 | return rc; |
| 982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Adrian Bunk | 3583a71 | 2008-07-22 20:21:23 +0300 | [diff] [blame] | 984 | static void selinux_write_opts(struct seq_file *m, |
| 985 | struct security_mnt_opts *opts) |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 986 | { |
| 987 | int i; |
| 988 | char *prefix; |
| 989 | |
| 990 | for (i = 0; i < opts->num_mnt_opts; i++) { |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 991 | char *has_comma; |
| 992 | |
| 993 | if (opts->mnt_opts[i]) |
| 994 | has_comma = strchr(opts->mnt_opts[i], ','); |
| 995 | else |
| 996 | has_comma = NULL; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 997 | |
| 998 | switch (opts->mnt_opts_flags[i]) { |
| 999 | case CONTEXT_MNT: |
| 1000 | prefix = CONTEXT_STR; |
| 1001 | break; |
| 1002 | case FSCONTEXT_MNT: |
| 1003 | prefix = FSCONTEXT_STR; |
| 1004 | break; |
| 1005 | case ROOTCONTEXT_MNT: |
| 1006 | prefix = ROOTCONTEXT_STR; |
| 1007 | break; |
| 1008 | case DEFCONTEXT_MNT: |
| 1009 | prefix = DEFCONTEXT_STR; |
| 1010 | break; |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 1011 | case SE_SBLABELSUPP: |
| 1012 | seq_putc(m, ','); |
| 1013 | seq_puts(m, LABELSUPP_STR); |
| 1014 | continue; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1015 | default: |
| 1016 | BUG(); |
Eric Paris | a35c6c8 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1017 | return; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1018 | }; |
| 1019 | /* we need a comma before each option */ |
| 1020 | seq_putc(m, ','); |
| 1021 | seq_puts(m, prefix); |
| 1022 | if (has_comma) |
| 1023 | seq_putc(m, '\"'); |
| 1024 | seq_puts(m, opts->mnt_opts[i]); |
| 1025 | if (has_comma) |
| 1026 | seq_putc(m, '\"'); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1031 | { |
| 1032 | struct security_mnt_opts opts; |
| 1033 | int rc; |
| 1034 | |
| 1035 | rc = selinux_get_mnt_opts(sb, &opts); |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1036 | if (rc) { |
| 1037 | /* before policy load we may get EINVAL, don't show anything */ |
| 1038 | if (rc == -EINVAL) |
| 1039 | rc = 0; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1040 | return rc; |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1041 | } |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1042 | |
| 1043 | selinux_write_opts(m, &opts); |
| 1044 | |
| 1045 | security_free_mnt_opts(&opts); |
| 1046 | |
| 1047 | return rc; |
| 1048 | } |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | static inline u16 inode_mode_to_security_class(umode_t mode) |
| 1051 | { |
| 1052 | switch (mode & S_IFMT) { |
| 1053 | case S_IFSOCK: |
| 1054 | return SECCLASS_SOCK_FILE; |
| 1055 | case S_IFLNK: |
| 1056 | return SECCLASS_LNK_FILE; |
| 1057 | case S_IFREG: |
| 1058 | return SECCLASS_FILE; |
| 1059 | case S_IFBLK: |
| 1060 | return SECCLASS_BLK_FILE; |
| 1061 | case S_IFDIR: |
| 1062 | return SECCLASS_DIR; |
| 1063 | case S_IFCHR: |
| 1064 | return SECCLASS_CHR_FILE; |
| 1065 | case S_IFIFO: |
| 1066 | return SECCLASS_FIFO_FILE; |
| 1067 | |
| 1068 | } |
| 1069 | |
| 1070 | return SECCLASS_FILE; |
| 1071 | } |
| 1072 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1073 | static inline int default_protocol_stream(int protocol) |
| 1074 | { |
| 1075 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); |
| 1076 | } |
| 1077 | |
| 1078 | static inline int default_protocol_dgram(int protocol) |
| 1079 | { |
| 1080 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); |
| 1081 | } |
| 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
| 1084 | { |
| 1085 | switch (family) { |
| 1086 | case PF_UNIX: |
| 1087 | switch (type) { |
| 1088 | case SOCK_STREAM: |
| 1089 | case SOCK_SEQPACKET: |
| 1090 | return SECCLASS_UNIX_STREAM_SOCKET; |
| 1091 | case SOCK_DGRAM: |
| 1092 | return SECCLASS_UNIX_DGRAM_SOCKET; |
| 1093 | } |
| 1094 | break; |
| 1095 | case PF_INET: |
| 1096 | case PF_INET6: |
| 1097 | switch (type) { |
| 1098 | case SOCK_STREAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1099 | if (default_protocol_stream(protocol)) |
| 1100 | return SECCLASS_TCP_SOCKET; |
| 1101 | else |
| 1102 | return SECCLASS_RAWIP_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | case SOCK_DGRAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1104 | if (default_protocol_dgram(protocol)) |
| 1105 | return SECCLASS_UDP_SOCKET; |
| 1106 | else |
| 1107 | return SECCLASS_RAWIP_SOCKET; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 1108 | case SOCK_DCCP: |
| 1109 | return SECCLASS_DCCP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1110 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return SECCLASS_RAWIP_SOCKET; |
| 1112 | } |
| 1113 | break; |
| 1114 | case PF_NETLINK: |
| 1115 | switch (protocol) { |
| 1116 | case NETLINK_ROUTE: |
| 1117 | return SECCLASS_NETLINK_ROUTE_SOCKET; |
| 1118 | case NETLINK_FIREWALL: |
| 1119 | return SECCLASS_NETLINK_FIREWALL_SOCKET; |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1120 | case NETLINK_SOCK_DIAG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
| 1122 | case NETLINK_NFLOG: |
| 1123 | return SECCLASS_NETLINK_NFLOG_SOCKET; |
| 1124 | case NETLINK_XFRM: |
| 1125 | return SECCLASS_NETLINK_XFRM_SOCKET; |
| 1126 | case NETLINK_SELINUX: |
| 1127 | return SECCLASS_NETLINK_SELINUX_SOCKET; |
| 1128 | case NETLINK_AUDIT: |
| 1129 | return SECCLASS_NETLINK_AUDIT_SOCKET; |
| 1130 | case NETLINK_IP6_FW: |
| 1131 | return SECCLASS_NETLINK_IP6FW_SOCKET; |
| 1132 | case NETLINK_DNRTMSG: |
| 1133 | return SECCLASS_NETLINK_DNRT_SOCKET; |
James Morris | 0c9b794 | 2005-04-16 15:24:13 -0700 | [diff] [blame] | 1134 | case NETLINK_KOBJECT_UEVENT: |
| 1135 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | default: |
| 1137 | return SECCLASS_NETLINK_SOCKET; |
| 1138 | } |
| 1139 | case PF_PACKET: |
| 1140 | return SECCLASS_PACKET_SOCKET; |
| 1141 | case PF_KEY: |
| 1142 | return SECCLASS_KEY_SOCKET; |
Christopher J. PeBenito | 3e3ff15 | 2006-06-09 00:25:03 -0700 | [diff] [blame] | 1143 | case PF_APPLETALK: |
| 1144 | return SECCLASS_APPLETALK_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | return SECCLASS_SOCKET; |
| 1148 | } |
| 1149 | |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 1150 | static int selinux_genfs_get_sid(struct dentry *dentry, |
| 1151 | u16 tclass, |
| 1152 | u16 flags, |
| 1153 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1155 | int rc; |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 1156 | struct super_block *sb = dentry->d_inode->i_sb; |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1157 | char *buffer, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1159 | buffer = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | if (!buffer) |
| 1161 | return -ENOMEM; |
| 1162 | |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1163 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
| 1164 | if (IS_ERR(path)) |
| 1165 | rc = PTR_ERR(path); |
| 1166 | else { |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 1167 | if (flags & SE_SBPROC) { |
| 1168 | /* each process gets a /proc/PID/ entry. Strip off the |
| 1169 | * PID part to get a valid selinux labeling. |
| 1170 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ |
| 1171 | while (path[1] >= '0' && path[1] <= '9') { |
| 1172 | path[1] = '/'; |
| 1173 | path++; |
| 1174 | } |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1175 | } |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 1176 | rc = security_genfs_sid(sb->s_type->name, path, tclass, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | free_page((unsigned long)buffer); |
| 1179 | return rc; |
| 1180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
| 1182 | /* The inode's security attributes must be initialized before first use. */ |
| 1183 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1184 | { |
| 1185 | struct superblock_security_struct *sbsec = NULL; |
| 1186 | struct inode_security_struct *isec = inode->i_security; |
| 1187 | u32 sid; |
| 1188 | struct dentry *dentry; |
| 1189 | #define INITCONTEXTLEN 255 |
| 1190 | char *context = NULL; |
| 1191 | unsigned len = 0; |
| 1192 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
| 1194 | if (isec->initialized) |
| 1195 | goto out; |
| 1196 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1197 | mutex_lock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (isec->initialized) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1199 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | sbsec = inode->i_sb->s_security; |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1202 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | /* Defer initialization until selinux_complete_init, |
| 1204 | after the initial policy is loaded and the security |
| 1205 | server is ready to handle calls. */ |
| 1206 | spin_lock(&sbsec->isec_lock); |
| 1207 | if (list_empty(&isec->list)) |
| 1208 | list_add(&isec->list, &sbsec->isec_head); |
| 1209 | spin_unlock(&sbsec->isec_lock); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1210 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | switch (sbsec->behavior) { |
| 1214 | case SECURITY_FS_USE_XATTR: |
| 1215 | if (!inode->i_op->getxattr) { |
| 1216 | isec->sid = sbsec->def_sid; |
| 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | /* Need a dentry, since the xattr API requires one. |
| 1221 | Life would be simpler if we could just pass the inode. */ |
| 1222 | if (opt_dentry) { |
| 1223 | /* Called from d_instantiate or d_splice_alias. */ |
| 1224 | dentry = dget(opt_dentry); |
| 1225 | } else { |
| 1226 | /* Called from selinux_complete_init, try to find a dentry. */ |
| 1227 | dentry = d_find_alias(inode); |
| 1228 | } |
| 1229 | if (!dentry) { |
Eric Paris | df7f54c | 2009-03-09 14:35:58 -0400 | [diff] [blame] | 1230 | /* |
| 1231 | * this is can be hit on boot when a file is accessed |
| 1232 | * before the policy is loaded. When we load policy we |
| 1233 | * may find inodes that have no dentry on the |
| 1234 | * sbsec->isec_head list. No reason to complain as these |
| 1235 | * will get fixed up the next time we go through |
| 1236 | * inode_doinit with a dentry, before these inodes could |
| 1237 | * be used again by userspace. |
| 1238 | */ |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1239 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | len = INITCONTEXTLEN; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1243 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | if (!context) { |
| 1245 | rc = -ENOMEM; |
| 1246 | dput(dentry); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1247 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1249 | context[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, |
| 1251 | context, len); |
| 1252 | if (rc == -ERANGE) { |
James Morris | 314dabb | 2009-08-10 22:00:13 +1000 | [diff] [blame] | 1253 | kfree(context); |
| 1254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | /* Need a larger buffer. Query for the right size. */ |
| 1256 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, |
| 1257 | NULL, 0); |
| 1258 | if (rc < 0) { |
| 1259 | dput(dentry); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1260 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | len = rc; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1263 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | if (!context) { |
| 1265 | rc = -ENOMEM; |
| 1266 | dput(dentry); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1267 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1269 | context[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | rc = inode->i_op->getxattr(dentry, |
| 1271 | XATTR_NAME_SELINUX, |
| 1272 | context, len); |
| 1273 | } |
| 1274 | dput(dentry); |
| 1275 | if (rc < 0) { |
| 1276 | if (rc != -ENODATA) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1277 | printk(KERN_WARNING "SELinux: %s: getxattr returned " |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 1278 | "%d for dev=%s ino=%ld\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | -rc, inode->i_sb->s_id, inode->i_ino); |
| 1280 | kfree(context); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1281 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
| 1283 | /* Map ENODATA to the default file SID */ |
| 1284 | sid = sbsec->def_sid; |
| 1285 | rc = 0; |
| 1286 | } else { |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1287 | rc = security_context_to_sid_default(context, rc, &sid, |
Stephen Smalley | 869ab51 | 2008-04-04 08:46:05 -0400 | [diff] [blame] | 1288 | sbsec->def_sid, |
| 1289 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | if (rc) { |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1291 | char *dev = inode->i_sb->s_id; |
| 1292 | unsigned long ino = inode->i_ino; |
| 1293 | |
| 1294 | if (rc == -EINVAL) { |
| 1295 | if (printk_ratelimit()) |
| 1296 | printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " |
| 1297 | "context=%s. This indicates you may need to relabel the inode or the " |
| 1298 | "filesystem in question.\n", ino, dev, context); |
| 1299 | } else { |
| 1300 | printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " |
| 1301 | "returned %d for dev=%s ino=%ld\n", |
| 1302 | __func__, context, -rc, dev, ino); |
| 1303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | kfree(context); |
| 1305 | /* Leave with the unlabeled SID */ |
| 1306 | rc = 0; |
| 1307 | break; |
| 1308 | } |
| 1309 | } |
| 1310 | kfree(context); |
| 1311 | isec->sid = sid; |
| 1312 | break; |
| 1313 | case SECURITY_FS_USE_TASK: |
| 1314 | isec->sid = isec->task_sid; |
| 1315 | break; |
| 1316 | case SECURITY_FS_USE_TRANS: |
| 1317 | /* Default to the fs SID. */ |
| 1318 | isec->sid = sbsec->sid; |
| 1319 | |
| 1320 | /* Try to obtain a transition SID. */ |
| 1321 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 1322 | rc = security_transition_sid(isec->task_sid, sbsec->sid, |
| 1323 | isec->sclass, NULL, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | break; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1328 | case SECURITY_FS_USE_MNTPOINT: |
| 1329 | isec->sid = sbsec->mntpoint_sid; |
| 1330 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | default: |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1332 | /* Default to the fs superblock SID. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | isec->sid = sbsec->sid; |
| 1334 | |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 1335 | if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { |
Paul Moore | 810be00 | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1336 | /* We must have a dentry to determine the label on |
| 1337 | * procfs inodes */ |
| 1338 | if (opt_dentry) |
| 1339 | /* Called from d_instantiate or |
| 1340 | * d_splice_alias. */ |
| 1341 | dentry = dget(opt_dentry); |
| 1342 | else |
| 1343 | /* Called from selinux_complete_init, try to |
| 1344 | * find a dentry. */ |
| 1345 | dentry = d_find_alias(inode); |
| 1346 | /* |
| 1347 | * This can be hit on boot when a file is accessed |
| 1348 | * before the policy is loaded. When we load policy we |
| 1349 | * may find inodes that have no dentry on the |
| 1350 | * sbsec->isec_head list. No reason to complain as |
| 1351 | * these will get fixed up the next time we go through |
| 1352 | * inode_doinit() with a dentry, before these inodes |
| 1353 | * could be used again by userspace. |
| 1354 | */ |
| 1355 | if (!dentry) |
| 1356 | goto out_unlock; |
| 1357 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Stephen Smalley | f9aecb3 | 2015-05-19 13:59:12 -0400 | [diff] [blame] | 1358 | rc = selinux_genfs_get_sid(dentry, isec->sclass, |
| 1359 | sbsec->flags, &sid); |
Paul Moore | 810be00 | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1360 | dput(dentry); |
| 1361 | if (rc) |
| 1362 | goto out_unlock; |
| 1363 | isec->sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
| 1365 | break; |
| 1366 | } |
| 1367 | |
| 1368 | isec->initialized = 1; |
| 1369 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1370 | out_unlock: |
| 1371 | mutex_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | out: |
| 1373 | if (isec->sclass == SECCLASS_FILE) |
| 1374 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return rc; |
| 1376 | } |
| 1377 | |
| 1378 | /* Convert a Linux signal to an access vector. */ |
| 1379 | static inline u32 signal_to_av(int sig) |
| 1380 | { |
| 1381 | u32 perm = 0; |
| 1382 | |
| 1383 | switch (sig) { |
| 1384 | case SIGCHLD: |
| 1385 | /* Commonly granted from child to parent. */ |
| 1386 | perm = PROCESS__SIGCHLD; |
| 1387 | break; |
| 1388 | case SIGKILL: |
| 1389 | /* Cannot be caught or ignored */ |
| 1390 | perm = PROCESS__SIGKILL; |
| 1391 | break; |
| 1392 | case SIGSTOP: |
| 1393 | /* Cannot be caught or ignored */ |
| 1394 | perm = PROCESS__SIGSTOP; |
| 1395 | break; |
| 1396 | default: |
| 1397 | /* All other signals. */ |
| 1398 | perm = PROCESS__SIGNAL; |
| 1399 | break; |
| 1400 | } |
| 1401 | |
| 1402 | return perm; |
| 1403 | } |
| 1404 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1405 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1406 | * Check permission between a pair of credentials |
| 1407 | * fork check, ptrace check, etc. |
| 1408 | */ |
| 1409 | static int cred_has_perm(const struct cred *actor, |
| 1410 | const struct cred *target, |
| 1411 | u32 perms) |
| 1412 | { |
| 1413 | u32 asid = cred_sid(actor), tsid = cred_sid(target); |
| 1414 | |
| 1415 | return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); |
| 1416 | } |
| 1417 | |
| 1418 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1419 | * Check permission between a pair of tasks, e.g. signal checks, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1420 | * fork check, ptrace check, etc. |
| 1421 | * tsk1 is the actor and tsk2 is the target |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1422 | * - this uses the default subjective creds of tsk1 |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1423 | */ |
| 1424 | static int task_has_perm(const struct task_struct *tsk1, |
| 1425 | const struct task_struct *tsk2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | u32 perms) |
| 1427 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1428 | const struct task_security_struct *__tsec1, *__tsec2; |
| 1429 | u32 sid1, sid2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1431 | rcu_read_lock(); |
| 1432 | __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; |
| 1433 | __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; |
| 1434 | rcu_read_unlock(); |
| 1435 | return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1438 | /* |
| 1439 | * Check permission between current and another task, e.g. signal checks, |
| 1440 | * fork check, ptrace check, etc. |
| 1441 | * current is the actor and tsk2 is the target |
| 1442 | * - this uses current's subjective creds |
| 1443 | */ |
| 1444 | static int current_has_perm(const struct task_struct *tsk, |
| 1445 | u32 perms) |
| 1446 | { |
| 1447 | u32 sid, tsid; |
| 1448 | |
| 1449 | sid = current_sid(); |
| 1450 | tsid = task_sid(tsk); |
| 1451 | return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); |
| 1452 | } |
| 1453 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1454 | #if CAP_LAST_CAP > 63 |
| 1455 | #error Fix SELinux to handle capabilities > 63. |
| 1456 | #endif |
| 1457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | /* Check whether a task is allowed to use a capability. */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1459 | static int cred_has_capability(const struct cred *cred, |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1460 | int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1462 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1463 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1464 | struct av_decision avd; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1465 | u16 sclass; |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 1466 | u32 sid = cred_sid(cred); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1467 | u32 av = CAP_TO_MASK(cap); |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1468 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1470 | COMMON_AUDIT_DATA_INIT(&ad, CAP); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1471 | ad.selinux_audit_data = &sad; |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1472 | ad.tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | ad.u.cap = cap; |
| 1474 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1475 | switch (CAP_TO_INDEX(cap)) { |
| 1476 | case 0: |
| 1477 | sclass = SECCLASS_CAPABILITY; |
| 1478 | break; |
| 1479 | case 1: |
| 1480 | sclass = SECCLASS_CAPABILITY2; |
| 1481 | break; |
| 1482 | default: |
| 1483 | printk(KERN_ERR |
| 1484 | "SELinux: out of range capability %d\n", cap); |
| 1485 | BUG(); |
Eric Paris | a35c6c8 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1486 | return -EINVAL; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1487 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1488 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1489 | rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1490 | if (audit == SECURITY_CAP_AUDIT) { |
| 1491 | int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0); |
| 1492 | if (rc2) |
| 1493 | return rc2; |
| 1494 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1495 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /* Check whether a task is allowed to use a system operation. */ |
| 1499 | static int task_has_system(struct task_struct *tsk, |
| 1500 | u32 perms) |
| 1501 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1502 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1504 | return avc_has_perm(sid, SECINITSID_KERNEL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | SECCLASS_SYSTEM, perms, NULL); |
| 1506 | } |
| 1507 | |
| 1508 | /* Check whether a task has a particular permission to an inode. |
| 1509 | The 'adp' parameter is optional and allows other audit |
| 1510 | data to be passed (e.g. the dentry). */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1511 | static int inode_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | struct inode *inode, |
| 1513 | u32 perms, |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1514 | struct common_audit_data *adp, |
| 1515 | unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1518 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 1520 | validate_creds(cred); |
| 1521 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1522 | if (unlikely(IS_PRIVATE(inode))) |
Stephen Smalley | bbaca6c | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 1523 | return 0; |
| 1524 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1525 | sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | isec = inode->i_security; |
| 1527 | |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1528 | 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] | 1529 | } |
| 1530 | |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 1531 | static int inode_has_perm_noadp(const struct cred *cred, |
| 1532 | struct inode *inode, |
| 1533 | u32 perms, |
| 1534 | unsigned flags) |
| 1535 | { |
| 1536 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1537 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 1538 | |
| 1539 | COMMON_AUDIT_DATA_INIT(&ad, INODE); |
| 1540 | ad.u.inode = inode; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1541 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 1542 | return inode_has_perm(cred, inode, perms, &ad, flags); |
| 1543 | } |
| 1544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1546 | the dentry to help the auditing code to more easily generate the |
| 1547 | pathname if needed. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1548 | static inline int dentry_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | struct dentry *dentry, |
| 1550 | u32 av) |
| 1551 | { |
| 1552 | struct inode *inode = dentry->d_inode; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1553 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1554 | struct selinux_audit_data sad = {0,}; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1555 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1556 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
| 1557 | ad.u.dentry = dentry; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1558 | ad.selinux_audit_data = &sad; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1559 | return inode_has_perm(cred, inode, av, &ad, 0); |
| 1560 | } |
| 1561 | |
| 1562 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1563 | the path to help the auditing code to more easily generate the |
| 1564 | pathname if needed. */ |
| 1565 | static inline int path_has_perm(const struct cred *cred, |
| 1566 | struct path *path, |
| 1567 | u32 av) |
| 1568 | { |
| 1569 | struct inode *inode = path->dentry->d_inode; |
| 1570 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1571 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1572 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1573 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1574 | ad.u.path = *path; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1575 | ad.selinux_audit_data = &sad; |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1576 | return inode_has_perm(cred, inode, av, &ad, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* Check whether a task can use an open file descriptor to |
| 1580 | access an inode in a given way. Check access to the |
| 1581 | descriptor itself, and then use dentry_has_perm to |
| 1582 | check a particular permission to the file. |
| 1583 | Access to the descriptor is implicitly granted if it |
| 1584 | has the same SID as the process. If av is zero, then |
| 1585 | access to the file is not checked, e.g. for cases |
| 1586 | where only the descriptor is affected like seek. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1587 | static int file_has_perm(const struct cred *cred, |
| 1588 | struct file *file, |
| 1589 | u32 av) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | struct file_security_struct *fsec = file->f_security; |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1592 | struct inode *inode = file->f_path.dentry->d_inode; |
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,}; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1595 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | int rc; |
| 1597 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1598 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
| 1599 | ad.u.path = file->f_path; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1600 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1602 | if (sid != fsec->sid) { |
| 1603 | rc = avc_has_perm(sid, fsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | SECCLASS_FD, |
| 1605 | FD__USE, |
| 1606 | &ad); |
| 1607 | if (rc) |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1608 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* av is zero if only checking access to the descriptor. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1612 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | if (av) |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1614 | rc = inode_has_perm(cred, inode, av, &ad, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1616 | out: |
| 1617 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /* Check whether a task can create a file. */ |
| 1621 | static int may_create(struct inode *dir, |
| 1622 | struct dentry *dentry, |
| 1623 | u16 tclass) |
| 1624 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 1625 | const struct task_security_struct *tsec = current_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | struct inode_security_struct *dsec; |
| 1627 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1628 | u32 sid, newsid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1629 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1630 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | int rc; |
| 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | dsec = dir->i_security; |
| 1634 | sbsec = dir->i_sb->s_security; |
| 1635 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1636 | sid = tsec->sid; |
| 1637 | newsid = tsec->create_sid; |
| 1638 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1639 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
| 1640 | ad.u.dentry = dentry; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1641 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1643 | rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | DIR__ADD_NAME | DIR__SEARCH, |
| 1645 | &ad); |
| 1646 | if (rc) |
| 1647 | return rc; |
| 1648 | |
David P. Quigley | cd89596 | 2009-01-16 09:22:04 -0500 | [diff] [blame] | 1649 | if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { |
Eric Paris | cb1e922 | 2011-04-28 15:11:21 -0400 | [diff] [blame] | 1650 | rc = security_transition_sid(sid, dsec->sid, tclass, |
| 1651 | &dentry->d_name, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | if (rc) |
| 1653 | return rc; |
| 1654 | } |
| 1655 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1656 | rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | if (rc) |
| 1658 | return rc; |
| 1659 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 1660 | rc = avc_has_perm(newsid, sbsec->sid, |
| 1661 | SECCLASS_FILESYSTEM, |
| 1662 | FILESYSTEM__ASSOCIATE, &ad); |
| 1663 | if (rc) |
| 1664 | return rc; |
| 1665 | |
| 1666 | rc = pft_inode_mknod(dir, dentry, 0, 0); |
| 1667 | |
| 1668 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1671 | /* Check whether a task can create a key. */ |
| 1672 | static int may_create_key(u32 ksid, |
| 1673 | struct task_struct *ctx) |
| 1674 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1675 | u32 sid = task_sid(ctx); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1676 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1677 | return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1680 | #define MAY_LINK 0 |
| 1681 | #define MAY_UNLINK 1 |
| 1682 | #define MAY_RMDIR 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ |
| 1685 | static int may_link(struct inode *dir, |
| 1686 | struct dentry *dentry, |
| 1687 | int kind) |
| 1688 | |
| 1689 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | struct inode_security_struct *dsec, *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1691 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1692 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1693 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | u32 av; |
| 1695 | int rc; |
| 1696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | dsec = dir->i_security; |
| 1698 | isec = dentry->d_inode->i_security; |
| 1699 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1700 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
| 1701 | ad.u.dentry = dentry; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1702 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
| 1704 | av = DIR__SEARCH; |
| 1705 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1706 | rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | if (rc) |
| 1708 | return rc; |
| 1709 | |
| 1710 | switch (kind) { |
| 1711 | case MAY_LINK: |
| 1712 | av = FILE__LINK; |
| 1713 | break; |
| 1714 | case MAY_UNLINK: |
| 1715 | av = FILE__UNLINK; |
| 1716 | break; |
| 1717 | case MAY_RMDIR: |
| 1718 | av = DIR__RMDIR; |
| 1719 | break; |
| 1720 | default: |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1721 | printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", |
| 1722 | __func__, kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
| 1725 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1726 | rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 1727 | if (rc) |
| 1728 | return rc; |
| 1729 | |
| 1730 | if (kind == MAY_UNLINK) |
| 1731 | rc = pft_inode_unlink(dir, dentry); |
| 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | return rc; |
| 1734 | } |
| 1735 | |
| 1736 | static inline int may_rename(struct inode *old_dir, |
| 1737 | struct dentry *old_dentry, |
| 1738 | struct inode *new_dir, |
| 1739 | struct dentry *new_dentry) |
| 1740 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1742 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1743 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1744 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | u32 av; |
| 1746 | int old_is_dir, new_is_dir; |
| 1747 | int rc; |
| 1748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | old_dsec = old_dir->i_security; |
| 1750 | old_isec = old_dentry->d_inode->i_security; |
| 1751 | old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); |
| 1752 | new_dsec = new_dir->i_security; |
| 1753 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1754 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 1755 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1757 | ad.u.dentry = old_dentry; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1758 | rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
| 1760 | if (rc) |
| 1761 | return rc; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1762 | rc = avc_has_perm(sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | old_isec->sclass, FILE__RENAME, &ad); |
| 1764 | if (rc) |
| 1765 | return rc; |
| 1766 | if (old_is_dir && new_dir != old_dir) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1767 | rc = avc_has_perm(sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | old_isec->sclass, DIR__REPARENT, &ad); |
| 1769 | if (rc) |
| 1770 | return rc; |
| 1771 | } |
| 1772 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1773 | ad.u.dentry = new_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | av = DIR__ADD_NAME | DIR__SEARCH; |
| 1775 | if (new_dentry->d_inode) |
| 1776 | av |= DIR__REMOVE_NAME; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1777 | rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | if (rc) |
| 1779 | return rc; |
| 1780 | if (new_dentry->d_inode) { |
| 1781 | new_isec = new_dentry->d_inode->i_security; |
| 1782 | new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1783 | rc = avc_has_perm(sid, new_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | new_isec->sclass, |
| 1785 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); |
| 1786 | if (rc) |
| 1787 | return rc; |
| 1788 | } |
| 1789 | |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | /* Check whether a task can perform a filesystem operation. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1794 | static int superblock_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | struct super_block *sb, |
| 1796 | u32 perms, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1797 | struct common_audit_data *ad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | struct superblock_security_struct *sbsec; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1800 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | sbsec = sb->s_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1803 | return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /* Convert a Linux mode and permission mask to an access vector. */ |
| 1807 | static inline u32 file_mask_to_av(int mode, int mask) |
| 1808 | { |
| 1809 | u32 av = 0; |
| 1810 | |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 1811 | if (!S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | if (mask & MAY_EXEC) |
| 1813 | av |= FILE__EXECUTE; |
| 1814 | if (mask & MAY_READ) |
| 1815 | av |= FILE__READ; |
| 1816 | |
| 1817 | if (mask & MAY_APPEND) |
| 1818 | av |= FILE__APPEND; |
| 1819 | else if (mask & MAY_WRITE) |
| 1820 | av |= FILE__WRITE; |
| 1821 | |
| 1822 | } else { |
| 1823 | if (mask & MAY_EXEC) |
| 1824 | av |= DIR__SEARCH; |
| 1825 | if (mask & MAY_WRITE) |
| 1826 | av |= DIR__WRITE; |
| 1827 | if (mask & MAY_READ) |
| 1828 | av |= DIR__READ; |
| 1829 | } |
| 1830 | |
| 1831 | return av; |
| 1832 | } |
| 1833 | |
| 1834 | /* Convert a Linux file to an access vector. */ |
| 1835 | static inline u32 file_to_av(struct file *file) |
| 1836 | { |
| 1837 | u32 av = 0; |
| 1838 | |
| 1839 | if (file->f_mode & FMODE_READ) |
| 1840 | av |= FILE__READ; |
| 1841 | if (file->f_mode & FMODE_WRITE) { |
| 1842 | if (file->f_flags & O_APPEND) |
| 1843 | av |= FILE__APPEND; |
| 1844 | else |
| 1845 | av |= FILE__WRITE; |
| 1846 | } |
Stephen Smalley | 0794c66 | 2008-03-17 08:55:18 -0400 | [diff] [blame] | 1847 | if (!av) { |
| 1848 | /* |
| 1849 | * Special file opened with flags 3 for ioctl-only use. |
| 1850 | */ |
| 1851 | av = FILE__IOCTL; |
| 1852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
| 1854 | return av; |
| 1855 | } |
| 1856 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1857 | /* |
| 1858 | * Convert a file to an access vector and include the correct open |
| 1859 | * open permission. |
| 1860 | */ |
| 1861 | static inline u32 open_file_to_av(struct file *file) |
| 1862 | { |
| 1863 | u32 av = file_to_av(file); |
| 1864 | |
Eric Paris | 49b7b8d | 2010-07-23 11:44:09 -0400 | [diff] [blame] | 1865 | if (selinux_policycap_openperm) |
| 1866 | av |= FILE__OPEN; |
| 1867 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1868 | return av; |
| 1869 | } |
| 1870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | /* Hook functions begin here. */ |
| 1872 | |
Stephen Smalley | 48a2370 | 2012-11-05 08:15:34 -0500 | [diff] [blame] | 1873 | static int selinux_binder_set_context_mgr(struct task_struct *mgr) |
| 1874 | { |
| 1875 | u32 mysid = current_sid(); |
| 1876 | u32 mgrsid = task_sid(mgr); |
| 1877 | |
| 1878 | return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL); |
| 1879 | } |
| 1880 | |
| 1881 | static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to) |
| 1882 | { |
| 1883 | u32 mysid = current_sid(); |
| 1884 | u32 fromsid = task_sid(from); |
| 1885 | u32 tosid = task_sid(to); |
| 1886 | int rc; |
| 1887 | |
| 1888 | if (mysid != fromsid) { |
| 1889 | rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL); |
| 1890 | if (rc) |
| 1891 | return rc; |
| 1892 | } |
| 1893 | |
| 1894 | return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL); |
| 1895 | } |
| 1896 | |
| 1897 | static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to) |
| 1898 | { |
| 1899 | u32 fromsid = task_sid(from); |
| 1900 | u32 tosid = task_sid(to); |
| 1901 | return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL); |
| 1902 | } |
| 1903 | |
| 1904 | static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file) |
| 1905 | { |
| 1906 | u32 sid = task_sid(to); |
| 1907 | struct file_security_struct *fsec = file->f_security; |
| 1908 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1909 | struct inode_security_struct *isec = inode->i_security; |
| 1910 | struct common_audit_data ad; |
| 1911 | struct selinux_audit_data sad = {0,}; |
| 1912 | int rc; |
| 1913 | |
| 1914 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
| 1915 | ad.u.path = file->f_path; |
| 1916 | ad.selinux_audit_data = &sad; |
| 1917 | |
| 1918 | if (sid != fsec->sid) { |
| 1919 | rc = avc_has_perm(sid, fsec->sid, |
| 1920 | SECCLASS_FD, |
| 1921 | FD__USE, |
| 1922 | &ad); |
| 1923 | if (rc) |
| 1924 | return rc; |
| 1925 | } |
| 1926 | |
| 1927 | if (unlikely(IS_PRIVATE(inode))) |
| 1928 | return 0; |
| 1929 | |
| 1930 | return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file), |
| 1931 | &ad); |
| 1932 | } |
| 1933 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 1934 | static int selinux_ptrace_access_check(struct task_struct *child, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1935 | unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | int rc; |
| 1938 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 1939 | rc = cap_ptrace_access_check(child, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | if (rc) |
| 1941 | return rc; |
| 1942 | |
Eric Paris | 69f594a | 2012-01-03 12:25:15 -0500 | [diff] [blame] | 1943 | if (mode & PTRACE_MODE_READ) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1944 | u32 sid = current_sid(); |
| 1945 | u32 csid = task_sid(child); |
| 1946 | return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1949 | return current_has_perm(child, PROCESS__PTRACE); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | static int selinux_ptrace_traceme(struct task_struct *parent) |
| 1953 | { |
| 1954 | int rc; |
| 1955 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 1956 | rc = cap_ptrace_traceme(parent); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1957 | if (rc) |
| 1958 | return rc; |
| 1959 | |
| 1960 | return task_has_perm(parent, current, PROCESS__PTRACE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1964 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | { |
| 1966 | int error; |
| 1967 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 1968 | error = current_has_perm(target, PROCESS__GETCAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | if (error) |
| 1970 | return error; |
| 1971 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 1972 | return cap_capget(target, effective, inheritable, permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1975 | static int selinux_capset(struct cred *new, const struct cred *old, |
| 1976 | const kernel_cap_t *effective, |
| 1977 | const kernel_cap_t *inheritable, |
| 1978 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | { |
| 1980 | int error; |
| 1981 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 1982 | error = cap_capset(new, old, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1983 | effective, inheritable, permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | if (error) |
| 1985 | return error; |
| 1986 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1987 | return cred_has_perm(old, new, PROCESS__SETCAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
James Morris | 5626d3e | 2009-01-30 10:05:06 +1100 | [diff] [blame] | 1990 | /* |
| 1991 | * (This comment used to live with the selinux_task_setuid hook, |
| 1992 | * which was removed). |
| 1993 | * |
| 1994 | * Since setuid only affects the current process, and since the SELinux |
| 1995 | * controls are not based on the Linux identity attributes, SELinux does not |
| 1996 | * need to control this operation. However, SELinux does control the use of |
| 1997 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. |
| 1998 | */ |
| 1999 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2000 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
| 2001 | int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | { |
| 2003 | int rc; |
| 2004 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2005 | rc = cap_capable(cred, ns, cap, audit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | if (rc) |
| 2007 | return rc; |
| 2008 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2009 | return cred_has_capability(cred, cap, audit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| 2013 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2014 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | int rc = 0; |
| 2016 | |
| 2017 | if (!sb) |
| 2018 | return 0; |
| 2019 | |
| 2020 | switch (cmds) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2021 | case Q_SYNC: |
| 2022 | case Q_QUOTAON: |
| 2023 | case Q_QUOTAOFF: |
| 2024 | case Q_SETINFO: |
| 2025 | case Q_SETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2026 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2027 | break; |
| 2028 | case Q_GETFMT: |
| 2029 | case Q_GETINFO: |
| 2030 | case Q_GETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2031 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2032 | break; |
| 2033 | default: |
| 2034 | rc = 0; /* let the kernel handle invalid cmds */ |
| 2035 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | } |
| 2037 | return rc; |
| 2038 | } |
| 2039 | |
| 2040 | static int selinux_quota_on(struct dentry *dentry) |
| 2041 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2042 | const struct cred *cred = current_cred(); |
| 2043 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2044 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | } |
| 2046 | |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 2047 | static int selinux_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | { |
| 2049 | int rc; |
| 2050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2052 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
| 2053 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2054 | rc = task_has_system(current, SYSTEM__SYSLOG_READ); |
| 2055 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2056 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
| 2057 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ |
| 2058 | /* Set level of messages printed to console */ |
| 2059 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2060 | rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); |
| 2061 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2062 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
| 2063 | case SYSLOG_ACTION_OPEN: /* Open log */ |
| 2064 | case SYSLOG_ACTION_READ: /* Read from log */ |
| 2065 | case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */ |
| 2066 | case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2067 | default: |
| 2068 | rc = task_has_system(current, SYSTEM__SYSLOG_MOD); |
| 2069 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | } |
| 2071 | return rc; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Check that a process has enough memory to allocate a new virtual |
| 2076 | * mapping. 0 means there is enough memory for the allocation to |
| 2077 | * succeed and -ENOMEM implies there is not. |
| 2078 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | * Do not audit the selinux permission check, as this is applied to all |
| 2080 | * processes that allocate mappings. |
| 2081 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2082 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | { |
| 2084 | int rc, cap_sys_admin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2086 | rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 2087 | SECURITY_CAP_NOAUDIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | if (rc == 0) |
| 2089 | cap_sys_admin = 1; |
| 2090 | |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2091 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* binprm security operations */ |
| 2095 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2096 | static int selinux_bprm_set_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2098 | const struct task_security_struct *old_tsec; |
| 2099 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | struct inode_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2101 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2102 | struct selinux_audit_data sad = {0,}; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2103 | struct inode *inode = bprm->file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | int rc; |
| 2105 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 2106 | rc = cap_bprm_set_creds(bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | if (rc) |
| 2108 | return rc; |
| 2109 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2110 | /* SELinux context only depends on initial program or script and not |
| 2111 | * the script interpreter */ |
| 2112 | if (bprm->cred_prepared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | return 0; |
| 2114 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2115 | old_tsec = current_security(); |
| 2116 | new_tsec = bprm->cred->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | isec = inode->i_security; |
| 2118 | |
| 2119 | /* Default to the current task SID. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2120 | new_tsec->sid = old_tsec->sid; |
| 2121 | new_tsec->osid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
Michael LeMay | 28eba5b | 2006-06-27 02:53:42 -0700 | [diff] [blame] | 2123 | /* Reset fs, key, and sock SIDs on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2124 | new_tsec->create_sid = 0; |
| 2125 | new_tsec->keycreate_sid = 0; |
| 2126 | new_tsec->sockcreate_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2128 | if (old_tsec->exec_sid) { |
| 2129 | new_tsec->sid = old_tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | /* Reset exec SID on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2131 | new_tsec->exec_sid = 0; |
Andy Lutomirski | 397a85e | 2012-01-30 08:17:26 -0800 | [diff] [blame] | 2132 | |
| 2133 | /* |
| 2134 | * Minimize confusion: if no_new_privs and a transition is |
| 2135 | * explicitly requested, then fail the exec. |
| 2136 | */ |
| 2137 | if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) |
| 2138 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | } else { |
| 2140 | /* Check for a default transition on this program. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2141 | rc = security_transition_sid(old_tsec->sid, isec->sid, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2142 | SECCLASS_PROCESS, NULL, |
| 2143 | &new_tsec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | if (rc) |
| 2145 | return rc; |
| 2146 | } |
| 2147 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2148 | COMMON_AUDIT_DATA_INIT(&ad, PATH); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2149 | ad.selinux_audit_data = &sad; |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2150 | ad.u.path = bprm->file->f_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | |
Andy Lutomirski | 397a85e | 2012-01-30 08:17:26 -0800 | [diff] [blame] | 2152 | if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) || |
| 2153 | (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2154 | new_tsec->sid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2156 | if (new_tsec->sid == old_tsec->sid) { |
| 2157 | rc = avc_has_perm(old_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
| 2159 | if (rc) |
| 2160 | return rc; |
| 2161 | } else { |
| 2162 | /* Check permissions for the transition. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2163 | rc = avc_has_perm(old_tsec->sid, new_tsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
| 2165 | if (rc) |
| 2166 | return rc; |
| 2167 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2168 | rc = avc_has_perm(new_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
| 2170 | if (rc) |
| 2171 | return rc; |
| 2172 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2173 | /* Check for shared state */ |
| 2174 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
| 2175 | rc = avc_has_perm(old_tsec->sid, new_tsec->sid, |
| 2176 | SECCLASS_PROCESS, PROCESS__SHARE, |
| 2177 | NULL); |
| 2178 | if (rc) |
| 2179 | return -EPERM; |
| 2180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2182 | /* Make sure that anyone attempting to ptrace over a task that |
| 2183 | * changes its SID has the appropriate permit */ |
| 2184 | if (bprm->unsafe & |
| 2185 | (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { |
| 2186 | struct task_struct *tracer; |
| 2187 | struct task_security_struct *sec; |
| 2188 | u32 ptsid = 0; |
| 2189 | |
| 2190 | rcu_read_lock(); |
Tejun Heo | 06d9847 | 2011-06-17 16:50:40 +0200 | [diff] [blame] | 2191 | tracer = ptrace_parent(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2192 | if (likely(tracer != NULL)) { |
| 2193 | sec = __task_cred(tracer)->security; |
| 2194 | ptsid = sec->sid; |
| 2195 | } |
| 2196 | rcu_read_unlock(); |
| 2197 | |
| 2198 | if (ptsid != 0) { |
| 2199 | rc = avc_has_perm(ptsid, new_tsec->sid, |
| 2200 | SECCLASS_PROCESS, |
| 2201 | PROCESS__PTRACE, NULL); |
| 2202 | if (rc) |
| 2203 | return -EPERM; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /* Clear any possibly unsafe personality bits on exec: */ |
| 2208 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | return 0; |
| 2212 | } |
| 2213 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2214 | static int selinux_bprm_secureexec(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 2216 | const struct task_security_struct *tsec = current_security(); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2217 | u32 sid, osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | int atsecure = 0; |
| 2219 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2220 | sid = tsec->sid; |
| 2221 | osid = tsec->osid; |
| 2222 | |
| 2223 | if (osid != sid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | /* Enable secure mode for SIDs transitions unless |
| 2225 | the noatsecure permission is granted between |
| 2226 | the two SIDs, i.e. ahp returns 0. */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2227 | atsecure = avc_has_perm(osid, sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2228 | SECCLASS_PROCESS, |
| 2229 | PROCESS__NOATSECURE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 2232 | return (atsecure || cap_bprm_secureexec(bprm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | /* Derived from fs/exec.c:flush_old_files. */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2236 | static inline void flush_unauthorized_files(const struct cred *cred, |
| 2237 | struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2239 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2240 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | struct file *file, *devnull = NULL; |
Stephen Smalley | b20c812 | 2006-09-25 23:32:03 -0700 | [diff] [blame] | 2242 | struct tty_struct *tty; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 2243 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | long j = -1; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2245 | int drop_tty = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2247 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | if (tty) { |
Nick Piggin | ee2ffa0 | 2010-08-18 04:37:35 +1000 | [diff] [blame] | 2249 | spin_lock(&tty_files_lock); |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2250 | if (!list_empty(&tty->tty_files)) { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2251 | struct tty_file_private *file_priv; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2252 | struct inode *inode; |
| 2253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | /* Revalidate access to controlling tty. |
| 2255 | Use inode_has_perm on the tty inode directly rather |
| 2256 | than using file_has_perm, as this particular open |
| 2257 | file may belong to another process and we are only |
| 2258 | interested in the inode-based check here. */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2259 | file_priv = list_first_entry(&tty->tty_files, |
| 2260 | struct tty_file_private, list); |
| 2261 | file = file_priv->file; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2262 | inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 2263 | if (inode_has_perm_noadp(cred, inode, |
| 2264 | FILE__READ | FILE__WRITE, 0)) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2265 | drop_tty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | } |
| 2267 | } |
Nick Piggin | ee2ffa0 | 2010-08-18 04:37:35 +1000 | [diff] [blame] | 2268 | spin_unlock(&tty_files_lock); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 2269 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | } |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 2271 | /* Reset controlling tty. */ |
| 2272 | if (drop_tty) |
| 2273 | no_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
| 2275 | /* Revalidate access to inherited open files. */ |
| 2276 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2277 | COMMON_AUDIT_DATA_INIT(&ad, INODE); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2278 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | |
| 2280 | spin_lock(&files->file_lock); |
| 2281 | for (;;) { |
| 2282 | unsigned long set, i; |
| 2283 | int fd; |
| 2284 | |
| 2285 | j++; |
Josh Boyer | f17e923 | 2012-07-25 10:40:34 -0400 | [diff] [blame] | 2286 | i = j * BITS_PER_LONG; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 2287 | fdt = files_fdtable(files); |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 2288 | if (i >= fdt->max_fds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | break; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 2290 | set = fdt->open_fds[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | if (!set) |
| 2292 | continue; |
| 2293 | spin_unlock(&files->file_lock); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2294 | for ( ; set ; i++, set >>= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | if (set & 1) { |
| 2296 | file = fget(i); |
| 2297 | if (!file) |
| 2298 | continue; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2299 | if (file_has_perm(cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | file, |
| 2301 | file_to_av(file))) { |
| 2302 | sys_close(i); |
| 2303 | fd = get_unused_fd(); |
| 2304 | if (fd != i) { |
| 2305 | if (fd >= 0) |
| 2306 | put_unused_fd(fd); |
| 2307 | fput(file); |
| 2308 | continue; |
| 2309 | } |
| 2310 | if (devnull) { |
Nick Piggin | 095975d | 2006-01-08 01:02:19 -0800 | [diff] [blame] | 2311 | get_file(devnull); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | } else { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2313 | devnull = dentry_open( |
| 2314 | dget(selinux_null), |
| 2315 | mntget(selinuxfs_mount), |
| 2316 | O_RDWR, cred); |
Akinobu Mita | fc5d81e | 2006-11-27 15:16:48 +0900 | [diff] [blame] | 2317 | if (IS_ERR(devnull)) { |
| 2318 | devnull = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | put_unused_fd(fd); |
| 2320 | fput(file); |
| 2321 | continue; |
| 2322 | } |
| 2323 | } |
| 2324 | fd_install(fd, devnull); |
| 2325 | } |
| 2326 | fput(file); |
| 2327 | } |
| 2328 | } |
| 2329 | spin_lock(&files->file_lock); |
| 2330 | |
| 2331 | } |
| 2332 | spin_unlock(&files->file_lock); |
| 2333 | } |
| 2334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2336 | * Prepare a process for imminent new credential changes due to exec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2338 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2340 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | struct rlimit *rlim, *initrlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | int rc, i; |
| 2343 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2344 | new_tsec = bprm->cred->security; |
| 2345 | if (new_tsec->sid == new_tsec->osid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | return; |
| 2347 | |
| 2348 | /* Close files for which the new task SID is not authorized. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2349 | flush_unauthorized_files(bprm->cred, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2351 | /* Always clear parent death signal on SID transitions. */ |
| 2352 | current->pdeath_signal = 0; |
| 2353 | |
| 2354 | /* Check whether the new SID can inherit resource limits from the old |
| 2355 | * SID. If not, reset all soft limits to the lower of the current |
| 2356 | * task's hard limit and the init task's soft limit. |
| 2357 | * |
| 2358 | * Note that the setting of hard limits (even to lower them) can be |
| 2359 | * controlled by the setrlimit check. The inclusion of the init task's |
| 2360 | * soft limit into the computation is to avoid resetting soft limits |
| 2361 | * higher than the default soft limit for cases where the default is |
| 2362 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. |
| 2363 | */ |
| 2364 | rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, |
| 2365 | PROCESS__RLIMITINH, NULL); |
| 2366 | if (rc) { |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2367 | /* protect against do_prlimit() */ |
| 2368 | task_lock(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2369 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 2370 | rlim = current->signal->rlim + i; |
| 2371 | initrlim = init_task.signal->rlim + i; |
| 2372 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); |
| 2373 | } |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2374 | task_unlock(current); |
| 2375 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | /* |
| 2380 | * Clean up the process immediately after the installation of new credentials |
| 2381 | * due to exec |
| 2382 | */ |
| 2383 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2384 | { |
| 2385 | const struct task_security_struct *tsec = current_security(); |
| 2386 | struct itimerval itimer; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2387 | u32 osid, sid; |
| 2388 | int rc, i; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2389 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2390 | osid = tsec->osid; |
| 2391 | sid = tsec->sid; |
| 2392 | |
| 2393 | if (sid == osid) |
| 2394 | return; |
| 2395 | |
| 2396 | /* Check whether the new SID can inherit signal state from the old SID. |
| 2397 | * If not, clear itimers to avoid subsequent signal generation and |
| 2398 | * flush and unblock signals. |
| 2399 | * |
| 2400 | * This must occur _after_ the task SID has been updated so that any |
| 2401 | * kill done after the flush will be checked against the new SID. |
| 2402 | */ |
| 2403 | rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if (rc) { |
| 2405 | memset(&itimer, 0, sizeof itimer); |
| 2406 | for (i = 0; i < 3; i++) |
| 2407 | do_setitimer(i, &itimer, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | spin_lock_irq(¤t->sighand->siglock); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2409 | if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { |
| 2410 | __flush_signals(current); |
| 2411 | flush_signal_handlers(current, 1); |
| 2412 | sigemptyset(¤t->blocked); |
| 2413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | spin_unlock_irq(¤t->sighand->siglock); |
| 2415 | } |
| 2416 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2417 | /* Wake up the parent if it is waiting so that it can recheck |
| 2418 | * wait permission to the new task SID. */ |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2419 | read_lock(&tasklist_lock); |
Oleg Nesterov | 0b7570e | 2009-09-23 15:56:46 -0700 | [diff] [blame] | 2420 | __wake_up_parent(current, current->real_parent); |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2421 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* superblock security operations */ |
| 2425 | |
| 2426 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2427 | { |
| 2428 | return superblock_alloc_security(sb); |
| 2429 | } |
| 2430 | |
| 2431 | static void selinux_sb_free_security(struct super_block *sb) |
| 2432 | { |
| 2433 | superblock_free_security(sb); |
| 2434 | } |
| 2435 | |
| 2436 | static inline int match_prefix(char *prefix, int plen, char *option, int olen) |
| 2437 | { |
| 2438 | if (plen > olen) |
| 2439 | return 0; |
| 2440 | |
| 2441 | return !memcmp(prefix, option, plen); |
| 2442 | } |
| 2443 | |
| 2444 | static inline int selinux_option(char *option, int len) |
| 2445 | { |
Eric Paris | 832cbd9 | 2008-04-01 13:24:09 -0400 | [diff] [blame] | 2446 | return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || |
| 2447 | match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || |
| 2448 | match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 2449 | match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || |
| 2450 | match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | static inline void take_option(char **to, char *from, int *first, int len) |
| 2454 | { |
| 2455 | if (!*first) { |
| 2456 | **to = ','; |
| 2457 | *to += 1; |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2458 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | *first = 0; |
| 2460 | memcpy(*to, from, len); |
| 2461 | *to += len; |
| 2462 | } |
| 2463 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2464 | static inline void take_selinux_option(char **to, char *from, int *first, |
| 2465 | int len) |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2466 | { |
| 2467 | int current_size = 0; |
| 2468 | |
| 2469 | if (!*first) { |
| 2470 | **to = '|'; |
| 2471 | *to += 1; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2472 | } else |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2473 | *first = 0; |
| 2474 | |
| 2475 | while (current_size < len) { |
| 2476 | if (*from != '"') { |
| 2477 | **to = *from; |
| 2478 | *to += 1; |
| 2479 | } |
| 2480 | from += 1; |
| 2481 | current_size += 1; |
| 2482 | } |
| 2483 | } |
| 2484 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 2485 | static int selinux_sb_copy_data(char *orig, char *copy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | { |
| 2487 | int fnosec, fsec, rc = 0; |
| 2488 | char *in_save, *in_curr, *in_end; |
| 2489 | char *sec_curr, *nosec_save, *nosec; |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2490 | int open_quote = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | |
| 2492 | in_curr = orig; |
| 2493 | sec_curr = copy; |
| 2494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | nosec = (char *)get_zeroed_page(GFP_KERNEL); |
| 2496 | if (!nosec) { |
| 2497 | rc = -ENOMEM; |
| 2498 | goto out; |
| 2499 | } |
| 2500 | |
| 2501 | nosec_save = nosec; |
| 2502 | fnosec = fsec = 1; |
| 2503 | in_save = in_end = orig; |
| 2504 | |
| 2505 | do { |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2506 | if (*in_end == '"') |
| 2507 | open_quote = !open_quote; |
| 2508 | if ((*in_end == ',' && open_quote == 0) || |
| 2509 | *in_end == '\0') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | int len = in_end - in_curr; |
| 2511 | |
| 2512 | if (selinux_option(in_curr, len)) |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2513 | take_selinux_option(&sec_curr, in_curr, &fsec, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | else |
| 2515 | take_option(&nosec, in_curr, &fnosec, len); |
| 2516 | |
| 2517 | in_curr = in_end + 1; |
| 2518 | } |
| 2519 | } while (*in_end++); |
| 2520 | |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 2521 | strcpy(in_save, nosec_save); |
Gerald Schaefer | da3caa2 | 2005-06-21 17:15:18 -0700 | [diff] [blame] | 2522 | free_page((unsigned long)nosec_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | out: |
| 2524 | return rc; |
| 2525 | } |
| 2526 | |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2527 | static int selinux_sb_remount(struct super_block *sb, void *data) |
| 2528 | { |
| 2529 | int rc, i, *flags; |
| 2530 | struct security_mnt_opts opts; |
| 2531 | char *secdata, **mount_options; |
| 2532 | struct superblock_security_struct *sbsec = sb->s_security; |
| 2533 | |
| 2534 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 2535 | return 0; |
| 2536 | |
| 2537 | if (!data) |
| 2538 | return 0; |
| 2539 | |
| 2540 | if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) |
| 2541 | return 0; |
| 2542 | |
| 2543 | security_init_mnt_opts(&opts); |
| 2544 | secdata = alloc_secdata(); |
| 2545 | if (!secdata) |
| 2546 | return -ENOMEM; |
| 2547 | rc = selinux_sb_copy_data(data, secdata); |
| 2548 | if (rc) |
| 2549 | goto out_free_secdata; |
| 2550 | |
| 2551 | rc = selinux_parse_opts_str(secdata, &opts); |
| 2552 | if (rc) |
| 2553 | goto out_free_secdata; |
| 2554 | |
| 2555 | mount_options = opts.mnt_opts; |
| 2556 | flags = opts.mnt_opts_flags; |
| 2557 | |
| 2558 | for (i = 0; i < opts.num_mnt_opts; i++) { |
| 2559 | u32 sid; |
| 2560 | size_t len; |
| 2561 | |
| 2562 | if (flags[i] == SE_SBLABELSUPP) |
| 2563 | continue; |
| 2564 | len = strlen(mount_options[i]); |
| 2565 | rc = security_context_to_sid(mount_options[i], len, &sid); |
| 2566 | if (rc) { |
| 2567 | printk(KERN_WARNING "SELinux: security_context_to_sid" |
| 2568 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 2569 | mount_options[i], sb->s_id, sb->s_type->name, rc); |
| 2570 | goto out_free_opts; |
| 2571 | } |
| 2572 | rc = -EINVAL; |
| 2573 | switch (flags[i]) { |
| 2574 | case FSCONTEXT_MNT: |
| 2575 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) |
| 2576 | goto out_bad_option; |
| 2577 | break; |
| 2578 | case CONTEXT_MNT: |
| 2579 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) |
| 2580 | goto out_bad_option; |
| 2581 | break; |
| 2582 | case ROOTCONTEXT_MNT: { |
| 2583 | struct inode_security_struct *root_isec; |
| 2584 | root_isec = sb->s_root->d_inode->i_security; |
| 2585 | |
| 2586 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) |
| 2587 | goto out_bad_option; |
| 2588 | break; |
| 2589 | } |
| 2590 | case DEFCONTEXT_MNT: |
| 2591 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) |
| 2592 | goto out_bad_option; |
| 2593 | break; |
| 2594 | default: |
| 2595 | goto out_free_opts; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | rc = 0; |
| 2600 | out_free_opts: |
| 2601 | security_free_mnt_opts(&opts); |
| 2602 | out_free_secdata: |
| 2603 | free_secdata(secdata); |
| 2604 | return rc; |
| 2605 | out_bad_option: |
| 2606 | printk(KERN_WARNING "SELinux: unable to change security options " |
| 2607 | "during remount (dev %s, type=%s)\n", sb->s_id, |
| 2608 | sb->s_type->name); |
| 2609 | goto out_free_opts; |
| 2610 | } |
| 2611 | |
James Morris | 12204e2 | 2008-12-19 10:44:42 +1100 | [diff] [blame] | 2612 | 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] | 2613 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2614 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2615 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2616 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | int rc; |
| 2618 | |
| 2619 | rc = superblock_doinit(sb, data); |
| 2620 | if (rc) |
| 2621 | return rc; |
| 2622 | |
James Morris | 7419224 | 2008-12-19 11:41:10 +1100 | [diff] [blame] | 2623 | /* Allow all mounts performed by the kernel */ |
| 2624 | if (flags & MS_KERNMOUNT) |
| 2625 | return 0; |
| 2626 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2627 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2628 | ad.selinux_audit_data = &sad; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2629 | ad.u.dentry = sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2630 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2633 | static int selinux_sb_statfs(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2635 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2636 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2637 | struct selinux_audit_data sad = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2639 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2640 | ad.selinux_audit_data = &sad; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2641 | ad.u.dentry = dentry->d_sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2642 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | } |
| 2644 | |
Al Viro | ddaa16d | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2645 | static int selinux_mount(const char *dev_name, |
Al Viro | b5266eb | 2008-03-22 17:48:24 -0400 | [diff] [blame] | 2646 | struct path *path, |
Al Viro | ddaa16d | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2647 | const char *type, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2648 | unsigned long flags, |
| 2649 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2651 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | |
| 2653 | if (flags & MS_REMOUNT) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 2654 | return superblock_has_perm(cred, path->dentry->d_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2655 | FILESYSTEM__REMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | else |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2657 | return path_has_perm(cred, path, FILE__MOUNTON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | static int selinux_umount(struct vfsmount *mnt, int flags) |
| 2661 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2662 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2664 | return superblock_has_perm(cred, mnt->mnt_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2665 | FILESYSTEM__UNMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | /* inode security operations */ |
| 2669 | |
| 2670 | static int selinux_inode_alloc_security(struct inode *inode) |
| 2671 | { |
| 2672 | return inode_alloc_security(inode); |
| 2673 | } |
| 2674 | |
| 2675 | static void selinux_inode_free_security(struct inode *inode) |
| 2676 | { |
| 2677 | inode_free_security(inode); |
| 2678 | } |
| 2679 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2680 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2681 | const struct qstr *qstr, char **name, |
| 2682 | void **value, size_t *len) |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2683 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 2684 | const struct task_security_struct *tsec = current_security(); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2685 | struct inode_security_struct *dsec; |
| 2686 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2687 | u32 sid, newsid, clen; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2688 | int rc; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2689 | char *namep = NULL, *context; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2690 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2691 | dsec = dir->i_security; |
| 2692 | sbsec = dir->i_sb->s_security; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2693 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2694 | sid = tsec->sid; |
| 2695 | newsid = tsec->create_sid; |
| 2696 | |
Eric Paris | 415103f | 2010-12-02 16:13:40 -0500 | [diff] [blame] | 2697 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 2698 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) |
| 2699 | newsid = sbsec->mntpoint_sid; |
| 2700 | else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2701 | rc = security_transition_sid(sid, dsec->sid, |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2702 | inode_mode_to_security_class(inode->i_mode), |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2703 | qstr, &newsid); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2704 | if (rc) { |
| 2705 | printk(KERN_WARNING "%s: " |
| 2706 | "security_transition_sid failed, rc=%d (dev=%s " |
| 2707 | "ino=%ld)\n", |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 2708 | __func__, |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2709 | -rc, inode->i_sb->s_id, inode->i_ino); |
| 2710 | return rc; |
| 2711 | } |
| 2712 | } |
| 2713 | |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2714 | /* Possibly defer initialization to selinux_complete_init. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 2715 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2716 | struct inode_security_struct *isec = inode->i_security; |
| 2717 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2718 | isec->sid = newsid; |
| 2719 | isec->initialized = 1; |
| 2720 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2721 | |
David P. Quigley | cd89596 | 2009-01-16 09:22:04 -0500 | [diff] [blame] | 2722 | if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP)) |
Stephen Smalley | 25a74f3 | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 2723 | return -EOPNOTSUPP; |
| 2724 | |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2725 | if (name) { |
Josef Bacik | a02fe13 | 2008-04-04 09:35:05 +1100 | [diff] [blame] | 2726 | namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2727 | if (!namep) |
| 2728 | return -ENOMEM; |
| 2729 | *name = namep; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2730 | } |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2731 | |
| 2732 | if (value && len) { |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2733 | rc = security_sid_to_context_force(newsid, &context, &clen); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2734 | if (rc) { |
| 2735 | kfree(namep); |
| 2736 | return rc; |
| 2737 | } |
| 2738 | *value = context; |
| 2739 | *len = clen; |
| 2740 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2741 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2742 | return 0; |
| 2743 | } |
| 2744 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2745 | 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] | 2746 | { |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2747 | int ret; |
| 2748 | |
| 2749 | ret = pft_inode_create(dir, dentry, mode); |
| 2750 | if (ret < 0) |
| 2751 | return ret; |
| 2752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | return may_create(dir, dentry, SECCLASS_FILE); |
| 2754 | } |
| 2755 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2756 | static int selinux_inode_post_create(struct inode *dir, struct dentry *dentry, |
| 2757 | umode_t mode) |
| 2758 | { |
| 2759 | int ret; |
| 2760 | |
| 2761 | ret = pft_inode_post_create(dir, dentry, mode); |
| 2762 | |
| 2763 | return ret; |
| 2764 | } |
| 2765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2767 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | return may_link(dir, old_dentry, MAY_LINK); |
| 2769 | } |
| 2770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 2772 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | return may_link(dir, dentry, MAY_UNLINK); |
| 2774 | } |
| 2775 | |
| 2776 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) |
| 2777 | { |
| 2778 | return may_create(dir, dentry, SECCLASS_LNK_FILE); |
| 2779 | } |
| 2780 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2781 | 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] | 2782 | { |
| 2783 | return may_create(dir, dentry, SECCLASS_DIR); |
| 2784 | } |
| 2785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 2787 | { |
| 2788 | return may_link(dir, dentry, MAY_RMDIR); |
| 2789 | } |
| 2790 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2791 | 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] | 2792 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
| 2794 | } |
| 2795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | 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] | 2797 | struct inode *new_inode, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | { |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2799 | int rc; |
| 2800 | |
| 2801 | rc = pft_inode_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2802 | if (rc) |
| 2803 | return rc; |
| 2804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2806 | } |
| 2807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | static int selinux_inode_readlink(struct dentry *dentry) |
| 2809 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2810 | const struct cred *cred = current_cred(); |
| 2811 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2812 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) |
| 2816 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2817 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2819 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 2822 | static int selinux_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2824 | const struct cred *cred = current_cred(); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2825 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2826 | struct selinux_audit_data sad = {0,}; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2827 | u32 perms; |
| 2828 | bool from_access; |
Al Viro | cf1dd1d | 2011-06-20 19:44:08 -0400 | [diff] [blame] | 2829 | unsigned flags = mask & MAY_NOT_BLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2831 | from_access = mask & MAY_ACCESS; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 2832 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
| 2833 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2834 | /* No permission to check. Existence test. */ |
| 2835 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2838 | COMMON_AUDIT_DATA_INIT(&ad, INODE); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2839 | ad.selinux_audit_data = &sad; |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 2840 | ad.u.inode = inode; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2841 | |
| 2842 | if (from_access) |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2843 | ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2844 | |
| 2845 | perms = file_mask_to_av(inode->i_mode, mask); |
| 2846 | |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 2847 | return inode_has_perm(cred, inode, perms, &ad, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 2851 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2852 | const struct cred *cred = current_cred(); |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 2853 | unsigned int ia_valid = iattr->ia_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 2855 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
| 2856 | if (ia_valid & ATTR_FORCE) { |
| 2857 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | |
| 2858 | ATTR_FORCE); |
| 2859 | if (!ia_valid) |
| 2860 | return 0; |
| 2861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 2863 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
| 2864 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2865 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2867 | return dentry_has_perm(cred, dentry, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) |
| 2871 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2872 | const struct cred *cred = current_cred(); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2873 | struct path path; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2874 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2875 | path.dentry = dentry; |
| 2876 | path.mnt = mnt; |
| 2877 | |
| 2878 | return path_has_perm(cred, &path, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2881 | static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2882 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2883 | const struct cred *cred = current_cred(); |
| 2884 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2885 | if (pft_inode_set_xattr(dentry, name) < 0) |
| 2886 | return -EACCES; |
| 2887 | |
| 2888 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2889 | if (!strncmp(name, XATTR_SECURITY_PREFIX, |
| 2890 | sizeof XATTR_SECURITY_PREFIX - 1)) { |
| 2891 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
| 2892 | if (!capable(CAP_SETFCAP)) |
| 2893 | return -EPERM; |
| 2894 | } else if (!capable(CAP_SYS_ADMIN)) { |
| 2895 | /* A different attribute in the security namespace. |
| 2896 | Restrict to administrator. */ |
| 2897 | return -EPERM; |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | /* Not an attribute we recognize, so just check the |
| 2902 | ordinary setattr permission. */ |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2903 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2906 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
| 2907 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | struct inode *inode = dentry->d_inode; |
| 2910 | struct inode_security_struct *isec = inode->i_security; |
| 2911 | struct superblock_security_struct *sbsec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2912 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2913 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2914 | u32 newsid, sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | int rc = 0; |
| 2916 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 2917 | if (strcmp(name, XATTR_NAME_SELINUX)) |
| 2918 | return selinux_inode_setotherxattr(dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | |
| 2920 | sbsec = inode->i_sb->s_security; |
David P. Quigley | cd89596 | 2009-01-16 09:22:04 -0500 | [diff] [blame] | 2921 | if (!(sbsec->flags & SE_SBLABELSUPP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | return -EOPNOTSUPP; |
| 2923 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2924 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | return -EPERM; |
| 2926 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2927 | COMMON_AUDIT_DATA_INIT(&ad, DENTRY); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 2928 | ad.selinux_audit_data = &sad; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2929 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2931 | rc = avc_has_perm(sid, isec->sid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | FILE__RELABELFROM, &ad); |
| 2933 | if (rc) |
| 2934 | return rc; |
| 2935 | |
| 2936 | rc = security_context_to_sid(value, size, &newsid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2937 | if (rc == -EINVAL) { |
| 2938 | if (!capable(CAP_MAC_ADMIN)) |
| 2939 | return rc; |
| 2940 | rc = security_context_to_sid_force(value, size, &newsid); |
| 2941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | if (rc) |
| 2943 | return rc; |
| 2944 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2945 | rc = avc_has_perm(sid, newsid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | FILE__RELABELTO, &ad); |
| 2947 | if (rc) |
| 2948 | return rc; |
| 2949 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2950 | rc = security_validate_transition(isec->sid, newsid, sid, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2951 | isec->sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | if (rc) |
| 2953 | return rc; |
| 2954 | |
| 2955 | return avc_has_perm(newsid, |
| 2956 | sbsec->sid, |
| 2957 | SECCLASS_FILESYSTEM, |
| 2958 | FILESYSTEM__ASSOCIATE, |
| 2959 | &ad); |
| 2960 | } |
| 2961 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2962 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 2963 | const void *value, size_t size, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2964 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | { |
| 2966 | struct inode *inode = dentry->d_inode; |
| 2967 | struct inode_security_struct *isec = inode->i_security; |
| 2968 | u32 newsid; |
| 2969 | int rc; |
| 2970 | |
| 2971 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 2972 | /* Not an attribute we recognize, so nothing to do. */ |
| 2973 | return; |
| 2974 | } |
| 2975 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2976 | rc = security_context_to_sid_force(value, size, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | if (rc) { |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2978 | printk(KERN_ERR "SELinux: unable to map context to SID" |
| 2979 | "for (%s, %lu), rc=%d\n", |
| 2980 | inode->i_sb->s_id, inode->i_ino, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | return; |
| 2982 | } |
| 2983 | |
| 2984 | isec->sid = newsid; |
| 2985 | return; |
| 2986 | } |
| 2987 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 2988 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2990 | const struct cred *cred = current_cred(); |
| 2991 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2992 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2995 | static int selinux_inode_listxattr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2997 | const struct cred *cred = current_cred(); |
| 2998 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2999 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3002 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3004 | if (strcmp(name, XATTR_NAME_SELINUX)) |
| 3005 | return selinux_inode_setotherxattr(dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | |
| 3007 | /* No one is allowed to remove a SELinux security label. |
| 3008 | You can change the label, but all data must be labeled. */ |
| 3009 | return -EACCES; |
| 3010 | } |
| 3011 | |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3012 | /* |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3013 | * Copy the inode security context value to the user. |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3014 | * |
| 3015 | * Permission check is handled by selinux_inode_getxattr hook. |
| 3016 | */ |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3017 | 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] | 3018 | { |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3019 | u32 size; |
| 3020 | int error; |
| 3021 | char *context = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | struct inode_security_struct *isec = inode->i_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 3024 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3025 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3027 | /* |
| 3028 | * If the caller has CAP_MAC_ADMIN, then get the raw context |
| 3029 | * value even if it is not defined by current policy; otherwise, |
| 3030 | * use the in-core value under current policy. |
| 3031 | * Use the non-auditing forms of the permission checks since |
| 3032 | * getxattr may be called by unprivileged processes commonly |
| 3033 | * and lack of permission just means that we fall back to the |
| 3034 | * in-core context value, not a denial. |
| 3035 | */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3036 | error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN, |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 3037 | SECURITY_CAP_NOAUDIT); |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3038 | if (!error) |
| 3039 | error = security_sid_to_context_force(isec->sid, &context, |
| 3040 | &size); |
| 3041 | else |
| 3042 | error = security_sid_to_context(isec->sid, &context, &size); |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3043 | if (error) |
| 3044 | return error; |
| 3045 | error = size; |
| 3046 | if (alloc) { |
| 3047 | *buffer = context; |
| 3048 | goto out_nofree; |
| 3049 | } |
| 3050 | kfree(context); |
| 3051 | out_nofree: |
| 3052 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3056 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
| 3058 | struct inode_security_struct *isec = inode->i_security; |
| 3059 | u32 newsid; |
| 3060 | int rc; |
| 3061 | |
| 3062 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3063 | return -EOPNOTSUPP; |
| 3064 | |
| 3065 | if (!value || !size) |
| 3066 | return -EACCES; |
| 3067 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3068 | rc = security_context_to_sid((void *)value, size, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | if (rc) |
| 3070 | return rc; |
| 3071 | |
| 3072 | isec->sid = newsid; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 3073 | isec->initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) |
| 3078 | { |
| 3079 | const int len = sizeof(XATTR_NAME_SELINUX); |
| 3080 | if (buffer && len <= buffer_size) |
| 3081 | memcpy(buffer, XATTR_NAME_SELINUX, len); |
| 3082 | return len; |
| 3083 | } |
| 3084 | |
Ahmed S. Darwish | 713a04a | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3085 | static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) |
| 3086 | { |
| 3087 | struct inode_security_struct *isec = inode->i_security; |
| 3088 | *secid = isec->sid; |
| 3089 | } |
| 3090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | /* file security operations */ |
| 3092 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3093 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3095 | const struct cred *cred = current_cred(); |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 3096 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
| 3099 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) |
| 3100 | mask |= MAY_APPEND; |
| 3101 | |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3102 | return file_has_perm(cred, file, |
| 3103 | file_mask_to_av(inode->i_mode, mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3106 | static int selinux_file_permission(struct file *file, int mask) |
| 3107 | { |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3108 | struct inode *inode = file->f_path.dentry->d_inode; |
| 3109 | struct file_security_struct *fsec = file->f_security; |
| 3110 | struct inode_security_struct *isec = inode->i_security; |
| 3111 | u32 sid = current_sid(); |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3112 | int ret; |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3113 | |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3114 | if (!mask) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3115 | /* No permission to check. Existence test. */ |
| 3116 | return 0; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3117 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3118 | ret = pft_file_permission(file, mask); |
| 3119 | if (ret < 0) |
| 3120 | return ret; |
| 3121 | |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3122 | if (sid == fsec->sid && fsec->isid == isec->sid && |
| 3123 | fsec->pseqno == avc_policy_seqno()) |
| 3124 | /* No change since dentry_open check. */ |
| 3125 | return 0; |
| 3126 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3127 | return selinux_revalidate_file_permission(file, mask); |
| 3128 | } |
| 3129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | static int selinux_file_alloc_security(struct file *file) |
| 3131 | { |
| 3132 | return file_alloc_security(file); |
| 3133 | } |
| 3134 | |
| 3135 | static void selinux_file_free_security(struct file *file) |
| 3136 | { |
| 3137 | file_free_security(file); |
| 3138 | } |
| 3139 | |
Jeff Vander Stoep | 581be71 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3140 | /* |
| 3141 | * Check whether a task has the ioctl permission and cmd |
| 3142 | * operation to an inode. |
| 3143 | */ |
| 3144 | int ioctl_has_perm(const struct cred *cred, struct file *file, |
| 3145 | u32 requested, u16 cmd) |
| 3146 | { |
| 3147 | struct common_audit_data ad; |
| 3148 | struct file_security_struct *fsec = file->f_security; |
| 3149 | struct inode *inode = file->f_path.dentry->d_inode; |
| 3150 | struct inode_security_struct *isec = inode->i_security; |
| 3151 | struct lsm_ioctlop_audit ioctl; |
| 3152 | u32 ssid = cred_sid(cred); |
| 3153 | struct selinux_audit_data sad = {0,}; |
| 3154 | int rc; |
| 3155 | u8 driver = cmd >> 8; |
| 3156 | u8 xperm = cmd & 0xff; |
| 3157 | |
| 3158 | COMMON_AUDIT_DATA_INIT(&ad, IOCTL_OP); |
| 3159 | ad.u.op = &ioctl; |
| 3160 | ad.u.op->cmd = cmd; |
| 3161 | ad.selinux_audit_data = &sad; |
| 3162 | ad.u.op->path = file->f_path; |
| 3163 | |
| 3164 | if (ssid != fsec->sid) { |
| 3165 | rc = avc_has_perm(ssid, fsec->sid, |
| 3166 | SECCLASS_FD, |
| 3167 | FD__USE, |
| 3168 | &ad); |
| 3169 | if (rc) |
| 3170 | goto out; |
| 3171 | } |
| 3172 | |
| 3173 | if (unlikely(IS_PRIVATE(inode))) |
| 3174 | return 0; |
| 3175 | |
| 3176 | rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, |
| 3177 | requested, driver, xperm, &ad); |
| 3178 | out: |
| 3179 | return rc; |
| 3180 | } |
| 3181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, |
| 3183 | unsigned long arg) |
| 3184 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3185 | const struct cred *cred = current_cred(); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3186 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3188 | switch (cmd) { |
| 3189 | case FIONREAD: |
| 3190 | /* fall through */ |
| 3191 | case FIBMAP: |
| 3192 | /* fall through */ |
| 3193 | case FIGETBSZ: |
| 3194 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3195 | case FS_IOC_GETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3196 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3197 | case FS_IOC_GETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3198 | error = file_has_perm(cred, file, FILE__GETATTR); |
| 3199 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3201 | case FS_IOC_SETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3202 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3203 | case FS_IOC_SETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3204 | error = file_has_perm(cred, file, FILE__SETATTR); |
| 3205 | break; |
| 3206 | |
| 3207 | /* sys_ioctl() checks */ |
| 3208 | case FIONBIO: |
| 3209 | /* fall through */ |
| 3210 | case FIOASYNC: |
| 3211 | error = file_has_perm(cred, file, 0); |
| 3212 | break; |
| 3213 | |
| 3214 | case KDSKBENT: |
| 3215 | case KDSKBSENT: |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3216 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
| 3217 | SECURITY_CAP_AUDIT); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3218 | break; |
| 3219 | |
| 3220 | /* default case assumes that the command will go |
| 3221 | * to the file's ioctl() function. |
| 3222 | */ |
| 3223 | default: |
Jeff Vander Stoep | 581be71 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3224 | error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3225 | } |
| 3226 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | } |
| 3228 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3229 | static int default_noexec; |
| 3230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
| 3232 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3233 | const struct cred *cred = current_cred(); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3234 | int rc = 0; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3235 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3236 | if (default_noexec && |
| 3237 | (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | /* |
| 3239 | * We are making executable an anonymous mapping or a |
| 3240 | * private file mapping that will also be writable. |
| 3241 | * This has an additional check. |
| 3242 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3243 | rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | if (rc) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3245 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | |
| 3248 | if (file) { |
| 3249 | /* read access is always possible with a mapping */ |
| 3250 | u32 av = FILE__READ; |
| 3251 | |
| 3252 | /* write access only matters if the mapping is shared */ |
| 3253 | if (shared && (prot & PROT_WRITE)) |
| 3254 | av |= FILE__WRITE; |
| 3255 | |
| 3256 | if (prot & PROT_EXEC) |
| 3257 | av |= FILE__EXECUTE; |
| 3258 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3259 | return file_has_perm(cred, file, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3261 | |
| 3262 | error: |
| 3263 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | static int selinux_file_mmap(struct file *file, unsigned long reqprot, |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3267 | unsigned long prot, unsigned long flags, |
| 3268 | unsigned long addr, unsigned long addr_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | { |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3270 | int rc = 0; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3271 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
Eric Paris | 84336d1a | 2009-07-31 12:54:05 -0400 | [diff] [blame] | 3273 | /* |
| 3274 | * notice that we are intentionally putting the SELinux check before |
| 3275 | * the secondary cap_file_mmap check. This is such a likely attempt |
| 3276 | * at bad behaviour/exploit that we always want to get the AVC, even |
| 3277 | * if DAC would have also denied the operation. |
| 3278 | */ |
Eric Paris | a2551df | 2009-07-31 12:54:11 -0400 | [diff] [blame] | 3279 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3280 | rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, |
| 3281 | MEMPROTECT__MMAP_ZERO, NULL); |
Eric Paris | 84336d1a | 2009-07-31 12:54:05 -0400 | [diff] [blame] | 3282 | if (rc) |
| 3283 | return rc; |
| 3284 | } |
| 3285 | |
| 3286 | /* do DAC check on address space usage */ |
| 3287 | rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only); |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 3288 | if (rc || addr_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | return rc; |
| 3290 | |
| 3291 | if (selinux_checkreqprot) |
| 3292 | prot = reqprot; |
| 3293 | |
| 3294 | return file_map_prot_check(file, prot, |
| 3295 | (flags & MAP_TYPE) == MAP_SHARED); |
| 3296 | } |
| 3297 | |
| 3298 | static int selinux_file_mprotect(struct vm_area_struct *vma, |
| 3299 | unsigned long reqprot, |
| 3300 | unsigned long prot) |
| 3301 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3302 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | |
| 3304 | if (selinux_checkreqprot) |
| 3305 | prot = reqprot; |
| 3306 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3307 | if (default_noexec && |
| 3308 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { |
James Morris | d541bbe | 2009-01-29 12:19:51 +1100 | [diff] [blame] | 3309 | int rc = 0; |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3310 | if (vma->vm_start >= vma->vm_mm->start_brk && |
| 3311 | vma->vm_end <= vma->vm_mm->brk) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3312 | rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3313 | } else if (!vma->vm_file && |
| 3314 | vma->vm_start <= vma->vm_mm->start_stack && |
| 3315 | vma->vm_end >= vma->vm_mm->start_stack) { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3316 | rc = current_has_perm(current, PROCESS__EXECSTACK); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3317 | } else if (vma->vm_file && vma->anon_vma) { |
| 3318 | /* |
| 3319 | * We are making executable a file mapping that has |
| 3320 | * had some COW done. Since pages might have been |
| 3321 | * written, check ability to execute the possibly |
| 3322 | * modified content. This typically should only |
| 3323 | * occur for text relocations. |
| 3324 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3325 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3326 | } |
Lorenzo Hernandez GarcÃa-Hierro | 6b99219 | 2005-06-25 14:54:34 -0700 | [diff] [blame] | 3327 | if (rc) |
| 3328 | return rc; |
| 3329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | |
| 3331 | return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); |
| 3332 | } |
| 3333 | |
| 3334 | static int selinux_file_lock(struct file *file, unsigned int cmd) |
| 3335 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3336 | const struct cred *cred = current_cred(); |
| 3337 | |
| 3338 | return file_has_perm(cred, file, FILE__LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | static int selinux_file_fcntl(struct file *file, unsigned int cmd, |
| 3342 | unsigned long arg) |
| 3343 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3344 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | int err = 0; |
| 3346 | |
| 3347 | switch (cmd) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3348 | case F_SETFL: |
| 3349 | if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { |
| 3350 | err = -EINVAL; |
| 3351 | break; |
| 3352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3354 | if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3355 | err = file_has_perm(cred, file, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3357 | } |
| 3358 | /* fall through */ |
| 3359 | case F_SETOWN: |
| 3360 | case F_SETSIG: |
| 3361 | case F_GETFL: |
| 3362 | case F_GETOWN: |
| 3363 | case F_GETSIG: |
| 3364 | /* Just check FD__USE permission */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3365 | err = file_has_perm(cred, file, 0); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3366 | break; |
| 3367 | case F_GETLK: |
| 3368 | case F_SETLK: |
| 3369 | case F_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | #if BITS_PER_LONG == 32 |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3371 | case F_GETLK64: |
| 3372 | case F_SETLK64: |
| 3373 | case F_SETLKW64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | #endif |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3375 | if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { |
| 3376 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3378 | } |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3379 | err = file_has_perm(cred, file, FILE__LOCK); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3380 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | return err; |
| 3384 | } |
| 3385 | |
| 3386 | static int selinux_file_set_fowner(struct file *file) |
| 3387 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | struct file_security_struct *fsec; |
| 3389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | fsec = file->f_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3391 | fsec->fown_sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | static int selinux_file_send_sigiotask(struct task_struct *tsk, |
| 3397 | struct fown_struct *fown, int signum) |
| 3398 | { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3399 | struct file *file; |
Stephen Smalley | 65c90bc | 2009-05-04 15:43:18 -0400 | [diff] [blame] | 3400 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | struct file_security_struct *fsec; |
| 3403 | |
| 3404 | /* struct fown_struct is never outside the context of a struct file */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3405 | file = container_of(fown, struct file, f_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | fsec = file->f_security; |
| 3408 | |
| 3409 | if (!signum) |
| 3410 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| 3411 | else |
| 3412 | perm = signal_to_av(signum); |
| 3413 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3414 | return avc_has_perm(fsec->fown_sid, sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | SECCLASS_PROCESS, perm, NULL); |
| 3416 | } |
| 3417 | |
| 3418 | static int selinux_file_receive(struct file *file) |
| 3419 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3420 | const struct cred *cred = current_cred(); |
| 3421 | |
| 3422 | return file_has_perm(cred, file, file_to_av(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 3425 | static int selinux_dentry_open(struct file *file, const struct cred *cred) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3426 | { |
| 3427 | struct file_security_struct *fsec; |
| 3428 | struct inode *inode; |
| 3429 | struct inode_security_struct *isec; |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3430 | int ret; |
| 3431 | |
| 3432 | ret = pft_file_open(file, cred); |
| 3433 | if (ret < 0) |
| 3434 | return ret; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3435 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3436 | inode = file->f_path.dentry->d_inode; |
| 3437 | fsec = file->f_security; |
| 3438 | isec = inode->i_security; |
| 3439 | /* |
| 3440 | * Save inode label and policy sequence number |
| 3441 | * at open-time so that selinux_file_permission |
| 3442 | * can determine whether revalidation is necessary. |
| 3443 | * Task label is already saved in the file security |
| 3444 | * struct as its SID. |
| 3445 | */ |
| 3446 | fsec->isid = isec->sid; |
| 3447 | fsec->pseqno = avc_policy_seqno(); |
| 3448 | /* |
| 3449 | * Since the inode label or policy seqno may have changed |
| 3450 | * between the selinux_inode_permission check and the saving |
| 3451 | * of state above, recheck that access is still permitted. |
| 3452 | * Otherwise, access might never be revalidated against the |
| 3453 | * new inode label or new policy. |
| 3454 | * This check is not redundant - do not remove. |
| 3455 | */ |
Linus Torvalds | 95f4efb | 2011-06-08 15:11:56 -0700 | [diff] [blame] | 3456 | 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] | 3457 | } |
| 3458 | |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3459 | static int selinux_file_close(struct file *file) |
| 3460 | { |
| 3461 | return pft_file_close(file); |
| 3462 | } |
| 3463 | |
| 3464 | static bool selinux_allow_merge_bio(struct bio *bio1, struct bio *bio2) |
| 3465 | { |
| 3466 | return pft_allow_merge_bio(bio1, bio2); |
| 3467 | } |
| 3468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | /* task security operations */ |
| 3470 | |
| 3471 | static int selinux_task_create(unsigned long clone_flags) |
| 3472 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3473 | return current_has_perm(current, PROCESS__FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3476 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3477 | * allocate the SELinux part of blank credentials |
| 3478 | */ |
| 3479 | static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 3480 | { |
| 3481 | struct task_security_struct *tsec; |
| 3482 | |
| 3483 | tsec = kzalloc(sizeof(struct task_security_struct), gfp); |
| 3484 | if (!tsec) |
| 3485 | return -ENOMEM; |
| 3486 | |
| 3487 | cred->security = tsec; |
| 3488 | return 0; |
| 3489 | } |
| 3490 | |
| 3491 | /* |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3492 | * detach and free the LSM part of a set of credentials |
| 3493 | */ |
| 3494 | static void selinux_cred_free(struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | { |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3496 | struct task_security_struct *tsec = cred->security; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3497 | |
Tetsuo Handa | 2edeaa3 | 2011-02-07 13:36:10 +0000 | [diff] [blame] | 3498 | /* |
| 3499 | * cred->security == NULL if security_cred_alloc_blank() or |
| 3500 | * security_prepare_creds() returned an error. |
| 3501 | */ |
| 3502 | BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3503 | cred->security = (void *) 0x7UL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3504 | kfree(tsec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3507 | /* |
| 3508 | * prepare a new set of credentials for modification |
| 3509 | */ |
| 3510 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3511 | gfp_t gfp) |
| 3512 | { |
| 3513 | const struct task_security_struct *old_tsec; |
| 3514 | struct task_security_struct *tsec; |
| 3515 | |
| 3516 | old_tsec = old->security; |
| 3517 | |
| 3518 | tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); |
| 3519 | if (!tsec) |
| 3520 | return -ENOMEM; |
| 3521 | |
| 3522 | new->security = tsec; |
| 3523 | return 0; |
| 3524 | } |
| 3525 | |
| 3526 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3527 | * transfer the SELinux data to a blank set of creds |
| 3528 | */ |
| 3529 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3530 | { |
| 3531 | const struct task_security_struct *old_tsec = old->security; |
| 3532 | struct task_security_struct *tsec = new->security; |
| 3533 | |
| 3534 | *tsec = *old_tsec; |
| 3535 | } |
| 3536 | |
| 3537 | /* |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3538 | * set the security data for a kernel service |
| 3539 | * - all the creation contexts are set to unlabelled |
| 3540 | */ |
| 3541 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3542 | { |
| 3543 | struct task_security_struct *tsec = new->security; |
| 3544 | u32 sid = current_sid(); |
| 3545 | int ret; |
| 3546 | |
| 3547 | ret = avc_has_perm(sid, secid, |
| 3548 | SECCLASS_KERNEL_SERVICE, |
| 3549 | KERNEL_SERVICE__USE_AS_OVERRIDE, |
| 3550 | NULL); |
| 3551 | if (ret == 0) { |
| 3552 | tsec->sid = secid; |
| 3553 | tsec->create_sid = 0; |
| 3554 | tsec->keycreate_sid = 0; |
| 3555 | tsec->sockcreate_sid = 0; |
| 3556 | } |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | /* |
| 3561 | * set the file creation context in a security record to the same as the |
| 3562 | * objective context of the specified inode |
| 3563 | */ |
| 3564 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3565 | { |
| 3566 | struct inode_security_struct *isec = inode->i_security; |
| 3567 | struct task_security_struct *tsec = new->security; |
| 3568 | u32 sid = current_sid(); |
| 3569 | int ret; |
| 3570 | |
| 3571 | ret = avc_has_perm(sid, isec->sid, |
| 3572 | SECCLASS_KERNEL_SERVICE, |
| 3573 | KERNEL_SERVICE__CREATE_FILES_AS, |
| 3574 | NULL); |
| 3575 | |
| 3576 | if (ret == 0) |
| 3577 | tsec->create_sid = isec->sid; |
David Howells | ef57471 | 2010-02-26 01:56:16 +0000 | [diff] [blame] | 3578 | return ret; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3579 | } |
| 3580 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3581 | static int selinux_kernel_module_request(char *kmod_name) |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3582 | { |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3583 | u32 sid; |
| 3584 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3585 | struct selinux_audit_data sad = {0,}; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3586 | |
| 3587 | sid = task_sid(current); |
| 3588 | |
| 3589 | COMMON_AUDIT_DATA_INIT(&ad, KMOD); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3590 | ad.selinux_audit_data = &sad; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3591 | ad.u.kmod_name = kmod_name; |
| 3592 | |
| 3593 | return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM, |
| 3594 | SYSTEM__MODULE_REQUEST, &ad); |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3595 | } |
| 3596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) |
| 3598 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3599 | return current_has_perm(p, PROCESS__SETPGID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | static int selinux_task_getpgid(struct task_struct *p) |
| 3603 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3604 | return current_has_perm(p, PROCESS__GETPGID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | static int selinux_task_getsid(struct task_struct *p) |
| 3608 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3609 | return current_has_perm(p, PROCESS__GETSESSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3612 | static void selinux_task_getsecid(struct task_struct *p, u32 *secid) |
| 3613 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3614 | *secid = task_sid(p); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | static int selinux_task_setnice(struct task_struct *p, int nice) |
| 3618 | { |
| 3619 | int rc; |
| 3620 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 3621 | rc = cap_task_setnice(p, nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | if (rc) |
| 3623 | return rc; |
| 3624 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3625 | return current_has_perm(p, PROCESS__SETSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3628 | static int selinux_task_setioprio(struct task_struct *p, int ioprio) |
| 3629 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3630 | int rc; |
| 3631 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 3632 | rc = cap_task_setioprio(p, ioprio); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3633 | if (rc) |
| 3634 | return rc; |
| 3635 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3636 | return current_has_perm(p, PROCESS__SETSCHED); |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3637 | } |
| 3638 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3639 | static int selinux_task_getioprio(struct task_struct *p) |
| 3640 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3641 | return current_has_perm(p, PROCESS__GETSCHED); |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3644 | static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, |
| 3645 | struct rlimit *new_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | { |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3647 | struct rlimit *old_rlim = p->signal->rlim + resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | |
| 3649 | /* Control the ability to change the hard limit (whether |
| 3650 | lowering or raising it), so that the hard limit can |
| 3651 | later be used as a safe reset point for the soft limit |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3652 | upon context transitions. See selinux_bprm_committing_creds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | if (old_rlim->rlim_max != new_rlim->rlim_max) |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3654 | return current_has_perm(p, PROCESS__SETRLIMIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | |
| 3656 | return 0; |
| 3657 | } |
| 3658 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 3659 | static int selinux_task_setscheduler(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3661 | int rc; |
| 3662 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 3663 | rc = cap_task_setscheduler(p); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 3664 | if (rc) |
| 3665 | return rc; |
| 3666 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3667 | return current_has_perm(p, PROCESS__SETSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | static int selinux_task_getscheduler(struct task_struct *p) |
| 3671 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3672 | return current_has_perm(p, PROCESS__GETSCHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | } |
| 3674 | |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 3675 | static int selinux_task_movememory(struct task_struct *p) |
| 3676 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3677 | return current_has_perm(p, PROCESS__SETSCHED); |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3680 | static int selinux_task_kill(struct task_struct *p, struct siginfo *info, |
| 3681 | int sig, u32 secid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | { |
| 3683 | u32 perm; |
| 3684 | int rc; |
| 3685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | if (!sig) |
| 3687 | perm = PROCESS__SIGNULL; /* null signal; existence test */ |
| 3688 | else |
| 3689 | perm = signal_to_av(sig); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3690 | if (secid) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3691 | rc = avc_has_perm(secid, task_sid(p), |
| 3692 | SECCLASS_PROCESS, perm, NULL); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3693 | else |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 3694 | rc = current_has_perm(p, perm); |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3695 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | } |
| 3697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | static int selinux_task_wait(struct task_struct *p) |
| 3699 | { |
Eric Paris | 8a53514 | 2007-10-22 16:10:31 -0400 | [diff] [blame] | 3700 | return task_has_perm(p, current, PROCESS__SIGCHLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | static void selinux_task_to_inode(struct task_struct *p, |
| 3704 | struct inode *inode) |
| 3705 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | struct inode_security_struct *isec = inode->i_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3707 | u32 sid = task_sid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3709 | isec->sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | isec->initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | } |
| 3712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3714 | static int selinux_parse_skb_ipv4(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3715 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | { |
| 3717 | int offset, ihlen, ret = -EINVAL; |
| 3718 | struct iphdr _iph, *ih; |
| 3719 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 3720 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); |
| 3722 | if (ih == NULL) |
| 3723 | goto out; |
| 3724 | |
| 3725 | ihlen = ih->ihl * 4; |
| 3726 | if (ihlen < sizeof(_iph)) |
| 3727 | goto out; |
| 3728 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3729 | ad->u.net->v4info.saddr = ih->saddr; |
| 3730 | ad->u.net->v4info.daddr = ih->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | ret = 0; |
| 3732 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3733 | if (proto) |
| 3734 | *proto = ih->protocol; |
| 3735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | switch (ih->protocol) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3737 | case IPPROTO_TCP: { |
| 3738 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3740 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 3741 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | |
| 3743 | offset += ihlen; |
| 3744 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 3745 | if (th == NULL) |
| 3746 | break; |
| 3747 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3748 | ad->u.net->sport = th->source; |
| 3749 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3753 | case IPPROTO_UDP: { |
| 3754 | struct udphdr _udph, *uh; |
| 3755 | |
| 3756 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 3757 | break; |
| 3758 | |
| 3759 | offset += ihlen; |
| 3760 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 3761 | if (uh == NULL) |
| 3762 | break; |
| 3763 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3764 | ad->u.net->sport = uh->source; |
| 3765 | ad->u.net->dport = uh->dest; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3766 | break; |
| 3767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3769 | case IPPROTO_DCCP: { |
| 3770 | struct dccp_hdr _dccph, *dh; |
| 3771 | |
| 3772 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 3773 | break; |
| 3774 | |
| 3775 | offset += ihlen; |
| 3776 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 3777 | if (dh == NULL) |
| 3778 | break; |
| 3779 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3780 | ad->u.net->sport = dh->dccph_sport; |
| 3781 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3782 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3783 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3784 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3785 | default: |
| 3786 | break; |
| 3787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | out: |
| 3789 | return ret; |
| 3790 | } |
| 3791 | |
| 3792 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 3793 | |
| 3794 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3795 | static int selinux_parse_skb_ipv6(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3796 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | { |
| 3798 | u8 nexthdr; |
| 3799 | int ret = -EINVAL, offset; |
| 3800 | struct ipv6hdr _ipv6h, *ip6; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 3801 | __be16 frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 3803 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 3805 | if (ip6 == NULL) |
| 3806 | goto out; |
| 3807 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3808 | ad->u.net->v6info.saddr = ip6->saddr; |
| 3809 | ad->u.net->v6info.daddr = ip6->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | ret = 0; |
| 3811 | |
| 3812 | nexthdr = ip6->nexthdr; |
| 3813 | offset += sizeof(_ipv6h); |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 3814 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | if (offset < 0) |
| 3816 | goto out; |
| 3817 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3818 | if (proto) |
| 3819 | *proto = nexthdr; |
| 3820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | switch (nexthdr) { |
| 3822 | case IPPROTO_TCP: { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3823 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | |
| 3825 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 3826 | if (th == NULL) |
| 3827 | break; |
| 3828 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3829 | ad->u.net->sport = th->source; |
| 3830 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | break; |
| 3832 | } |
| 3833 | |
| 3834 | case IPPROTO_UDP: { |
| 3835 | struct udphdr _udph, *uh; |
| 3836 | |
| 3837 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 3838 | if (uh == NULL) |
| 3839 | break; |
| 3840 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3841 | ad->u.net->sport = uh->source; |
| 3842 | ad->u.net->dport = uh->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | break; |
| 3844 | } |
| 3845 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3846 | case IPPROTO_DCCP: { |
| 3847 | struct dccp_hdr _dccph, *dh; |
| 3848 | |
| 3849 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 3850 | if (dh == NULL) |
| 3851 | break; |
| 3852 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3853 | ad->u.net->sport = dh->dccph_sport; |
| 3854 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3855 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3856 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 3857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | /* includes fragments */ |
| 3859 | default: |
| 3860 | break; |
| 3861 | } |
| 3862 | out: |
| 3863 | return ret; |
| 3864 | } |
| 3865 | |
| 3866 | #endif /* IPV6 */ |
| 3867 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3868 | 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] | 3869 | char **_addrp, int src, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | { |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3871 | char *addrp; |
| 3872 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3874 | switch (ad->u.net->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | case PF_INET: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3876 | ret = selinux_parse_skb_ipv4(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3877 | if (ret) |
| 3878 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3879 | addrp = (char *)(src ? &ad->u.net->v4info.saddr : |
| 3880 | &ad->u.net->v4info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3881 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | |
| 3883 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 3884 | case PF_INET6: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 3885 | ret = selinux_parse_skb_ipv6(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3886 | if (ret) |
| 3887 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3888 | addrp = (char *)(src ? &ad->u.net->v6info.saddr : |
| 3889 | &ad->u.net->v6info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3890 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | #endif /* IPV6 */ |
| 3892 | default: |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3893 | addrp = NULL; |
| 3894 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3897 | parse_error: |
| 3898 | printk(KERN_WARNING |
| 3899 | "SELinux: failure in selinux_parse_skb()," |
| 3900 | " unable to parse packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | return ret; |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 3902 | |
| 3903 | okay: |
| 3904 | if (_addrp) |
| 3905 | *_addrp = addrp; |
| 3906 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | } |
| 3908 | |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3909 | /** |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3910 | * selinux_skb_peerlbl_sid - Determine the peer label of a packet |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3911 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 3912 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3913 | * @sid: the packet's peer label SID |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3914 | * |
| 3915 | * Description: |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3916 | * Check the various different forms of network peer labeling and determine |
| 3917 | * the peer label/SID for the packet; most of the magic actually occurs in |
| 3918 | * the security server function security_net_peersid_cmp(). The function |
| 3919 | * returns zero if the value in @sid is valid (although it may be SECSID_NULL) |
| 3920 | * or -EACCES if @sid is invalid due to inconsistencies with the different |
| 3921 | * peer labels. |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3922 | * |
| 3923 | */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3924 | 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] | 3925 | { |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 3926 | int err; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3927 | u32 xfrm_sid; |
| 3928 | u32 nlbl_sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3929 | u32 nlbl_type; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3930 | |
Paul Moore | c09348c | 2013-12-10 14:57:54 -0500 | [diff] [blame] | 3931 | selinux_xfrm_skb_sid(skb, &xfrm_sid); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3932 | selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3933 | |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 3934 | err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); |
| 3935 | if (unlikely(err)) { |
| 3936 | printk(KERN_WARNING |
| 3937 | "SELinux: failure in selinux_skb_peerlbl_sid()," |
| 3938 | " unable to determine packet's peer label\n"); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3939 | return -EACCES; |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 3940 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 3941 | |
| 3942 | return 0; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 3943 | } |
| 3944 | |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 3945 | /** |
| 3946 | * selinux_conn_sid - Determine the child socket label for a connection |
| 3947 | * @sk_sid: the parent socket's SID |
| 3948 | * @skb_sid: the packet's SID |
| 3949 | * @conn_sid: the resulting connection SID |
| 3950 | * |
| 3951 | * If @skb_sid is valid then the user:role:type information from @sk_sid is |
| 3952 | * combined with the MLS information from @skb_sid in order to create |
| 3953 | * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy |
| 3954 | * of @sk_sid. Returns zero on success, negative values on failure. |
| 3955 | * |
| 3956 | */ |
| 3957 | static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) |
| 3958 | { |
| 3959 | int err = 0; |
| 3960 | |
| 3961 | if (skb_sid != SECSID_NULL) |
| 3962 | err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid); |
| 3963 | else |
| 3964 | *conn_sid = sk_sid; |
| 3965 | |
| 3966 | return err; |
| 3967 | } |
| 3968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | /* socket security operations */ |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3970 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3971 | static int socket_sockcreate_sid(const struct task_security_struct *tsec, |
| 3972 | u16 secclass, u32 *socksid) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3973 | { |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 3974 | if (tsec->sockcreate_sid > SECSID_NULL) { |
| 3975 | *socksid = tsec->sockcreate_sid; |
| 3976 | return 0; |
| 3977 | } |
| 3978 | |
| 3979 | return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, |
| 3980 | socksid); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 3981 | } |
| 3982 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3983 | 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] | 3984 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3985 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3986 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 3987 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3988 | struct lsm_network_audit net = {0,}; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3989 | u32 tsid = task_sid(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | |
Satya Durga Srinivasu Prabhala | 9028065 | 2013-09-24 15:23:48 -0700 | [diff] [blame] | 3991 | if (unlikely(!sksec)) { |
| 3992 | pr_warn("SELinux: sksec is NULL, socket is already freed\n"); |
| 3993 | return -EINVAL; |
| 3994 | } |
| 3995 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 3996 | if (sksec->sid == SECINITSID_KERNEL) |
| 3997 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3999 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4000 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4001 | ad.u.net = &net; |
| 4002 | ad.u.net->sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4004 | return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | static int selinux_socket_create(int family, int type, |
| 4008 | int protocol, int kern) |
| 4009 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4010 | const struct task_security_struct *tsec = current_security(); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4011 | u32 newsid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4012 | u16 secclass; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4013 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | |
| 4015 | if (kern) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4016 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4018 | secclass = socket_type_to_security_class(family, type, protocol); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4019 | rc = socket_sockcreate_sid(tsec, secclass, &newsid); |
| 4020 | if (rc) |
| 4021 | return rc; |
| 4022 | |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4023 | return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4026 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 4027 | int type, int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4029 | const struct task_security_struct *tsec = current_security(); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4030 | struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4031 | struct sk_security_struct *sksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4032 | int err = 0; |
| 4033 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4034 | isec->sclass = socket_type_to_security_class(family, type, protocol); |
| 4035 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4036 | if (kern) |
| 4037 | isec->sid = SECINITSID_KERNEL; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4038 | else { |
| 4039 | err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid)); |
| 4040 | if (err) |
| 4041 | return err; |
| 4042 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | isec->initialized = 1; |
| 4045 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4046 | if (sock->sk) { |
| 4047 | sksec = sock->sk->sk_security; |
| 4048 | sksec->sid = isec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4049 | sksec->sclass = isec->sclass; |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4050 | err = selinux_netlbl_socket_post_create(sock->sk, family); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4053 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | /* Range of port numbers used to automatically bind. |
| 4057 | Need to determine whether we should perform a name_bind |
| 4058 | permission check between the socket and the port number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | |
| 4060 | static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4061 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4062 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | u16 family; |
| 4064 | int err; |
| 4065 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4066 | err = sock_has_perm(current, sk, SOCKET__BIND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | if (err) |
| 4068 | goto out; |
| 4069 | |
| 4070 | /* |
| 4071 | * 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] | 4072 | * Multiple address binding for SCTP is not supported yet: we just |
| 4073 | * check the first address now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4075 | family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | if (family == PF_INET || family == PF_INET6) { |
| 4077 | char *addrp; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4078 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4079 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4080 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4081 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | struct sockaddr_in *addr4 = NULL; |
| 4083 | struct sockaddr_in6 *addr6 = NULL; |
| 4084 | unsigned short snum; |
James Morris | e399f98 | 2008-06-12 01:39:58 +1000 | [diff] [blame] | 4085 | u32 sid, node_perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | if (family == PF_INET) { |
| 4088 | addr4 = (struct sockaddr_in *)address; |
| 4089 | snum = ntohs(addr4->sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | addrp = (char *)&addr4->sin_addr.s_addr; |
| 4091 | } else { |
| 4092 | addr6 = (struct sockaddr_in6 *)address; |
| 4093 | snum = ntohs(addr6->sin6_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | addrp = (char *)&addr6->sin6_addr.s6_addr; |
| 4095 | } |
| 4096 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4097 | if (snum) { |
| 4098 | int low, high; |
| 4099 | |
| 4100 | inet_get_local_port_range(&low, &high); |
| 4101 | |
| 4102 | if (snum < max(PROT_SOCK, low) || snum > high) { |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4103 | err = sel_netport_sid(sk->sk_protocol, |
| 4104 | snum, &sid); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4105 | if (err) |
| 4106 | goto out; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4107 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4108 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4109 | ad.u.net = &net; |
| 4110 | ad.u.net->sport = htons(snum); |
| 4111 | ad.u.net->family = family; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4112 | err = avc_has_perm(sksec->sid, sid, |
| 4113 | sksec->sclass, |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4114 | SOCKET__NAME_BIND, &ad); |
| 4115 | if (err) |
| 4116 | goto out; |
| 4117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4119 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4120 | switch (sksec->sclass) { |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4121 | case SECCLASS_TCP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | node_perm = TCP_SOCKET__NODE_BIND; |
| 4123 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4124 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4125 | case SECCLASS_UDP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | node_perm = UDP_SOCKET__NODE_BIND; |
| 4127 | break; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4128 | |
| 4129 | case SECCLASS_DCCP_SOCKET: |
| 4130 | node_perm = DCCP_SOCKET__NODE_BIND; |
| 4131 | break; |
| 4132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | default: |
| 4134 | node_perm = RAWIP_SOCKET__NODE_BIND; |
| 4135 | break; |
| 4136 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4137 | |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4138 | err = sel_netnode_sid(addrp, family, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | if (err) |
| 4140 | goto out; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4141 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4142 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4143 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4144 | ad.u.net = &net; |
| 4145 | ad.u.net->sport = htons(snum); |
| 4146 | ad.u.net->family = family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | |
| 4148 | if (family == PF_INET) |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4149 | ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | else |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4151 | ad.u.net->v6info.saddr = addr6->sin6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4153 | err = avc_has_perm(sksec->sid, sid, |
| 4154 | sksec->sclass, node_perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | if (err) |
| 4156 | goto out; |
| 4157 | } |
| 4158 | out: |
| 4159 | return err; |
| 4160 | } |
| 4161 | |
| 4162 | static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4163 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4164 | struct sock *sk = sock->sk; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4165 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | int err; |
| 4167 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4168 | err = sock_has_perm(current, sk, SOCKET__CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | if (err) |
| 4170 | return err; |
| 4171 | |
| 4172 | /* |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4173 | * 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] | 4174 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4175 | if (sksec->sclass == SECCLASS_TCP_SOCKET || |
| 4176 | sksec->sclass == SECCLASS_DCCP_SOCKET) { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4177 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4178 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4179 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | struct sockaddr_in *addr4 = NULL; |
| 4181 | struct sockaddr_in6 *addr6 = NULL; |
| 4182 | unsigned short snum; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4183 | u32 sid, perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | |
| 4185 | if (sk->sk_family == PF_INET) { |
| 4186 | addr4 = (struct sockaddr_in *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4187 | if (addrlen < sizeof(struct sockaddr_in)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | return -EINVAL; |
| 4189 | snum = ntohs(addr4->sin_port); |
| 4190 | } else { |
| 4191 | addr6 = (struct sockaddr_in6 *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4192 | if (addrlen < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | return -EINVAL; |
| 4194 | snum = ntohs(addr6->sin6_port); |
| 4195 | } |
| 4196 | |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4197 | err = sel_netport_sid(sk->sk_protocol, snum, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | if (err) |
| 4199 | goto out; |
| 4200 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4201 | perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4202 | TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; |
| 4203 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4204 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4205 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4206 | ad.u.net = &net; |
| 4207 | ad.u.net->dport = htons(snum); |
| 4208 | ad.u.net->family = sk->sk_family; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4209 | err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | if (err) |
| 4211 | goto out; |
| 4212 | } |
| 4213 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4214 | err = selinux_netlbl_socket_connect(sk, address); |
| 4215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | out: |
| 4217 | return err; |
| 4218 | } |
| 4219 | |
| 4220 | static int selinux_socket_listen(struct socket *sock, int backlog) |
| 4221 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4222 | return sock_has_perm(current, sock->sk, SOCKET__LISTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | static int selinux_socket_accept(struct socket *sock, struct socket *newsock) |
| 4226 | { |
| 4227 | int err; |
| 4228 | struct inode_security_struct *isec; |
| 4229 | struct inode_security_struct *newisec; |
| 4230 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4231 | err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | if (err) |
| 4233 | return err; |
| 4234 | |
| 4235 | newisec = SOCK_INODE(newsock)->i_security; |
| 4236 | |
| 4237 | isec = SOCK_INODE(sock)->i_security; |
| 4238 | newisec->sclass = isec->sclass; |
| 4239 | newisec->sid = isec->sid; |
| 4240 | newisec->initialized = 1; |
| 4241 | |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
| 4245 | static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4246 | int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4248 | return sock_has_perm(current, sock->sk, SOCKET__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 4252 | int size, int flags) |
| 4253 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4254 | return sock_has_perm(current, sock->sk, SOCKET__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | static int selinux_socket_getsockname(struct socket *sock) |
| 4258 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4259 | return sock_has_perm(current, sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | static int selinux_socket_getpeername(struct socket *sock) |
| 4263 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4264 | return sock_has_perm(current, sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | } |
| 4266 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4267 | static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | { |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4269 | int err; |
| 4270 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4271 | err = sock_has_perm(current, sock->sk, SOCKET__SETOPT); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4272 | if (err) |
| 4273 | return err; |
| 4274 | |
| 4275 | return selinux_netlbl_socket_setsockopt(sock, level, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | static int selinux_socket_getsockopt(struct socket *sock, int level, |
| 4279 | int optname) |
| 4280 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4281 | return sock_has_perm(current, sock->sk, SOCKET__GETOPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | static int selinux_socket_shutdown(struct socket *sock, int how) |
| 4285 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4286 | return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4289 | static int selinux_socket_unix_stream_connect(struct sock *sock, |
| 4290 | struct sock *other, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | struct sock *newsk) |
| 4292 | { |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4293 | struct sk_security_struct *sksec_sock = sock->sk_security; |
| 4294 | struct sk_security_struct *sksec_other = other->sk_security; |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4295 | struct sk_security_struct *sksec_new = newsk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4296 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4297 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4298 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | int err; |
| 4300 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4301 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4302 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4303 | ad.u.net = &net; |
| 4304 | ad.u.net->sk = other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4306 | err = avc_has_perm(sksec_sock->sid, sksec_other->sid, |
| 4307 | sksec_other->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | UNIX_STREAM_SOCKET__CONNECTTO, &ad); |
| 4309 | if (err) |
| 4310 | return err; |
| 4311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | /* server child socket */ |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4313 | sksec_new->peer_sid = sksec_sock->sid; |
| 4314 | err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, |
| 4315 | &sksec_new->sid); |
| 4316 | if (err) |
| 4317 | return err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4318 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4319 | /* connecting socket */ |
| 4320 | sksec_sock->peer_sid = sksec_new->sid; |
| 4321 | |
| 4322 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | static int selinux_socket_unix_may_send(struct socket *sock, |
| 4326 | struct socket *other) |
| 4327 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4328 | struct sk_security_struct *ssec = sock->sk->sk_security; |
| 4329 | struct sk_security_struct *osec = other->sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4330 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4331 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4332 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4334 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4335 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4336 | ad.u.net = &net; |
| 4337 | ad.u.net->sk = other->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4339 | return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, |
| 4340 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4343 | static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, |
| 4344 | u32 peer_sid, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4345 | struct common_audit_data *ad) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4346 | { |
| 4347 | int err; |
| 4348 | u32 if_sid; |
| 4349 | u32 node_sid; |
| 4350 | |
| 4351 | err = sel_netif_sid(ifindex, &if_sid); |
| 4352 | if (err) |
| 4353 | return err; |
| 4354 | err = avc_has_perm(peer_sid, if_sid, |
| 4355 | SECCLASS_NETIF, NETIF__INGRESS, ad); |
| 4356 | if (err) |
| 4357 | return err; |
| 4358 | |
| 4359 | err = sel_netnode_sid(addrp, family, &node_sid); |
| 4360 | if (err) |
| 4361 | return err; |
| 4362 | return avc_has_perm(peer_sid, node_sid, |
| 4363 | SECCLASS_NODE, NODE__RECVFROM, ad); |
| 4364 | } |
| 4365 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4366 | 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] | 4367 | u16 family) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4368 | { |
Paul Moore | 277d342 | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 4369 | int err = 0; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4370 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4371 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4372 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4373 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4374 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4375 | char *addrp; |
| 4376 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4377 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4378 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4379 | ad.u.net = &net; |
| 4380 | ad.u.net->netif = skb->skb_iif; |
| 4381 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4382 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
| 4383 | if (err) |
| 4384 | return err; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4385 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4386 | if (selinux_secmark_enabled()) { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4387 | err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4388 | PACKET__RECV, &ad); |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4389 | if (err) |
| 4390 | return err; |
| 4391 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4392 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4393 | err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); |
| 4394 | if (err) |
| 4395 | return err; |
| 4396 | err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4397 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4398 | return err; |
| 4399 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4400 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4401 | static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 4402 | { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4403 | int err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4404 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4405 | u16 family = sk->sk_family; |
| 4406 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4407 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4408 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4409 | struct lsm_network_audit net = {0,}; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4410 | char *addrp; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4411 | u8 secmark_active; |
| 4412 | u8 peerlbl_active; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4413 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4414 | if (family != PF_INET && family != PF_INET6) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4415 | return 0; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4416 | |
| 4417 | /* Handle mapped IPv4 packets arriving via IPv6 sockets */ |
Al Viro | 87fcd70 | 2006-12-04 22:00:55 +0000 | [diff] [blame] | 4418 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4419 | family = PF_INET; |
| 4420 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4421 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4422 | * to the selinux_sock_rcv_skb_compat() function to deal with the |
| 4423 | * special handling. We do this in an attempt to keep this function |
| 4424 | * as fast and as clean as possible. */ |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4425 | if (!selinux_policycap_netpeer) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4426 | return selinux_sock_rcv_skb_compat(sk, skb, family); |
| 4427 | |
| 4428 | secmark_active = selinux_secmark_enabled(); |
| 4429 | peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); |
| 4430 | if (!secmark_active && !peerlbl_active) |
| 4431 | return 0; |
| 4432 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4433 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4434 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4435 | ad.u.net = &net; |
| 4436 | ad.u.net->netif = skb->skb_iif; |
| 4437 | ad.u.net->family = family; |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4438 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4439 | if (err) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4440 | return err; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4441 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4442 | if (peerlbl_active) { |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4443 | u32 peer_sid; |
| 4444 | |
| 4445 | err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); |
| 4446 | if (err) |
| 4447 | return err; |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 4448 | err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4449 | peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4450 | if (err) { |
| 4451 | selinux_netlbl_err(skb, err, 0); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4452 | return err; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4453 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4454 | err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, |
| 4455 | PEER__RECV, &ad); |
Chad Hanson | 619af68 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4456 | if (err) { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4457 | selinux_netlbl_err(skb, err, 0); |
Chad Hanson | 619af68 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4458 | return err; |
| 4459 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4460 | } |
| 4461 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4462 | if (secmark_active) { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4463 | err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, |
| 4464 | PACKET__RECV, &ad); |
| 4465 | if (err) |
| 4466 | return err; |
| 4467 | } |
| 4468 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4469 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | } |
| 4471 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4472 | static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, |
| 4473 | int __user *optlen, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | { |
| 4475 | int err = 0; |
| 4476 | char *scontext; |
| 4477 | u32 scontext_len; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4478 | struct sk_security_struct *sksec = sock->sk->sk_security; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4479 | u32 peer_sid = SECSID_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4481 | if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || |
| 4482 | sksec->sclass == SECCLASS_TCP_SOCKET) |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4483 | peer_sid = sksec->peer_sid; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4484 | if (peer_sid == SECSID_NULL) |
| 4485 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4487 | err = security_sid_to_context(peer_sid, &scontext, &scontext_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | if (err) |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4489 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | |
| 4491 | if (scontext_len > len) { |
| 4492 | err = -ERANGE; |
| 4493 | goto out_len; |
| 4494 | } |
| 4495 | |
| 4496 | if (copy_to_user(optval, scontext, scontext_len)) |
| 4497 | err = -EFAULT; |
| 4498 | |
| 4499 | out_len: |
| 4500 | if (put_user(scontext_len, optlen)) |
| 4501 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | kfree(scontext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | return err; |
| 4504 | } |
| 4505 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4506 | 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] | 4507 | { |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4508 | u32 peer_secid = SECSID_NULL; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4509 | u16 family; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 4510 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4511 | if (skb && skb->protocol == htons(ETH_P_IP)) |
| 4512 | family = PF_INET; |
| 4513 | else if (skb && skb->protocol == htons(ETH_P_IPV6)) |
| 4514 | family = PF_INET6; |
| 4515 | else if (sock) |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4516 | family = sock->sk->sk_family; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4517 | else |
| 4518 | goto out; |
| 4519 | |
| 4520 | if (sock && family == PF_UNIX) |
Ahmed S. Darwish | 713a04a | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 4521 | selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4522 | else if (skb) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4523 | selinux_skb_peerlbl_sid(skb, family, &peer_secid); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4524 | |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4525 | out: |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4526 | *secid = peer_secid; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4527 | if (peer_secid == SECSID_NULL) |
| 4528 | return -EINVAL; |
| 4529 | return 0; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 4532 | 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] | 4533 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4534 | struct sk_security_struct *sksec; |
| 4535 | |
| 4536 | sksec = kzalloc(sizeof(*sksec), priority); |
| 4537 | if (!sksec) |
| 4538 | return -ENOMEM; |
| 4539 | |
| 4540 | sksec->peer_sid = SECINITSID_UNLABELED; |
| 4541 | sksec->sid = SECINITSID_UNLABELED; |
| 4542 | selinux_netlbl_sk_security_reset(sksec); |
| 4543 | sk->sk_security = sksec; |
| 4544 | |
| 4545 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | static void selinux_sk_free_security(struct sock *sk) |
| 4549 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4550 | struct sk_security_struct *sksec = sk->sk_security; |
| 4551 | |
| 4552 | sk->sk_security = NULL; |
| 4553 | selinux_netlbl_sk_security_free(sksec); |
| 4554 | kfree(sksec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | } |
| 4556 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4557 | static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) |
| 4558 | { |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4559 | struct sk_security_struct *sksec = sk->sk_security; |
| 4560 | struct sk_security_struct *newsksec = newsk->sk_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4561 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4562 | newsksec->sid = sksec->sid; |
| 4563 | newsksec->peer_sid = sksec->peer_sid; |
| 4564 | newsksec->sclass = sksec->sclass; |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 4565 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4566 | selinux_netlbl_sk_security_reset(newsksec); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4567 | } |
| 4568 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4569 | static void selinux_sk_getsecid(struct sock *sk, u32 *secid) |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4570 | { |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4571 | if (!sk) |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4572 | *secid = SECINITSID_ANY_SOCKET; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4573 | else { |
| 4574 | struct sk_security_struct *sksec = sk->sk_security; |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4575 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4576 | *secid = sksec->sid; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4577 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4578 | } |
| 4579 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4580 | static void selinux_sock_graft(struct sock *sk, struct socket *parent) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4581 | { |
| 4582 | struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; |
| 4583 | struct sk_security_struct *sksec = sk->sk_security; |
| 4584 | |
David Woodhouse | 2148ccc | 2006-09-29 15:50:25 -0700 | [diff] [blame] | 4585 | if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || |
| 4586 | sk->sk_family == PF_UNIX) |
| 4587 | isec->sid = sksec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4588 | sksec->sclass = isec->sclass; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4589 | } |
| 4590 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 4591 | static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 4592 | struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4593 | { |
| 4594 | struct sk_security_struct *sksec = sk->sk_security; |
| 4595 | int err; |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4596 | u16 family = sk->sk_family; |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4597 | u32 connsid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4598 | u32 peersid; |
| 4599 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4600 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 4601 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 4602 | family = PF_INET; |
| 4603 | |
| 4604 | err = selinux_skb_peerlbl_sid(skb, family, &peersid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4605 | if (err) |
| 4606 | return err; |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4607 | err = selinux_conn_sid(sksec->sid, peersid, &connsid); |
| 4608 | if (err) |
| 4609 | return err; |
| 4610 | req->secid = connsid; |
| 4611 | req->peer_secid = peersid; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 4612 | |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4613 | return selinux_netlbl_inet_conn_request(req, family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 4616 | static void selinux_inet_csk_clone(struct sock *newsk, |
| 4617 | const struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4618 | { |
| 4619 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 4620 | |
| 4621 | newsksec->sid = req->secid; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4622 | newsksec->peer_sid = req->peer_secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4623 | /* NOTE: Ideally, we should also get the isec->sid for the |
| 4624 | new socket in sync, but we don't have the isec available yet. |
| 4625 | So we will wait until sock_graft to do it, by which |
| 4626 | time it will have been created and available. */ |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 4627 | |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 4628 | /* We don't need to take any sort of lock here as we are the only |
| 4629 | * thread with access to newsksec */ |
Paul Moore | 389fb80 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4630 | selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4633 | 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] | 4634 | { |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4635 | u16 family = sk->sk_family; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4636 | struct sk_security_struct *sksec = sk->sk_security; |
| 4637 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4638 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 4639 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 4640 | family = PF_INET; |
| 4641 | |
| 4642 | selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 4643 | } |
| 4644 | |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 4645 | static int selinux_secmark_relabel_packet(u32 sid) |
| 4646 | { |
| 4647 | const struct task_security_struct *__tsec; |
| 4648 | u32 tsid; |
| 4649 | |
| 4650 | __tsec = current_security(); |
| 4651 | tsid = __tsec->sid; |
| 4652 | |
| 4653 | return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); |
| 4654 | } |
| 4655 | |
| 4656 | static void selinux_secmark_refcount_inc(void) |
| 4657 | { |
| 4658 | atomic_inc(&selinux_secmark_refcount); |
| 4659 | } |
| 4660 | |
| 4661 | static void selinux_secmark_refcount_dec(void) |
| 4662 | { |
| 4663 | atomic_dec(&selinux_secmark_refcount); |
| 4664 | } |
| 4665 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 4666 | static void selinux_req_classify_flow(const struct request_sock *req, |
| 4667 | struct flowi *fl) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4668 | { |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 4669 | fl->flowi_secid = req->secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 4672 | static int selinux_tun_dev_create(void) |
| 4673 | { |
| 4674 | u32 sid = current_sid(); |
| 4675 | |
| 4676 | /* we aren't taking into account the "sockcreate" SID since the socket |
| 4677 | * that is being created here is not a socket in the traditional sense, |
| 4678 | * instead it is a private sock, accessible only to the kernel, and |
| 4679 | * representing a wide range of network traffic spanning multiple |
| 4680 | * connections unlike traditional sockets - check the TUN driver to |
| 4681 | * get a better understanding of why this socket is special */ |
| 4682 | |
| 4683 | return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, |
| 4684 | NULL); |
| 4685 | } |
| 4686 | |
| 4687 | static void selinux_tun_dev_post_create(struct sock *sk) |
| 4688 | { |
| 4689 | struct sk_security_struct *sksec = sk->sk_security; |
| 4690 | |
| 4691 | /* we don't currently perform any NetLabel based labeling here and it |
| 4692 | * isn't clear that we would want to do so anyway; while we could apply |
| 4693 | * labeling without the support of the TUN user the resulting labeled |
| 4694 | * traffic from the other end of the connection would almost certainly |
| 4695 | * cause confusion to the TUN user that had no idea network labeling |
| 4696 | * protocols were being used */ |
| 4697 | |
| 4698 | /* see the comments in selinux_tun_dev_create() about why we don't use |
| 4699 | * the sockcreate SID here */ |
| 4700 | |
| 4701 | sksec->sid = current_sid(); |
| 4702 | sksec->sclass = SECCLASS_TUN_SOCKET; |
| 4703 | } |
| 4704 | |
| 4705 | static int selinux_tun_dev_attach(struct sock *sk) |
| 4706 | { |
| 4707 | struct sk_security_struct *sksec = sk->sk_security; |
| 4708 | u32 sid = current_sid(); |
| 4709 | int err; |
| 4710 | |
| 4711 | err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET, |
| 4712 | TUN_SOCKET__RELABELFROM, NULL); |
| 4713 | if (err) |
| 4714 | return err; |
| 4715 | err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, |
| 4716 | TUN_SOCKET__RELABELTO, NULL); |
| 4717 | if (err) |
| 4718 | return err; |
| 4719 | |
| 4720 | sksec->sid = sid; |
| 4721 | |
| 4722 | return 0; |
| 4723 | } |
| 4724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) |
| 4726 | { |
| 4727 | int err = 0; |
| 4728 | u32 perm; |
| 4729 | struct nlmsghdr *nlh; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4730 | struct sk_security_struct *sksec = sk->sk_security; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | if (skb->len < NLMSG_SPACE(0)) { |
| 4733 | err = -EINVAL; |
| 4734 | goto out; |
| 4735 | } |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 4736 | nlh = nlmsg_hdr(skb); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4737 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4738 | err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | if (err) { |
| 4740 | if (err == -EINVAL) { |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 4741 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | "SELinux: unrecognized netlink message" |
| 4743 | " type=%hu for sclass=%hu\n", |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4744 | nlh->nlmsg_type, sksec->sclass); |
Eric Paris | 39c9aed | 2008-11-05 09:34:42 -0500 | [diff] [blame] | 4745 | if (!selinux_enforcing || security_get_allow_unknown()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | err = 0; |
| 4747 | } |
| 4748 | |
| 4749 | /* Ignore */ |
| 4750 | if (err == -ENOENT) |
| 4751 | err = 0; |
| 4752 | goto out; |
| 4753 | } |
| 4754 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4755 | err = sock_has_perm(current, sk, perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | out: |
| 4757 | return err; |
| 4758 | } |
| 4759 | |
| 4760 | #ifdef CONFIG_NETFILTER |
| 4761 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4762 | static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, |
| 4763 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4765 | int err; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4766 | char *addrp; |
| 4767 | u32 peer_sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4768 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4769 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4770 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4771 | u8 secmark_active; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4772 | u8 netlbl_active; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4773 | u8 peerlbl_active; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4774 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4775 | if (!selinux_policycap_netpeer) |
| 4776 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4777 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4778 | secmark_active = selinux_secmark_enabled(); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4779 | netlbl_active = netlbl_enabled(); |
| 4780 | peerlbl_active = netlbl_active || selinux_xfrm_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4781 | if (!secmark_active && !peerlbl_active) |
| 4782 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4783 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4784 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) |
| 4785 | return NF_DROP; |
| 4786 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4787 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4788 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4789 | ad.u.net = &net; |
| 4790 | ad.u.net->netif = ifindex; |
| 4791 | ad.u.net->family = family; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4792 | if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) |
| 4793 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4795 | if (peerlbl_active) { |
| 4796 | err = selinux_inet_sys_rcv_skb(ifindex, addrp, family, |
| 4797 | peer_sid, &ad); |
| 4798 | if (err) { |
| 4799 | selinux_netlbl_err(skb, err, 1); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4800 | return NF_DROP; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4801 | } |
| 4802 | } |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4803 | |
| 4804 | if (secmark_active) |
| 4805 | if (avc_has_perm(peer_sid, skb->secmark, |
| 4806 | SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) |
| 4807 | return NF_DROP; |
| 4808 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4809 | if (netlbl_active) |
| 4810 | /* we do this in the FORWARD path and not the POST_ROUTING |
| 4811 | * path because we want to make sure we apply the necessary |
| 4812 | * labeling before IPsec is applied so we can leverage AH |
| 4813 | * protection */ |
| 4814 | if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) |
| 4815 | return NF_DROP; |
| 4816 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4817 | return NF_ACCEPT; |
| 4818 | } |
| 4819 | |
| 4820 | static unsigned int selinux_ipv4_forward(unsigned int hooknum, |
| 4821 | struct sk_buff *skb, |
| 4822 | const struct net_device *in, |
| 4823 | const struct net_device *out, |
| 4824 | int (*okfn)(struct sk_buff *)) |
| 4825 | { |
| 4826 | return selinux_ip_forward(skb, in->ifindex, PF_INET); |
| 4827 | } |
| 4828 | |
| 4829 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 4830 | static unsigned int selinux_ipv6_forward(unsigned int hooknum, |
| 4831 | struct sk_buff *skb, |
| 4832 | const struct net_device *in, |
| 4833 | const struct net_device *out, |
| 4834 | int (*okfn)(struct sk_buff *)) |
| 4835 | { |
| 4836 | return selinux_ip_forward(skb, in->ifindex, PF_INET6); |
| 4837 | } |
| 4838 | #endif /* IPV6 */ |
| 4839 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4840 | static unsigned int selinux_ip_output(struct sk_buff *skb, |
| 4841 | u16 family) |
| 4842 | { |
Paul Moore | 3ac7b8d | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 4843 | struct sock *sk; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4844 | u32 sid; |
| 4845 | |
| 4846 | if (!netlbl_enabled()) |
| 4847 | return NF_ACCEPT; |
| 4848 | |
| 4849 | /* we do this in the LOCAL_OUT path and not the POST_ROUTING path |
| 4850 | * because we want to make sure we apply the necessary labeling |
| 4851 | * before IPsec is applied so we can leverage AH protection */ |
Paul Moore | 3ac7b8d | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 4852 | sk = skb->sk; |
| 4853 | if (sk) { |
| 4854 | struct sk_security_struct *sksec; |
| 4855 | |
| 4856 | if (sk->sk_state == TCP_LISTEN) |
| 4857 | /* if the socket is the listening state then this |
| 4858 | * packet is a SYN-ACK packet which means it needs to |
| 4859 | * be labeled based on the connection/request_sock and |
| 4860 | * not the parent socket. unfortunately, we can't |
| 4861 | * lookup the request_sock yet as it isn't queued on |
| 4862 | * the parent socket until after the SYN-ACK is sent. |
| 4863 | * the "solution" is to simply pass the packet as-is |
| 4864 | * as any IP option based labeling should be copied |
| 4865 | * from the initial connection request (in the IP |
| 4866 | * layer). it is far from ideal, but until we get a |
| 4867 | * security label in the packet itself this is the |
| 4868 | * best we can do. */ |
| 4869 | return NF_ACCEPT; |
| 4870 | |
| 4871 | /* standard practice, label using the parent socket */ |
| 4872 | sksec = sk->sk_security; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 4873 | sid = sksec->sid; |
| 4874 | } else |
| 4875 | sid = SECINITSID_KERNEL; |
| 4876 | if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) |
| 4877 | return NF_DROP; |
| 4878 | |
| 4879 | return NF_ACCEPT; |
| 4880 | } |
| 4881 | |
| 4882 | static unsigned int selinux_ipv4_output(unsigned int hooknum, |
| 4883 | struct sk_buff *skb, |
| 4884 | const struct net_device *in, |
| 4885 | const struct net_device *out, |
| 4886 | int (*okfn)(struct sk_buff *)) |
| 4887 | { |
| 4888 | return selinux_ip_output(skb, PF_INET); |
| 4889 | } |
| 4890 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4891 | static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, |
| 4892 | int ifindex, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4893 | u16 family) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4894 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4895 | struct sock *sk = skb->sk; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4896 | struct sk_security_struct *sksec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4897 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4898 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4899 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4900 | char *addrp; |
| 4901 | u8 proto; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4902 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4903 | if (sk == NULL) |
| 4904 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4905 | sksec = sk->sk_security; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4906 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4907 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4908 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4909 | ad.u.net = &net; |
| 4910 | ad.u.net->netif = ifindex; |
| 4911 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4912 | if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) |
| 4913 | return NF_DROP; |
| 4914 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4915 | if (selinux_secmark_enabled()) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4916 | if (avc_has_perm(sksec->sid, skb->secmark, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4917 | SECCLASS_PACKET, PACKET__SEND, &ad)) |
Eric Paris | 2fe66ec | 2010-11-23 06:28:08 +0000 | [diff] [blame] | 4918 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4919 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4920 | if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) |
| 4921 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4922 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4923 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | } |
| 4925 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4926 | static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, |
| 4927 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4929 | u32 secmark_perm; |
| 4930 | u32 peer_sid; |
| 4931 | struct sock *sk; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4932 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 4933 | struct selinux_audit_data sad = {0,}; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4934 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4935 | char *addrp; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4936 | u8 secmark_active; |
| 4937 | u8 peerlbl_active; |
| 4938 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4939 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4940 | * to the selinux_ip_postroute_compat() function to deal with the |
| 4941 | * special handling. We do this in an attempt to keep this function |
| 4942 | * as fast and as clean as possible. */ |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4943 | if (!selinux_policycap_netpeer) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4944 | return selinux_ip_postroute_compat(skb, ifindex, family); |
Paul Moore | a737983 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 4945 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4946 | secmark_active = selinux_secmark_enabled(); |
| 4947 | peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); |
| 4948 | if (!secmark_active && !peerlbl_active) |
| 4949 | return NF_ACCEPT; |
| 4950 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4951 | sk = skb->sk; |
Paul Moore | a737983 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 4952 | |
| 4953 | #ifdef CONFIG_XFRM |
| 4954 | /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec |
| 4955 | * packet transformation so allow the packet to pass without any checks |
| 4956 | * since we'll have another chance to perform access control checks |
| 4957 | * when the packet is on it's final way out. |
| 4958 | * NOTE: there appear to be some IPv6 multicast cases where skb->dst |
| 4959 | * is NULL, in this case go ahead and apply access control. |
| 4960 | * is NULL, in this case go ahead and apply access control. |
| 4961 | * NOTE: if this is a local socket (skb->sk != NULL) that is in the |
| 4962 | * TCP listening state we cannot wait until the XFRM processing |
| 4963 | * is done as we will miss out on the SA label if we do; |
| 4964 | * unfortunately, this means more work, but it is only once per |
| 4965 | * connection. */ |
| 4966 | if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && |
| 4967 | !(sk != NULL && sk->sk_state == TCP_LISTEN)) |
| 4968 | return NF_ACCEPT; |
| 4969 | #endif |
| 4970 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4971 | if (sk == NULL) { |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4972 | /* Without an associated socket the packet is either coming |
| 4973 | * from the kernel or it is being forwarded; check the packet |
| 4974 | * to determine which and if the packet is being forwarded |
| 4975 | * query the packet directly to determine the security label. */ |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 4976 | if (skb->skb_iif) { |
| 4977 | secmark_perm = PACKET__FORWARD_OUT; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4978 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 4979 | return NF_DROP; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 4980 | } else { |
| 4981 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4982 | peer_sid = SECINITSID_KERNEL; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 4983 | } |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4984 | } else if (sk->sk_state == TCP_LISTEN) { |
| 4985 | /* Locally generated packet but the associated socket is in the |
| 4986 | * listening state which means this is a SYN-ACK packet. In |
| 4987 | * this particular case the correct security label is assigned |
| 4988 | * to the connection/request_sock but unfortunately we can't |
| 4989 | * query the request_sock as it isn't queued on the parent |
| 4990 | * socket until after the SYN-ACK packet is sent; the only |
| 4991 | * viable choice is to regenerate the label like we do in |
| 4992 | * selinux_inet_conn_request(). See also selinux_ip_output() |
| 4993 | * for similar problems. */ |
| 4994 | u32 skb_sid; |
| 4995 | struct sk_security_struct *sksec = sk->sk_security; |
| 4996 | if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) |
| 4997 | return NF_DROP; |
Paul Moore | a737983 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 4998 | /* At this point, if the returned skb peerlbl is SECSID_NULL |
| 4999 | * and the packet has been through at least one XFRM |
| 5000 | * transformation then we must be dealing with the "final" |
| 5001 | * form of labeled IPsec packet; since we've already applied |
| 5002 | * all of our access controls on this packet we can safely |
| 5003 | * pass the packet. */ |
| 5004 | if (skb_sid == SECSID_NULL) { |
| 5005 | switch (family) { |
| 5006 | case PF_INET: |
| 5007 | if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) |
| 5008 | return NF_ACCEPT; |
| 5009 | break; |
| 5010 | case PF_INET6: |
| 5011 | if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) |
| 5012 | return NF_ACCEPT; |
| 5013 | default: |
| 5014 | return NF_DROP_ERR(-ECONNREFUSED); |
| 5015 | } |
| 5016 | } |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5017 | if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) |
| 5018 | return NF_DROP; |
| 5019 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5020 | } else { |
Paul Moore | 96d2c17 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5021 | /* Locally generated packet, fetch the security label from the |
| 5022 | * associated socket. */ |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5023 | struct sk_security_struct *sksec = sk->sk_security; |
| 5024 | peer_sid = sksec->sid; |
| 5025 | secmark_perm = PACKET__SEND; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5026 | } |
| 5027 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5028 | COMMON_AUDIT_DATA_INIT(&ad, NET); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5029 | ad.selinux_audit_data = &sad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5030 | ad.u.net = &net; |
| 5031 | ad.u.net->netif = ifindex; |
| 5032 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5033 | if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5034 | return NF_DROP; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5035 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5036 | if (secmark_active) |
| 5037 | if (avc_has_perm(peer_sid, skb->secmark, |
| 5038 | SECCLASS_PACKET, secmark_perm, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5039 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5040 | |
| 5041 | if (peerlbl_active) { |
| 5042 | u32 if_sid; |
| 5043 | u32 node_sid; |
| 5044 | |
| 5045 | if (sel_netif_sid(ifindex, &if_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5046 | return NF_DROP; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5047 | if (avc_has_perm(peer_sid, if_sid, |
| 5048 | SECCLASS_NETIF, NETIF__EGRESS, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5049 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5050 | |
| 5051 | if (sel_netnode_sid(addrp, family, &node_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5052 | return NF_DROP; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5053 | if (avc_has_perm(peer_sid, node_sid, |
| 5054 | SECCLASS_NODE, NODE__SENDTO, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5055 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | return NF_ACCEPT; |
| 5059 | } |
| 5060 | |
| 5061 | static unsigned int selinux_ipv4_postroute(unsigned int hooknum, |
| 5062 | struct sk_buff *skb, |
| 5063 | const struct net_device *in, |
| 5064 | const struct net_device *out, |
| 5065 | int (*okfn)(struct sk_buff *)) |
| 5066 | { |
| 5067 | return selinux_ip_postroute(skb, out->ifindex, PF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5071 | static unsigned int selinux_ipv6_postroute(unsigned int hooknum, |
| 5072 | struct sk_buff *skb, |
| 5073 | const struct net_device *in, |
| 5074 | const struct net_device *out, |
| 5075 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5077 | return selinux_ip_postroute(skb, out->ifindex, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | #endif /* IPV6 */ |
| 5080 | |
| 5081 | #endif /* CONFIG_NETFILTER */ |
| 5082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 5084 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | int err; |
| 5086 | |
Eric Paris | 200ac53 | 2009-02-12 15:01:04 -0500 | [diff] [blame] | 5087 | err = cap_netlink_send(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | if (err) |
| 5089 | return err; |
| 5090 | |
Stephen Smalley | 941fc5b | 2009-10-01 14:48:23 -0400 | [diff] [blame] | 5091 | return selinux_nlmsg_perm(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | } |
| 5093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | static int ipc_alloc_security(struct task_struct *task, |
| 5095 | struct kern_ipc_perm *perm, |
| 5096 | u16 sclass) |
| 5097 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | struct ipc_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5099 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 5101 | isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | if (!isec) |
| 5103 | return -ENOMEM; |
| 5104 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5105 | sid = task_sid(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | isec->sclass = sclass; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5107 | isec->sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | perm->security = isec; |
| 5109 | |
| 5110 | return 0; |
| 5111 | } |
| 5112 | |
| 5113 | static void ipc_free_security(struct kern_ipc_perm *perm) |
| 5114 | { |
| 5115 | struct ipc_security_struct *isec = perm->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | perm->security = NULL; |
| 5117 | kfree(isec); |
| 5118 | } |
| 5119 | |
| 5120 | static int msg_msg_alloc_security(struct msg_msg *msg) |
| 5121 | { |
| 5122 | struct msg_security_struct *msec; |
| 5123 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 5124 | msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | if (!msec) |
| 5126 | return -ENOMEM; |
| 5127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | msec->sid = SECINITSID_UNLABELED; |
| 5129 | msg->security = msec; |
| 5130 | |
| 5131 | return 0; |
| 5132 | } |
| 5133 | |
| 5134 | static void msg_msg_free_security(struct msg_msg *msg) |
| 5135 | { |
| 5136 | struct msg_security_struct *msec = msg->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | |
| 5138 | msg->security = NULL; |
| 5139 | kfree(msec); |
| 5140 | } |
| 5141 | |
| 5142 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5143 | u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5146 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5147 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5148 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | isec = ipc_perms->security; |
| 5151 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5152 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5153 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | ad.u.ipc_id = ipc_perms->key; |
| 5155 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5156 | return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | static int selinux_msg_msg_alloc_security(struct msg_msg *msg) |
| 5160 | { |
| 5161 | return msg_msg_alloc_security(msg); |
| 5162 | } |
| 5163 | |
| 5164 | static void selinux_msg_msg_free_security(struct msg_msg *msg) |
| 5165 | { |
| 5166 | msg_msg_free_security(msg); |
| 5167 | } |
| 5168 | |
| 5169 | /* message queue security operations */ |
| 5170 | static int selinux_msg_queue_alloc_security(struct msg_queue *msq) |
| 5171 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5173 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5174 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5175 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | int rc; |
| 5177 | |
| 5178 | rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); |
| 5179 | if (rc) |
| 5180 | return rc; |
| 5181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | isec = msq->q_perm.security; |
| 5183 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5184 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5185 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5186 | ad.u.ipc_id = msq->q_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5188 | rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | MSGQ__CREATE, &ad); |
| 5190 | if (rc) { |
| 5191 | ipc_free_security(&msq->q_perm); |
| 5192 | return rc; |
| 5193 | } |
| 5194 | return 0; |
| 5195 | } |
| 5196 | |
| 5197 | static void selinux_msg_queue_free_security(struct msg_queue *msq) |
| 5198 | { |
| 5199 | ipc_free_security(&msq->q_perm); |
| 5200 | } |
| 5201 | |
| 5202 | static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) |
| 5203 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5205 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5206 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5207 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | isec = msq->q_perm.security; |
| 5210 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5211 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5212 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | ad.u.ipc_id = msq->q_perm.key; |
| 5214 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5215 | return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | MSGQ__ASSOCIATE, &ad); |
| 5217 | } |
| 5218 | |
| 5219 | static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) |
| 5220 | { |
| 5221 | int err; |
| 5222 | int perms; |
| 5223 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5224 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | case IPC_INFO: |
| 5226 | case MSG_INFO: |
| 5227 | /* No specific object, just general system-wide information. */ |
| 5228 | return task_has_system(current, SYSTEM__IPC_INFO); |
| 5229 | case IPC_STAT: |
| 5230 | case MSG_STAT: |
| 5231 | perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; |
| 5232 | break; |
| 5233 | case IPC_SET: |
| 5234 | perms = MSGQ__SETATTR; |
| 5235 | break; |
| 5236 | case IPC_RMID: |
| 5237 | perms = MSGQ__DESTROY; |
| 5238 | break; |
| 5239 | default: |
| 5240 | return 0; |
| 5241 | } |
| 5242 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5243 | err = ipc_has_perm(&msq->q_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | return err; |
| 5245 | } |
| 5246 | |
| 5247 | static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) |
| 5248 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | struct ipc_security_struct *isec; |
| 5250 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5251 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5252 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5253 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | int rc; |
| 5255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | isec = msq->q_perm.security; |
| 5257 | msec = msg->security; |
| 5258 | |
| 5259 | /* |
| 5260 | * First time through, need to assign label to the message |
| 5261 | */ |
| 5262 | if (msec->sid == SECINITSID_UNLABELED) { |
| 5263 | /* |
| 5264 | * Compute new sid based on current process and |
| 5265 | * message queue this message will be stored in |
| 5266 | */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5267 | rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 5268 | NULL, &msec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | if (rc) |
| 5270 | return rc; |
| 5271 | } |
| 5272 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5273 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5274 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | ad.u.ipc_id = msq->q_perm.key; |
| 5276 | |
| 5277 | /* Can this process write to the queue? */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5278 | rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | MSGQ__WRITE, &ad); |
| 5280 | if (!rc) |
| 5281 | /* Can this process send the message */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5282 | rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, |
| 5283 | MSG__SEND, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | if (!rc) |
| 5285 | /* Can the message be put in the queue? */ |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5286 | rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, |
| 5287 | MSGQ__ENQUEUE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | |
| 5289 | return rc; |
| 5290 | } |
| 5291 | |
| 5292 | static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, |
| 5293 | struct task_struct *target, |
| 5294 | long type, int mode) |
| 5295 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | struct ipc_security_struct *isec; |
| 5297 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5298 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5299 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5300 | u32 sid = task_sid(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | int rc; |
| 5302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | isec = msq->q_perm.security; |
| 5304 | msec = msg->security; |
| 5305 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5306 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5307 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5308 | ad.u.ipc_id = msq->q_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5310 | rc = avc_has_perm(sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | SECCLASS_MSGQ, MSGQ__READ, &ad); |
| 5312 | if (!rc) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5313 | rc = avc_has_perm(sid, msec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | SECCLASS_MSG, MSG__RECEIVE, &ad); |
| 5315 | return rc; |
| 5316 | } |
| 5317 | |
| 5318 | /* Shared Memory security operations */ |
| 5319 | static int selinux_shm_alloc_security(struct shmid_kernel *shp) |
| 5320 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5322 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5323 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5324 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | int rc; |
| 5326 | |
| 5327 | rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); |
| 5328 | if (rc) |
| 5329 | return rc; |
| 5330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | isec = shp->shm_perm.security; |
| 5332 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5333 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5334 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5335 | ad.u.ipc_id = shp->shm_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5337 | rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | SHM__CREATE, &ad); |
| 5339 | if (rc) { |
| 5340 | ipc_free_security(&shp->shm_perm); |
| 5341 | return rc; |
| 5342 | } |
| 5343 | return 0; |
| 5344 | } |
| 5345 | |
| 5346 | static void selinux_shm_free_security(struct shmid_kernel *shp) |
| 5347 | { |
| 5348 | ipc_free_security(&shp->shm_perm); |
| 5349 | } |
| 5350 | |
| 5351 | static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) |
| 5352 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5354 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5355 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5356 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | isec = shp->shm_perm.security; |
| 5359 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5360 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5361 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | ad.u.ipc_id = shp->shm_perm.key; |
| 5363 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5364 | return avc_has_perm(sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | SHM__ASSOCIATE, &ad); |
| 5366 | } |
| 5367 | |
| 5368 | /* Note, at this point, shp is locked down */ |
| 5369 | static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) |
| 5370 | { |
| 5371 | int perms; |
| 5372 | int err; |
| 5373 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5374 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | case IPC_INFO: |
| 5376 | case SHM_INFO: |
| 5377 | /* No specific object, just general system-wide information. */ |
| 5378 | return task_has_system(current, SYSTEM__IPC_INFO); |
| 5379 | case IPC_STAT: |
| 5380 | case SHM_STAT: |
| 5381 | perms = SHM__GETATTR | SHM__ASSOCIATE; |
| 5382 | break; |
| 5383 | case IPC_SET: |
| 5384 | perms = SHM__SETATTR; |
| 5385 | break; |
| 5386 | case SHM_LOCK: |
| 5387 | case SHM_UNLOCK: |
| 5388 | perms = SHM__LOCK; |
| 5389 | break; |
| 5390 | case IPC_RMID: |
| 5391 | perms = SHM__DESTROY; |
| 5392 | break; |
| 5393 | default: |
| 5394 | return 0; |
| 5395 | } |
| 5396 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5397 | err = ipc_has_perm(&shp->shm_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | return err; |
| 5399 | } |
| 5400 | |
| 5401 | static int selinux_shm_shmat(struct shmid_kernel *shp, |
| 5402 | char __user *shmaddr, int shmflg) |
| 5403 | { |
| 5404 | u32 perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | |
| 5406 | if (shmflg & SHM_RDONLY) |
| 5407 | perms = SHM__READ; |
| 5408 | else |
| 5409 | perms = SHM__READ | SHM__WRITE; |
| 5410 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5411 | return ipc_has_perm(&shp->shm_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* Semaphore security operations */ |
| 5415 | static int selinux_sem_alloc_security(struct sem_array *sma) |
| 5416 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5418 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5419 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5420 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | int rc; |
| 5422 | |
| 5423 | rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); |
| 5424 | if (rc) |
| 5425 | return rc; |
| 5426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | isec = sma->sem_perm.security; |
| 5428 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5429 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5430 | ad.selinux_audit_data = &sad; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5431 | ad.u.ipc_id = sma->sem_perm.key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5433 | rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | SEM__CREATE, &ad); |
| 5435 | if (rc) { |
| 5436 | ipc_free_security(&sma->sem_perm); |
| 5437 | return rc; |
| 5438 | } |
| 5439 | return 0; |
| 5440 | } |
| 5441 | |
| 5442 | static void selinux_sem_free_security(struct sem_array *sma) |
| 5443 | { |
| 5444 | ipc_free_security(&sma->sem_perm); |
| 5445 | } |
| 5446 | |
| 5447 | static int selinux_sem_associate(struct sem_array *sma, int semflg) |
| 5448 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5450 | struct common_audit_data ad; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5451 | struct selinux_audit_data sad = {0,}; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5452 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | isec = sma->sem_perm.security; |
| 5455 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5456 | COMMON_AUDIT_DATA_INIT(&ad, IPC); |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 5457 | ad.selinux_audit_data = &sad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | ad.u.ipc_id = sma->sem_perm.key; |
| 5459 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5460 | return avc_has_perm(sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | SEM__ASSOCIATE, &ad); |
| 5462 | } |
| 5463 | |
| 5464 | /* Note, at this point, sma is locked down */ |
| 5465 | static int selinux_sem_semctl(struct sem_array *sma, int cmd) |
| 5466 | { |
| 5467 | int err; |
| 5468 | u32 perms; |
| 5469 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5470 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | case IPC_INFO: |
| 5472 | case SEM_INFO: |
| 5473 | /* No specific object, just general system-wide information. */ |
| 5474 | return task_has_system(current, SYSTEM__IPC_INFO); |
| 5475 | case GETPID: |
| 5476 | case GETNCNT: |
| 5477 | case GETZCNT: |
| 5478 | perms = SEM__GETATTR; |
| 5479 | break; |
| 5480 | case GETVAL: |
| 5481 | case GETALL: |
| 5482 | perms = SEM__READ; |
| 5483 | break; |
| 5484 | case SETVAL: |
| 5485 | case SETALL: |
| 5486 | perms = SEM__WRITE; |
| 5487 | break; |
| 5488 | case IPC_RMID: |
| 5489 | perms = SEM__DESTROY; |
| 5490 | break; |
| 5491 | case IPC_SET: |
| 5492 | perms = SEM__SETATTR; |
| 5493 | break; |
| 5494 | case IPC_STAT: |
| 5495 | case SEM_STAT: |
| 5496 | perms = SEM__GETATTR | SEM__ASSOCIATE; |
| 5497 | break; |
| 5498 | default: |
| 5499 | return 0; |
| 5500 | } |
| 5501 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5502 | err = ipc_has_perm(&sma->sem_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | return err; |
| 5504 | } |
| 5505 | |
| 5506 | static int selinux_sem_semop(struct sem_array *sma, |
| 5507 | struct sembuf *sops, unsigned nsops, int alter) |
| 5508 | { |
| 5509 | u32 perms; |
| 5510 | |
| 5511 | if (alter) |
| 5512 | perms = SEM__READ | SEM__WRITE; |
| 5513 | else |
| 5514 | perms = SEM__READ; |
| 5515 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5516 | return ipc_has_perm(&sma->sem_perm, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) |
| 5520 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | u32 av = 0; |
| 5522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | av = 0; |
| 5524 | if (flag & S_IRUGO) |
| 5525 | av |= IPC__UNIX_READ; |
| 5526 | if (flag & S_IWUGO) |
| 5527 | av |= IPC__UNIX_WRITE; |
| 5528 | |
| 5529 | if (av == 0) |
| 5530 | return 0; |
| 5531 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5532 | return ipc_has_perm(ipcp, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | } |
| 5534 | |
Ahmed S. Darwish | 713a04a | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 5535 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 5536 | { |
| 5537 | struct ipc_security_struct *isec = ipcp->security; |
| 5538 | *secid = isec->sid; |
| 5539 | } |
| 5540 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5541 | static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | { |
| 5543 | if (inode) |
| 5544 | inode_doinit_with_dentry(inode, dentry); |
| 5545 | } |
| 5546 | |
| 5547 | static int selinux_getprocattr(struct task_struct *p, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 5548 | char *name, char **value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5550 | const struct task_security_struct *__tsec; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 5551 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | int error; |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 5553 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | |
| 5555 | if (current != p) { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5556 | error = current_has_perm(p, PROCESS__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | if (error) |
| 5558 | return error; |
| 5559 | } |
| 5560 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5561 | rcu_read_lock(); |
| 5562 | __tsec = __task_cred(p)->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | |
| 5564 | if (!strcmp(name, "current")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5565 | sid = __tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | else if (!strcmp(name, "prev")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5567 | sid = __tsec->osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | else if (!strcmp(name, "exec")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5569 | sid = __tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | else if (!strcmp(name, "fscreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5571 | sid = __tsec->create_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5572 | else if (!strcmp(name, "keycreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5573 | sid = __tsec->keycreate_sid; |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 5574 | else if (!strcmp(name, "sockcreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5575 | sid = __tsec->sockcreate_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | else |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5577 | goto invalid; |
| 5578 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | |
| 5580 | if (!sid) |
| 5581 | return 0; |
| 5582 | |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 5583 | error = security_sid_to_context(sid, value, &len); |
| 5584 | if (error) |
| 5585 | return error; |
| 5586 | return len; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5587 | |
| 5588 | invalid: |
| 5589 | rcu_read_unlock(); |
| 5590 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | static int selinux_setprocattr(struct task_struct *p, |
| 5594 | char *name, void *value, size_t size) |
| 5595 | { |
| 5596 | struct task_security_struct *tsec; |
Roland McGrath | 0356357 | 2008-03-26 15:46:39 -0700 | [diff] [blame] | 5597 | struct task_struct *tracer; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5598 | struct cred *new; |
| 5599 | u32 sid = 0, ptsid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | int error; |
| 5601 | char *str = value; |
| 5602 | |
| 5603 | if (current != p) { |
| 5604 | /* SELinux only allows a process to change its own |
| 5605 | security attributes. */ |
| 5606 | return -EACCES; |
| 5607 | } |
| 5608 | |
| 5609 | /* |
| 5610 | * Basic control over ability to set these attributes at all. |
| 5611 | * current == p, but we'll pass them separately in case the |
| 5612 | * above restriction is ever removed. |
| 5613 | */ |
| 5614 | if (!strcmp(name, "exec")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5615 | error = current_has_perm(p, PROCESS__SETEXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | else if (!strcmp(name, "fscreate")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5617 | error = current_has_perm(p, PROCESS__SETFSCREATE); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5618 | else if (!strcmp(name, "keycreate")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5619 | error = current_has_perm(p, PROCESS__SETKEYCREATE); |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 5620 | else if (!strcmp(name, "sockcreate")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5621 | error = current_has_perm(p, PROCESS__SETSOCKCREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | else if (!strcmp(name, "current")) |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 5623 | error = current_has_perm(p, PROCESS__SETCURRENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | else |
| 5625 | error = -EINVAL; |
| 5626 | if (error) |
| 5627 | return error; |
| 5628 | |
| 5629 | /* Obtain a SID for the context, if one was specified. */ |
Stephen Smalley | e44b57e | 2017-01-31 11:54:04 -0500 | [diff] [blame] | 5630 | if (size && str[0] && str[0] != '\n') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | if (str[size-1] == '\n') { |
| 5632 | str[size-1] = 0; |
| 5633 | size--; |
| 5634 | } |
| 5635 | error = security_context_to_sid(value, size, &sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 5636 | if (error == -EINVAL && !strcmp(name, "fscreate")) { |
| 5637 | if (!capable(CAP_MAC_ADMIN)) |
| 5638 | return error; |
| 5639 | error = security_context_to_sid_force(value, size, |
| 5640 | &sid); |
| 5641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | if (error) |
| 5643 | return error; |
| 5644 | } |
| 5645 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5646 | new = prepare_creds(); |
| 5647 | if (!new) |
| 5648 | return -ENOMEM; |
| 5649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | /* Permission checking based on the specified context is |
| 5651 | performed during the actual operation (execve, |
| 5652 | open/mkdir/...), when we know the full context of the |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5653 | operation. See selinux_bprm_set_creds for the execve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | checks and may_create for the file creation checks. The |
| 5655 | operation will then fail if the context is not permitted. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5656 | tsec = new->security; |
| 5657 | if (!strcmp(name, "exec")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | tsec->exec_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5659 | } else if (!strcmp(name, "fscreate")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | tsec->create_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5661 | } else if (!strcmp(name, "keycreate")) { |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5662 | error = may_create_key(sid, p); |
| 5663 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5664 | goto abort_change; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5665 | tsec->keycreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5666 | } else if (!strcmp(name, "sockcreate")) { |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 5667 | tsec->sockcreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5668 | } else if (!strcmp(name, "current")) { |
| 5669 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | if (sid == 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5671 | goto abort_change; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 5672 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5673 | /* Only allow single threaded processes to change context */ |
| 5674 | error = -EPERM; |
Oleg Nesterov | 5bb459b | 2009-07-10 03:48:23 +0200 | [diff] [blame] | 5675 | if (!current_is_single_threaded()) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5676 | error = security_bounded_transition(tsec->sid, sid); |
| 5677 | if (error) |
| 5678 | goto abort_change; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | |
| 5681 | /* Check permissions for the transition. */ |
| 5682 | error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5683 | PROCESS__DYNTRANSITION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5685 | goto abort_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | |
| 5687 | /* Check for ptracing, and update the task SID if ok. |
| 5688 | Otherwise, leave SID unchanged and fail. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5689 | ptsid = 0; |
Oleg Nesterov | cdeb0b0 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 5690 | rcu_read_lock(); |
Tejun Heo | 06d9847 | 2011-06-17 16:50:40 +0200 | [diff] [blame] | 5691 | tracer = ptrace_parent(p); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5692 | if (tracer) |
| 5693 | ptsid = task_sid(tracer); |
Oleg Nesterov | cdeb0b0 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 5694 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5696 | if (tracer) { |
| 5697 | error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, |
| 5698 | PROCESS__PTRACE, NULL); |
| 5699 | if (error) |
| 5700 | goto abort_change; |
| 5701 | } |
| 5702 | |
| 5703 | tsec->sid = sid; |
| 5704 | } else { |
| 5705 | error = -EINVAL; |
| 5706 | goto abort_change; |
| 5707 | } |
| 5708 | |
| 5709 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | return size; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5711 | |
| 5712 | abort_change: |
| 5713 | abort_creds(new); |
| 5714 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | } |
| 5716 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5717 | static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 5718 | { |
| 5719 | return security_sid_to_context(secid, secdata, seclen); |
| 5720 | } |
| 5721 | |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 5722 | 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] | 5723 | { |
| 5724 | return security_context_to_sid(secdata, seclen, secid); |
| 5725 | } |
| 5726 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5727 | static void selinux_release_secctx(char *secdata, u32 seclen) |
| 5728 | { |
Paul Moore | 088999e | 2007-08-01 11:12:58 -0400 | [diff] [blame] | 5729 | kfree(secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5730 | } |
| 5731 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 5732 | /* |
| 5733 | * called with inode->i_mutex locked |
| 5734 | */ |
| 5735 | static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 5736 | { |
| 5737 | return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); |
| 5738 | } |
| 5739 | |
| 5740 | /* |
| 5741 | * called with inode->i_mutex locked |
| 5742 | */ |
| 5743 | static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 5744 | { |
| 5745 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); |
| 5746 | } |
| 5747 | |
| 5748 | static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 5749 | { |
| 5750 | int len = 0; |
| 5751 | len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 5752 | ctx, true); |
| 5753 | if (len < 0) |
| 5754 | return len; |
| 5755 | *ctxlen = len; |
| 5756 | return 0; |
| 5757 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5758 | #ifdef CONFIG_KEYS |
| 5759 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5760 | static int selinux_key_alloc(struct key *k, const struct cred *cred, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 5761 | unsigned long flags) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5762 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5763 | const struct task_security_struct *tsec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5764 | struct key_security_struct *ksec; |
| 5765 | |
| 5766 | ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); |
| 5767 | if (!ksec) |
| 5768 | return -ENOMEM; |
| 5769 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5770 | tsec = cred->security; |
| 5771 | if (tsec->keycreate_sid) |
| 5772 | ksec->sid = tsec->keycreate_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 5773 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5774 | ksec->sid = tsec->sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5775 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5776 | k->security = ksec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5777 | return 0; |
| 5778 | } |
| 5779 | |
| 5780 | static void selinux_key_free(struct key *k) |
| 5781 | { |
| 5782 | struct key_security_struct *ksec = k->security; |
| 5783 | |
| 5784 | k->security = NULL; |
| 5785 | kfree(ksec); |
| 5786 | } |
| 5787 | |
| 5788 | static int selinux_key_permission(key_ref_t key_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5789 | const struct cred *cred, |
| 5790 | key_perm_t perm) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5791 | { |
| 5792 | struct key *key; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5793 | struct key_security_struct *ksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5794 | u32 sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5795 | |
| 5796 | /* if no specific permissions are requested, we skip the |
| 5797 | permission check. No serious, additional covert channels |
| 5798 | appear to be created. */ |
| 5799 | if (perm == 0) |
| 5800 | return 0; |
| 5801 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5802 | sid = cred_sid(cred); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5803 | |
| 5804 | key = key_ref_to_ptr(key_ref); |
| 5805 | ksec = key->security; |
| 5806 | |
| 5807 | return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5808 | } |
| 5809 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 5810 | static int selinux_key_getsecurity(struct key *key, char **_buffer) |
| 5811 | { |
| 5812 | struct key_security_struct *ksec = key->security; |
| 5813 | char *context = NULL; |
| 5814 | unsigned len; |
| 5815 | int rc; |
| 5816 | |
| 5817 | rc = security_sid_to_context(ksec->sid, &context, &len); |
| 5818 | if (!rc) |
| 5819 | rc = len; |
| 5820 | *_buffer = context; |
| 5821 | return rc; |
| 5822 | } |
| 5823 | |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 5824 | #endif |
| 5825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | static struct security_operations selinux_ops = { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 5827 | .name = "selinux", |
| 5828 | |
Stephen Smalley | 48a2370 | 2012-11-05 08:15:34 -0500 | [diff] [blame] | 5829 | .binder_set_context_mgr = selinux_binder_set_context_mgr, |
| 5830 | .binder_transaction = selinux_binder_transaction, |
| 5831 | .binder_transfer_binder = selinux_binder_transfer_binder, |
| 5832 | .binder_transfer_file = selinux_binder_transfer_file, |
| 5833 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 5834 | .ptrace_access_check = selinux_ptrace_access_check, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 5835 | .ptrace_traceme = selinux_ptrace_traceme, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | .capget = selinux_capget, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5837 | .capset = selinux_capset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | .capable = selinux_capable, |
| 5839 | .quotactl = selinux_quotactl, |
| 5840 | .quota_on = selinux_quota_on, |
| 5841 | .syslog = selinux_syslog, |
| 5842 | .vm_enough_memory = selinux_vm_enough_memory, |
| 5843 | |
| 5844 | .netlink_send = selinux_netlink_send, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 5846 | .bprm_set_creds = selinux_bprm_set_creds, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 5847 | .bprm_committing_creds = selinux_bprm_committing_creds, |
| 5848 | .bprm_committed_creds = selinux_bprm_committed_creds, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | .bprm_secureexec = selinux_bprm_secureexec, |
| 5850 | |
| 5851 | .sb_alloc_security = selinux_sb_alloc_security, |
| 5852 | .sb_free_security = selinux_sb_free_security, |
| 5853 | .sb_copy_data = selinux_sb_copy_data, |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 5854 | .sb_remount = selinux_sb_remount, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5855 | .sb_kern_mount = selinux_sb_kern_mount, |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 5856 | .sb_show_options = selinux_sb_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | .sb_statfs = selinux_sb_statfs, |
| 5858 | .sb_mount = selinux_mount, |
| 5859 | .sb_umount = selinux_umount, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 5860 | .sb_set_mnt_opts = selinux_set_mnt_opts, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5861 | .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 5862 | .sb_parse_opts_str = selinux_parse_opts_str, |
| 5863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | |
| 5865 | .inode_alloc_security = selinux_inode_alloc_security, |
| 5866 | .inode_free_security = selinux_inode_free_security, |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 5867 | .inode_init_security = selinux_inode_init_security, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | .inode_create = selinux_inode_create, |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 5869 | .inode_post_create = selinux_inode_post_create, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | .inode_link = selinux_inode_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | .inode_unlink = selinux_inode_unlink, |
| 5872 | .inode_symlink = selinux_inode_symlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | .inode_mkdir = selinux_inode_mkdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | .inode_rmdir = selinux_inode_rmdir, |
| 5875 | .inode_mknod = selinux_inode_mknod, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | .inode_rename = selinux_inode_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | .inode_readlink = selinux_inode_readlink, |
| 5878 | .inode_follow_link = selinux_inode_follow_link, |
| 5879 | .inode_permission = selinux_inode_permission, |
| 5880 | .inode_setattr = selinux_inode_setattr, |
| 5881 | .inode_getattr = selinux_inode_getattr, |
| 5882 | .inode_setxattr = selinux_inode_setxattr, |
| 5883 | .inode_post_setxattr = selinux_inode_post_setxattr, |
| 5884 | .inode_getxattr = selinux_inode_getxattr, |
| 5885 | .inode_listxattr = selinux_inode_listxattr, |
| 5886 | .inode_removexattr = selinux_inode_removexattr, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5887 | .inode_getsecurity = selinux_inode_getsecurity, |
| 5888 | .inode_setsecurity = selinux_inode_setsecurity, |
| 5889 | .inode_listsecurity = selinux_inode_listsecurity, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 5890 | .inode_getsecid = selinux_inode_getsecid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
| 5892 | .file_permission = selinux_file_permission, |
| 5893 | .file_alloc_security = selinux_file_alloc_security, |
| 5894 | .file_free_security = selinux_file_free_security, |
| 5895 | .file_ioctl = selinux_file_ioctl, |
| 5896 | .file_mmap = selinux_file_mmap, |
| 5897 | .file_mprotect = selinux_file_mprotect, |
| 5898 | .file_lock = selinux_file_lock, |
| 5899 | .file_fcntl = selinux_file_fcntl, |
| 5900 | .file_set_fowner = selinux_file_set_fowner, |
| 5901 | .file_send_sigiotask = selinux_file_send_sigiotask, |
| 5902 | .file_receive = selinux_file_receive, |
| 5903 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5904 | .dentry_open = selinux_dentry_open, |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 5905 | .file_close = selinux_file_close, |
| 5906 | .allow_merge_bio = selinux_allow_merge_bio, |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 5907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | .task_create = selinux_task_create, |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 5909 | .cred_alloc_blank = selinux_cred_alloc_blank, |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 5910 | .cred_free = selinux_cred_free, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 5911 | .cred_prepare = selinux_cred_prepare, |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 5912 | .cred_transfer = selinux_cred_transfer, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 5913 | .kernel_act_as = selinux_kernel_act_as, |
| 5914 | .kernel_create_files_as = selinux_kernel_create_files_as, |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 5915 | .kernel_module_request = selinux_kernel_module_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | .task_setpgid = selinux_task_setpgid, |
| 5917 | .task_getpgid = selinux_task_getpgid, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5918 | .task_getsid = selinux_task_getsid, |
David Quigley | f9008e4 | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 5919 | .task_getsecid = selinux_task_getsecid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | .task_setnice = selinux_task_setnice, |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 5921 | .task_setioprio = selinux_task_setioprio, |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 5922 | .task_getioprio = selinux_task_getioprio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | .task_setrlimit = selinux_task_setrlimit, |
| 5924 | .task_setscheduler = selinux_task_setscheduler, |
| 5925 | .task_getscheduler = selinux_task_getscheduler, |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 5926 | .task_movememory = selinux_task_movememory, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | .task_kill = selinux_task_kill, |
| 5928 | .task_wait = selinux_task_wait, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5929 | .task_to_inode = selinux_task_to_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | |
| 5931 | .ipc_permission = selinux_ipc_permission, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 5932 | .ipc_getsecid = selinux_ipc_getsecid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | |
| 5934 | .msg_msg_alloc_security = selinux_msg_msg_alloc_security, |
| 5935 | .msg_msg_free_security = selinux_msg_msg_free_security, |
| 5936 | |
| 5937 | .msg_queue_alloc_security = selinux_msg_queue_alloc_security, |
| 5938 | .msg_queue_free_security = selinux_msg_queue_free_security, |
| 5939 | .msg_queue_associate = selinux_msg_queue_associate, |
| 5940 | .msg_queue_msgctl = selinux_msg_queue_msgctl, |
| 5941 | .msg_queue_msgsnd = selinux_msg_queue_msgsnd, |
| 5942 | .msg_queue_msgrcv = selinux_msg_queue_msgrcv, |
| 5943 | |
| 5944 | .shm_alloc_security = selinux_shm_alloc_security, |
| 5945 | .shm_free_security = selinux_shm_free_security, |
| 5946 | .shm_associate = selinux_shm_associate, |
| 5947 | .shm_shmctl = selinux_shm_shmctl, |
| 5948 | .shm_shmat = selinux_shm_shmat, |
| 5949 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5950 | .sem_alloc_security = selinux_sem_alloc_security, |
| 5951 | .sem_free_security = selinux_sem_free_security, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | .sem_associate = selinux_sem_associate, |
| 5953 | .sem_semctl = selinux_sem_semctl, |
| 5954 | .sem_semop = selinux_sem_semop, |
| 5955 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5956 | .d_instantiate = selinux_d_instantiate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5958 | .getprocattr = selinux_getprocattr, |
| 5959 | .setprocattr = selinux_setprocattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5961 | .secid_to_secctx = selinux_secid_to_secctx, |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 5962 | .secctx_to_secid = selinux_secctx_to_secid, |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5963 | .release_secctx = selinux_release_secctx, |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 5964 | .inode_notifysecctx = selinux_inode_notifysecctx, |
| 5965 | .inode_setsecctx = selinux_inode_setsecctx, |
| 5966 | .inode_getsecctx = selinux_inode_getsecctx, |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5967 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5968 | .unix_stream_connect = selinux_socket_unix_stream_connect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | .unix_may_send = selinux_socket_unix_may_send, |
| 5970 | |
| 5971 | .socket_create = selinux_socket_create, |
| 5972 | .socket_post_create = selinux_socket_post_create, |
| 5973 | .socket_bind = selinux_socket_bind, |
| 5974 | .socket_connect = selinux_socket_connect, |
| 5975 | .socket_listen = selinux_socket_listen, |
| 5976 | .socket_accept = selinux_socket_accept, |
| 5977 | .socket_sendmsg = selinux_socket_sendmsg, |
| 5978 | .socket_recvmsg = selinux_socket_recvmsg, |
| 5979 | .socket_getsockname = selinux_socket_getsockname, |
| 5980 | .socket_getpeername = selinux_socket_getpeername, |
| 5981 | .socket_getsockopt = selinux_socket_getsockopt, |
| 5982 | .socket_setsockopt = selinux_socket_setsockopt, |
| 5983 | .socket_shutdown = selinux_socket_shutdown, |
| 5984 | .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5985 | .socket_getpeersec_stream = selinux_socket_getpeersec_stream, |
| 5986 | .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | .sk_alloc_security = selinux_sk_alloc_security, |
| 5988 | .sk_free_security = selinux_sk_free_security, |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5989 | .sk_clone_security = selinux_sk_clone_security, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5990 | .sk_getsecid = selinux_sk_getsecid, |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5991 | .sock_graft = selinux_sock_graft, |
| 5992 | .inet_conn_request = selinux_inet_conn_request, |
| 5993 | .inet_csk_clone = selinux_inet_csk_clone, |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5994 | .inet_conn_established = selinux_inet_conn_established, |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5995 | .secmark_relabel_packet = selinux_secmark_relabel_packet, |
| 5996 | .secmark_refcount_inc = selinux_secmark_refcount_inc, |
| 5997 | .secmark_refcount_dec = selinux_secmark_refcount_dec, |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5998 | .req_classify_flow = selinux_req_classify_flow, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5999 | .tun_dev_create = selinux_tun_dev_create, |
| 6000 | .tun_dev_post_create = selinux_tun_dev_post_create, |
| 6001 | .tun_dev_attach = selinux_tun_dev_attach, |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 6002 | |
| 6003 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 6004 | .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, |
| 6005 | .xfrm_policy_clone_security = selinux_xfrm_policy_clone, |
| 6006 | .xfrm_policy_free_security = selinux_xfrm_policy_free, |
Catherine Zhang | c8c05a8 | 2006-06-08 23:39:49 -0700 | [diff] [blame] | 6007 | .xfrm_policy_delete_security = selinux_xfrm_policy_delete, |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 6008 | .xfrm_state_alloc_security = selinux_xfrm_state_alloc, |
| 6009 | .xfrm_state_free_security = selinux_xfrm_state_free, |
Catherine Zhang | c8c05a8 | 2006-06-08 23:39:49 -0700 | [diff] [blame] | 6010 | .xfrm_state_delete_security = selinux_xfrm_state_delete, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6011 | .xfrm_policy_lookup = selinux_xfrm_policy_lookup, |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 6012 | .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 6013 | .xfrm_decode_session = selinux_xfrm_decode_session, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6015 | |
| 6016 | #ifdef CONFIG_KEYS |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6017 | .key_alloc = selinux_key_alloc, |
| 6018 | .key_free = selinux_key_free, |
| 6019 | .key_permission = selinux_key_permission, |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6020 | .key_getsecurity = selinux_key_getsecurity, |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6021 | #endif |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6022 | |
| 6023 | #ifdef CONFIG_AUDIT |
| 6024 | .audit_rule_init = selinux_audit_rule_init, |
| 6025 | .audit_rule_known = selinux_audit_rule_known, |
| 6026 | .audit_rule_match = selinux_audit_rule_match, |
| 6027 | .audit_rule_free = selinux_audit_rule_free, |
| 6028 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | }; |
| 6030 | |
| 6031 | static __init int selinux_init(void) |
| 6032 | { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6033 | if (!security_module_enable(&selinux_ops)) { |
| 6034 | selinux_enabled = 0; |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | if (!selinux_enabled) { |
| 6039 | printk(KERN_INFO "SELinux: Disabled at boot.\n"); |
| 6040 | return 0; |
| 6041 | } |
| 6042 | |
| 6043 | printk(KERN_INFO "SELinux: Initializing.\n"); |
| 6044 | |
| 6045 | /* Set the security state for the initial task. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6046 | cred_init_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 6048 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 6049 | |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 6050 | sel_inode_cache = kmem_cache_create("selinux_inode_security", |
| 6051 | sizeof(struct inode_security_struct), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 6052 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | avc_init(); |
| 6054 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6055 | if (register_security(&selinux_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | panic("SELinux: Unable to register with kernel.\n"); |
| 6057 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6058 | if (selinux_enforcing) |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 6059 | printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6060 | else |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 6061 | printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | return 0; |
| 6064 | } |
| 6065 | |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 6066 | static void delayed_superblock_init(struct super_block *sb, void *unused) |
| 6067 | { |
| 6068 | superblock_doinit(sb, NULL); |
| 6069 | } |
| 6070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | void selinux_complete_init(void) |
| 6072 | { |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 6073 | printk(KERN_DEBUG "SELinux: Completing initialization.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | |
| 6075 | /* Set up any superblocks initialized prior to the policy load. */ |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 6076 | printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 6077 | iterate_supers(delayed_superblock_init, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | /* SELinux requires early initialization in order to label |
| 6081 | all processes and objects when they are created. */ |
| 6082 | security_initcall(selinux_init); |
| 6083 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 6084 | #if defined(CONFIG_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 6086 | static struct nf_hook_ops selinux_ipv4_ops[] = { |
| 6087 | { |
| 6088 | .hook = selinux_ipv4_postroute, |
| 6089 | .owner = THIS_MODULE, |
| 6090 | .pf = PF_INET, |
| 6091 | .hooknum = NF_INET_POST_ROUTING, |
| 6092 | .priority = NF_IP_PRI_SELINUX_LAST, |
| 6093 | }, |
| 6094 | { |
| 6095 | .hook = selinux_ipv4_forward, |
| 6096 | .owner = THIS_MODULE, |
| 6097 | .pf = PF_INET, |
| 6098 | .hooknum = NF_INET_FORWARD, |
| 6099 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 6100 | }, |
| 6101 | { |
| 6102 | .hook = selinux_ipv4_output, |
| 6103 | .owner = THIS_MODULE, |
| 6104 | .pf = PF_INET, |
| 6105 | .hooknum = NF_INET_LOCAL_OUT, |
| 6106 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 6107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | }; |
| 6109 | |
| 6110 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 6111 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 6112 | static struct nf_hook_ops selinux_ipv6_ops[] = { |
| 6113 | { |
| 6114 | .hook = selinux_ipv6_postroute, |
| 6115 | .owner = THIS_MODULE, |
| 6116 | .pf = PF_INET6, |
| 6117 | .hooknum = NF_INET_POST_ROUTING, |
| 6118 | .priority = NF_IP6_PRI_SELINUX_LAST, |
| 6119 | }, |
| 6120 | { |
| 6121 | .hook = selinux_ipv6_forward, |
| 6122 | .owner = THIS_MODULE, |
| 6123 | .pf = PF_INET6, |
| 6124 | .hooknum = NF_INET_FORWARD, |
| 6125 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 6126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | }; |
| 6128 | |
| 6129 | #endif /* IPV6 */ |
| 6130 | |
| 6131 | static int __init selinux_nf_ip_init(void) |
| 6132 | { |
| 6133 | int err = 0; |
| 6134 | |
| 6135 | if (!selinux_enabled) |
| 6136 | goto out; |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 6137 | |
| 6138 | printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); |
| 6139 | |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 6140 | err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); |
| 6141 | if (err) |
| 6142 | panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | |
| 6144 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 6145 | err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); |
| 6146 | if (err) |
| 6147 | panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | #endif /* IPV6 */ |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 6149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | out: |
| 6151 | return err; |
| 6152 | } |
| 6153 | |
| 6154 | __initcall(selinux_nf_ip_init); |
| 6155 | |
| 6156 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 6157 | static void selinux_nf_ip_exit(void) |
| 6158 | { |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 6159 | printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 6161 | nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 6163 | nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | #endif /* IPV6 */ |
| 6165 | } |
| 6166 | #endif |
| 6167 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 6168 | #else /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | |
| 6170 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 6171 | #define selinux_nf_ip_exit() |
| 6172 | #endif |
| 6173 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 6174 | #endif /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | |
| 6176 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6177 | static int selinux_disabled; |
| 6178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | int selinux_disable(void) |
| 6180 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | if (ss_initialized) { |
| 6182 | /* Not permitted after initial policy load. */ |
| 6183 | return -EINVAL; |
| 6184 | } |
| 6185 | |
| 6186 | if (selinux_disabled) { |
| 6187 | /* Only do this once. */ |
| 6188 | return -EINVAL; |
| 6189 | } |
| 6190 | |
| 6191 | printk(KERN_INFO "SELinux: Disabled at runtime.\n"); |
| 6192 | |
| 6193 | selinux_disabled = 1; |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 6194 | selinux_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | |
wzt.wzt@gmail.com | 189b3b1 | 2010-02-23 23:15:28 +0800 | [diff] [blame] | 6196 | reset_security_ops(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | |
Eric Paris | af8ff04 | 2009-09-20 21:23:01 -0400 | [diff] [blame] | 6198 | /* Try to destroy the avc node cache */ |
| 6199 | avc_disable(); |
| 6200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6201 | /* Unregister netfilter hooks. */ |
| 6202 | selinux_nf_ip_exit(); |
| 6203 | |
| 6204 | /* Unregister selinuxfs. */ |
| 6205 | exit_sel_fs(); |
| 6206 | |
| 6207 | return 0; |
| 6208 | } |
| 6209 | #endif |