blob: 57a7b362c86d9307fd6a8c96cdf233014102855f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
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 Paris828dfe12008-04-17 13:17:49 -040023 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050027#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070029#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 Paris0b24dcb2011-02-25 15:39:20 -050040#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050044#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040046#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
48#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/netfilter_ipv4.h>
50#include <linux/netfilter_ipv6.h>
51#include <linux/tty.h>
52#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070053#include <net/ip.h> /* for local_port_range[] */
Eric Dumazetca10b9e2013-04-08 17:58:11 +000054#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore216c4a72013-12-04 16:10:45 -050056#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050057#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050058#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/bitops.h>
63#include <linux/interrupt.h>
64#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000065#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/tcp.h>
67#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080068#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/quota.h>
70#include <linux/un.h> /* for Unix socket types */
71#include <net/af_unix.h> /* for Unix socket types */
72#include <linux/parser.h>
73#include <linux/nfs_mount.h>
74#include <net/ipv6.h>
75#include <linux/hugetlb.h>
76#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070078#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070079#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070080#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070081#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080082#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070083#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040084#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000085#include <linux/msg.h>
86#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include "avc.h"
89#include "objsec.h"
90#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050091#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040092#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080093#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050094#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020095#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100096#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David P. Quigley11689d42009-01-16 09:22:03 -050098#define NUM_SEL_MNT_OPTS 5
Eric Parisc9180a52007-11-30 13:00:35 -050099
James Morris20510f22007-10-16 23:31:32 -0700100extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Paul Moored621d352008-01-29 08:43:36 -0500102/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000103static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400106int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static int __init enforcing_setup(char *str)
109{
Eric Parisf5269712008-05-14 11:27:45 -0400110 unsigned long enforcing;
111 if (!strict_strtoul(str, 0, &enforcing))
112 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 1;
114}
115__setup("enforcing=", enforcing_setup);
116#endif
117
118#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
119int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
120
121static int __init selinux_enabled_setup(char *str)
122{
Eric Parisf5269712008-05-14 11:27:45 -0400123 unsigned long enabled;
124 if (!strict_strtoul(str, 0, &enabled))
125 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 1;
127}
128__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400129#else
130int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#endif
132
Christoph Lametere18b8902006-12-06 20:33:20 -0800133static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800134
Paul Moored621d352008-01-29 08:43:36 -0500135/**
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 */
145static int selinux_secmark_enabled(void)
146{
147 return (atomic_read(&selinux_secmark_refcount) > 0);
148}
149
David Howellsd84f4f92008-11-14 10:39:23 +1100150/*
151 * initialise the security for the init task
152 */
153static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
David Howells3b11a1d2008-11-14 10:39:26 +1100155 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct task_security_struct *tsec;
157
James Morris89d155e2005-10-30 14:59:21 -0800158 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100160 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
David Howellsd84f4f92008-11-14 10:39:23 +1100162 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100163 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
David Howells275bb412008-11-14 10:39:19 +1100166/*
David Howells88e67f32008-11-14 10:39:21 +1100167 * get the security ID of a set of credentials
168 */
169static 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 Howells3b11a1d2008-11-14 10:39:26 +1100178 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100179 */
180static inline u32 task_sid(const struct task_struct *task)
181{
David Howells275bb412008-11-14 10:39:19 +1100182 u32 sid;
183
184 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100185 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100186 rcu_read_unlock();
187 return sid;
188}
189
190/*
David Howells3b11a1d2008-11-14 10:39:26 +1100191 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100192 */
193static inline u32 current_sid(void)
194{
Paul Moore5fb49872010-04-22 14:46:19 -0400195 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100196
197 return tsec->sid;
198}
199
David Howells88e67f32008-11-14 10:39:21 +1100200/* Allocate and free functions for each kind of security blob. */
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int inode_alloc_security(struct inode *inode)
203{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100205 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Josef Bacika02fe132008-04-04 09:35:05 +1100207 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!isec)
209 return -ENOMEM;
210
Eric Paris23970742006-09-25 23:32:01 -0700211 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 isec->inode = inode;
214 isec->sid = SECINITSID_UNLABELED;
215 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100216 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 inode->i_security = isec;
218
219 return 0;
220}
221
222static void inode_free_security(struct inode *inode)
223{
224 struct inode_security_struct *isec = inode->i_security;
225 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_lock(&sbsec->isec_lock);
228 if (!list_empty(&isec->list))
229 list_del_init(&isec->list);
230 spin_unlock(&sbsec->isec_lock);
231
232 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800233 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static int file_alloc_security(struct file *file)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100239 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800241 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!fsec)
243 return -ENOMEM;
244
David Howells275bb412008-11-14 10:39:19 +1100245 fsec->sid = sid;
246 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 file->f_security = fsec;
248
249 return 0;
250}
251
252static void file_free_security(struct file *file)
253{
254 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 file->f_security = NULL;
256 kfree(fsec);
257}
258
259static int superblock_alloc_security(struct super_block *sb)
260{
261 struct superblock_security_struct *sbsec;
262
James Morris89d155e2005-10-30 14:59:21 -0800263 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!sbsec)
265 return -ENOMEM;
266
Eric Parisbc7e9822006-09-25 23:32:02 -0700267 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 INIT_LIST_HEAD(&sbsec->isec_head);
269 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 sbsec->sb = sb;
271 sbsec->sid = SECINITSID_UNLABELED;
272 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700273 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 sb->s_security = sbsec;
275
276 return 0;
277}
278
279static void superblock_free_security(struct super_block *sb)
280{
281 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 sb->s_security = NULL;
283 kfree(sbsec);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/* The file system's label must be initialized prior to use. */
287
Stephen Hemminger634a5392010-03-04 21:59:03 -0800288static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 "uses xattr",
290 "uses transition SIDs",
291 "uses task SIDs",
292 "uses genfs_contexts",
293 "not configured for labeling",
294 "uses mountpoint labeling",
295};
296
297static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
298
299static inline int inode_doinit(struct inode *inode)
300{
301 return inode_doinit_with_dentry(inode, NULL);
302}
303
304enum {
Eric Paris31e87932007-09-19 17:19:12 -0400305 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 Opt_context = 1,
307 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500308 Opt_defcontext = 3,
309 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500310 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Steven Whitehousea447c092008-10-13 10:46:57 +0100313static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400314 {Opt_context, CONTEXT_STR "%s"},
315 {Opt_fscontext, FSCONTEXT_STR "%s"},
316 {Opt_defcontext, DEFCONTEXT_STR "%s"},
317 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500318 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400319 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
322#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
323
Eric Parisc312feb2006-07-10 04:43:53 -0700324static int may_context_mount_sb_relabel(u32 sid,
325 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100326 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700327{
David Howells275bb412008-11-14 10:39:19 +1100328 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700329 int rc;
330
331 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
332 FILESYSTEM__RELABELFROM, NULL);
333 if (rc)
334 return rc;
335
336 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
337 FILESYSTEM__RELABELTO, NULL);
338 return rc;
339}
340
Eric Paris08089252006-07-10 04:43:55 -0700341static int may_context_mount_inode_relabel(u32 sid,
342 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100343 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700344{
David Howells275bb412008-11-14 10:39:19 +1100345 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700346 int rc;
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(sid, sbsec->sid, SECCLASS_FILESYSTEM,
353 FILESYSTEM__ASSOCIATE, NULL);
354 return rc;
355}
356
Eric Parisc9180a52007-11-30 13:00:35 -0500357static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct superblock_security_struct *sbsec = sb->s_security;
360 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500361 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int rc = 0;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
365 /* Make sure that the xattr handler exists and that no
366 error other than -ENODATA is returned by getxattr on
367 the root directory. -ENODATA is ok, as this may be
368 the first boot of the SELinux kernel before we have
369 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500370 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
372 "xattr support\n", sb->s_id, sb->s_type->name);
373 rc = -EOPNOTSUPP;
374 goto out;
375 }
Eric Parisc9180a52007-11-30 13:00:35 -0500376 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (rc < 0 && rc != -ENODATA) {
378 if (rc == -EOPNOTSUPP)
379 printk(KERN_WARNING "SELinux: (dev %s, type "
380 "%s) has no security xattr handler\n",
381 sb->s_id, sb->s_type->name);
382 else
383 printk(KERN_WARNING "SELinux: (dev %s, type "
384 "%s) getxattr errno %d\n", sb->s_id,
385 sb->s_type->name, -rc);
386 goto out;
387 }
388 }
389
David P. Quigley11689d42009-01-16 09:22:03 -0500390 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Eric Parisc9180a52007-11-30 13:00:35 -0500392 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500393 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500395 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500396 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sb->s_id, sb->s_type->name,
398 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
David P. Quigley11689d42009-01-16 09:22:03 -0500400 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
401 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
402 sbsec->behavior == SECURITY_FS_USE_NONE ||
403 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
404 sbsec->flags &= ~SE_SBLABELSUPP;
405
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400406 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
407 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
408 sbsec->flags |= SE_SBLABELSUPP;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500411 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Initialize any other inodes associated with the superblock, e.g.
414 inodes created prior to initial policy load or inodes created
415 during get_sb by a pseudo filesystem that directly
416 populates itself. */
417 spin_lock(&sbsec->isec_lock);
418next_inode:
419 if (!list_empty(&sbsec->isec_head)) {
420 struct inode_security_struct *isec =
421 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500422 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct inode *inode = isec->inode;
424 spin_unlock(&sbsec->isec_lock);
425 inode = igrab(inode);
426 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500427 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 inode_doinit(inode);
429 iput(inode);
430 }
431 spin_lock(&sbsec->isec_lock);
432 list_del_init(&isec->list);
433 goto next_inode;
434 }
435 spin_unlock(&sbsec->isec_lock);
436out:
Eric Parisc9180a52007-11-30 13:00:35 -0500437 return rc;
438}
439
440/*
441 * This function should allow an FS to ask what it's mount security
442 * options were so it can use those later for submounts, displaying
443 * mount options, or whatever.
444 */
445static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500446 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500447{
448 int rc = 0, i;
449 struct superblock_security_struct *sbsec = sb->s_security;
450 char *context = NULL;
451 u32 len;
452 char tmp;
453
Eric Parise0007522008-03-05 10:31:54 -0500454 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500455
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500456 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500457 return -EINVAL;
458
459 if (!ss_initialized)
460 return -EINVAL;
461
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500462 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500463 /* count the number of mount options for this sb */
464 for (i = 0; i < 8; i++) {
465 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500466 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500467 tmp >>= 1;
468 }
David P. Quigley11689d42009-01-16 09:22:03 -0500469 /* Check if the Label support flag is set */
470 if (sbsec->flags & SE_SBLABELSUPP)
471 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500472
Eric Parise0007522008-03-05 10:31:54 -0500473 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
474 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500475 rc = -ENOMEM;
476 goto out_free;
477 }
478
Eric Parise0007522008-03-05 10:31:54 -0500479 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
480 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500481 rc = -ENOMEM;
482 goto out_free;
483 }
484
485 i = 0;
486 if (sbsec->flags & FSCONTEXT_MNT) {
487 rc = security_sid_to_context(sbsec->sid, &context, &len);
488 if (rc)
489 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500490 opts->mnt_opts[i] = context;
491 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500492 }
493 if (sbsec->flags & CONTEXT_MNT) {
494 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
495 if (rc)
496 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500497 opts->mnt_opts[i] = context;
498 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500499 }
500 if (sbsec->flags & DEFCONTEXT_MNT) {
501 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
502 if (rc)
503 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500504 opts->mnt_opts[i] = context;
505 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500506 }
507 if (sbsec->flags & ROOTCONTEXT_MNT) {
508 struct inode *root = sbsec->sb->s_root->d_inode;
509 struct inode_security_struct *isec = root->i_security;
510
511 rc = security_sid_to_context(isec->sid, &context, &len);
512 if (rc)
513 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500514 opts->mnt_opts[i] = context;
515 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500516 }
David P. Quigley11689d42009-01-16 09:22:03 -0500517 if (sbsec->flags & SE_SBLABELSUPP) {
518 opts->mnt_opts[i] = NULL;
519 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
520 }
Eric Parisc9180a52007-11-30 13:00:35 -0500521
Eric Parise0007522008-03-05 10:31:54 -0500522 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500523
524 return 0;
525
526out_free:
Eric Parise0007522008-03-05 10:31:54 -0500527 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500528 return rc;
529}
530
531static int bad_option(struct superblock_security_struct *sbsec, char flag,
532 u32 old_sid, u32 new_sid)
533{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500534 char mnt_flags = sbsec->flags & SE_MNTMASK;
535
Eric Parisc9180a52007-11-30 13:00:35 -0500536 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500537 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500538 if (!(sbsec->flags & flag) ||
539 (old_sid != new_sid))
540 return 1;
541
542 /* check if we were passed the same options twice,
543 * aka someone passed context=a,context=b
544 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500545 if (!(sbsec->flags & SE_SBINITIALIZED))
546 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500547 return 1;
548 return 0;
549}
Eric Parise0007522008-03-05 10:31:54 -0500550
Eric Parisc9180a52007-11-30 13:00:35 -0500551/*
552 * Allow filesystems with binary mount data to explicitly set mount point
553 * labeling information.
554 */
Eric Parise0007522008-03-05 10:31:54 -0500555static int selinux_set_mnt_opts(struct super_block *sb,
556 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500557{
David Howells275bb412008-11-14 10:39:19 +1100558 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500559 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500560 struct superblock_security_struct *sbsec = sb->s_security;
561 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000562 struct inode *inode = sbsec->sb->s_root->d_inode;
563 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500564 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
565 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500566 char **mount_options = opts->mnt_opts;
567 int *flags = opts->mnt_opts_flags;
568 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500569
570 mutex_lock(&sbsec->lock);
571
572 if (!ss_initialized) {
573 if (!num_opts) {
574 /* Defer initialization until selinux_complete_init,
575 after the initial policy is loaded and the security
576 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500577 goto out;
578 }
579 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400580 printk(KERN_WARNING "SELinux: Unable to set superblock options "
581 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500582 goto out;
583 }
584
585 /*
Eric Parise0007522008-03-05 10:31:54 -0500586 * Binary mount data FS will come through this function twice. Once
587 * from an explicit call and once from the generic calls from the vfs.
588 * Since the generic VFS calls will not contain any security mount data
589 * we need to skip the double mount verification.
590 *
591 * This does open a hole in which we will not notice if the first
592 * mount using this sb set explict options and a second mount using
593 * this sb does not set any security options. (The first options
594 * will be used for both mounts)
595 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500596 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500597 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400598 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500599
600 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500601 * parse the mount options, check if they are valid sids.
602 * also check if someone is trying to mount the same sb more
603 * than once with different security options.
604 */
605 for (i = 0; i < num_opts; i++) {
606 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500607
608 if (flags[i] == SE_SBLABELSUPP)
609 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500610 rc = security_context_to_sid(mount_options[i],
611 strlen(mount_options[i]), &sid);
612 if (rc) {
613 printk(KERN_WARNING "SELinux: security_context_to_sid"
614 "(%s) failed for (dev %s, type %s) errno=%d\n",
615 mount_options[i], sb->s_id, name, rc);
616 goto out;
617 }
618 switch (flags[i]) {
619 case FSCONTEXT_MNT:
620 fscontext_sid = sid;
621
622 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
623 fscontext_sid))
624 goto out_double_mount;
625
626 sbsec->flags |= FSCONTEXT_MNT;
627 break;
628 case CONTEXT_MNT:
629 context_sid = sid;
630
631 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
632 context_sid))
633 goto out_double_mount;
634
635 sbsec->flags |= CONTEXT_MNT;
636 break;
637 case ROOTCONTEXT_MNT:
638 rootcontext_sid = sid;
639
640 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
641 rootcontext_sid))
642 goto out_double_mount;
643
644 sbsec->flags |= ROOTCONTEXT_MNT;
645
646 break;
647 case DEFCONTEXT_MNT:
648 defcontext_sid = sid;
649
650 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
651 defcontext_sid))
652 goto out_double_mount;
653
654 sbsec->flags |= DEFCONTEXT_MNT;
655
656 break;
657 default:
658 rc = -EINVAL;
659 goto out;
660 }
661 }
662
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500663 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500664 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500665 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500666 goto out_double_mount;
667 rc = 0;
668 goto out;
669 }
670
James Morris089be432008-07-15 18:32:49 +1000671 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500672 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500673
674 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500675 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500676 if (rc) {
677 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000678 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500679 goto out;
680 }
681
682 /* sets the context of the superblock for the fs being mounted. */
683 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100684 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500685 if (rc)
686 goto out;
687
688 sbsec->sid = fscontext_sid;
689 }
690
691 /*
692 * Switch to using mount point labeling behavior.
693 * sets the label used on all file below the mountpoint, and will set
694 * the superblock context if not already set.
695 */
696 if (context_sid) {
697 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100698 rc = may_context_mount_sb_relabel(context_sid, sbsec,
699 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500700 if (rc)
701 goto out;
702 sbsec->sid = context_sid;
703 } else {
David Howells275bb412008-11-14 10:39:19 +1100704 rc = may_context_mount_inode_relabel(context_sid, sbsec,
705 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500706 if (rc)
707 goto out;
708 }
709 if (!rootcontext_sid)
710 rootcontext_sid = context_sid;
711
712 sbsec->mntpoint_sid = context_sid;
713 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
714 }
715
716 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100717 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
718 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500719 if (rc)
720 goto out;
721
722 root_isec->sid = rootcontext_sid;
723 root_isec->initialized = 1;
724 }
725
726 if (defcontext_sid) {
727 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
728 rc = -EINVAL;
729 printk(KERN_WARNING "SELinux: defcontext option is "
730 "invalid for this filesystem type\n");
731 goto out;
732 }
733
734 if (defcontext_sid != sbsec->def_sid) {
735 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100736 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500737 if (rc)
738 goto out;
739 }
740
741 sbsec->def_sid = defcontext_sid;
742 }
743
744 rc = sb_finish_set_opts(sb);
745out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700746 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500748out_double_mount:
749 rc = -EINVAL;
750 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
751 "security settings for (dev %s, type %s)\n", sb->s_id, name);
752 goto out;
753}
754
Jeff Layton094f7b62013-04-01 08:14:24 -0400755static int selinux_cmp_sb_context(const struct super_block *oldsb,
756 const struct super_block *newsb)
757{
758 struct superblock_security_struct *old = oldsb->s_security;
759 struct superblock_security_struct *new = newsb->s_security;
760 char oldflags = old->flags & SE_MNTMASK;
761 char newflags = new->flags & SE_MNTMASK;
762
763 if (oldflags != newflags)
764 goto mismatch;
765 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
766 goto mismatch;
767 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
768 goto mismatch;
769 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
770 goto mismatch;
771 if (oldflags & ROOTCONTEXT_MNT) {
772 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
773 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
774 if (oldroot->sid != newroot->sid)
775 goto mismatch;
776 }
777 return 0;
778mismatch:
779 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
780 "different security settings for (dev %s, "
781 "type %s)\n", newsb->s_id, newsb->s_type->name);
782 return -EBUSY;
783}
784
785static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Eric Parisc9180a52007-11-30 13:00:35 -0500786 struct super_block *newsb)
787{
788 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
789 struct superblock_security_struct *newsbsec = newsb->s_security;
790
791 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
792 int set_context = (oldsbsec->flags & CONTEXT_MNT);
793 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
794
Eric Paris0f5e6422008-04-21 16:24:11 -0400795 /*
796 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400797 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400798 */
Al Viroe8c26252010-03-23 06:36:54 -0400799 if (!ss_initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400800 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500801
Eric Parisc9180a52007-11-30 13:00:35 -0500802 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500803 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500804
Jeff Layton094f7b62013-04-01 08:14:24 -0400805 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500806 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400807 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400808
Eric Parisc9180a52007-11-30 13:00:35 -0500809 mutex_lock(&newsbsec->lock);
810
811 newsbsec->flags = oldsbsec->flags;
812
813 newsbsec->sid = oldsbsec->sid;
814 newsbsec->def_sid = oldsbsec->def_sid;
815 newsbsec->behavior = oldsbsec->behavior;
816
817 if (set_context) {
818 u32 sid = oldsbsec->mntpoint_sid;
819
820 if (!set_fscontext)
821 newsbsec->sid = sid;
822 if (!set_rootcontext) {
823 struct inode *newinode = newsb->s_root->d_inode;
824 struct inode_security_struct *newisec = newinode->i_security;
825 newisec->sid = sid;
826 }
827 newsbsec->mntpoint_sid = sid;
828 }
829 if (set_rootcontext) {
830 const struct inode *oldinode = oldsb->s_root->d_inode;
831 const struct inode_security_struct *oldisec = oldinode->i_security;
832 struct inode *newinode = newsb->s_root->d_inode;
833 struct inode_security_struct *newisec = newinode->i_security;
834
835 newisec->sid = oldisec->sid;
836 }
837
838 sb_finish_set_opts(newsb);
839 mutex_unlock(&newsbsec->lock);
Jeff Layton094f7b62013-04-01 08:14:24 -0400840 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500841}
842
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200843static int selinux_parse_opts_str(char *options,
844 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500845{
Eric Parise0007522008-03-05 10:31:54 -0500846 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500847 char *context = NULL, *defcontext = NULL;
848 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500849 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500850
Eric Parise0007522008-03-05 10:31:54 -0500851 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500852
853 /* Standard string-based options. */
854 while ((p = strsep(&options, "|")) != NULL) {
855 int token;
856 substring_t args[MAX_OPT_ARGS];
857
858 if (!*p)
859 continue;
860
861 token = match_token(p, tokens, args);
862
863 switch (token) {
864 case Opt_context:
865 if (context || defcontext) {
866 rc = -EINVAL;
867 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
868 goto out_err;
869 }
870 context = match_strdup(&args[0]);
871 if (!context) {
872 rc = -ENOMEM;
873 goto out_err;
874 }
875 break;
876
877 case Opt_fscontext:
878 if (fscontext) {
879 rc = -EINVAL;
880 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
881 goto out_err;
882 }
883 fscontext = match_strdup(&args[0]);
884 if (!fscontext) {
885 rc = -ENOMEM;
886 goto out_err;
887 }
888 break;
889
890 case Opt_rootcontext:
891 if (rootcontext) {
892 rc = -EINVAL;
893 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
894 goto out_err;
895 }
896 rootcontext = match_strdup(&args[0]);
897 if (!rootcontext) {
898 rc = -ENOMEM;
899 goto out_err;
900 }
901 break;
902
903 case Opt_defcontext:
904 if (context || defcontext) {
905 rc = -EINVAL;
906 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
907 goto out_err;
908 }
909 defcontext = match_strdup(&args[0]);
910 if (!defcontext) {
911 rc = -ENOMEM;
912 goto out_err;
913 }
914 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500915 case Opt_labelsupport:
916 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500917 default:
918 rc = -EINVAL;
919 printk(KERN_WARNING "SELinux: unknown mount option\n");
920 goto out_err;
921
922 }
923 }
924
Eric Parise0007522008-03-05 10:31:54 -0500925 rc = -ENOMEM;
926 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
927 if (!opts->mnt_opts)
928 goto out_err;
929
930 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
931 if (!opts->mnt_opts_flags) {
932 kfree(opts->mnt_opts);
933 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500934 }
935
Eric Parise0007522008-03-05 10:31:54 -0500936 if (fscontext) {
937 opts->mnt_opts[num_mnt_opts] = fscontext;
938 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
939 }
940 if (context) {
941 opts->mnt_opts[num_mnt_opts] = context;
942 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
943 }
944 if (rootcontext) {
945 opts->mnt_opts[num_mnt_opts] = rootcontext;
946 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
947 }
948 if (defcontext) {
949 opts->mnt_opts[num_mnt_opts] = defcontext;
950 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
951 }
952
953 opts->num_mnt_opts = num_mnt_opts;
954 return 0;
955
Eric Parisc9180a52007-11-30 13:00:35 -0500956out_err:
957 kfree(context);
958 kfree(defcontext);
959 kfree(fscontext);
960 kfree(rootcontext);
961 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
Eric Parise0007522008-03-05 10:31:54 -0500963/*
964 * string mount options parsing and call set the sbsec
965 */
966static int superblock_doinit(struct super_block *sb, void *data)
967{
968 int rc = 0;
969 char *options = data;
970 struct security_mnt_opts opts;
971
972 security_init_mnt_opts(&opts);
973
974 if (!data)
975 goto out;
976
977 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
978
979 rc = selinux_parse_opts_str(options, &opts);
980 if (rc)
981 goto out_err;
982
983out:
984 rc = selinux_set_mnt_opts(sb, &opts);
985
986out_err:
987 security_free_mnt_opts(&opts);
988 return rc;
989}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Adrian Bunk3583a712008-07-22 20:21:23 +0300991static void selinux_write_opts(struct seq_file *m,
992 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000993{
994 int i;
995 char *prefix;
996
997 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500998 char *has_comma;
999
1000 if (opts->mnt_opts[i])
1001 has_comma = strchr(opts->mnt_opts[i], ',');
1002 else
1003 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001004
1005 switch (opts->mnt_opts_flags[i]) {
1006 case CONTEXT_MNT:
1007 prefix = CONTEXT_STR;
1008 break;
1009 case FSCONTEXT_MNT:
1010 prefix = FSCONTEXT_STR;
1011 break;
1012 case ROOTCONTEXT_MNT:
1013 prefix = ROOTCONTEXT_STR;
1014 break;
1015 case DEFCONTEXT_MNT:
1016 prefix = DEFCONTEXT_STR;
1017 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001018 case SE_SBLABELSUPP:
1019 seq_putc(m, ',');
1020 seq_puts(m, LABELSUPP_STR);
1021 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001022 default:
1023 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001024 return;
Eric Paris2069f452008-07-04 09:47:13 +10001025 };
1026 /* we need a comma before each option */
1027 seq_putc(m, ',');
1028 seq_puts(m, prefix);
1029 if (has_comma)
1030 seq_putc(m, '\"');
1031 seq_puts(m, opts->mnt_opts[i]);
1032 if (has_comma)
1033 seq_putc(m, '\"');
1034 }
1035}
1036
1037static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1038{
1039 struct security_mnt_opts opts;
1040 int rc;
1041
1042 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001043 if (rc) {
1044 /* before policy load we may get EINVAL, don't show anything */
1045 if (rc == -EINVAL)
1046 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001047 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001048 }
Eric Paris2069f452008-07-04 09:47:13 +10001049
1050 selinux_write_opts(m, &opts);
1051
1052 security_free_mnt_opts(&opts);
1053
1054 return rc;
1055}
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057static inline u16 inode_mode_to_security_class(umode_t mode)
1058{
1059 switch (mode & S_IFMT) {
1060 case S_IFSOCK:
1061 return SECCLASS_SOCK_FILE;
1062 case S_IFLNK:
1063 return SECCLASS_LNK_FILE;
1064 case S_IFREG:
1065 return SECCLASS_FILE;
1066 case S_IFBLK:
1067 return SECCLASS_BLK_FILE;
1068 case S_IFDIR:
1069 return SECCLASS_DIR;
1070 case S_IFCHR:
1071 return SECCLASS_CHR_FILE;
1072 case S_IFIFO:
1073 return SECCLASS_FIFO_FILE;
1074
1075 }
1076
1077 return SECCLASS_FILE;
1078}
1079
James Morris13402582005-09-30 14:24:34 -04001080static inline int default_protocol_stream(int protocol)
1081{
1082 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1083}
1084
1085static inline int default_protocol_dgram(int protocol)
1086{
1087 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1091{
1092 switch (family) {
1093 case PF_UNIX:
1094 switch (type) {
1095 case SOCK_STREAM:
1096 case SOCK_SEQPACKET:
1097 return SECCLASS_UNIX_STREAM_SOCKET;
1098 case SOCK_DGRAM:
1099 return SECCLASS_UNIX_DGRAM_SOCKET;
1100 }
1101 break;
1102 case PF_INET:
1103 case PF_INET6:
1104 switch (type) {
1105 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001106 if (default_protocol_stream(protocol))
1107 return SECCLASS_TCP_SOCKET;
1108 else
1109 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001111 if (default_protocol_dgram(protocol))
1112 return SECCLASS_UDP_SOCKET;
1113 else
1114 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001115 case SOCK_DCCP:
1116 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001117 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return SECCLASS_RAWIP_SOCKET;
1119 }
1120 break;
1121 case PF_NETLINK:
1122 switch (protocol) {
1123 case NETLINK_ROUTE:
1124 return SECCLASS_NETLINK_ROUTE_SOCKET;
1125 case NETLINK_FIREWALL:
1126 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001127 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1129 case NETLINK_NFLOG:
1130 return SECCLASS_NETLINK_NFLOG_SOCKET;
1131 case NETLINK_XFRM:
1132 return SECCLASS_NETLINK_XFRM_SOCKET;
1133 case NETLINK_SELINUX:
1134 return SECCLASS_NETLINK_SELINUX_SOCKET;
1135 case NETLINK_AUDIT:
1136 return SECCLASS_NETLINK_AUDIT_SOCKET;
1137 case NETLINK_IP6_FW:
1138 return SECCLASS_NETLINK_IP6FW_SOCKET;
1139 case NETLINK_DNRTMSG:
1140 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001141 case NETLINK_KOBJECT_UEVENT:
1142 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 default:
1144 return SECCLASS_NETLINK_SOCKET;
1145 }
1146 case PF_PACKET:
1147 return SECCLASS_PACKET_SOCKET;
1148 case PF_KEY:
1149 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001150 case PF_APPLETALK:
1151 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
1154 return SECCLASS_SOCKET;
1155}
1156
1157#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001158static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 u16 tclass,
1160 u32 *sid)
1161{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001162 int rc;
1163 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Eric Paris828dfe12008-04-17 13:17:49 -04001165 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!buffer)
1167 return -ENOMEM;
1168
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001169 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1170 if (IS_ERR(path))
1171 rc = PTR_ERR(path);
1172 else {
1173 /* each process gets a /proc/PID/ entry. Strip off the
1174 * PID part to get a valid selinux labeling.
1175 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1176 while (path[1] >= '0' && path[1] <= '9') {
1177 path[1] = '/';
1178 path++;
1179 }
1180 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 free_page((unsigned long)buffer);
1183 return rc;
1184}
1185#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001186static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 u16 tclass,
1188 u32 *sid)
1189{
1190 return -EINVAL;
1191}
1192#endif
1193
1194/* The inode's security attributes must be initialized before first use. */
1195static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1196{
1197 struct superblock_security_struct *sbsec = NULL;
1198 struct inode_security_struct *isec = inode->i_security;
1199 u32 sid;
1200 struct dentry *dentry;
1201#define INITCONTEXTLEN 255
1202 char *context = NULL;
1203 unsigned len = 0;
1204 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if (isec->initialized)
1207 goto out;
1208
Eric Paris23970742006-09-25 23:32:01 -07001209 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001211 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001214 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* Defer initialization until selinux_complete_init,
1216 after the initial policy is loaded and the security
1217 server is ready to handle calls. */
1218 spin_lock(&sbsec->isec_lock);
1219 if (list_empty(&isec->list))
1220 list_add(&isec->list, &sbsec->isec_head);
1221 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001222 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 switch (sbsec->behavior) {
1226 case SECURITY_FS_USE_XATTR:
1227 if (!inode->i_op->getxattr) {
1228 isec->sid = sbsec->def_sid;
1229 break;
1230 }
1231
1232 /* Need a dentry, since the xattr API requires one.
1233 Life would be simpler if we could just pass the inode. */
1234 if (opt_dentry) {
1235 /* Called from d_instantiate or d_splice_alias. */
1236 dentry = dget(opt_dentry);
1237 } else {
1238 /* Called from selinux_complete_init, try to find a dentry. */
1239 dentry = d_find_alias(inode);
1240 }
1241 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001242 /*
1243 * this is can be hit on boot when a file is accessed
1244 * before the policy is loaded. When we load policy we
1245 * may find inodes that have no dentry on the
1246 * sbsec->isec_head list. No reason to complain as these
1247 * will get fixed up the next time we go through
1248 * inode_doinit with a dentry, before these inodes could
1249 * be used again by userspace.
1250 */
Eric Paris23970742006-09-25 23:32:01 -07001251 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
1254 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001255 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!context) {
1257 rc = -ENOMEM;
1258 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001259 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001261 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1263 context, len);
1264 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001265 kfree(context);
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /* Need a larger buffer. Query for the right size. */
1268 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1269 NULL, 0);
1270 if (rc < 0) {
1271 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001272 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001275 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (!context) {
1277 rc = -ENOMEM;
1278 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001279 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001281 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 rc = inode->i_op->getxattr(dentry,
1283 XATTR_NAME_SELINUX,
1284 context, len);
1285 }
1286 dput(dentry);
1287 if (rc < 0) {
1288 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001289 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001290 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 -rc, inode->i_sb->s_id, inode->i_ino);
1292 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001293 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 /* Map ENODATA to the default file SID */
1296 sid = sbsec->def_sid;
1297 rc = 0;
1298 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001299 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001300 sbsec->def_sid,
1301 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001303 char *dev = inode->i_sb->s_id;
1304 unsigned long ino = inode->i_ino;
1305
1306 if (rc == -EINVAL) {
1307 if (printk_ratelimit())
1308 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1309 "context=%s. This indicates you may need to relabel the inode or the "
1310 "filesystem in question.\n", ino, dev, context);
1311 } else {
1312 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1313 "returned %d for dev=%s ino=%ld\n",
1314 __func__, context, -rc, dev, ino);
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 kfree(context);
1317 /* Leave with the unlabeled SID */
1318 rc = 0;
1319 break;
1320 }
1321 }
1322 kfree(context);
1323 isec->sid = sid;
1324 break;
1325 case SECURITY_FS_USE_TASK:
1326 isec->sid = isec->task_sid;
1327 break;
1328 case SECURITY_FS_USE_TRANS:
1329 /* Default to the fs SID. */
1330 isec->sid = sbsec->sid;
1331
1332 /* Try to obtain a transition SID. */
1333 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001334 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1335 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001337 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 isec->sid = sid;
1339 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001340 case SECURITY_FS_USE_MNTPOINT:
1341 isec->sid = sbsec->mntpoint_sid;
1342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001344 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 isec->sid = sbsec->sid;
1346
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001347 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001348 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001350 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 isec->sclass,
1352 &sid);
1353 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001354 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 isec->sid = sid;
1356 }
1357 }
1358 break;
1359 }
1360
1361 isec->initialized = 1;
1362
Eric Paris23970742006-09-25 23:32:01 -07001363out_unlock:
1364 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365out:
1366 if (isec->sclass == SECCLASS_FILE)
1367 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return rc;
1369}
1370
1371/* Convert a Linux signal to an access vector. */
1372static inline u32 signal_to_av(int sig)
1373{
1374 u32 perm = 0;
1375
1376 switch (sig) {
1377 case SIGCHLD:
1378 /* Commonly granted from child to parent. */
1379 perm = PROCESS__SIGCHLD;
1380 break;
1381 case SIGKILL:
1382 /* Cannot be caught or ignored */
1383 perm = PROCESS__SIGKILL;
1384 break;
1385 case SIGSTOP:
1386 /* Cannot be caught or ignored */
1387 perm = PROCESS__SIGSTOP;
1388 break;
1389 default:
1390 /* All other signals. */
1391 perm = PROCESS__SIGNAL;
1392 break;
1393 }
1394
1395 return perm;
1396}
1397
David Howells275bb412008-11-14 10:39:19 +11001398/*
David Howellsd84f4f92008-11-14 10:39:23 +11001399 * Check permission between a pair of credentials
1400 * fork check, ptrace check, etc.
1401 */
1402static int cred_has_perm(const struct cred *actor,
1403 const struct cred *target,
1404 u32 perms)
1405{
1406 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1407
1408 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1409}
1410
1411/*
David Howells88e67f32008-11-14 10:39:21 +11001412 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001413 * fork check, ptrace check, etc.
1414 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001415 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001416 */
1417static int task_has_perm(const struct task_struct *tsk1,
1418 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 u32 perms)
1420{
David Howells275bb412008-11-14 10:39:19 +11001421 const struct task_security_struct *__tsec1, *__tsec2;
1422 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
David Howells275bb412008-11-14 10:39:19 +11001424 rcu_read_lock();
1425 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1426 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1427 rcu_read_unlock();
1428 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
David Howells3b11a1d2008-11-14 10:39:26 +11001431/*
1432 * Check permission between current and another task, e.g. signal checks,
1433 * fork check, ptrace check, etc.
1434 * current is the actor and tsk2 is the target
1435 * - this uses current's subjective creds
1436 */
1437static int current_has_perm(const struct task_struct *tsk,
1438 u32 perms)
1439{
1440 u32 sid, tsid;
1441
1442 sid = current_sid();
1443 tsid = task_sid(tsk);
1444 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1445}
1446
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001447#if CAP_LAST_CAP > 63
1448#error Fix SELinux to handle capabilities > 63.
1449#endif
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001452static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001453 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Thomas Liu2bf49692009-07-14 12:14:09 -04001455 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001456 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001457 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001458 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001459 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001460 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Eric Paris50c205f2012-04-04 15:01:43 -04001462 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ad.u.cap = cap;
1464
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001465 switch (CAP_TO_INDEX(cap)) {
1466 case 0:
1467 sclass = SECCLASS_CAPABILITY;
1468 break;
1469 case 1:
1470 sclass = SECCLASS_CAPABILITY2;
1471 break;
1472 default:
1473 printk(KERN_ERR
1474 "SELinux: out of range capability %d\n", cap);
1475 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001476 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001477 }
Eric Paris06112162008-11-11 22:02:50 +11001478
David Howells275bb412008-11-14 10:39:19 +11001479 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001480 if (audit == SECURITY_CAP_AUDIT) {
1481 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1482 if (rc2)
1483 return rc2;
1484 }
Eric Paris06112162008-11-11 22:02:50 +11001485 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488/* Check whether a task is allowed to use a system operation. */
1489static int task_has_system(struct task_struct *tsk,
1490 u32 perms)
1491{
David Howells275bb412008-11-14 10:39:19 +11001492 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
David Howells275bb412008-11-14 10:39:19 +11001494 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 SECCLASS_SYSTEM, perms, NULL);
1496}
1497
1498/* Check whether a task has a particular permission to an inode.
1499 The 'adp' parameter is optional and allows other audit
1500 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001501static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct inode *inode,
1503 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001504 struct common_audit_data *adp,
1505 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001508 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
David Howellse0e81732009-09-02 09:13:40 +01001510 validate_creds(cred);
1511
Eric Paris828dfe12008-04-17 13:17:49 -04001512 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001513 return 0;
1514
David Howells88e67f32008-11-14 10:39:21 +11001515 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 isec = inode->i_security;
1517
Eric Paris9ade0cf2011-04-25 16:26:29 -04001518 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521/* Same as inode_has_perm, but pass explicit audit data containing
1522 the dentry to help the auditing code to more easily generate the
1523 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001524static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct dentry *dentry,
1526 u32 av)
1527{
1528 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001529 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001530
Eric Paris50c205f2012-04-04 15:01:43 -04001531 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001532 ad.u.dentry = dentry;
1533 return inode_has_perm(cred, inode, av, &ad, 0);
1534}
1535
1536/* Same as inode_has_perm, but pass explicit audit data containing
1537 the path to help the auditing code to more easily generate the
1538 pathname if needed. */
1539static inline int path_has_perm(const struct cred *cred,
1540 struct path *path,
1541 u32 av)
1542{
1543 struct inode *inode = path->dentry->d_inode;
1544 struct common_audit_data ad;
1545
Eric Paris50c205f2012-04-04 15:01:43 -04001546 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001547 ad.u.path = *path;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001548 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551/* Check whether a task can use an open file descriptor to
1552 access an inode in a given way. Check access to the
1553 descriptor itself, and then use dentry_has_perm to
1554 check a particular permission to the file.
1555 Access to the descriptor is implicitly granted if it
1556 has the same SID as the process. If av is zero, then
1557 access to the file is not checked, e.g. for cases
1558 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001559static int file_has_perm(const struct cred *cred,
1560 struct file *file,
1561 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001564 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001565 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001566 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int rc;
1568
Eric Paris50c205f2012-04-04 15:01:43 -04001569 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04001570 ad.u.path = file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
David Howells275bb412008-11-14 10:39:19 +11001572 if (sid != fsec->sid) {
1573 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 SECCLASS_FD,
1575 FD__USE,
1576 &ad);
1577 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
1581 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001582 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001584 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
David Howells88e67f32008-11-14 10:39:21 +11001586out:
1587 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590/* Check whether a task can create a file. */
1591static int may_create(struct inode *dir,
1592 struct dentry *dentry,
1593 u16 tclass)
1594{
Paul Moore5fb49872010-04-22 14:46:19 -04001595 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 struct inode_security_struct *dsec;
1597 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001598 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001599 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int rc;
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 dsec = dir->i_security;
1603 sbsec = dir->i_sb->s_security;
1604
David Howells275bb412008-11-14 10:39:19 +11001605 sid = tsec->sid;
1606 newsid = tsec->create_sid;
1607
Eric Paris50c205f2012-04-04 15:01:43 -04001608 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001609 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
David Howells275bb412008-11-14 10:39:19 +11001611 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 DIR__ADD_NAME | DIR__SEARCH,
1613 &ad);
1614 if (rc)
1615 return rc;
1616
David P. Quigleycd895962009-01-16 09:22:04 -05001617 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001618 rc = security_transition_sid(sid, dsec->sid, tclass,
1619 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (rc)
1621 return rc;
1622 }
1623
David Howells275bb412008-11-14 10:39:19 +11001624 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (rc)
1626 return rc;
1627
1628 return avc_has_perm(newsid, sbsec->sid,
1629 SECCLASS_FILESYSTEM,
1630 FILESYSTEM__ASSOCIATE, &ad);
1631}
1632
Michael LeMay4eb582c2006-06-26 00:24:57 -07001633/* Check whether a task can create a key. */
1634static int may_create_key(u32 ksid,
1635 struct task_struct *ctx)
1636{
David Howells275bb412008-11-14 10:39:19 +11001637 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001638
David Howells275bb412008-11-14 10:39:19 +11001639 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001640}
1641
Eric Paris828dfe12008-04-17 13:17:49 -04001642#define MAY_LINK 0
1643#define MAY_UNLINK 1
1644#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646/* Check whether a task can link, unlink, or rmdir a file/directory. */
1647static int may_link(struct inode *dir,
1648 struct dentry *dentry,
1649 int kind)
1650
1651{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001653 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001654 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 u32 av;
1656 int rc;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 dsec = dir->i_security;
1659 isec = dentry->d_inode->i_security;
1660
Eric Paris50c205f2012-04-04 15:01:43 -04001661 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001662 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 av = DIR__SEARCH;
1665 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001666 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (rc)
1668 return rc;
1669
1670 switch (kind) {
1671 case MAY_LINK:
1672 av = FILE__LINK;
1673 break;
1674 case MAY_UNLINK:
1675 av = FILE__UNLINK;
1676 break;
1677 case MAY_RMDIR:
1678 av = DIR__RMDIR;
1679 break;
1680 default:
Eric Paris744ba352008-04-17 11:52:44 -04001681 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1682 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684 }
1685
David Howells275bb412008-11-14 10:39:19 +11001686 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return rc;
1688}
1689
1690static inline int may_rename(struct inode *old_dir,
1691 struct dentry *old_dentry,
1692 struct inode *new_dir,
1693 struct dentry *new_dentry)
1694{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001696 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001697 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 u32 av;
1699 int old_is_dir, new_is_dir;
1700 int rc;
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 old_dsec = old_dir->i_security;
1703 old_isec = old_dentry->d_inode->i_security;
1704 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1705 new_dsec = new_dir->i_security;
1706
Eric Paris50c205f2012-04-04 15:01:43 -04001707 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Eric Parisa2694342011-04-25 13:10:27 -04001709 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001710 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1712 if (rc)
1713 return rc;
David Howells275bb412008-11-14 10:39:19 +11001714 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 old_isec->sclass, FILE__RENAME, &ad);
1716 if (rc)
1717 return rc;
1718 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001719 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 old_isec->sclass, DIR__REPARENT, &ad);
1721 if (rc)
1722 return rc;
1723 }
1724
Eric Parisa2694342011-04-25 13:10:27 -04001725 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 av = DIR__ADD_NAME | DIR__SEARCH;
1727 if (new_dentry->d_inode)
1728 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001729 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (rc)
1731 return rc;
1732 if (new_dentry->d_inode) {
1733 new_isec = new_dentry->d_inode->i_security;
1734 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001735 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 new_isec->sclass,
1737 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1738 if (rc)
1739 return rc;
1740 }
1741
1742 return 0;
1743}
1744
1745/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001746static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct super_block *sb,
1748 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001749 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001752 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001755 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/* Convert a Linux mode and permission mask to an access vector. */
1759static inline u32 file_mask_to_av(int mode, int mask)
1760{
1761 u32 av = 0;
1762
Al Virodba19c62011-07-25 20:49:29 -04001763 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (mask & MAY_EXEC)
1765 av |= FILE__EXECUTE;
1766 if (mask & MAY_READ)
1767 av |= FILE__READ;
1768
1769 if (mask & MAY_APPEND)
1770 av |= FILE__APPEND;
1771 else if (mask & MAY_WRITE)
1772 av |= FILE__WRITE;
1773
1774 } else {
1775 if (mask & MAY_EXEC)
1776 av |= DIR__SEARCH;
1777 if (mask & MAY_WRITE)
1778 av |= DIR__WRITE;
1779 if (mask & MAY_READ)
1780 av |= DIR__READ;
1781 }
1782
1783 return av;
1784}
1785
1786/* Convert a Linux file to an access vector. */
1787static inline u32 file_to_av(struct file *file)
1788{
1789 u32 av = 0;
1790
1791 if (file->f_mode & FMODE_READ)
1792 av |= FILE__READ;
1793 if (file->f_mode & FMODE_WRITE) {
1794 if (file->f_flags & O_APPEND)
1795 av |= FILE__APPEND;
1796 else
1797 av |= FILE__WRITE;
1798 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001799 if (!av) {
1800 /*
1801 * Special file opened with flags 3 for ioctl-only use.
1802 */
1803 av = FILE__IOCTL;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 return av;
1807}
1808
Eric Paris8b6a5a32008-10-29 17:06:46 -04001809/*
1810 * Convert a file to an access vector and include the correct open
1811 * open permission.
1812 */
1813static inline u32 open_file_to_av(struct file *file)
1814{
1815 u32 av = file_to_av(file);
1816
Eric Paris49b7b8d2010-07-23 11:44:09 -04001817 if (selinux_policycap_openperm)
1818 av |= FILE__OPEN;
1819
Eric Paris8b6a5a32008-10-29 17:06:46 -04001820 return av;
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/* Hook functions begin here. */
1824
Ingo Molnar9e488582009-05-07 19:26:19 +10001825static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001826 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int rc;
1829
Ingo Molnar9e488582009-05-07 19:26:19 +10001830 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (rc)
1832 return rc;
1833
Eric Paris69f594a2012-01-03 12:25:15 -05001834 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001835 u32 sid = current_sid();
1836 u32 csid = task_sid(child);
1837 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001838 }
1839
David Howells3b11a1d2008-11-14 10:39:26 +11001840 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001841}
1842
1843static int selinux_ptrace_traceme(struct task_struct *parent)
1844{
1845 int rc;
1846
Eric Paris200ac532009-02-12 15:01:04 -05001847 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001848 if (rc)
1849 return rc;
1850
1851 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001855 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 int error;
1858
David Howells3b11a1d2008-11-14 10:39:26 +11001859 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (error)
1861 return error;
1862
Eric Paris200ac532009-02-12 15:01:04 -05001863 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
David Howellsd84f4f92008-11-14 10:39:23 +11001866static int selinux_capset(struct cred *new, const struct cred *old,
1867 const kernel_cap_t *effective,
1868 const kernel_cap_t *inheritable,
1869 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 int error;
1872
Eric Paris200ac532009-02-12 15:01:04 -05001873 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001874 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (error)
1876 return error;
1877
David Howellsd84f4f92008-11-14 10:39:23 +11001878 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
James Morris5626d3e2009-01-30 10:05:06 +11001881/*
1882 * (This comment used to live with the selinux_task_setuid hook,
1883 * which was removed).
1884 *
1885 * Since setuid only affects the current process, and since the SELinux
1886 * controls are not based on the Linux identity attributes, SELinux does not
1887 * need to control this operation. However, SELinux does control the use of
1888 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1889 */
1890
Eric Paris6a9de492012-01-03 12:25:14 -05001891static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1892 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 int rc;
1895
Eric Paris6a9de492012-01-03 12:25:14 -05001896 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (rc)
1898 return rc;
1899
Eric Paris6a9de492012-01-03 12:25:14 -05001900 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1904{
David Howells88e67f32008-11-14 10:39:21 +11001905 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int rc = 0;
1907
1908 if (!sb)
1909 return 0;
1910
1911 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001912 case Q_SYNC:
1913 case Q_QUOTAON:
1914 case Q_QUOTAOFF:
1915 case Q_SETINFO:
1916 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001917 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001918 break;
1919 case Q_GETFMT:
1920 case Q_GETINFO:
1921 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001922 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001923 break;
1924 default:
1925 rc = 0; /* let the kernel handle invalid cmds */
1926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 return rc;
1929}
1930
1931static int selinux_quota_on(struct dentry *dentry)
1932{
David Howells88e67f32008-11-14 10:39:21 +11001933 const struct cred *cred = current_cred();
1934
Eric Paris2875fa02011-04-28 16:04:24 -04001935 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Eric Paris12b30522010-11-15 18:36:29 -05001938static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int rc;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001943 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
1944 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001945 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
1946 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001947 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
1948 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
1949 /* Set level of messages printed to console */
1950 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04001951 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
1952 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001953 case SYSLOG_ACTION_CLOSE: /* Close log */
1954 case SYSLOG_ACTION_OPEN: /* Open log */
1955 case SYSLOG_ACTION_READ: /* Read from log */
1956 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
1957 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001958 default:
1959 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
1960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 return rc;
1963}
1964
1965/*
1966 * Check that a process has enough memory to allocate a new virtual
1967 * mapping. 0 means there is enough memory for the allocation to
1968 * succeed and -ENOMEM implies there is not.
1969 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 * Do not audit the selinux permission check, as this is applied to all
1971 * processes that allocate mappings.
1972 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001973static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Eric Paris6a9de492012-01-03 12:25:14 -05001977 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00001978 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (rc == 0)
1980 cap_sys_admin = 1;
1981
Alan Cox34b4e4a2007-08-22 14:01:28 -07001982 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985/* binprm security operations */
1986
David Howellsa6f76f22008-11-14 10:39:24 +11001987static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
David Howellsa6f76f22008-11-14 10:39:24 +11001989 const struct task_security_struct *old_tsec;
1990 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001992 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05001993 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 int rc;
1995
Eric Paris200ac532009-02-12 15:01:04 -05001996 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (rc)
1998 return rc;
1999
David Howellsa6f76f22008-11-14 10:39:24 +11002000 /* SELinux context only depends on initial program or script and not
2001 * the script interpreter */
2002 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
2004
David Howellsa6f76f22008-11-14 10:39:24 +11002005 old_tsec = current_security();
2006 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 isec = inode->i_security;
2008
2009 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002010 new_tsec->sid = old_tsec->sid;
2011 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Michael LeMay28eba5b2006-06-27 02:53:42 -07002013 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002014 new_tsec->create_sid = 0;
2015 new_tsec->keycreate_sid = 0;
2016 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
David Howellsa6f76f22008-11-14 10:39:24 +11002018 if (old_tsec->exec_sid) {
2019 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002021 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002022
2023 /*
2024 * Minimize confusion: if no_new_privs and a transition is
2025 * explicitly requested, then fail the exec.
2026 */
2027 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2028 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 } else {
2030 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002031 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002032 SECCLASS_PROCESS, NULL,
2033 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (rc)
2035 return rc;
2036 }
2037
Eric Paris50c205f2012-04-04 15:01:43 -04002038 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04002039 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002041 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2042 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002043 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
David Howellsa6f76f22008-11-14 10:39:24 +11002045 if (new_tsec->sid == old_tsec->sid) {
2046 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2048 if (rc)
2049 return rc;
2050 } else {
2051 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002052 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2054 if (rc)
2055 return rc;
2056
David Howellsa6f76f22008-11-14 10:39:24 +11002057 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2059 if (rc)
2060 return rc;
2061
David Howellsa6f76f22008-11-14 10:39:24 +11002062 /* Check for shared state */
2063 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2064 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2065 SECCLASS_PROCESS, PROCESS__SHARE,
2066 NULL);
2067 if (rc)
2068 return -EPERM;
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
David Howellsa6f76f22008-11-14 10:39:24 +11002071 /* Make sure that anyone attempting to ptrace over a task that
2072 * changes its SID has the appropriate permit */
2073 if (bprm->unsafe &
2074 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2075 struct task_struct *tracer;
2076 struct task_security_struct *sec;
2077 u32 ptsid = 0;
2078
2079 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002080 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002081 if (likely(tracer != NULL)) {
2082 sec = __task_cred(tracer)->security;
2083 ptsid = sec->sid;
2084 }
2085 rcu_read_unlock();
2086
2087 if (ptsid != 0) {
2088 rc = avc_has_perm(ptsid, new_tsec->sid,
2089 SECCLASS_PROCESS,
2090 PROCESS__PTRACE, NULL);
2091 if (rc)
2092 return -EPERM;
2093 }
2094 }
2095
2096 /* Clear any possibly unsafe personality bits on exec: */
2097 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return 0;
2101}
2102
Eric Paris828dfe12008-04-17 13:17:49 -04002103static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Paul Moore5fb49872010-04-22 14:46:19 -04002105 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002106 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 int atsecure = 0;
2108
David Howells275bb412008-11-14 10:39:19 +11002109 sid = tsec->sid;
2110 osid = tsec->osid;
2111
2112 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /* Enable secure mode for SIDs transitions unless
2114 the noatsecure permission is granted between
2115 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002116 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002117 SECCLASS_PROCESS,
2118 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
Eric Paris200ac532009-02-12 15:01:04 -05002121 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Al Viroc3c073f2012-08-21 22:32:06 -04002124static int match_file(const void *p, struct file *file, unsigned fd)
2125{
2126 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2127}
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002130static inline void flush_unauthorized_files(const struct cred *cred,
2131 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002134 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002135 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002136 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002138 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002140 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002141 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002142 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* Revalidate access to controlling tty.
Eric Paris602a8dd2012-04-04 15:01:42 -04002145 Use path_has_perm on the tty path directly rather
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 than using file_has_perm, as this particular open
2147 file may belong to another process and we are only
2148 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002149 file_priv = list_first_entry(&tty->tty_files,
2150 struct tty_file_private, list);
2151 file = file_priv->file;
Eric Paris602a8dd2012-04-04 15:01:42 -04002152 if (path_has_perm(cred, &file->f_path, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002153 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002155 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002156 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002158 /* Reset controlling tty. */
2159 if (drop_tty)
2160 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002163 n = iterate_fd(files, 0, match_file, cred);
2164 if (!n) /* none found? */
2165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Al Viroc3c073f2012-08-21 22:32:06 -04002167 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002168 if (IS_ERR(devnull))
2169 devnull = NULL;
2170 /* replace all the matching ones with this */
2171 do {
2172 replace_fd(n - 1, devnull, 0);
2173 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2174 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002175 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178/*
David Howellsa6f76f22008-11-14 10:39:24 +11002179 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 */
David Howellsa6f76f22008-11-14 10:39:24 +11002181static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
David Howellsa6f76f22008-11-14 10:39:24 +11002183 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int rc, i;
2186
David Howellsa6f76f22008-11-14 10:39:24 +11002187 new_tsec = bprm->cred->security;
2188 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return;
2190
2191 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002192 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
David Howellsa6f76f22008-11-14 10:39:24 +11002194 /* Always clear parent death signal on SID transitions. */
2195 current->pdeath_signal = 0;
2196
2197 /* Check whether the new SID can inherit resource limits from the old
2198 * SID. If not, reset all soft limits to the lower of the current
2199 * task's hard limit and the init task's soft limit.
2200 *
2201 * Note that the setting of hard limits (even to lower them) can be
2202 * controlled by the setrlimit check. The inclusion of the init task's
2203 * soft limit into the computation is to avoid resetting soft limits
2204 * higher than the default soft limit for cases where the default is
2205 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2206 */
2207 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2208 PROCESS__RLIMITINH, NULL);
2209 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002210 /* protect against do_prlimit() */
2211 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002212 for (i = 0; i < RLIM_NLIMITS; i++) {
2213 rlim = current->signal->rlim + i;
2214 initrlim = init_task.signal->rlim + i;
2215 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2216 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002217 task_unlock(current);
2218 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002219 }
2220}
2221
2222/*
2223 * Clean up the process immediately after the installation of new credentials
2224 * due to exec
2225 */
2226static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2227{
2228 const struct task_security_struct *tsec = current_security();
2229 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002230 u32 osid, sid;
2231 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002232
David Howellsa6f76f22008-11-14 10:39:24 +11002233 osid = tsec->osid;
2234 sid = tsec->sid;
2235
2236 if (sid == osid)
2237 return;
2238
2239 /* Check whether the new SID can inherit signal state from the old SID.
2240 * If not, clear itimers to avoid subsequent signal generation and
2241 * flush and unblock signals.
2242 *
2243 * This must occur _after_ the task SID has been updated so that any
2244 * kill done after the flush will be checked against the new SID.
2245 */
2246 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (rc) {
2248 memset(&itimer, 0, sizeof itimer);
2249 for (i = 0; i < 3; i++)
2250 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002252 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2253 __flush_signals(current);
2254 flush_signal_handlers(current, 1);
2255 sigemptyset(&current->blocked);
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 spin_unlock_irq(&current->sighand->siglock);
2258 }
2259
David Howellsa6f76f22008-11-14 10:39:24 +11002260 /* Wake up the parent if it is waiting so that it can recheck
2261 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002262 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002263 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002264 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267/* superblock security operations */
2268
2269static int selinux_sb_alloc_security(struct super_block *sb)
2270{
2271 return superblock_alloc_security(sb);
2272}
2273
2274static void selinux_sb_free_security(struct super_block *sb)
2275{
2276 superblock_free_security(sb);
2277}
2278
2279static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2280{
2281 if (plen > olen)
2282 return 0;
2283
2284 return !memcmp(prefix, option, plen);
2285}
2286
2287static inline int selinux_option(char *option, int len)
2288{
Eric Paris832cbd92008-04-01 13:24:09 -04002289 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2290 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2291 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002292 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2293 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296static inline void take_option(char **to, char *from, int *first, int len)
2297{
2298 if (!*first) {
2299 **to = ',';
2300 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002301 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 *first = 0;
2303 memcpy(*to, from, len);
2304 *to += len;
2305}
2306
Eric Paris828dfe12008-04-17 13:17:49 -04002307static inline void take_selinux_option(char **to, char *from, int *first,
2308 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002309{
2310 int current_size = 0;
2311
2312 if (!*first) {
2313 **to = '|';
2314 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002315 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002316 *first = 0;
2317
2318 while (current_size < len) {
2319 if (*from != '"') {
2320 **to = *from;
2321 *to += 1;
2322 }
2323 from += 1;
2324 current_size += 1;
2325 }
2326}
2327
Eric Parise0007522008-03-05 10:31:54 -05002328static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 int fnosec, fsec, rc = 0;
2331 char *in_save, *in_curr, *in_end;
2332 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002333 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 in_curr = orig;
2336 sec_curr = copy;
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2339 if (!nosec) {
2340 rc = -ENOMEM;
2341 goto out;
2342 }
2343
2344 nosec_save = nosec;
2345 fnosec = fsec = 1;
2346 in_save = in_end = orig;
2347
2348 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002349 if (*in_end == '"')
2350 open_quote = !open_quote;
2351 if ((*in_end == ',' && open_quote == 0) ||
2352 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int len = in_end - in_curr;
2354
2355 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002356 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 else
2358 take_option(&nosec, in_curr, &fnosec, len);
2359
2360 in_curr = in_end + 1;
2361 }
2362 } while (*in_end++);
2363
Eric Paris6931dfc2005-06-30 02:58:51 -07002364 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002365 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366out:
2367 return rc;
2368}
2369
Eric Paris026eb162011-03-03 16:09:14 -05002370static int selinux_sb_remount(struct super_block *sb, void *data)
2371{
2372 int rc, i, *flags;
2373 struct security_mnt_opts opts;
2374 char *secdata, **mount_options;
2375 struct superblock_security_struct *sbsec = sb->s_security;
2376
2377 if (!(sbsec->flags & SE_SBINITIALIZED))
2378 return 0;
2379
2380 if (!data)
2381 return 0;
2382
2383 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2384 return 0;
2385
2386 security_init_mnt_opts(&opts);
2387 secdata = alloc_secdata();
2388 if (!secdata)
2389 return -ENOMEM;
2390 rc = selinux_sb_copy_data(data, secdata);
2391 if (rc)
2392 goto out_free_secdata;
2393
2394 rc = selinux_parse_opts_str(secdata, &opts);
2395 if (rc)
2396 goto out_free_secdata;
2397
2398 mount_options = opts.mnt_opts;
2399 flags = opts.mnt_opts_flags;
2400
2401 for (i = 0; i < opts.num_mnt_opts; i++) {
2402 u32 sid;
2403 size_t len;
2404
2405 if (flags[i] == SE_SBLABELSUPP)
2406 continue;
2407 len = strlen(mount_options[i]);
2408 rc = security_context_to_sid(mount_options[i], len, &sid);
2409 if (rc) {
2410 printk(KERN_WARNING "SELinux: security_context_to_sid"
2411 "(%s) failed for (dev %s, type %s) errno=%d\n",
2412 mount_options[i], sb->s_id, sb->s_type->name, rc);
2413 goto out_free_opts;
2414 }
2415 rc = -EINVAL;
2416 switch (flags[i]) {
2417 case FSCONTEXT_MNT:
2418 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2419 goto out_bad_option;
2420 break;
2421 case CONTEXT_MNT:
2422 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2423 goto out_bad_option;
2424 break;
2425 case ROOTCONTEXT_MNT: {
2426 struct inode_security_struct *root_isec;
2427 root_isec = sb->s_root->d_inode->i_security;
2428
2429 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2430 goto out_bad_option;
2431 break;
2432 }
2433 case DEFCONTEXT_MNT:
2434 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2435 goto out_bad_option;
2436 break;
2437 default:
2438 goto out_free_opts;
2439 }
2440 }
2441
2442 rc = 0;
2443out_free_opts:
2444 security_free_mnt_opts(&opts);
2445out_free_secdata:
2446 free_secdata(secdata);
2447 return rc;
2448out_bad_option:
2449 printk(KERN_WARNING "SELinux: unable to change security options "
2450 "during remount (dev %s, type=%s)\n", sb->s_id,
2451 sb->s_type->name);
2452 goto out_free_opts;
2453}
2454
James Morris12204e22008-12-19 10:44:42 +11002455static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
David Howells88e67f32008-11-14 10:39:21 +11002457 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002458 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 int rc;
2460
2461 rc = superblock_doinit(sb, data);
2462 if (rc)
2463 return rc;
2464
James Morris74192242008-12-19 11:41:10 +11002465 /* Allow all mounts performed by the kernel */
2466 if (flags & MS_KERNMOUNT)
2467 return 0;
2468
Eric Paris50c205f2012-04-04 15:01:43 -04002469 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002470 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002471 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
David Howells726c3342006-06-23 02:02:58 -07002474static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
David Howells88e67f32008-11-14 10:39:21 +11002476 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002477 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Eric Paris50c205f2012-04-04 15:01:43 -04002479 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002480 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002481 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Al Viro808d4e32012-10-11 11:42:01 -04002484static int selinux_mount(const char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002485 struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002486 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002487 unsigned long flags,
2488 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
David Howells88e67f32008-11-14 10:39:21 +11002490 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002493 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002494 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 else
Eric Paris2875fa02011-04-28 16:04:24 -04002496 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
2499static int selinux_umount(struct vfsmount *mnt, int flags)
2500{
David Howells88e67f32008-11-14 10:39:21 +11002501 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
David Howells88e67f32008-11-14 10:39:21 +11002503 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002504 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
2507/* inode security operations */
2508
2509static int selinux_inode_alloc_security(struct inode *inode)
2510{
2511 return inode_alloc_security(inode);
2512}
2513
2514static void selinux_inode_free_security(struct inode *inode)
2515{
2516 inode_free_security(inode);
2517}
2518
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002519static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002520 const struct qstr *qstr, char **name,
2521 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002522{
Paul Moore5fb49872010-04-22 14:46:19 -04002523 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002524 struct inode_security_struct *dsec;
2525 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002526 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002527 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002528 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002529
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002530 dsec = dir->i_security;
2531 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002532
David Howells275bb412008-11-14 10:39:19 +11002533 sid = tsec->sid;
2534 newsid = tsec->create_sid;
2535
Eric Paris415103f2010-12-02 16:13:40 -05002536 if ((sbsec->flags & SE_SBINITIALIZED) &&
2537 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2538 newsid = sbsec->mntpoint_sid;
2539 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002540 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002541 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002542 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002543 if (rc) {
2544 printk(KERN_WARNING "%s: "
2545 "security_transition_sid failed, rc=%d (dev=%s "
2546 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002547 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002548 -rc, inode->i_sb->s_id, inode->i_ino);
2549 return rc;
2550 }
2551 }
2552
Eric Paris296fddf2006-09-25 23:32:00 -07002553 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002554 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002555 struct inode_security_struct *isec = inode->i_security;
2556 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2557 isec->sid = newsid;
2558 isec->initialized = 1;
2559 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002560
David P. Quigleycd895962009-01-16 09:22:04 -05002561 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002562 return -EOPNOTSUPP;
2563
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002564 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002565 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002566 if (!namep)
2567 return -ENOMEM;
2568 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002569 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002570
2571 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002572 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002573 if (rc) {
2574 kfree(namep);
2575 return rc;
2576 }
2577 *value = context;
2578 *len = clen;
2579 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002580
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002581 return 0;
2582}
2583
Al Viro4acdaf22011-07-26 01:42:34 -04002584static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 return may_create(dir, dentry, SECCLASS_FILE);
2587}
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return may_link(dir, old_dentry, MAY_LINK);
2592}
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return may_link(dir, dentry, MAY_UNLINK);
2597}
2598
2599static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2600{
2601 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2602}
2603
Al Viro18bb1db2011-07-26 01:41:39 -04002604static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 return may_create(dir, dentry, SECCLASS_DIR);
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2610{
2611 return may_link(dir, dentry, MAY_RMDIR);
2612}
2613
Al Viro1a67aaf2011-07-26 01:52:52 -04002614static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002620 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2623}
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625static int selinux_inode_readlink(struct dentry *dentry)
2626{
David Howells88e67f32008-11-14 10:39:21 +11002627 const struct cred *cred = current_cred();
2628
Eric Paris2875fa02011-04-28 16:04:24 -04002629 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2633{
David Howells88e67f32008-11-14 10:39:21 +11002634 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Eric Paris2875fa02011-04-28 16:04:24 -04002636 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
Eric Parisd4cf970d2012-04-04 15:01:42 -04002639static noinline int audit_inode_permission(struct inode *inode,
2640 u32 perms, u32 audited, u32 denied,
2641 unsigned flags)
2642{
2643 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002644 struct inode_security_struct *isec = inode->i_security;
2645 int rc;
2646
Eric Paris50c205f2012-04-04 15:01:43 -04002647 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002648 ad.u.inode = inode;
2649
2650 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2651 audited, denied, &ad, flags);
2652 if (rc)
2653 return rc;
2654 return 0;
2655}
2656
Al Viroe74f71e2011-06-20 19:38:15 -04002657static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
David Howells88e67f32008-11-14 10:39:21 +11002659 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002660 u32 perms;
2661 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002662 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002663 struct inode_security_struct *isec;
2664 u32 sid;
2665 struct av_decision avd;
2666 int rc, rc2;
2667 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Eric Parisb782e0a2010-07-23 11:44:03 -04002669 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002670 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2671
Eric Parisb782e0a2010-07-23 11:44:03 -04002672 /* No permission to check. Existence test. */
2673 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Eric Paris2e334052012-04-04 15:01:42 -04002676 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002677
Eric Paris2e334052012-04-04 15:01:42 -04002678 if (unlikely(IS_PRIVATE(inode)))
2679 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002680
2681 perms = file_mask_to_av(inode->i_mode, mask);
2682
Eric Paris2e334052012-04-04 15:01:42 -04002683 sid = cred_sid(cred);
2684 isec = inode->i_security;
2685
2686 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2687 audited = avc_audit_required(perms, &avd, rc,
2688 from_access ? FILE__AUDIT_ACCESS : 0,
2689 &denied);
2690 if (likely(!audited))
2691 return rc;
2692
Eric Parisd4cf970d2012-04-04 15:01:42 -04002693 rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002694 if (rc2)
2695 return rc2;
2696 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2700{
David Howells88e67f32008-11-14 10:39:21 +11002701 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002702 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04002703 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002705 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2706 if (ia_valid & ATTR_FORCE) {
2707 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2708 ATTR_FORCE);
2709 if (!ia_valid)
2710 return 0;
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002713 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2714 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002715 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Eric Paris3d2195c2012-07-06 14:13:30 -04002717 if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
Eric Paris95dbf732012-04-04 13:45:34 -04002718 av |= FILE__OPEN;
2719
2720 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2724{
David Howells88e67f32008-11-14 10:39:21 +11002725 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002726 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002727
Eric Paris2875fa02011-04-28 16:04:24 -04002728 path.dentry = dentry;
2729 path.mnt = mnt;
2730
2731 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
David Howells8f0cfa52008-04-29 00:59:41 -07002734static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002735{
David Howells88e67f32008-11-14 10:39:21 +11002736 const struct cred *cred = current_cred();
2737
Serge E. Hallynb5376772007-10-16 23:31:36 -07002738 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2739 sizeof XATTR_SECURITY_PREFIX - 1)) {
2740 if (!strcmp(name, XATTR_NAME_CAPS)) {
2741 if (!capable(CAP_SETFCAP))
2742 return -EPERM;
2743 } else if (!capable(CAP_SYS_ADMIN)) {
2744 /* A different attribute in the security namespace.
2745 Restrict to administrator. */
2746 return -EPERM;
2747 }
2748 }
2749
2750 /* Not an attribute we recognize, so just check the
2751 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002752 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002753}
2754
David Howells8f0cfa52008-04-29 00:59:41 -07002755static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2756 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct inode *inode = dentry->d_inode;
2759 struct inode_security_struct *isec = inode->i_security;
2760 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002761 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002762 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 int rc = 0;
2764
Serge E. Hallynb5376772007-10-16 23:31:36 -07002765 if (strcmp(name, XATTR_NAME_SELINUX))
2766 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002769 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return -EOPNOTSUPP;
2771
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002772 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -EPERM;
2774
Eric Paris50c205f2012-04-04 15:01:43 -04002775 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002776 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
David Howells275bb412008-11-14 10:39:19 +11002778 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 FILE__RELABELFROM, &ad);
2780 if (rc)
2781 return rc;
2782
2783 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002784 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04002785 if (!capable(CAP_MAC_ADMIN)) {
2786 struct audit_buffer *ab;
2787 size_t audit_size;
2788 const char *str;
2789
2790 /* We strip a nul only if it is at the end, otherwise the
2791 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01002792 if (value) {
2793 str = value;
2794 if (str[size - 1] == '\0')
2795 audit_size = size - 1;
2796 else
2797 audit_size = size;
2798 } else {
2799 str = "";
2800 audit_size = 0;
2801 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04002802 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2803 audit_log_format(ab, "op=setxattr invalid_context=");
2804 audit_log_n_untrustedstring(ab, value, audit_size);
2805 audit_log_end(ab);
2806
Stephen Smalley12b29f32008-05-07 13:03:20 -04002807 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04002808 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04002809 rc = security_context_to_sid_force(value, size, &newsid);
2810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (rc)
2812 return rc;
2813
David Howells275bb412008-11-14 10:39:19 +11002814 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 FILE__RELABELTO, &ad);
2816 if (rc)
2817 return rc;
2818
David Howells275bb412008-11-14 10:39:19 +11002819 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002820 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (rc)
2822 return rc;
2823
2824 return avc_has_perm(newsid,
2825 sbsec->sid,
2826 SECCLASS_FILESYSTEM,
2827 FILESYSTEM__ASSOCIATE,
2828 &ad);
2829}
2830
David Howells8f0cfa52008-04-29 00:59:41 -07002831static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002832 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002833 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835 struct inode *inode = dentry->d_inode;
2836 struct inode_security_struct *isec = inode->i_security;
2837 u32 newsid;
2838 int rc;
2839
2840 if (strcmp(name, XATTR_NAME_SELINUX)) {
2841 /* Not an attribute we recognize, so nothing to do. */
2842 return;
2843 }
2844
Stephen Smalley12b29f32008-05-07 13:03:20 -04002845 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002847 printk(KERN_ERR "SELinux: unable to map context to SID"
2848 "for (%s, %lu), rc=%d\n",
2849 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return;
2851 }
2852
2853 isec->sid = newsid;
2854 return;
2855}
2856
David Howells8f0cfa52008-04-29 00:59:41 -07002857static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
David Howells88e67f32008-11-14 10:39:21 +11002859 const struct cred *cred = current_cred();
2860
Eric Paris2875fa02011-04-28 16:04:24 -04002861 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
2863
Eric Paris828dfe12008-04-17 13:17:49 -04002864static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
David Howells88e67f32008-11-14 10:39:21 +11002866 const struct cred *cred = current_cred();
2867
Eric Paris2875fa02011-04-28 16:04:24 -04002868 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
David Howells8f0cfa52008-04-29 00:59:41 -07002871static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002873 if (strcmp(name, XATTR_NAME_SELINUX))
2874 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 /* No one is allowed to remove a SELinux security label.
2877 You can change the label, but all data must be labeled. */
2878 return -EACCES;
2879}
2880
James Morrisd381d8a2005-10-30 14:59:22 -08002881/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002882 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002883 *
2884 * Permission check is handled by selinux_inode_getxattr hook.
2885 */
David P. Quigley42492592008-02-04 22:29:39 -08002886static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
David P. Quigley42492592008-02-04 22:29:39 -08002888 u32 size;
2889 int error;
2890 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002893 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2894 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002896 /*
2897 * If the caller has CAP_MAC_ADMIN, then get the raw context
2898 * value even if it is not defined by current policy; otherwise,
2899 * use the in-core value under current policy.
2900 * Use the non-auditing forms of the permission checks since
2901 * getxattr may be called by unprivileged processes commonly
2902 * and lack of permission just means that we fall back to the
2903 * in-core context value, not a denial.
2904 */
Eric Paris6a9de492012-01-03 12:25:14 -05002905 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002906 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002907 if (!error)
2908 error = security_sid_to_context_force(isec->sid, &context,
2909 &size);
2910 else
2911 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002912 if (error)
2913 return error;
2914 error = size;
2915 if (alloc) {
2916 *buffer = context;
2917 goto out_nofree;
2918 }
2919 kfree(context);
2920out_nofree:
2921 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
2924static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04002925 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 struct inode_security_struct *isec = inode->i_security;
2928 u32 newsid;
2929 int rc;
2930
2931 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2932 return -EOPNOTSUPP;
2933
2934 if (!value || !size)
2935 return -EACCES;
2936
Eric Paris828dfe12008-04-17 13:17:49 -04002937 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (rc)
2939 return rc;
2940
2941 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002942 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return 0;
2944}
2945
2946static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2947{
2948 const int len = sizeof(XATTR_NAME_SELINUX);
2949 if (buffer && len <= buffer_size)
2950 memcpy(buffer, XATTR_NAME_SELINUX, len);
2951 return len;
2952}
2953
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02002954static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2955{
2956 struct inode_security_struct *isec = inode->i_security;
2957 *secid = isec->sid;
2958}
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960/* file security operations */
2961
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002962static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
David Howells88e67f32008-11-14 10:39:21 +11002964 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05002965 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2968 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
2969 mask |= MAY_APPEND;
2970
Paul Moore389fb8002009-03-27 17:10:34 -04002971 return file_has_perm(cred, file,
2972 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002975static int selinux_file_permission(struct file *file, int mask)
2976{
Al Viro496ad9a2013-01-23 17:07:38 -05002977 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04002978 struct file_security_struct *fsec = file->f_security;
2979 struct inode_security_struct *isec = inode->i_security;
2980 u32 sid = current_sid();
2981
Paul Moore389fb8002009-03-27 17:10:34 -04002982 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002983 /* No permission to check. Existence test. */
2984 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002985
Stephen Smalley20dda182009-06-22 14:54:53 -04002986 if (sid == fsec->sid && fsec->isid == isec->sid &&
2987 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04002988 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04002989 return 0;
2990
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002991 return selinux_revalidate_file_permission(file, mask);
2992}
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994static int selinux_file_alloc_security(struct file *file)
2995{
2996 return file_alloc_security(file);
2997}
2998
2999static void selinux_file_free_security(struct file *file)
3000{
3001 file_free_security(file);
3002}
3003
3004static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3005 unsigned long arg)
3006{
David Howells88e67f32008-11-14 10:39:21 +11003007 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003008 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Eric Paris0b24dcb2011-02-25 15:39:20 -05003010 switch (cmd) {
3011 case FIONREAD:
3012 /* fall through */
3013 case FIBMAP:
3014 /* fall through */
3015 case FIGETBSZ:
3016 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003017 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003018 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003019 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003020 error = file_has_perm(cred, file, FILE__GETATTR);
3021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Al Viro2f99c362012-03-23 16:04:05 -04003023 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003024 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003025 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003026 error = file_has_perm(cred, file, FILE__SETATTR);
3027 break;
3028
3029 /* sys_ioctl() checks */
3030 case FIONBIO:
3031 /* fall through */
3032 case FIOASYNC:
3033 error = file_has_perm(cred, file, 0);
3034 break;
3035
3036 case KDSKBENT:
3037 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003038 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3039 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003040 break;
3041
3042 /* default case assumes that the command will go
3043 * to the file's ioctl() function.
3044 */
3045 default:
3046 error = file_has_perm(cred, file, FILE__IOCTL);
3047 }
3048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003051static int default_noexec;
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3054{
David Howells88e67f32008-11-14 10:39:21 +11003055 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003056 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003057
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003058 if (default_noexec &&
3059 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 /*
3061 * We are making executable an anonymous mapping or a
3062 * private file mapping that will also be writable.
3063 * This has an additional check.
3064 */
David Howellsd84f4f92008-11-14 10:39:23 +11003065 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003067 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 if (file) {
3071 /* read access is always possible with a mapping */
3072 u32 av = FILE__READ;
3073
3074 /* write access only matters if the mapping is shared */
3075 if (shared && (prot & PROT_WRITE))
3076 av |= FILE__WRITE;
3077
3078 if (prot & PROT_EXEC)
3079 av |= FILE__EXECUTE;
3080
David Howells88e67f32008-11-14 10:39:21 +11003081 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
David Howellsd84f4f92008-11-14 10:39:23 +11003083
3084error:
3085 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Al Viroe5467852012-05-30 13:30:51 -04003088static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Eric Parised032182007-06-28 15:55:21 -04003090 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003091 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Eric Paris84336d1a2009-07-31 12:54:05 -04003093 /*
3094 * notice that we are intentionally putting the SELinux check before
3095 * the secondary cap_file_mmap check. This is such a likely attempt
3096 * at bad behaviour/exploit that we always want to get the AVC, even
3097 * if DAC would have also denied the operation.
3098 */
Eric Parisa2551df2009-07-31 12:54:11 -04003099 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003100 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3101 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003102 if (rc)
3103 return rc;
3104 }
3105
3106 /* do DAC check on address space usage */
Al Viroe5467852012-05-30 13:30:51 -04003107 return cap_mmap_addr(addr);
3108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Al Viroe5467852012-05-30 13:30:51 -04003110static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3111 unsigned long prot, unsigned long flags)
3112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (selinux_checkreqprot)
3114 prot = reqprot;
3115
3116 return file_map_prot_check(file, prot,
3117 (flags & MAP_TYPE) == MAP_SHARED);
3118}
3119
3120static int selinux_file_mprotect(struct vm_area_struct *vma,
3121 unsigned long reqprot,
3122 unsigned long prot)
3123{
David Howells88e67f32008-11-14 10:39:21 +11003124 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 if (selinux_checkreqprot)
3127 prot = reqprot;
3128
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003129 if (default_noexec &&
3130 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003131 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003132 if (vma->vm_start >= vma->vm_mm->start_brk &&
3133 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003134 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003135 } else if (!vma->vm_file &&
3136 vma->vm_start <= vma->vm_mm->start_stack &&
3137 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003138 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003139 } else if (vma->vm_file && vma->anon_vma) {
3140 /*
3141 * We are making executable a file mapping that has
3142 * had some COW done. Since pages might have been
3143 * written, check ability to execute the possibly
3144 * modified content. This typically should only
3145 * occur for text relocations.
3146 */
David Howellsd84f4f92008-11-14 10:39:23 +11003147 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003148 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003149 if (rc)
3150 return rc;
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3154}
3155
3156static int selinux_file_lock(struct file *file, unsigned int cmd)
3157{
David Howells88e67f32008-11-14 10:39:21 +11003158 const struct cred *cred = current_cred();
3159
3160 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
3163static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3164 unsigned long arg)
3165{
David Howells88e67f32008-11-14 10:39:21 +11003166 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 int err = 0;
3168
3169 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003170 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003171 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003172 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003174 }
3175 /* fall through */
3176 case F_SETOWN:
3177 case F_SETSIG:
3178 case F_GETFL:
3179 case F_GETOWN:
3180 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003181 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003182 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003183 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003184 break;
3185 case F_GETLK:
3186 case F_SETLK:
3187 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003189 case F_GETLK64:
3190 case F_SETLK64:
3191 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192#endif
David Howells88e67f32008-11-14 10:39:21 +11003193 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196
3197 return err;
3198}
3199
3200static int selinux_file_set_fowner(struct file *file)
3201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct file_security_struct *fsec;
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003205 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 return 0;
3208}
3209
3210static int selinux_file_send_sigiotask(struct task_struct *tsk,
3211 struct fown_struct *fown, int signum)
3212{
Eric Paris828dfe12008-04-17 13:17:49 -04003213 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003214 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 struct file_security_struct *fsec;
3217
3218 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003219 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 fsec = file->f_security;
3222
3223 if (!signum)
3224 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3225 else
3226 perm = signal_to_av(signum);
3227
David Howells275bb412008-11-14 10:39:19 +11003228 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 SECCLASS_PROCESS, perm, NULL);
3230}
3231
3232static int selinux_file_receive(struct file *file)
3233{
David Howells88e67f32008-11-14 10:39:21 +11003234 const struct cred *cred = current_cred();
3235
3236 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
Eric Paris83d49852012-04-04 13:45:40 -04003239static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003240{
3241 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003242 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003243
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003244 fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05003245 isec = file_inode(file)->i_security;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003246 /*
3247 * Save inode label and policy sequence number
3248 * at open-time so that selinux_file_permission
3249 * can determine whether revalidation is necessary.
3250 * Task label is already saved in the file security
3251 * struct as its SID.
3252 */
3253 fsec->isid = isec->sid;
3254 fsec->pseqno = avc_policy_seqno();
3255 /*
3256 * Since the inode label or policy seqno may have changed
3257 * between the selinux_inode_permission check and the saving
3258 * of state above, recheck that access is still permitted.
3259 * Otherwise, access might never be revalidated against the
3260 * new inode label or new policy.
3261 * This check is not redundant - do not remove.
3262 */
Eric Paris602a8dd2012-04-04 15:01:42 -04003263 return path_has_perm(cred, &file->f_path, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003264}
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266/* task security operations */
3267
3268static int selinux_task_create(unsigned long clone_flags)
3269{
David Howells3b11a1d2008-11-14 10:39:26 +11003270 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
David Howellsf1752ee2008-11-14 10:39:17 +11003273/*
David Howellsee18d642009-09-02 09:14:21 +01003274 * allocate the SELinux part of blank credentials
3275 */
3276static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3277{
3278 struct task_security_struct *tsec;
3279
3280 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3281 if (!tsec)
3282 return -ENOMEM;
3283
3284 cred->security = tsec;
3285 return 0;
3286}
3287
3288/*
David Howellsf1752ee2008-11-14 10:39:17 +11003289 * detach and free the LSM part of a set of credentials
3290 */
3291static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
David Howellsf1752ee2008-11-14 10:39:17 +11003293 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003294
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003295 /*
3296 * cred->security == NULL if security_cred_alloc_blank() or
3297 * security_prepare_creds() returned an error.
3298 */
3299 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003300 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003301 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
David Howellsd84f4f92008-11-14 10:39:23 +11003304/*
3305 * prepare a new set of credentials for modification
3306 */
3307static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3308 gfp_t gfp)
3309{
3310 const struct task_security_struct *old_tsec;
3311 struct task_security_struct *tsec;
3312
3313 old_tsec = old->security;
3314
3315 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3316 if (!tsec)
3317 return -ENOMEM;
3318
3319 new->security = tsec;
3320 return 0;
3321}
3322
3323/*
David Howellsee18d642009-09-02 09:14:21 +01003324 * transfer the SELinux data to a blank set of creds
3325 */
3326static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3327{
3328 const struct task_security_struct *old_tsec = old->security;
3329 struct task_security_struct *tsec = new->security;
3330
3331 *tsec = *old_tsec;
3332}
3333
3334/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003335 * set the security data for a kernel service
3336 * - all the creation contexts are set to unlabelled
3337 */
3338static int selinux_kernel_act_as(struct cred *new, u32 secid)
3339{
3340 struct task_security_struct *tsec = new->security;
3341 u32 sid = current_sid();
3342 int ret;
3343
3344 ret = avc_has_perm(sid, secid,
3345 SECCLASS_KERNEL_SERVICE,
3346 KERNEL_SERVICE__USE_AS_OVERRIDE,
3347 NULL);
3348 if (ret == 0) {
3349 tsec->sid = secid;
3350 tsec->create_sid = 0;
3351 tsec->keycreate_sid = 0;
3352 tsec->sockcreate_sid = 0;
3353 }
3354 return ret;
3355}
3356
3357/*
3358 * set the file creation context in a security record to the same as the
3359 * objective context of the specified inode
3360 */
3361static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3362{
3363 struct inode_security_struct *isec = inode->i_security;
3364 struct task_security_struct *tsec = new->security;
3365 u32 sid = current_sid();
3366 int ret;
3367
3368 ret = avc_has_perm(sid, isec->sid,
3369 SECCLASS_KERNEL_SERVICE,
3370 KERNEL_SERVICE__CREATE_FILES_AS,
3371 NULL);
3372
3373 if (ret == 0)
3374 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003375 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003376}
3377
Eric Parisdd8dbf22009-11-03 16:35:32 +11003378static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003379{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003380 u32 sid;
3381 struct common_audit_data ad;
3382
3383 sid = task_sid(current);
3384
Eric Paris50c205f2012-04-04 15:01:43 -04003385 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003386 ad.u.kmod_name = kmod_name;
3387
3388 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3389 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003390}
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3393{
David Howells3b11a1d2008-11-14 10:39:26 +11003394 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397static int selinux_task_getpgid(struct task_struct *p)
3398{
David Howells3b11a1d2008-11-14 10:39:26 +11003399 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
3402static int selinux_task_getsid(struct task_struct *p)
3403{
David Howells3b11a1d2008-11-14 10:39:26 +11003404 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
David Quigleyf9008e42006-06-30 01:55:46 -07003407static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3408{
David Howells275bb412008-11-14 10:39:19 +11003409 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003410}
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412static int selinux_task_setnice(struct task_struct *p, int nice)
3413{
3414 int rc;
3415
Eric Paris200ac532009-02-12 15:01:04 -05003416 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (rc)
3418 return rc;
3419
David Howells3b11a1d2008-11-14 10:39:26 +11003420 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
James Morris03e68062006-06-23 02:03:58 -07003423static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3424{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003425 int rc;
3426
Eric Paris200ac532009-02-12 15:01:04 -05003427 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003428 if (rc)
3429 return rc;
3430
David Howells3b11a1d2008-11-14 10:39:26 +11003431 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003432}
3433
David Quigleya1836a42006-06-30 01:55:49 -07003434static int selinux_task_getioprio(struct task_struct *p)
3435{
David Howells3b11a1d2008-11-14 10:39:26 +11003436 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003437}
3438
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003439static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3440 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003442 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
3444 /* Control the ability to change the hard limit (whether
3445 lowering or raising it), so that the hard limit can
3446 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003447 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003449 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 return 0;
3452}
3453
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003454static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003456 int rc;
3457
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003458 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003459 if (rc)
3460 return rc;
3461
David Howells3b11a1d2008-11-14 10:39:26 +11003462 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463}
3464
3465static int selinux_task_getscheduler(struct task_struct *p)
3466{
David Howells3b11a1d2008-11-14 10:39:26 +11003467 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
David Quigley35601542006-06-23 02:04:01 -07003470static int selinux_task_movememory(struct task_struct *p)
3471{
David Howells3b11a1d2008-11-14 10:39:26 +11003472 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003473}
3474
David Quigleyf9008e42006-06-30 01:55:46 -07003475static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3476 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
3478 u32 perm;
3479 int rc;
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (!sig)
3482 perm = PROCESS__SIGNULL; /* null signal; existence test */
3483 else
3484 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003485 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003486 rc = avc_has_perm(secid, task_sid(p),
3487 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003488 else
David Howells3b11a1d2008-11-14 10:39:26 +11003489 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003490 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static int selinux_task_wait(struct task_struct *p)
3494{
Eric Paris8a535142007-10-22 16:10:31 -04003495 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498static void selinux_task_to_inode(struct task_struct *p,
3499 struct inode *inode)
3500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003502 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
David Howells275bb412008-11-14 10:39:19 +11003504 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003509static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003510 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
3512 int offset, ihlen, ret = -EINVAL;
3513 struct iphdr _iph, *ih;
3514
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003515 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3517 if (ih == NULL)
3518 goto out;
3519
3520 ihlen = ih->ihl * 4;
3521 if (ihlen < sizeof(_iph))
3522 goto out;
3523
Eric Paris48c62af2012-04-02 13:15:44 -04003524 ad->u.net->v4info.saddr = ih->saddr;
3525 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 ret = 0;
3527
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003528 if (proto)
3529 *proto = ih->protocol;
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003532 case IPPROTO_TCP: {
3533 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Eric Paris828dfe12008-04-17 13:17:49 -04003535 if (ntohs(ih->frag_off) & IP_OFFSET)
3536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 offset += ihlen;
3539 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3540 if (th == NULL)
3541 break;
3542
Eric Paris48c62af2012-04-02 13:15:44 -04003543 ad->u.net->sport = th->source;
3544 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Eric Paris828dfe12008-04-17 13:17:49 -04003548 case IPPROTO_UDP: {
3549 struct udphdr _udph, *uh;
3550
3551 if (ntohs(ih->frag_off) & IP_OFFSET)
3552 break;
3553
3554 offset += ihlen;
3555 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3556 if (uh == NULL)
3557 break;
3558
Eric Paris48c62af2012-04-02 13:15:44 -04003559 ad->u.net->sport = uh->source;
3560 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003561 break;
3562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
James Morris2ee92d42006-11-13 16:09:01 -08003564 case IPPROTO_DCCP: {
3565 struct dccp_hdr _dccph, *dh;
3566
3567 if (ntohs(ih->frag_off) & IP_OFFSET)
3568 break;
3569
3570 offset += ihlen;
3571 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3572 if (dh == NULL)
3573 break;
3574
Eric Paris48c62af2012-04-02 13:15:44 -04003575 ad->u.net->sport = dh->dccph_sport;
3576 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003577 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003578 }
James Morris2ee92d42006-11-13 16:09:01 -08003579
Eric Paris828dfe12008-04-17 13:17:49 -04003580 default:
3581 break;
3582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583out:
3584 return ret;
3585}
3586
3587#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3588
3589/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003590static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003591 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 u8 nexthdr;
3594 int ret = -EINVAL, offset;
3595 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003596 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003598 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3600 if (ip6 == NULL)
3601 goto out;
3602
Eric Paris48c62af2012-04-02 13:15:44 -04003603 ad->u.net->v6info.saddr = ip6->saddr;
3604 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 ret = 0;
3606
3607 nexthdr = ip6->nexthdr;
3608 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003609 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 if (offset < 0)
3611 goto out;
3612
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003613 if (proto)
3614 *proto = nexthdr;
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 switch (nexthdr) {
3617 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003618 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3621 if (th == NULL)
3622 break;
3623
Eric Paris48c62af2012-04-02 13:15:44 -04003624 ad->u.net->sport = th->source;
3625 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 break;
3627 }
3628
3629 case IPPROTO_UDP: {
3630 struct udphdr _udph, *uh;
3631
3632 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3633 if (uh == NULL)
3634 break;
3635
Eric Paris48c62af2012-04-02 13:15:44 -04003636 ad->u.net->sport = uh->source;
3637 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 break;
3639 }
3640
James Morris2ee92d42006-11-13 16:09:01 -08003641 case IPPROTO_DCCP: {
3642 struct dccp_hdr _dccph, *dh;
3643
3644 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3645 if (dh == NULL)
3646 break;
3647
Eric Paris48c62af2012-04-02 13:15:44 -04003648 ad->u.net->sport = dh->dccph_sport;
3649 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003650 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003651 }
James Morris2ee92d42006-11-13 16:09:01 -08003652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /* includes fragments */
3654 default:
3655 break;
3656 }
3657out:
3658 return ret;
3659}
3660
3661#endif /* IPV6 */
3662
Thomas Liu2bf49692009-07-14 12:14:09 -04003663static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003664 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
David Howellscf9481e2008-07-27 21:31:07 +10003666 char *addrp;
3667 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Eric Paris48c62af2012-04-02 13:15:44 -04003669 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003671 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003672 if (ret)
3673 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003674 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3675 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003676 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3679 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003680 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003681 if (ret)
3682 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003683 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3684 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003685 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686#endif /* IPV6 */
3687 default:
David Howellscf9481e2008-07-27 21:31:07 +10003688 addrp = NULL;
3689 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 }
3691
David Howellscf9481e2008-07-27 21:31:07 +10003692parse_error:
3693 printk(KERN_WARNING
3694 "SELinux: failure in selinux_parse_skb(),"
3695 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003697
3698okay:
3699 if (_addrp)
3700 *_addrp = addrp;
3701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Paul Moore4f6a9932007-03-01 14:35:22 -05003704/**
Paul Moore220deb92008-01-29 08:38:23 -05003705 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003706 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003707 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003708 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003709 *
3710 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003711 * Check the various different forms of network peer labeling and determine
3712 * the peer label/SID for the packet; most of the magic actually occurs in
3713 * the security server function security_net_peersid_cmp(). The function
3714 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3715 * or -EACCES if @sid is invalid due to inconsistencies with the different
3716 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003717 *
3718 */
Paul Moore220deb92008-01-29 08:38:23 -05003719static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003720{
Paul Moore71f1cb02008-01-29 08:51:16 -05003721 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003722 u32 xfrm_sid;
3723 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003724 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003725
3726 selinux_skb_xfrm_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003727 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003728
Paul Moore71f1cb02008-01-29 08:51:16 -05003729 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3730 if (unlikely(err)) {
3731 printk(KERN_WARNING
3732 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3733 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003734 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003735 }
Paul Moore220deb92008-01-29 08:38:23 -05003736
3737 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003738}
3739
Paul Moore348d7862013-12-04 16:10:51 -05003740/**
3741 * selinux_conn_sid - Determine the child socket label for a connection
3742 * @sk_sid: the parent socket's SID
3743 * @skb_sid: the packet's SID
3744 * @conn_sid: the resulting connection SID
3745 *
3746 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3747 * combined with the MLS information from @skb_sid in order to create
3748 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3749 * of @sk_sid. Returns zero on success, negative values on failure.
3750 *
3751 */
3752static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3753{
3754 int err = 0;
3755
3756 if (skb_sid != SECSID_NULL)
3757 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3758 else
3759 *conn_sid = sk_sid;
3760
3761 return err;
3762}
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003765
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003766static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3767 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003768{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003769 if (tsec->sockcreate_sid > SECSID_NULL) {
3770 *socksid = tsec->sockcreate_sid;
3771 return 0;
3772 }
3773
3774 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3775 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003776}
3777
Paul Moore253bfae2010-04-22 14:46:19 -04003778static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Paul Moore253bfae2010-04-22 14:46:19 -04003780 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003781 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003782 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003783 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Paul Moore253bfae2010-04-22 14:46:19 -04003785 if (sksec->sid == SECINITSID_KERNEL)
3786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Eric Paris50c205f2012-04-04 15:01:43 -04003788 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003789 ad.u.net = &net;
3790 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Paul Moore253bfae2010-04-22 14:46:19 -04003792 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
3795static int selinux_socket_create(int family, int type,
3796 int protocol, int kern)
3797{
Paul Moore5fb49872010-04-22 14:46:19 -04003798 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003799 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003800 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003801 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
David Howells275bb412008-11-14 10:39:19 +11003806 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003807 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3808 if (rc)
3809 return rc;
3810
Paul Moored4f2d972010-04-22 14:46:18 -04003811 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003814static int selinux_socket_post_create(struct socket *sock, int family,
3815 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
Paul Moore5fb49872010-04-22 14:46:19 -04003817 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003818 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003819 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003820 int err = 0;
3821
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003822 isec->sclass = socket_type_to_security_class(family, type, protocol);
3823
David Howells275bb412008-11-14 10:39:19 +11003824 if (kern)
3825 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003826 else {
3827 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3828 if (err)
3829 return err;
3830 }
David Howells275bb412008-11-14 10:39:19 +11003831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 isec->initialized = 1;
3833
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003834 if (sock->sk) {
3835 sksec = sock->sk->sk_security;
3836 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003837 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04003838 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003839 }
3840
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003841 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
3844/* Range of port numbers used to automatically bind.
3845 Need to determine whether we should perform a name_bind
3846 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
3848static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3849{
Paul Moore253bfae2010-04-22 14:46:19 -04003850 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 u16 family;
3852 int err;
3853
Paul Moore253bfae2010-04-22 14:46:19 -04003854 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 if (err)
3856 goto out;
3857
3858 /*
3859 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003860 * Multiple address binding for SCTP is not supported yet: we just
3861 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 */
Paul Moore253bfae2010-04-22 14:46:19 -04003863 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 if (family == PF_INET || family == PF_INET6) {
3865 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003866 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003867 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003868 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 struct sockaddr_in *addr4 = NULL;
3870 struct sockaddr_in6 *addr6 = NULL;
3871 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003872 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 if (family == PF_INET) {
3875 addr4 = (struct sockaddr_in *)address;
3876 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 addrp = (char *)&addr4->sin_addr.s_addr;
3878 } else {
3879 addr6 = (struct sockaddr_in6 *)address;
3880 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 addrp = (char *)&addr6->sin6_addr.s6_addr;
3882 }
3883
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003884 if (snum) {
3885 int low, high;
3886
3887 inet_get_local_port_range(&low, &high);
3888
3889 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003890 err = sel_netport_sid(sk->sk_protocol,
3891 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003892 if (err)
3893 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04003894 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003895 ad.u.net = &net;
3896 ad.u.net->sport = htons(snum);
3897 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04003898 err = avc_has_perm(sksec->sid, sid,
3899 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003900 SOCKET__NAME_BIND, &ad);
3901 if (err)
3902 goto out;
3903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 }
Eric Paris828dfe12008-04-17 13:17:49 -04003905
Paul Moore253bfae2010-04-22 14:46:19 -04003906 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04003907 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 node_perm = TCP_SOCKET__NODE_BIND;
3909 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003910
James Morris13402582005-09-30 14:24:34 -04003911 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 node_perm = UDP_SOCKET__NODE_BIND;
3913 break;
James Morris2ee92d42006-11-13 16:09:01 -08003914
3915 case SECCLASS_DCCP_SOCKET:
3916 node_perm = DCCP_SOCKET__NODE_BIND;
3917 break;
3918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 default:
3920 node_perm = RAWIP_SOCKET__NODE_BIND;
3921 break;
3922 }
Eric Paris828dfe12008-04-17 13:17:49 -04003923
Paul Moore224dfbd2008-01-29 08:38:13 -05003924 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 if (err)
3926 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04003927
Eric Paris50c205f2012-04-04 15:01:43 -04003928 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003929 ad.u.net = &net;
3930 ad.u.net->sport = htons(snum);
3931 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04003934 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 else
Eric Paris48c62af2012-04-02 13:15:44 -04003936 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Paul Moore253bfae2010-04-22 14:46:19 -04003938 err = avc_has_perm(sksec->sid, sid,
3939 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (err)
3941 goto out;
3942 }
3943out:
3944 return err;
3945}
3946
3947static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3948{
Paul Moore014ab192008-10-10 10:16:33 -04003949 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04003950 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 int err;
3952
Paul Moore253bfae2010-04-22 14:46:19 -04003953 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (err)
3955 return err;
3956
3957 /*
James Morris2ee92d42006-11-13 16:09:01 -08003958 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 */
Paul Moore253bfae2010-04-22 14:46:19 -04003960 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
3961 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04003962 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003963 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 struct sockaddr_in *addr4 = NULL;
3965 struct sockaddr_in6 *addr6 = NULL;
3966 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08003967 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 if (sk->sk_family == PF_INET) {
3970 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003971 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return -EINVAL;
3973 snum = ntohs(addr4->sin_port);
3974 } else {
3975 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003976 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 return -EINVAL;
3978 snum = ntohs(addr6->sin6_port);
3979 }
3980
Paul Moore3e112172008-04-10 10:48:14 -04003981 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 if (err)
3983 goto out;
3984
Paul Moore253bfae2010-04-22 14:46:19 -04003985 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08003986 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
3987
Eric Paris50c205f2012-04-04 15:01:43 -04003988 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003989 ad.u.net = &net;
3990 ad.u.net->dport = htons(snum);
3991 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04003992 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 if (err)
3994 goto out;
3995 }
3996
Paul Moore014ab192008-10-10 10:16:33 -04003997 err = selinux_netlbl_socket_connect(sk, address);
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999out:
4000 return err;
4001}
4002
4003static int selinux_socket_listen(struct socket *sock, int backlog)
4004{
Paul Moore253bfae2010-04-22 14:46:19 -04004005 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
4008static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4009{
4010 int err;
4011 struct inode_security_struct *isec;
4012 struct inode_security_struct *newisec;
4013
Paul Moore253bfae2010-04-22 14:46:19 -04004014 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (err)
4016 return err;
4017
4018 newisec = SOCK_INODE(newsock)->i_security;
4019
4020 isec = SOCK_INODE(sock)->i_security;
4021 newisec->sclass = isec->sclass;
4022 newisec->sid = isec->sid;
4023 newisec->initialized = 1;
4024
4025 return 0;
4026}
4027
4028static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004029 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Paul Moore253bfae2010-04-22 14:46:19 -04004031 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
4034static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4035 int size, int flags)
4036{
Paul Moore253bfae2010-04-22 14:46:19 -04004037 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
4040static int selinux_socket_getsockname(struct socket *sock)
4041{
Paul Moore253bfae2010-04-22 14:46:19 -04004042 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
4045static int selinux_socket_getpeername(struct socket *sock)
4046{
Paul Moore253bfae2010-04-22 14:46:19 -04004047 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
Eric Paris828dfe12008-04-17 13:17:49 -04004050static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
Paul Mooref8687af2006-10-30 15:22:15 -08004052 int err;
4053
Paul Moore253bfae2010-04-22 14:46:19 -04004054 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004055 if (err)
4056 return err;
4057
4058 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
4061static int selinux_socket_getsockopt(struct socket *sock, int level,
4062 int optname)
4063{
Paul Moore253bfae2010-04-22 14:46:19 -04004064 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
4067static int selinux_socket_shutdown(struct socket *sock, int how)
4068{
Paul Moore253bfae2010-04-22 14:46:19 -04004069 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
David S. Miller3610cda2011-01-05 15:38:53 -08004072static int selinux_socket_unix_stream_connect(struct sock *sock,
4073 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 struct sock *newsk)
4075{
David S. Miller3610cda2011-01-05 15:38:53 -08004076 struct sk_security_struct *sksec_sock = sock->sk_security;
4077 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004078 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004079 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004080 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 int err;
4082
Eric Paris50c205f2012-04-04 15:01:43 -04004083 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004084 ad.u.net = &net;
4085 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Paul Moore4d1e2452010-04-22 14:46:18 -04004087 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4088 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4090 if (err)
4091 return err;
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004094 sksec_new->peer_sid = sksec_sock->sid;
4095 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4096 &sksec_new->sid);
4097 if (err)
4098 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004099
Paul Moore4d1e2452010-04-22 14:46:18 -04004100 /* connecting socket */
4101 sksec_sock->peer_sid = sksec_new->sid;
4102
4103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
4106static int selinux_socket_unix_may_send(struct socket *sock,
4107 struct socket *other)
4108{
Paul Moore253bfae2010-04-22 14:46:19 -04004109 struct sk_security_struct *ssec = sock->sk->sk_security;
4110 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004111 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004112 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Eric Paris50c205f2012-04-04 15:01:43 -04004114 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004115 ad.u.net = &net;
4116 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Paul Moore253bfae2010-04-22 14:46:19 -04004118 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4119 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
Paul Mooreeffad8d2008-01-29 08:49:27 -05004122static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4123 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004124 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004125{
4126 int err;
4127 u32 if_sid;
4128 u32 node_sid;
4129
4130 err = sel_netif_sid(ifindex, &if_sid);
4131 if (err)
4132 return err;
4133 err = avc_has_perm(peer_sid, if_sid,
4134 SECCLASS_NETIF, NETIF__INGRESS, ad);
4135 if (err)
4136 return err;
4137
4138 err = sel_netnode_sid(addrp, family, &node_sid);
4139 if (err)
4140 return err;
4141 return avc_has_perm(peer_sid, node_sid,
4142 SECCLASS_NODE, NODE__RECVFROM, ad);
4143}
4144
Paul Moore220deb92008-01-29 08:38:23 -05004145static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004146 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004147{
Paul Moore277d3422008-12-31 12:54:11 -05004148 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004149 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004150 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004151 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004152 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004153 char *addrp;
4154
Eric Paris50c205f2012-04-04 15:01:43 -04004155 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004156 ad.u.net = &net;
4157 ad.u.net->netif = skb->skb_iif;
4158 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004159 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4160 if (err)
4161 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004162
Paul Moore58bfbb52009-03-27 17:10:41 -04004163 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004164 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004165 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004166 if (err)
4167 return err;
4168 }
Paul Moore220deb92008-01-29 08:38:23 -05004169
Steffen Klassertb9679a72011-02-23 12:55:21 +01004170 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4171 if (err)
4172 return err;
4173 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004174
James Morris4e5ab4c2006-06-09 00:33:33 -07004175 return err;
4176}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004177
James Morris4e5ab4c2006-06-09 00:33:33 -07004178static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4179{
Paul Moore220deb92008-01-29 08:38:23 -05004180 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004181 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004182 u16 family = sk->sk_family;
4183 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004184 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004185 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004186 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004187 u8 secmark_active;
4188 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004189
James Morris4e5ab4c2006-06-09 00:33:33 -07004190 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004191 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004192
4193 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004194 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004195 family = PF_INET;
4196
Paul Moored8395c82008-10-10 10:16:30 -04004197 /* If any sort of compatibility mode is enabled then handoff processing
4198 * to the selinux_sock_rcv_skb_compat() function to deal with the
4199 * special handling. We do this in an attempt to keep this function
4200 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004201 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004202 return selinux_sock_rcv_skb_compat(sk, skb, family);
4203
4204 secmark_active = selinux_secmark_enabled();
4205 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4206 if (!secmark_active && !peerlbl_active)
4207 return 0;
4208
Eric Paris50c205f2012-04-04 15:01:43 -04004209 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004210 ad.u.net = &net;
4211 ad.u.net->netif = skb->skb_iif;
4212 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004213 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004214 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004215 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004216
Paul Moored8395c82008-10-10 10:16:30 -04004217 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004218 u32 peer_sid;
4219
4220 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4221 if (err)
4222 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004223 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004224 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004225 if (err) {
4226 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004227 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004228 }
Paul Moored621d352008-01-29 08:43:36 -05004229 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4230 PEER__RECV, &ad);
Chad Hansonfaecbbe2013-12-23 17:45:01 -05004231 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004232 selinux_netlbl_err(skb, err, 0);
Chad Hansonfaecbbe2013-12-23 17:45:01 -05004233 return err;
4234 }
Paul Moored621d352008-01-29 08:43:36 -05004235 }
4236
Paul Moored8395c82008-10-10 10:16:30 -04004237 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004238 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4239 PACKET__RECV, &ad);
4240 if (err)
4241 return err;
4242 }
4243
Paul Moored621d352008-01-29 08:43:36 -05004244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004247static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4248 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
4250 int err = 0;
4251 char *scontext;
4252 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004253 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004254 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Paul Moore253bfae2010-04-22 14:46:19 -04004256 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4257 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004258 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004259 if (peer_sid == SECSID_NULL)
4260 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004262 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
4266 if (scontext_len > len) {
4267 err = -ERANGE;
4268 goto out_len;
4269 }
4270
4271 if (copy_to_user(optval, scontext, scontext_len))
4272 err = -EFAULT;
4273
4274out_len:
4275 if (put_user(scontext_len, optlen))
4276 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return err;
4279}
4280
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004281static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004282{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004283 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004284 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004285
Paul Mooreaa862902008-10-10 10:16:29 -04004286 if (skb && skb->protocol == htons(ETH_P_IP))
4287 family = PF_INET;
4288 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4289 family = PF_INET6;
4290 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004291 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004292 else
4293 goto out;
4294
4295 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02004296 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004297 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004298 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004299
Paul Moore75e22912008-01-29 08:38:04 -05004300out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004301 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004302 if (peer_secid == SECSID_NULL)
4303 return -EINVAL;
4304 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004305}
4306
Al Viro7d877f32005-10-21 03:20:43 -04004307static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Paul Moore84914b72010-04-22 14:46:18 -04004309 struct sk_security_struct *sksec;
4310
4311 sksec = kzalloc(sizeof(*sksec), priority);
4312 if (!sksec)
4313 return -ENOMEM;
4314
4315 sksec->peer_sid = SECINITSID_UNLABELED;
4316 sksec->sid = SECINITSID_UNLABELED;
4317 selinux_netlbl_sk_security_reset(sksec);
4318 sk->sk_security = sksec;
4319
4320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}
4322
4323static void selinux_sk_free_security(struct sock *sk)
4324{
Paul Moore84914b72010-04-22 14:46:18 -04004325 struct sk_security_struct *sksec = sk->sk_security;
4326
4327 sk->sk_security = NULL;
4328 selinux_netlbl_sk_security_free(sksec);
4329 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330}
4331
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004332static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4333{
Eric Parisdd3e7832010-04-07 15:08:46 -04004334 struct sk_security_struct *sksec = sk->sk_security;
4335 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004336
Eric Parisdd3e7832010-04-07 15:08:46 -04004337 newsksec->sid = sksec->sid;
4338 newsksec->peer_sid = sksec->peer_sid;
4339 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004340
Eric Parisdd3e7832010-04-07 15:08:46 -04004341 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004342}
4343
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004344static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004345{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004346 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004347 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004348 else {
4349 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004350
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004351 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004352 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004353}
4354
Eric Paris828dfe12008-04-17 13:17:49 -04004355static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004356{
4357 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4358 struct sk_security_struct *sksec = sk->sk_security;
4359
David Woodhouse2148ccc2006-09-29 15:50:25 -07004360 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4361 sk->sk_family == PF_UNIX)
4362 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004363 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004364}
4365
Adrian Bunk9a673e52006-08-15 00:03:53 -07004366static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4367 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004368{
4369 struct sk_security_struct *sksec = sk->sk_security;
4370 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004371 u16 family = sk->sk_family;
Paul Moore348d7862013-12-04 16:10:51 -05004372 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004373 u32 peersid;
4374
Paul Mooreaa862902008-10-10 10:16:29 -04004375 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4376 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4377 family = PF_INET;
4378
4379 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004380 if (err)
4381 return err;
Paul Moore348d7862013-12-04 16:10:51 -05004382 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4383 if (err)
4384 return err;
4385 req->secid = connsid;
4386 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004387
Paul Moore389fb8002009-03-27 17:10:34 -04004388 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004389}
4390
Adrian Bunk9a673e52006-08-15 00:03:53 -07004391static void selinux_inet_csk_clone(struct sock *newsk,
4392 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004393{
4394 struct sk_security_struct *newsksec = newsk->sk_security;
4395
4396 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004397 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004398 /* NOTE: Ideally, we should also get the isec->sid for the
4399 new socket in sync, but we don't have the isec available yet.
4400 So we will wait until sock_graft to do it, by which
4401 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004402
Paul Moore9f2ad662006-11-17 17:38:53 -05004403 /* We don't need to take any sort of lock here as we are the only
4404 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004405 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004406}
4407
Paul Moore014ab192008-10-10 10:16:33 -04004408static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004409{
Paul Mooreaa862902008-10-10 10:16:29 -04004410 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004411 struct sk_security_struct *sksec = sk->sk_security;
4412
Paul Mooreaa862902008-10-10 10:16:29 -04004413 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4414 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4415 family = PF_INET;
4416
4417 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004418}
4419
Eric Dumazetca10b9e2013-04-08 17:58:11 +00004420static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4421{
4422 skb_set_owner_w(skb, sk);
4423}
4424
Eric Paris2606fd12010-10-13 16:24:41 -04004425static int selinux_secmark_relabel_packet(u32 sid)
4426{
4427 const struct task_security_struct *__tsec;
4428 u32 tsid;
4429
4430 __tsec = current_security();
4431 tsid = __tsec->sid;
4432
4433 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4434}
4435
4436static void selinux_secmark_refcount_inc(void)
4437{
4438 atomic_inc(&selinux_secmark_refcount);
4439}
4440
4441static void selinux_secmark_refcount_dec(void)
4442{
4443 atomic_dec(&selinux_secmark_refcount);
4444}
4445
Adrian Bunk9a673e52006-08-15 00:03:53 -07004446static void selinux_req_classify_flow(const struct request_sock *req,
4447 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004448{
David S. Miller1d28f422011-03-12 00:29:39 -05004449 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004450}
4451
Paul Moore5dbbaf22013-01-14 07:12:19 +00004452static int selinux_tun_dev_alloc_security(void **security)
4453{
4454 struct tun_security_struct *tunsec;
4455
4456 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4457 if (!tunsec)
4458 return -ENOMEM;
4459 tunsec->sid = current_sid();
4460
4461 *security = tunsec;
4462 return 0;
4463}
4464
4465static void selinux_tun_dev_free_security(void *security)
4466{
4467 kfree(security);
4468}
4469
Paul Mooreed6d76e2009-08-28 18:12:49 -04004470static int selinux_tun_dev_create(void)
4471{
4472 u32 sid = current_sid();
4473
4474 /* we aren't taking into account the "sockcreate" SID since the socket
4475 * that is being created here is not a socket in the traditional sense,
4476 * instead it is a private sock, accessible only to the kernel, and
4477 * representing a wide range of network traffic spanning multiple
4478 * connections unlike traditional sockets - check the TUN driver to
4479 * get a better understanding of why this socket is special */
4480
4481 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4482 NULL);
4483}
4484
Paul Moore5dbbaf22013-01-14 07:12:19 +00004485static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004486{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004487 struct tun_security_struct *tunsec = security;
4488
4489 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4490 TUN_SOCKET__ATTACH_QUEUE, NULL);
4491}
4492
4493static int selinux_tun_dev_attach(struct sock *sk, void *security)
4494{
4495 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004496 struct sk_security_struct *sksec = sk->sk_security;
4497
4498 /* we don't currently perform any NetLabel based labeling here and it
4499 * isn't clear that we would want to do so anyway; while we could apply
4500 * labeling without the support of the TUN user the resulting labeled
4501 * traffic from the other end of the connection would almost certainly
4502 * cause confusion to the TUN user that had no idea network labeling
4503 * protocols were being used */
4504
Paul Moore5dbbaf22013-01-14 07:12:19 +00004505 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004506 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004507
4508 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004509}
4510
Paul Moore5dbbaf22013-01-14 07:12:19 +00004511static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004512{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004513 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004514 u32 sid = current_sid();
4515 int err;
4516
Paul Moore5dbbaf22013-01-14 07:12:19 +00004517 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04004518 TUN_SOCKET__RELABELFROM, NULL);
4519 if (err)
4520 return err;
4521 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4522 TUN_SOCKET__RELABELTO, NULL);
4523 if (err)
4524 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004525 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004526
4527 return 0;
4528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4531{
4532 int err = 0;
4533 u32 perm;
4534 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004535 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004536
Hong zhi guo77954982013-03-27 06:49:35 +00004537 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 err = -EINVAL;
4539 goto out;
4540 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004541 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004542
Paul Moore253bfae2010-04-22 14:46:19 -04004543 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (err) {
4545 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004546 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 "SELinux: unrecognized netlink message"
4548 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004549 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004550 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 err = 0;
4552 }
4553
4554 /* Ignore */
4555 if (err == -ENOENT)
4556 err = 0;
4557 goto out;
4558 }
4559
Paul Moore253bfae2010-04-22 14:46:19 -04004560 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561out:
4562 return err;
4563}
4564
4565#ifdef CONFIG_NETFILTER
4566
Paul Mooreeffad8d2008-01-29 08:49:27 -05004567static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4568 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Paul Mooredfaebe92008-10-10 10:16:31 -04004570 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004571 char *addrp;
4572 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004573 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004574 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004575 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004576 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004577 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004578
Paul Mooreeffad8d2008-01-29 08:49:27 -05004579 if (!selinux_policycap_netpeer)
4580 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004581
Paul Mooreeffad8d2008-01-29 08:49:27 -05004582 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004583 netlbl_active = netlbl_enabled();
4584 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004585 if (!secmark_active && !peerlbl_active)
4586 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004587
Paul Moored8395c82008-10-10 10:16:30 -04004588 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4589 return NF_DROP;
4590
Eric Paris50c205f2012-04-04 15:01:43 -04004591 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004592 ad.u.net = &net;
4593 ad.u.net->netif = ifindex;
4594 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004595 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4596 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Paul Mooredfaebe92008-10-10 10:16:31 -04004598 if (peerlbl_active) {
4599 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4600 peer_sid, &ad);
4601 if (err) {
4602 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004603 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004604 }
4605 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004606
4607 if (secmark_active)
4608 if (avc_has_perm(peer_sid, skb->secmark,
4609 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4610 return NF_DROP;
4611
Paul Moore948bf852008-10-10 10:16:32 -04004612 if (netlbl_active)
4613 /* we do this in the FORWARD path and not the POST_ROUTING
4614 * path because we want to make sure we apply the necessary
4615 * labeling before IPsec is applied so we can leverage AH
4616 * protection */
4617 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4618 return NF_DROP;
4619
Paul Mooreeffad8d2008-01-29 08:49:27 -05004620 return NF_ACCEPT;
4621}
4622
4623static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4624 struct sk_buff *skb,
4625 const struct net_device *in,
4626 const struct net_device *out,
4627 int (*okfn)(struct sk_buff *))
4628{
4629 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4630}
4631
4632#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4633static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4634 struct sk_buff *skb,
4635 const struct net_device *in,
4636 const struct net_device *out,
4637 int (*okfn)(struct sk_buff *))
4638{
4639 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4640}
4641#endif /* IPV6 */
4642
Paul Moore948bf852008-10-10 10:16:32 -04004643static unsigned int selinux_ip_output(struct sk_buff *skb,
4644 u16 family)
4645{
Paul Moore216c4a72013-12-04 16:10:45 -05004646 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004647 u32 sid;
4648
4649 if (!netlbl_enabled())
4650 return NF_ACCEPT;
4651
4652 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4653 * because we want to make sure we apply the necessary labeling
4654 * before IPsec is applied so we can leverage AH protection */
Paul Moore216c4a72013-12-04 16:10:45 -05004655 sk = skb->sk;
4656 if (sk) {
4657 struct sk_security_struct *sksec;
4658
4659 if (sk->sk_state == TCP_LISTEN)
4660 /* if the socket is the listening state then this
4661 * packet is a SYN-ACK packet which means it needs to
4662 * be labeled based on the connection/request_sock and
4663 * not the parent socket. unfortunately, we can't
4664 * lookup the request_sock yet as it isn't queued on
4665 * the parent socket until after the SYN-ACK is sent.
4666 * the "solution" is to simply pass the packet as-is
4667 * as any IP option based labeling should be copied
4668 * from the initial connection request (in the IP
4669 * layer). it is far from ideal, but until we get a
4670 * security label in the packet itself this is the
4671 * best we can do. */
4672 return NF_ACCEPT;
4673
4674 /* standard practice, label using the parent socket */
4675 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004676 sid = sksec->sid;
4677 } else
4678 sid = SECINITSID_KERNEL;
4679 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4680 return NF_DROP;
4681
4682 return NF_ACCEPT;
4683}
4684
4685static unsigned int selinux_ipv4_output(unsigned int hooknum,
4686 struct sk_buff *skb,
4687 const struct net_device *in,
4688 const struct net_device *out,
4689 int (*okfn)(struct sk_buff *))
4690{
4691 return selinux_ip_output(skb, PF_INET);
4692}
4693
Paul Mooreeffad8d2008-01-29 08:49:27 -05004694static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4695 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004696 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004697{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004698 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004699 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004700 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004701 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004702 char *addrp;
4703 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004704
Paul Mooreeffad8d2008-01-29 08:49:27 -05004705 if (sk == NULL)
4706 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004707 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004708
Eric Paris50c205f2012-04-04 15:01:43 -04004709 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004710 ad.u.net = &net;
4711 ad.u.net->netif = ifindex;
4712 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004713 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4714 return NF_DROP;
4715
Paul Moore58bfbb52009-03-27 17:10:41 -04004716 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004717 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004718 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004719 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004720
Steffen Klassertb9679a72011-02-23 12:55:21 +01004721 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4722 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004723
Paul Mooreeffad8d2008-01-29 08:49:27 -05004724 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
Paul Mooreeffad8d2008-01-29 08:49:27 -05004727static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4728 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004730 u32 secmark_perm;
4731 u32 peer_sid;
4732 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004733 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004734 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004735 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004736 u8 secmark_active;
4737 u8 peerlbl_active;
4738
Paul Mooreeffad8d2008-01-29 08:49:27 -05004739 /* If any sort of compatibility mode is enabled then handoff processing
4740 * to the selinux_ip_postroute_compat() function to deal with the
4741 * special handling. We do this in an attempt to keep this function
4742 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004743 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004744 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004745#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004746 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4747 * packet transformation so allow the packet to pass without any checks
4748 * since we'll have another chance to perform access control checks
4749 * when the packet is on it's final way out.
4750 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4751 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004752 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004753 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004754#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004755 secmark_active = selinux_secmark_enabled();
4756 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4757 if (!secmark_active && !peerlbl_active)
4758 return NF_ACCEPT;
4759
Paul Mooreeffad8d2008-01-29 08:49:27 -05004760 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004761 if (sk == NULL) {
Paul Moore348d7862013-12-04 16:10:51 -05004762 /* Without an associated socket the packet is either coming
4763 * from the kernel or it is being forwarded; check the packet
4764 * to determine which and if the packet is being forwarded
4765 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004766 if (skb->skb_iif) {
4767 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004768 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004769 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004770 } else {
4771 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004772 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004773 }
Paul Moore348d7862013-12-04 16:10:51 -05004774 } else if (sk->sk_state == TCP_LISTEN) {
4775 /* Locally generated packet but the associated socket is in the
4776 * listening state which means this is a SYN-ACK packet. In
4777 * this particular case the correct security label is assigned
4778 * to the connection/request_sock but unfortunately we can't
4779 * query the request_sock as it isn't queued on the parent
4780 * socket until after the SYN-ACK packet is sent; the only
4781 * viable choice is to regenerate the label like we do in
4782 * selinux_inet_conn_request(). See also selinux_ip_output()
4783 * for similar problems. */
4784 u32 skb_sid;
4785 struct sk_security_struct *sksec = sk->sk_security;
4786 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4787 return NF_DROP;
4788 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4789 return NF_DROP;
4790 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004791 } else {
Paul Moore348d7862013-12-04 16:10:51 -05004792 /* Locally generated packet, fetch the security label from the
4793 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004794 struct sk_security_struct *sksec = sk->sk_security;
4795 peer_sid = sksec->sid;
4796 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004797 }
4798
Eric Paris50c205f2012-04-04 15:01:43 -04004799 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004800 ad.u.net = &net;
4801 ad.u.net->netif = ifindex;
4802 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004803 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004804 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004805
Paul Mooreeffad8d2008-01-29 08:49:27 -05004806 if (secmark_active)
4807 if (avc_has_perm(peer_sid, skb->secmark,
4808 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004809 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004810
4811 if (peerlbl_active) {
4812 u32 if_sid;
4813 u32 node_sid;
4814
4815 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004816 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004817 if (avc_has_perm(peer_sid, if_sid,
4818 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004819 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004820
4821 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004822 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004823 if (avc_has_perm(peer_sid, node_sid,
4824 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004825 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004826 }
4827
4828 return NF_ACCEPT;
4829}
4830
4831static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4832 struct sk_buff *skb,
4833 const struct net_device *in,
4834 const struct net_device *out,
4835 int (*okfn)(struct sk_buff *))
4836{
4837 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838}
4839
4840#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004841static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4842 struct sk_buff *skb,
4843 const struct net_device *in,
4844 const struct net_device *out,
4845 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004847 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849#endif /* IPV6 */
4850
4851#endif /* CONFIG_NETFILTER */
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 int err;
4856
Eric Paris200ac532009-02-12 15:01:04 -05004857 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 if (err)
4859 return err;
4860
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004861 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862}
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864static int ipc_alloc_security(struct task_struct *task,
4865 struct kern_ipc_perm *perm,
4866 u16 sclass)
4867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004869 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
James Morris89d155e2005-10-30 14:59:21 -08004871 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 if (!isec)
4873 return -ENOMEM;
4874
David Howells275bb412008-11-14 10:39:19 +11004875 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004877 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 perm->security = isec;
4879
4880 return 0;
4881}
4882
4883static void ipc_free_security(struct kern_ipc_perm *perm)
4884{
4885 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 perm->security = NULL;
4887 kfree(isec);
4888}
4889
4890static int msg_msg_alloc_security(struct msg_msg *msg)
4891{
4892 struct msg_security_struct *msec;
4893
James Morris89d155e2005-10-30 14:59:21 -08004894 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (!msec)
4896 return -ENOMEM;
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 msec->sid = SECINITSID_UNLABELED;
4899 msg->security = msec;
4900
4901 return 0;
4902}
4903
4904static void msg_msg_free_security(struct msg_msg *msg)
4905{
4906 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 msg->security = NULL;
4909 kfree(msec);
4910}
4911
4912static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07004913 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004916 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004917 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 isec = ipc_perms->security;
4920
Eric Paris50c205f2012-04-04 15:01:43 -04004921 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 ad.u.ipc_id = ipc_perms->key;
4923
David Howells275bb412008-11-14 10:39:19 +11004924 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925}
4926
4927static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
4928{
4929 return msg_msg_alloc_security(msg);
4930}
4931
4932static void selinux_msg_msg_free_security(struct msg_msg *msg)
4933{
4934 msg_msg_free_security(msg);
4935}
4936
4937/* message queue security operations */
4938static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
4939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004941 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004942 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 int rc;
4944
4945 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
4946 if (rc)
4947 return rc;
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 isec = msq->q_perm.security;
4950
Eric Paris50c205f2012-04-04 15:01:43 -04004951 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04004952 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
David Howells275bb412008-11-14 10:39:19 +11004954 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 MSGQ__CREATE, &ad);
4956 if (rc) {
4957 ipc_free_security(&msq->q_perm);
4958 return rc;
4959 }
4960 return 0;
4961}
4962
4963static void selinux_msg_queue_free_security(struct msg_queue *msq)
4964{
4965 ipc_free_security(&msq->q_perm);
4966}
4967
4968static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
4969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004971 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004972 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 isec = msq->q_perm.security;
4975
Eric Paris50c205f2012-04-04 15:01:43 -04004976 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 ad.u.ipc_id = msq->q_perm.key;
4978
David Howells275bb412008-11-14 10:39:19 +11004979 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 MSGQ__ASSOCIATE, &ad);
4981}
4982
4983static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
4984{
4985 int err;
4986 int perms;
4987
Eric Paris828dfe12008-04-17 13:17:49 -04004988 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 case IPC_INFO:
4990 case MSG_INFO:
4991 /* No specific object, just general system-wide information. */
4992 return task_has_system(current, SYSTEM__IPC_INFO);
4993 case IPC_STAT:
4994 case MSG_STAT:
4995 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
4996 break;
4997 case IPC_SET:
4998 perms = MSGQ__SETATTR;
4999 break;
5000 case IPC_RMID:
5001 perms = MSGQ__DESTROY;
5002 break;
5003 default:
5004 return 0;
5005 }
5006
Stephen Smalley6af963f2005-05-01 08:58:39 -07005007 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 return err;
5009}
5010
5011static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 struct ipc_security_struct *isec;
5014 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005015 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005016 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 int rc;
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 isec = msq->q_perm.security;
5020 msec = msg->security;
5021
5022 /*
5023 * First time through, need to assign label to the message
5024 */
5025 if (msec->sid == SECINITSID_UNLABELED) {
5026 /*
5027 * Compute new sid based on current process and
5028 * message queue this message will be stored in
5029 */
David Howells275bb412008-11-14 10:39:19 +11005030 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005031 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 if (rc)
5033 return rc;
5034 }
5035
Eric Paris50c205f2012-04-04 15:01:43 -04005036 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 ad.u.ipc_id = msq->q_perm.key;
5038
5039 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005040 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 MSGQ__WRITE, &ad);
5042 if (!rc)
5043 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005044 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5045 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (!rc)
5047 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005048 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5049 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
5051 return rc;
5052}
5053
5054static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5055 struct task_struct *target,
5056 long type, int mode)
5057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 struct ipc_security_struct *isec;
5059 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005060 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005061 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 int rc;
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 isec = msq->q_perm.security;
5065 msec = msg->security;
5066
Eric Paris50c205f2012-04-04 15:01:43 -04005067 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005068 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
David Howells275bb412008-11-14 10:39:19 +11005070 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 SECCLASS_MSGQ, MSGQ__READ, &ad);
5072 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005073 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 SECCLASS_MSG, MSG__RECEIVE, &ad);
5075 return rc;
5076}
5077
5078/* Shared Memory security operations */
5079static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005082 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005083 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 int rc;
5085
5086 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5087 if (rc)
5088 return rc;
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 isec = shp->shm_perm.security;
5091
Eric Paris50c205f2012-04-04 15:01:43 -04005092 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005093 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
David Howells275bb412008-11-14 10:39:19 +11005095 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 SHM__CREATE, &ad);
5097 if (rc) {
5098 ipc_free_security(&shp->shm_perm);
5099 return rc;
5100 }
5101 return 0;
5102}
5103
5104static void selinux_shm_free_security(struct shmid_kernel *shp)
5105{
5106 ipc_free_security(&shp->shm_perm);
5107}
5108
5109static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005112 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005113 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 isec = shp->shm_perm.security;
5116
Eric Paris50c205f2012-04-04 15:01:43 -04005117 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 ad.u.ipc_id = shp->shm_perm.key;
5119
David Howells275bb412008-11-14 10:39:19 +11005120 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 SHM__ASSOCIATE, &ad);
5122}
5123
5124/* Note, at this point, shp is locked down */
5125static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5126{
5127 int perms;
5128 int err;
5129
Eric Paris828dfe12008-04-17 13:17:49 -04005130 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 case IPC_INFO:
5132 case SHM_INFO:
5133 /* No specific object, just general system-wide information. */
5134 return task_has_system(current, SYSTEM__IPC_INFO);
5135 case IPC_STAT:
5136 case SHM_STAT:
5137 perms = SHM__GETATTR | SHM__ASSOCIATE;
5138 break;
5139 case IPC_SET:
5140 perms = SHM__SETATTR;
5141 break;
5142 case SHM_LOCK:
5143 case SHM_UNLOCK:
5144 perms = SHM__LOCK;
5145 break;
5146 case IPC_RMID:
5147 perms = SHM__DESTROY;
5148 break;
5149 default:
5150 return 0;
5151 }
5152
Stephen Smalley6af963f2005-05-01 08:58:39 -07005153 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 return err;
5155}
5156
5157static int selinux_shm_shmat(struct shmid_kernel *shp,
5158 char __user *shmaddr, int shmflg)
5159{
5160 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
5162 if (shmflg & SHM_RDONLY)
5163 perms = SHM__READ;
5164 else
5165 perms = SHM__READ | SHM__WRITE;
5166
Stephen Smalley6af963f2005-05-01 08:58:39 -07005167 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168}
5169
5170/* Semaphore security operations */
5171static int selinux_sem_alloc_security(struct sem_array *sma)
5172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005174 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005175 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 int rc;
5177
5178 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5179 if (rc)
5180 return rc;
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 isec = sma->sem_perm.security;
5183
Eric Paris50c205f2012-04-04 15:01:43 -04005184 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005185 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
David Howells275bb412008-11-14 10:39:19 +11005187 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 SEM__CREATE, &ad);
5189 if (rc) {
5190 ipc_free_security(&sma->sem_perm);
5191 return rc;
5192 }
5193 return 0;
5194}
5195
5196static void selinux_sem_free_security(struct sem_array *sma)
5197{
5198 ipc_free_security(&sma->sem_perm);
5199}
5200
5201static int selinux_sem_associate(struct sem_array *sma, int semflg)
5202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005204 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005205 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 isec = sma->sem_perm.security;
5208
Eric Paris50c205f2012-04-04 15:01:43 -04005209 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 ad.u.ipc_id = sma->sem_perm.key;
5211
David Howells275bb412008-11-14 10:39:19 +11005212 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 SEM__ASSOCIATE, &ad);
5214}
5215
5216/* Note, at this point, sma is locked down */
5217static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5218{
5219 int err;
5220 u32 perms;
5221
Eric Paris828dfe12008-04-17 13:17:49 -04005222 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 case IPC_INFO:
5224 case SEM_INFO:
5225 /* No specific object, just general system-wide information. */
5226 return task_has_system(current, SYSTEM__IPC_INFO);
5227 case GETPID:
5228 case GETNCNT:
5229 case GETZCNT:
5230 perms = SEM__GETATTR;
5231 break;
5232 case GETVAL:
5233 case GETALL:
5234 perms = SEM__READ;
5235 break;
5236 case SETVAL:
5237 case SETALL:
5238 perms = SEM__WRITE;
5239 break;
5240 case IPC_RMID:
5241 perms = SEM__DESTROY;
5242 break;
5243 case IPC_SET:
5244 perms = SEM__SETATTR;
5245 break;
5246 case IPC_STAT:
5247 case SEM_STAT:
5248 perms = SEM__GETATTR | SEM__ASSOCIATE;
5249 break;
5250 default:
5251 return 0;
5252 }
5253
Stephen Smalley6af963f2005-05-01 08:58:39 -07005254 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return err;
5256}
5257
5258static int selinux_sem_semop(struct sem_array *sma,
5259 struct sembuf *sops, unsigned nsops, int alter)
5260{
5261 u32 perms;
5262
5263 if (alter)
5264 perms = SEM__READ | SEM__WRITE;
5265 else
5266 perms = SEM__READ;
5267
Stephen Smalley6af963f2005-05-01 08:58:39 -07005268 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
5271static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 u32 av = 0;
5274
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 av = 0;
5276 if (flag & S_IRUGO)
5277 av |= IPC__UNIX_READ;
5278 if (flag & S_IWUGO)
5279 av |= IPC__UNIX_WRITE;
5280
5281 if (av == 0)
5282 return 0;
5283
Stephen Smalley6af963f2005-05-01 08:58:39 -07005284 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285}
5286
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02005287static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5288{
5289 struct ipc_security_struct *isec = ipcp->security;
5290 *secid = isec->sid;
5291}
5292
Eric Paris828dfe12008-04-17 13:17:49 -04005293static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
5295 if (inode)
5296 inode_doinit_with_dentry(inode, dentry);
5297}
5298
5299static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005300 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301{
David Howells275bb412008-11-14 10:39:19 +11005302 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005303 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005305 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
5307 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005308 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 if (error)
5310 return error;
5311 }
5312
David Howells275bb412008-11-14 10:39:19 +11005313 rcu_read_lock();
5314 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
5316 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005317 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005319 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005321 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005323 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005324 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005325 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005326 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005327 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 else
David Howells275bb412008-11-14 10:39:19 +11005329 goto invalid;
5330 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 if (!sid)
5333 return 0;
5334
Al Viro04ff9702007-03-12 16:17:58 +00005335 error = security_sid_to_context(sid, value, &len);
5336 if (error)
5337 return error;
5338 return len;
David Howells275bb412008-11-14 10:39:19 +11005339
5340invalid:
5341 rcu_read_unlock();
5342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343}
5344
5345static int selinux_setprocattr(struct task_struct *p,
5346 char *name, void *value, size_t size)
5347{
5348 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005349 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005350 struct cred *new;
5351 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 int error;
5353 char *str = value;
5354
5355 if (current != p) {
5356 /* SELinux only allows a process to change its own
5357 security attributes. */
5358 return -EACCES;
5359 }
5360
5361 /*
5362 * Basic control over ability to set these attributes at all.
5363 * current == p, but we'll pass them separately in case the
5364 * above restriction is ever removed.
5365 */
5366 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005367 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005369 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005370 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005371 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005372 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005373 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005375 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 else
5377 error = -EINVAL;
5378 if (error)
5379 return error;
5380
5381 /* Obtain a SID for the context, if one was specified. */
5382 if (size && str[1] && str[1] != '\n') {
5383 if (str[size-1] == '\n') {
5384 str[size-1] = 0;
5385 size--;
5386 }
5387 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005388 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005389 if (!capable(CAP_MAC_ADMIN)) {
5390 struct audit_buffer *ab;
5391 size_t audit_size;
5392
5393 /* We strip a nul only if it is at the end, otherwise the
5394 * context contains a nul and we should audit that */
5395 if (str[size - 1] == '\0')
5396 audit_size = size - 1;
5397 else
5398 audit_size = size;
5399 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5400 audit_log_format(ab, "op=fscreate invalid_context=");
5401 audit_log_n_untrustedstring(ab, value, audit_size);
5402 audit_log_end(ab);
5403
Stephen Smalley12b29f32008-05-07 13:03:20 -04005404 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005405 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005406 error = security_context_to_sid_force(value, size,
5407 &sid);
5408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 if (error)
5410 return error;
5411 }
5412
David Howellsd84f4f92008-11-14 10:39:23 +11005413 new = prepare_creds();
5414 if (!new)
5415 return -ENOMEM;
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 /* Permission checking based on the specified context is
5418 performed during the actual operation (execve,
5419 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005420 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 checks and may_create for the file creation checks. The
5422 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005423 tsec = new->security;
5424 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005426 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005428 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005429 error = may_create_key(sid, p);
5430 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005431 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005432 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005433 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005434 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005435 } else if (!strcmp(name, "current")) {
5436 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005438 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005439
David Howellsd84f4f92008-11-14 10:39:23 +11005440 /* Only allow single threaded processes to change context */
5441 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005442 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005443 error = security_bounded_transition(tsec->sid, sid);
5444 if (error)
5445 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 /* Check permissions for the transition. */
5449 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005450 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005452 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
5454 /* Check for ptracing, and update the task SID if ok.
5455 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005456 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005458 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005459 if (tracer)
5460 ptsid = task_sid(tracer);
5461 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
David Howellsd84f4f92008-11-14 10:39:23 +11005463 if (tracer) {
5464 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5465 PROCESS__PTRACE, NULL);
5466 if (error)
5467 goto abort_change;
5468 }
5469
5470 tsec->sid = sid;
5471 } else {
5472 error = -EINVAL;
5473 goto abort_change;
5474 }
5475
5476 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005478
5479abort_change:
5480 abort_creds(new);
5481 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482}
5483
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005484static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5485{
5486 return security_sid_to_context(secid, secdata, seclen);
5487}
5488
David Howells7bf570d2008-04-29 20:52:51 +01005489static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005490{
5491 return security_context_to_sid(secdata, seclen, secid);
5492}
5493
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005494static void selinux_release_secctx(char *secdata, u32 seclen)
5495{
Paul Moore088999e2007-08-01 11:12:58 -04005496 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005497}
5498
David P. Quigley1ee65e32009-09-03 14:25:57 -04005499/*
5500 * called with inode->i_mutex locked
5501 */
5502static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5503{
5504 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5505}
5506
5507/*
5508 * called with inode->i_mutex locked
5509 */
5510static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5511{
5512 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5513}
5514
5515static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5516{
5517 int len = 0;
5518 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5519 ctx, true);
5520 if (len < 0)
5521 return len;
5522 *ctxlen = len;
5523 return 0;
5524}
Michael LeMayd7200242006-06-22 14:47:17 -07005525#ifdef CONFIG_KEYS
5526
David Howellsd84f4f92008-11-14 10:39:23 +11005527static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005528 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005529{
David Howellsd84f4f92008-11-14 10:39:23 +11005530 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005531 struct key_security_struct *ksec;
5532
5533 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5534 if (!ksec)
5535 return -ENOMEM;
5536
David Howellsd84f4f92008-11-14 10:39:23 +11005537 tsec = cred->security;
5538 if (tsec->keycreate_sid)
5539 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005540 else
David Howellsd84f4f92008-11-14 10:39:23 +11005541 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005542
David Howells275bb412008-11-14 10:39:19 +11005543 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005544 return 0;
5545}
5546
5547static void selinux_key_free(struct key *k)
5548{
5549 struct key_security_struct *ksec = k->security;
5550
5551 k->security = NULL;
5552 kfree(ksec);
5553}
5554
5555static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005556 const struct cred *cred,
5557 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005558{
5559 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005560 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005561 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005562
5563 /* if no specific permissions are requested, we skip the
5564 permission check. No serious, additional covert channels
5565 appear to be created. */
5566 if (perm == 0)
5567 return 0;
5568
David Howellsd84f4f92008-11-14 10:39:23 +11005569 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005570
5571 key = key_ref_to_ptr(key_ref);
5572 ksec = key->security;
5573
5574 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005575}
5576
David Howells70a5bb72008-04-29 01:01:26 -07005577static int selinux_key_getsecurity(struct key *key, char **_buffer)
5578{
5579 struct key_security_struct *ksec = key->security;
5580 char *context = NULL;
5581 unsigned len;
5582 int rc;
5583
5584 rc = security_sid_to_context(ksec->sid, &context, &len);
5585 if (!rc)
5586 rc = len;
5587 *_buffer = context;
5588 return rc;
5589}
5590
Michael LeMayd7200242006-06-22 14:47:17 -07005591#endif
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005594 .name = "selinux",
5595
Ingo Molnar9e488582009-05-07 19:26:19 +10005596 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005597 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005599 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 .capable = selinux_capable,
5601 .quotactl = selinux_quotactl,
5602 .quota_on = selinux_quota_on,
5603 .syslog = selinux_syslog,
5604 .vm_enough_memory = selinux_vm_enough_memory,
5605
5606 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
David Howellsa6f76f22008-11-14 10:39:24 +11005608 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005609 .bprm_committing_creds = selinux_bprm_committing_creds,
5610 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 .bprm_secureexec = selinux_bprm_secureexec,
5612
5613 .sb_alloc_security = selinux_sb_alloc_security,
5614 .sb_free_security = selinux_sb_free_security,
5615 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005616 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005617 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005618 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 .sb_statfs = selinux_sb_statfs,
5620 .sb_mount = selinux_mount,
5621 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005622 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005623 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005624 .sb_parse_opts_str = selinux_parse_opts_str,
5625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 .inode_alloc_security = selinux_inode_alloc_security,
5628 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005629 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 .inode_unlink = selinux_inode_unlink,
5633 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 .inode_rmdir = selinux_inode_rmdir,
5636 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 .inode_readlink = selinux_inode_readlink,
5639 .inode_follow_link = selinux_inode_follow_link,
5640 .inode_permission = selinux_inode_permission,
5641 .inode_setattr = selinux_inode_setattr,
5642 .inode_getattr = selinux_inode_getattr,
5643 .inode_setxattr = selinux_inode_setxattr,
5644 .inode_post_setxattr = selinux_inode_post_setxattr,
5645 .inode_getxattr = selinux_inode_getxattr,
5646 .inode_listxattr = selinux_inode_listxattr,
5647 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005648 .inode_getsecurity = selinux_inode_getsecurity,
5649 .inode_setsecurity = selinux_inode_setsecurity,
5650 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005651 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 .file_permission = selinux_file_permission,
5654 .file_alloc_security = selinux_file_alloc_security,
5655 .file_free_security = selinux_file_free_security,
5656 .file_ioctl = selinux_file_ioctl,
Al Viroe5467852012-05-30 13:30:51 -04005657 .mmap_file = selinux_mmap_file,
5658 .mmap_addr = selinux_mmap_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 .file_mprotect = selinux_file_mprotect,
5660 .file_lock = selinux_file_lock,
5661 .file_fcntl = selinux_file_fcntl,
5662 .file_set_fowner = selinux_file_set_fowner,
5663 .file_send_sigiotask = selinux_file_send_sigiotask,
5664 .file_receive = selinux_file_receive,
5665
Eric Paris83d49852012-04-04 13:45:40 -04005666 .file_open = selinux_file_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005667
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005669 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005670 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005671 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005672 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005673 .kernel_act_as = selinux_kernel_act_as,
5674 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005675 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 .task_setpgid = selinux_task_setpgid,
5677 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005678 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005679 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005681 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005682 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 .task_setrlimit = selinux_task_setrlimit,
5684 .task_setscheduler = selinux_task_setscheduler,
5685 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005686 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 .task_kill = selinux_task_kill,
5688 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005689 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
5691 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005692 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5695 .msg_msg_free_security = selinux_msg_msg_free_security,
5696
5697 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5698 .msg_queue_free_security = selinux_msg_queue_free_security,
5699 .msg_queue_associate = selinux_msg_queue_associate,
5700 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5701 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5702 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5703
5704 .shm_alloc_security = selinux_shm_alloc_security,
5705 .shm_free_security = selinux_shm_free_security,
5706 .shm_associate = selinux_shm_associate,
5707 .shm_shmctl = selinux_shm_shmctl,
5708 .shm_shmat = selinux_shm_shmat,
5709
Eric Paris828dfe12008-04-17 13:17:49 -04005710 .sem_alloc_security = selinux_sem_alloc_security,
5711 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 .sem_associate = selinux_sem_associate,
5713 .sem_semctl = selinux_sem_semctl,
5714 .sem_semop = selinux_sem_semop,
5715
Eric Paris828dfe12008-04-17 13:17:49 -04005716 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Eric Paris828dfe12008-04-17 13:17:49 -04005718 .getprocattr = selinux_getprocattr,
5719 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005721 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005722 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005723 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005724 .inode_notifysecctx = selinux_inode_notifysecctx,
5725 .inode_setsecctx = selinux_inode_setsecctx,
5726 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005727
Eric Paris828dfe12008-04-17 13:17:49 -04005728 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 .unix_may_send = selinux_socket_unix_may_send,
5730
5731 .socket_create = selinux_socket_create,
5732 .socket_post_create = selinux_socket_post_create,
5733 .socket_bind = selinux_socket_bind,
5734 .socket_connect = selinux_socket_connect,
5735 .socket_listen = selinux_socket_listen,
5736 .socket_accept = selinux_socket_accept,
5737 .socket_sendmsg = selinux_socket_sendmsg,
5738 .socket_recvmsg = selinux_socket_recvmsg,
5739 .socket_getsockname = selinux_socket_getsockname,
5740 .socket_getpeername = selinux_socket_getpeername,
5741 .socket_getsockopt = selinux_socket_getsockopt,
5742 .socket_setsockopt = selinux_socket_setsockopt,
5743 .socket_shutdown = selinux_socket_shutdown,
5744 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005745 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5746 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 .sk_alloc_security = selinux_sk_alloc_security,
5748 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005749 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005750 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005751 .sock_graft = selinux_sock_graft,
5752 .inet_conn_request = selinux_inet_conn_request,
5753 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005754 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005755 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5756 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5757 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005758 .req_classify_flow = selinux_req_classify_flow,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005759 .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5760 .tun_dev_free_security = selinux_tun_dev_free_security,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005761 .tun_dev_create = selinux_tun_dev_create,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005762 .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005763 .tun_dev_attach = selinux_tun_dev_attach,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005764 .tun_dev_open = selinux_tun_dev_open,
Eric Dumazetca10b9e2013-04-08 17:58:11 +00005765 .skb_owned_by = selinux_skb_owned_by,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005766
5767#ifdef CONFIG_SECURITY_NETWORK_XFRM
5768 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5769 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5770 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005771 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005772 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5773 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005774 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005775 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005776 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005777 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005779
5780#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005781 .key_alloc = selinux_key_alloc,
5782 .key_free = selinux_key_free,
5783 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005784 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005785#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005786
5787#ifdef CONFIG_AUDIT
5788 .audit_rule_init = selinux_audit_rule_init,
5789 .audit_rule_known = selinux_audit_rule_known,
5790 .audit_rule_match = selinux_audit_rule_match,
5791 .audit_rule_free = selinux_audit_rule_free,
5792#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793};
5794
5795static __init int selinux_init(void)
5796{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005797 if (!security_module_enable(&selinux_ops)) {
5798 selinux_enabled = 0;
5799 return 0;
5800 }
5801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 if (!selinux_enabled) {
5803 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5804 return 0;
5805 }
5806
5807 printk(KERN_INFO "SELinux: Initializing.\n");
5808
5809 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005810 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005812 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5813
James Morris7cae7e22006-03-22 00:09:22 -08005814 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5815 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005816 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 avc_init();
5818
Eric Paris828dfe12008-04-17 13:17:49 -04005819 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 panic("SELinux: Unable to register with kernel.\n");
5821
Eric Paris828dfe12008-04-17 13:17:49 -04005822 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005823 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005824 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005825 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 return 0;
5828}
5829
Al Viroe8c26252010-03-23 06:36:54 -04005830static void delayed_superblock_init(struct super_block *sb, void *unused)
5831{
5832 superblock_doinit(sb, NULL);
5833}
5834
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835void selinux_complete_init(void)
5836{
Eric Parisfadcdb42007-02-22 18:11:31 -05005837 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
5839 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005840 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005841 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842}
5843
5844/* SELinux requires early initialization in order to label
5845 all processes and objects when they are created. */
5846security_initcall(selinux_init);
5847
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005848#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Paul Mooreeffad8d2008-01-29 08:49:27 -05005850static struct nf_hook_ops selinux_ipv4_ops[] = {
5851 {
5852 .hook = selinux_ipv4_postroute,
5853 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005854 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005855 .hooknum = NF_INET_POST_ROUTING,
5856 .priority = NF_IP_PRI_SELINUX_LAST,
5857 },
5858 {
5859 .hook = selinux_ipv4_forward,
5860 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005861 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005862 .hooknum = NF_INET_FORWARD,
5863 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005864 },
5865 {
5866 .hook = selinux_ipv4_output,
5867 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005868 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04005869 .hooknum = NF_INET_LOCAL_OUT,
5870 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872};
5873
5874#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5875
Paul Mooreeffad8d2008-01-29 08:49:27 -05005876static struct nf_hook_ops selinux_ipv6_ops[] = {
5877 {
5878 .hook = selinux_ipv6_postroute,
5879 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005880 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005881 .hooknum = NF_INET_POST_ROUTING,
5882 .priority = NF_IP6_PRI_SELINUX_LAST,
5883 },
5884 {
5885 .hook = selinux_ipv6_forward,
5886 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005887 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005888 .hooknum = NF_INET_FORWARD,
5889 .priority = NF_IP6_PRI_SELINUX_FIRST,
5890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891};
5892
5893#endif /* IPV6 */
5894
5895static int __init selinux_nf_ip_init(void)
5896{
5897 int err = 0;
5898
5899 if (!selinux_enabled)
5900 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05005901
5902 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
5903
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005904 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
5905 if (err)
5906 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907
5908#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005909 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
5910 if (err)
5911 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914out:
5915 return err;
5916}
5917
5918__initcall(selinux_nf_ip_init);
5919
5920#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5921static void selinux_nf_ip_exit(void)
5922{
Eric Parisfadcdb42007-02-22 18:11:31 -05005923 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005925 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005927 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928#endif /* IPV6 */
5929}
5930#endif
5931
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005932#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5935#define selinux_nf_ip_exit()
5936#endif
5937
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005938#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04005941static int selinux_disabled;
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943int selinux_disable(void)
5944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 if (ss_initialized) {
5946 /* Not permitted after initial policy load. */
5947 return -EINVAL;
5948 }
5949
5950 if (selinux_disabled) {
5951 /* Only do this once. */
5952 return -EINVAL;
5953 }
5954
5955 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
5956
5957 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04005958 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08005960 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
Eric Parisaf8ff042009-09-20 21:23:01 -04005962 /* Try to destroy the avc node cache */
5963 avc_disable();
5964
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 /* Unregister netfilter hooks. */
5966 selinux_nf_ip_exit();
5967
5968 /* Unregister selinuxfs. */
5969 exit_sel_fs();
5970
5971 return 0;
5972}
5973#endif