blob: 3c02be3f6732c95ff2ea5ed5438470de3de47193 [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[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore220deb92008-01-29 08:38:23 -050055#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050056#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040057#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070059#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/bitops.h>
61#include <linux/interrupt.h>
62#include <linux/netdevice.h> /* for network interface checks */
63#include <linux/netlink.h>
64#include <linux/tcp.h>
65#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080066#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/quota.h>
68#include <linux/un.h> /* for Unix socket types */
69#include <net/af_unix.h> /* for Unix socket types */
70#include <linux/parser.h>
71#include <linux/nfs_mount.h>
72#include <net/ipv6.h>
73#include <linux/hugetlb.h>
74#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070076#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070077#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070078#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070079#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080080#include <linux/syslog.h>
Serge E. Hallyn3486740a2011-03-23 16:43:17 -070081#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040082#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000083#include <linux/msg.h>
84#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include "avc.h"
87#include "objsec.h"
88#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050089#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040090#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080091#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050092#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020093#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100094#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
David P. Quigley11689d42009-01-16 09:22:03 -050096#define NUM_SEL_MNT_OPTS 5
Eric Parisc9180a52007-11-30 13:00:35 -050097
James Morris20510f22007-10-16 23:31:32 -070098extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Paul Moored621d352008-01-29 08:43:36 -0500100/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000101static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400104int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static int __init enforcing_setup(char *str)
107{
Eric Parisf5269712008-05-14 11:27:45 -0400108 unsigned long enforcing;
109 if (!strict_strtoul(str, 0, &enforcing))
110 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return 1;
112}
113__setup("enforcing=", enforcing_setup);
114#endif
115
116#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
117int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
118
119static int __init selinux_enabled_setup(char *str)
120{
Eric Parisf5269712008-05-14 11:27:45 -0400121 unsigned long enabled;
122 if (!strict_strtoul(str, 0, &enabled))
123 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 1;
125}
126__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400127#else
128int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
130
Christoph Lametere18b8902006-12-06 20:33:20 -0800131static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800132
Paul Moored621d352008-01-29 08:43:36 -0500133/**
134 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
135 *
136 * Description:
137 * This function checks the SECMARK reference counter to see if any SECMARK
138 * targets are currently configured, if the reference counter is greater than
139 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
140 * enabled, false (0) if SECMARK is disabled.
141 *
142 */
143static int selinux_secmark_enabled(void)
144{
145 return (atomic_read(&selinux_secmark_refcount) > 0);
146}
147
David Howellsd84f4f92008-11-14 10:39:23 +1100148/*
149 * initialise the security for the init task
150 */
151static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
David Howells3b11a1d2008-11-14 10:39:26 +1100153 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct task_security_struct *tsec;
155
James Morris89d155e2005-10-30 14:59:21 -0800156 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100158 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David Howellsd84f4f92008-11-14 10:39:23 +1100160 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100161 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
David Howells275bb412008-11-14 10:39:19 +1100164/*
David Howells88e67f32008-11-14 10:39:21 +1100165 * get the security ID of a set of credentials
166 */
167static inline u32 cred_sid(const struct cred *cred)
168{
169 const struct task_security_struct *tsec;
170
171 tsec = cred->security;
172 return tsec->sid;
173}
174
175/*
David Howells3b11a1d2008-11-14 10:39:26 +1100176 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100177 */
178static inline u32 task_sid(const struct task_struct *task)
179{
David Howells275bb412008-11-14 10:39:19 +1100180 u32 sid;
181
182 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100183 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100184 rcu_read_unlock();
185 return sid;
186}
187
188/*
David Howells3b11a1d2008-11-14 10:39:26 +1100189 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100190 */
191static inline u32 current_sid(void)
192{
Paul Moore5fb49872010-04-22 14:46:19 -0400193 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100194
195 return tsec->sid;
196}
197
David Howells88e67f32008-11-14 10:39:21 +1100198/* Allocate and free functions for each kind of security blob. */
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int inode_alloc_security(struct inode *inode)
201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100203 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Josef Bacika02fe132008-04-04 09:35:05 +1100205 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!isec)
207 return -ENOMEM;
208
Eric Paris23970742006-09-25 23:32:01 -0700209 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 isec->inode = inode;
212 isec->sid = SECINITSID_UNLABELED;
213 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100214 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 inode->i_security = isec;
216
217 return 0;
218}
219
220static void inode_free_security(struct inode *inode)
221{
222 struct inode_security_struct *isec = inode->i_security;
223 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spin_lock(&sbsec->isec_lock);
226 if (!list_empty(&isec->list))
227 list_del_init(&isec->list);
228 spin_unlock(&sbsec->isec_lock);
229
230 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800231 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static int file_alloc_security(struct file *file)
235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100237 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800239 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!fsec)
241 return -ENOMEM;
242
David Howells275bb412008-11-14 10:39:19 +1100243 fsec->sid = sid;
244 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 file->f_security = fsec;
246
247 return 0;
248}
249
250static void file_free_security(struct file *file)
251{
252 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 file->f_security = NULL;
254 kfree(fsec);
255}
256
257static int superblock_alloc_security(struct super_block *sb)
258{
259 struct superblock_security_struct *sbsec;
260
James Morris89d155e2005-10-30 14:59:21 -0800261 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!sbsec)
263 return -ENOMEM;
264
Eric Parisbc7e9822006-09-25 23:32:02 -0700265 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 INIT_LIST_HEAD(&sbsec->isec_head);
267 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sbsec->sb = sb;
269 sbsec->sid = SECINITSID_UNLABELED;
270 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700271 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sb->s_security = sbsec;
273
274 return 0;
275}
276
277static void superblock_free_security(struct super_block *sb)
278{
279 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sb->s_security = NULL;
281 kfree(sbsec);
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* The file system's label must be initialized prior to use. */
285
Stephen Hemminger634a5392010-03-04 21:59:03 -0800286static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 "uses xattr",
288 "uses transition SIDs",
289 "uses task SIDs",
290 "uses genfs_contexts",
291 "not configured for labeling",
292 "uses mountpoint labeling",
293};
294
295static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
296
297static inline int inode_doinit(struct inode *inode)
298{
299 return inode_doinit_with_dentry(inode, NULL);
300}
301
302enum {
Eric Paris31e87932007-09-19 17:19:12 -0400303 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 Opt_context = 1,
305 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500306 Opt_defcontext = 3,
307 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500308 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
Steven Whitehousea447c092008-10-13 10:46:57 +0100311static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400312 {Opt_context, CONTEXT_STR "%s"},
313 {Opt_fscontext, FSCONTEXT_STR "%s"},
314 {Opt_defcontext, DEFCONTEXT_STR "%s"},
315 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500316 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400317 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
320#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
321
Eric Parisc312feb2006-07-10 04:43:53 -0700322static int may_context_mount_sb_relabel(u32 sid,
323 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100324 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700325{
David Howells275bb412008-11-14 10:39:19 +1100326 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700327 int rc;
328
329 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
330 FILESYSTEM__RELABELFROM, NULL);
331 if (rc)
332 return rc;
333
334 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
335 FILESYSTEM__RELABELTO, NULL);
336 return rc;
337}
338
Eric Paris08089252006-07-10 04:43:55 -0700339static int may_context_mount_inode_relabel(u32 sid,
340 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100341 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700342{
David Howells275bb412008-11-14 10:39:19 +1100343 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700344 int rc;
345 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
346 FILESYSTEM__RELABELFROM, NULL);
347 if (rc)
348 return rc;
349
350 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
351 FILESYSTEM__ASSOCIATE, NULL);
352 return rc;
353}
354
Eric Parisc9180a52007-11-30 13:00:35 -0500355static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct superblock_security_struct *sbsec = sb->s_security;
358 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500359 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int rc = 0;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
363 /* Make sure that the xattr handler exists and that no
364 error other than -ENODATA is returned by getxattr on
365 the root directory. -ENODATA is ok, as this may be
366 the first boot of the SELinux kernel before we have
367 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500368 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
370 "xattr support\n", sb->s_id, sb->s_type->name);
371 rc = -EOPNOTSUPP;
372 goto out;
373 }
Eric Parisc9180a52007-11-30 13:00:35 -0500374 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (rc < 0 && rc != -ENODATA) {
376 if (rc == -EOPNOTSUPP)
377 printk(KERN_WARNING "SELinux: (dev %s, type "
378 "%s) has no security xattr handler\n",
379 sb->s_id, sb->s_type->name);
380 else
381 printk(KERN_WARNING "SELinux: (dev %s, type "
382 "%s) getxattr errno %d\n", sb->s_id,
383 sb->s_type->name, -rc);
384 goto out;
385 }
386 }
387
David P. Quigley11689d42009-01-16 09:22:03 -0500388 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Eric Parisc9180a52007-11-30 13:00:35 -0500390 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500391 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500393 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500394 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sb->s_id, sb->s_type->name,
396 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David P. Quigley11689d42009-01-16 09:22:03 -0500398 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
399 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
400 sbsec->behavior == SECURITY_FS_USE_NONE ||
401 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
402 sbsec->flags &= ~SE_SBLABELSUPP;
403
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400404 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
405 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
406 sbsec->flags |= SE_SBLABELSUPP;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500409 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* Initialize any other inodes associated with the superblock, e.g.
412 inodes created prior to initial policy load or inodes created
413 during get_sb by a pseudo filesystem that directly
414 populates itself. */
415 spin_lock(&sbsec->isec_lock);
416next_inode:
417 if (!list_empty(&sbsec->isec_head)) {
418 struct inode_security_struct *isec =
419 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500420 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct inode *inode = isec->inode;
422 spin_unlock(&sbsec->isec_lock);
423 inode = igrab(inode);
424 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500425 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 inode_doinit(inode);
427 iput(inode);
428 }
429 spin_lock(&sbsec->isec_lock);
430 list_del_init(&isec->list);
431 goto next_inode;
432 }
433 spin_unlock(&sbsec->isec_lock);
434out:
Eric Parisc9180a52007-11-30 13:00:35 -0500435 return rc;
436}
437
438/*
439 * This function should allow an FS to ask what it's mount security
440 * options were so it can use those later for submounts, displaying
441 * mount options, or whatever.
442 */
443static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500444 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500445{
446 int rc = 0, i;
447 struct superblock_security_struct *sbsec = sb->s_security;
448 char *context = NULL;
449 u32 len;
450 char tmp;
451
Eric Parise0007522008-03-05 10:31:54 -0500452 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500453
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500454 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500455 return -EINVAL;
456
457 if (!ss_initialized)
458 return -EINVAL;
459
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500460 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500461 /* count the number of mount options for this sb */
462 for (i = 0; i < 8; i++) {
463 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500464 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500465 tmp >>= 1;
466 }
David P. Quigley11689d42009-01-16 09:22:03 -0500467 /* Check if the Label support flag is set */
468 if (sbsec->flags & SE_SBLABELSUPP)
469 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500470
Eric Parise0007522008-03-05 10:31:54 -0500471 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
472 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500473 rc = -ENOMEM;
474 goto out_free;
475 }
476
Eric Parise0007522008-03-05 10:31:54 -0500477 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
478 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500479 rc = -ENOMEM;
480 goto out_free;
481 }
482
483 i = 0;
484 if (sbsec->flags & FSCONTEXT_MNT) {
485 rc = security_sid_to_context(sbsec->sid, &context, &len);
486 if (rc)
487 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500488 opts->mnt_opts[i] = context;
489 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500490 }
491 if (sbsec->flags & CONTEXT_MNT) {
492 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
493 if (rc)
494 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500495 opts->mnt_opts[i] = context;
496 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500497 }
498 if (sbsec->flags & DEFCONTEXT_MNT) {
499 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
500 if (rc)
501 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500502 opts->mnt_opts[i] = context;
503 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500504 }
505 if (sbsec->flags & ROOTCONTEXT_MNT) {
506 struct inode *root = sbsec->sb->s_root->d_inode;
507 struct inode_security_struct *isec = root->i_security;
508
509 rc = security_sid_to_context(isec->sid, &context, &len);
510 if (rc)
511 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500512 opts->mnt_opts[i] = context;
513 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500514 }
David P. Quigley11689d42009-01-16 09:22:03 -0500515 if (sbsec->flags & SE_SBLABELSUPP) {
516 opts->mnt_opts[i] = NULL;
517 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
518 }
Eric Parisc9180a52007-11-30 13:00:35 -0500519
Eric Parise0007522008-03-05 10:31:54 -0500520 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500521
522 return 0;
523
524out_free:
Eric Parise0007522008-03-05 10:31:54 -0500525 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500526 return rc;
527}
528
529static int bad_option(struct superblock_security_struct *sbsec, char flag,
530 u32 old_sid, u32 new_sid)
531{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500532 char mnt_flags = sbsec->flags & SE_MNTMASK;
533
Eric Parisc9180a52007-11-30 13:00:35 -0500534 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500535 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500536 if (!(sbsec->flags & flag) ||
537 (old_sid != new_sid))
538 return 1;
539
540 /* check if we were passed the same options twice,
541 * aka someone passed context=a,context=b
542 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500543 if (!(sbsec->flags & SE_SBINITIALIZED))
544 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500545 return 1;
546 return 0;
547}
Eric Parise0007522008-03-05 10:31:54 -0500548
Eric Parisc9180a52007-11-30 13:00:35 -0500549/*
550 * Allow filesystems with binary mount data to explicitly set mount point
551 * labeling information.
552 */
Eric Parise0007522008-03-05 10:31:54 -0500553static int selinux_set_mnt_opts(struct super_block *sb,
554 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500555{
David Howells275bb412008-11-14 10:39:19 +1100556 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500557 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500558 struct superblock_security_struct *sbsec = sb->s_security;
559 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000560 struct inode *inode = sbsec->sb->s_root->d_inode;
561 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500562 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
563 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500564 char **mount_options = opts->mnt_opts;
565 int *flags = opts->mnt_opts_flags;
566 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500567
568 mutex_lock(&sbsec->lock);
569
570 if (!ss_initialized) {
571 if (!num_opts) {
572 /* Defer initialization until selinux_complete_init,
573 after the initial policy is loaded and the security
574 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500575 goto out;
576 }
577 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400578 printk(KERN_WARNING "SELinux: Unable to set superblock options "
579 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500580 goto out;
581 }
582
583 /*
Eric Parise0007522008-03-05 10:31:54 -0500584 * Binary mount data FS will come through this function twice. Once
585 * from an explicit call and once from the generic calls from the vfs.
586 * Since the generic VFS calls will not contain any security mount data
587 * we need to skip the double mount verification.
588 *
589 * This does open a hole in which we will not notice if the first
590 * mount using this sb set explict options and a second mount using
591 * this sb does not set any security options. (The first options
592 * will be used for both mounts)
593 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500594 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500595 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400596 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500597
598 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500599 * parse the mount options, check if they are valid sids.
600 * also check if someone is trying to mount the same sb more
601 * than once with different security options.
602 */
603 for (i = 0; i < num_opts; i++) {
604 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500605
606 if (flags[i] == SE_SBLABELSUPP)
607 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500608 rc = security_context_to_sid(mount_options[i],
609 strlen(mount_options[i]), &sid);
610 if (rc) {
611 printk(KERN_WARNING "SELinux: security_context_to_sid"
612 "(%s) failed for (dev %s, type %s) errno=%d\n",
613 mount_options[i], sb->s_id, name, rc);
614 goto out;
615 }
616 switch (flags[i]) {
617 case FSCONTEXT_MNT:
618 fscontext_sid = sid;
619
620 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
621 fscontext_sid))
622 goto out_double_mount;
623
624 sbsec->flags |= FSCONTEXT_MNT;
625 break;
626 case CONTEXT_MNT:
627 context_sid = sid;
628
629 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
630 context_sid))
631 goto out_double_mount;
632
633 sbsec->flags |= CONTEXT_MNT;
634 break;
635 case ROOTCONTEXT_MNT:
636 rootcontext_sid = sid;
637
638 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
639 rootcontext_sid))
640 goto out_double_mount;
641
642 sbsec->flags |= ROOTCONTEXT_MNT;
643
644 break;
645 case DEFCONTEXT_MNT:
646 defcontext_sid = sid;
647
648 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
649 defcontext_sid))
650 goto out_double_mount;
651
652 sbsec->flags |= DEFCONTEXT_MNT;
653
654 break;
655 default:
656 rc = -EINVAL;
657 goto out;
658 }
659 }
660
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500661 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500662 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500663 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500664 goto out_double_mount;
665 rc = 0;
666 goto out;
667 }
668
James Morris089be432008-07-15 18:32:49 +1000669 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500670 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500671
672 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500673 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500674 if (rc) {
675 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000676 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500677 goto out;
678 }
679
680 /* sets the context of the superblock for the fs being mounted. */
681 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100682 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500683 if (rc)
684 goto out;
685
686 sbsec->sid = fscontext_sid;
687 }
688
689 /*
690 * Switch to using mount point labeling behavior.
691 * sets the label used on all file below the mountpoint, and will set
692 * the superblock context if not already set.
693 */
694 if (context_sid) {
695 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100696 rc = may_context_mount_sb_relabel(context_sid, sbsec,
697 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500698 if (rc)
699 goto out;
700 sbsec->sid = context_sid;
701 } else {
David Howells275bb412008-11-14 10:39:19 +1100702 rc = may_context_mount_inode_relabel(context_sid, sbsec,
703 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500704 if (rc)
705 goto out;
706 }
707 if (!rootcontext_sid)
708 rootcontext_sid = context_sid;
709
710 sbsec->mntpoint_sid = context_sid;
711 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
712 }
713
714 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100715 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
716 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500717 if (rc)
718 goto out;
719
720 root_isec->sid = rootcontext_sid;
721 root_isec->initialized = 1;
722 }
723
724 if (defcontext_sid) {
725 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
726 rc = -EINVAL;
727 printk(KERN_WARNING "SELinux: defcontext option is "
728 "invalid for this filesystem type\n");
729 goto out;
730 }
731
732 if (defcontext_sid != sbsec->def_sid) {
733 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100734 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500735 if (rc)
736 goto out;
737 }
738
739 sbsec->def_sid = defcontext_sid;
740 }
741
742 rc = sb_finish_set_opts(sb);
743out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700744 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500746out_double_mount:
747 rc = -EINVAL;
748 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
749 "security settings for (dev %s, type %s)\n", sb->s_id, name);
750 goto out;
751}
752
Jeff Layton094f7b62013-04-01 08:14:24 -0400753static int selinux_cmp_sb_context(const struct super_block *oldsb,
754 const struct super_block *newsb)
755{
756 struct superblock_security_struct *old = oldsb->s_security;
757 struct superblock_security_struct *new = newsb->s_security;
758 char oldflags = old->flags & SE_MNTMASK;
759 char newflags = new->flags & SE_MNTMASK;
760
761 if (oldflags != newflags)
762 goto mismatch;
763 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
764 goto mismatch;
765 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
766 goto mismatch;
767 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
768 goto mismatch;
769 if (oldflags & ROOTCONTEXT_MNT) {
770 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
771 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
772 if (oldroot->sid != newroot->sid)
773 goto mismatch;
774 }
775 return 0;
776mismatch:
777 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
778 "different security settings for (dev %s, "
779 "type %s)\n", newsb->s_id, newsb->s_type->name);
780 return -EBUSY;
781}
782
783static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Eric Parisc9180a52007-11-30 13:00:35 -0500784 struct super_block *newsb)
785{
786 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
787 struct superblock_security_struct *newsbsec = newsb->s_security;
788
789 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
790 int set_context = (oldsbsec->flags & CONTEXT_MNT);
791 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
792
Eric Paris0f5e6422008-04-21 16:24:11 -0400793 /*
794 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400795 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400796 */
Al Viroe8c26252010-03-23 06:36:54 -0400797 if (!ss_initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400798 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500799
Eric Parisc9180a52007-11-30 13:00:35 -0500800 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500801 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500802
Jeff Layton094f7b62013-04-01 08:14:24 -0400803 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500804 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400805 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400806
Eric Parisc9180a52007-11-30 13:00:35 -0500807 mutex_lock(&newsbsec->lock);
808
809 newsbsec->flags = oldsbsec->flags;
810
811 newsbsec->sid = oldsbsec->sid;
812 newsbsec->def_sid = oldsbsec->def_sid;
813 newsbsec->behavior = oldsbsec->behavior;
814
815 if (set_context) {
816 u32 sid = oldsbsec->mntpoint_sid;
817
818 if (!set_fscontext)
819 newsbsec->sid = sid;
820 if (!set_rootcontext) {
821 struct inode *newinode = newsb->s_root->d_inode;
822 struct inode_security_struct *newisec = newinode->i_security;
823 newisec->sid = sid;
824 }
825 newsbsec->mntpoint_sid = sid;
826 }
827 if (set_rootcontext) {
828 const struct inode *oldinode = oldsb->s_root->d_inode;
829 const struct inode_security_struct *oldisec = oldinode->i_security;
830 struct inode *newinode = newsb->s_root->d_inode;
831 struct inode_security_struct *newisec = newinode->i_security;
832
833 newisec->sid = oldisec->sid;
834 }
835
836 sb_finish_set_opts(newsb);
837 mutex_unlock(&newsbsec->lock);
Jeff Layton094f7b62013-04-01 08:14:24 -0400838 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500839}
840
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200841static int selinux_parse_opts_str(char *options,
842 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500843{
Eric Parise0007522008-03-05 10:31:54 -0500844 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500845 char *context = NULL, *defcontext = NULL;
846 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500847 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500848
Eric Parise0007522008-03-05 10:31:54 -0500849 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500850
851 /* Standard string-based options. */
852 while ((p = strsep(&options, "|")) != NULL) {
853 int token;
854 substring_t args[MAX_OPT_ARGS];
855
856 if (!*p)
857 continue;
858
859 token = match_token(p, tokens, args);
860
861 switch (token) {
862 case Opt_context:
863 if (context || defcontext) {
864 rc = -EINVAL;
865 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
866 goto out_err;
867 }
868 context = match_strdup(&args[0]);
869 if (!context) {
870 rc = -ENOMEM;
871 goto out_err;
872 }
873 break;
874
875 case Opt_fscontext:
876 if (fscontext) {
877 rc = -EINVAL;
878 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
879 goto out_err;
880 }
881 fscontext = match_strdup(&args[0]);
882 if (!fscontext) {
883 rc = -ENOMEM;
884 goto out_err;
885 }
886 break;
887
888 case Opt_rootcontext:
889 if (rootcontext) {
890 rc = -EINVAL;
891 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
892 goto out_err;
893 }
894 rootcontext = match_strdup(&args[0]);
895 if (!rootcontext) {
896 rc = -ENOMEM;
897 goto out_err;
898 }
899 break;
900
901 case Opt_defcontext:
902 if (context || defcontext) {
903 rc = -EINVAL;
904 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
905 goto out_err;
906 }
907 defcontext = match_strdup(&args[0]);
908 if (!defcontext) {
909 rc = -ENOMEM;
910 goto out_err;
911 }
912 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500913 case Opt_labelsupport:
914 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500915 default:
916 rc = -EINVAL;
917 printk(KERN_WARNING "SELinux: unknown mount option\n");
918 goto out_err;
919
920 }
921 }
922
Eric Parise0007522008-03-05 10:31:54 -0500923 rc = -ENOMEM;
924 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
925 if (!opts->mnt_opts)
926 goto out_err;
927
928 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
929 if (!opts->mnt_opts_flags) {
930 kfree(opts->mnt_opts);
931 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500932 }
933
Eric Parise0007522008-03-05 10:31:54 -0500934 if (fscontext) {
935 opts->mnt_opts[num_mnt_opts] = fscontext;
936 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
937 }
938 if (context) {
939 opts->mnt_opts[num_mnt_opts] = context;
940 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
941 }
942 if (rootcontext) {
943 opts->mnt_opts[num_mnt_opts] = rootcontext;
944 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
945 }
946 if (defcontext) {
947 opts->mnt_opts[num_mnt_opts] = defcontext;
948 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
949 }
950
951 opts->num_mnt_opts = num_mnt_opts;
952 return 0;
953
Eric Parisc9180a52007-11-30 13:00:35 -0500954out_err:
955 kfree(context);
956 kfree(defcontext);
957 kfree(fscontext);
958 kfree(rootcontext);
959 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
Eric Parise0007522008-03-05 10:31:54 -0500961/*
962 * string mount options parsing and call set the sbsec
963 */
964static int superblock_doinit(struct super_block *sb, void *data)
965{
966 int rc = 0;
967 char *options = data;
968 struct security_mnt_opts opts;
969
970 security_init_mnt_opts(&opts);
971
972 if (!data)
973 goto out;
974
975 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
976
977 rc = selinux_parse_opts_str(options, &opts);
978 if (rc)
979 goto out_err;
980
981out:
982 rc = selinux_set_mnt_opts(sb, &opts);
983
984out_err:
985 security_free_mnt_opts(&opts);
986 return rc;
987}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Adrian Bunk3583a712008-07-22 20:21:23 +0300989static void selinux_write_opts(struct seq_file *m,
990 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000991{
992 int i;
993 char *prefix;
994
995 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500996 char *has_comma;
997
998 if (opts->mnt_opts[i])
999 has_comma = strchr(opts->mnt_opts[i], ',');
1000 else
1001 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001002
1003 switch (opts->mnt_opts_flags[i]) {
1004 case CONTEXT_MNT:
1005 prefix = CONTEXT_STR;
1006 break;
1007 case FSCONTEXT_MNT:
1008 prefix = FSCONTEXT_STR;
1009 break;
1010 case ROOTCONTEXT_MNT:
1011 prefix = ROOTCONTEXT_STR;
1012 break;
1013 case DEFCONTEXT_MNT:
1014 prefix = DEFCONTEXT_STR;
1015 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001016 case SE_SBLABELSUPP:
1017 seq_putc(m, ',');
1018 seq_puts(m, LABELSUPP_STR);
1019 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001020 default:
1021 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001022 return;
Eric Paris2069f452008-07-04 09:47:13 +10001023 };
1024 /* we need a comma before each option */
1025 seq_putc(m, ',');
1026 seq_puts(m, prefix);
1027 if (has_comma)
1028 seq_putc(m, '\"');
1029 seq_puts(m, opts->mnt_opts[i]);
1030 if (has_comma)
1031 seq_putc(m, '\"');
1032 }
1033}
1034
1035static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1036{
1037 struct security_mnt_opts opts;
1038 int rc;
1039
1040 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001041 if (rc) {
1042 /* before policy load we may get EINVAL, don't show anything */
1043 if (rc == -EINVAL)
1044 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001045 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001046 }
Eric Paris2069f452008-07-04 09:47:13 +10001047
1048 selinux_write_opts(m, &opts);
1049
1050 security_free_mnt_opts(&opts);
1051
1052 return rc;
1053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055static inline u16 inode_mode_to_security_class(umode_t mode)
1056{
1057 switch (mode & S_IFMT) {
1058 case S_IFSOCK:
1059 return SECCLASS_SOCK_FILE;
1060 case S_IFLNK:
1061 return SECCLASS_LNK_FILE;
1062 case S_IFREG:
1063 return SECCLASS_FILE;
1064 case S_IFBLK:
1065 return SECCLASS_BLK_FILE;
1066 case S_IFDIR:
1067 return SECCLASS_DIR;
1068 case S_IFCHR:
1069 return SECCLASS_CHR_FILE;
1070 case S_IFIFO:
1071 return SECCLASS_FIFO_FILE;
1072
1073 }
1074
1075 return SECCLASS_FILE;
1076}
1077
James Morris13402582005-09-30 14:24:34 -04001078static inline int default_protocol_stream(int protocol)
1079{
1080 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1081}
1082
1083static inline int default_protocol_dgram(int protocol)
1084{
1085 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1089{
1090 switch (family) {
1091 case PF_UNIX:
1092 switch (type) {
1093 case SOCK_STREAM:
1094 case SOCK_SEQPACKET:
1095 return SECCLASS_UNIX_STREAM_SOCKET;
1096 case SOCK_DGRAM:
1097 return SECCLASS_UNIX_DGRAM_SOCKET;
1098 }
1099 break;
1100 case PF_INET:
1101 case PF_INET6:
1102 switch (type) {
1103 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001104 if (default_protocol_stream(protocol))
1105 return SECCLASS_TCP_SOCKET;
1106 else
1107 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001109 if (default_protocol_dgram(protocol))
1110 return SECCLASS_UDP_SOCKET;
1111 else
1112 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001113 case SOCK_DCCP:
1114 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001115 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return SECCLASS_RAWIP_SOCKET;
1117 }
1118 break;
1119 case PF_NETLINK:
1120 switch (protocol) {
1121 case NETLINK_ROUTE:
1122 return SECCLASS_NETLINK_ROUTE_SOCKET;
1123 case NETLINK_FIREWALL:
1124 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001125 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1127 case NETLINK_NFLOG:
1128 return SECCLASS_NETLINK_NFLOG_SOCKET;
1129 case NETLINK_XFRM:
1130 return SECCLASS_NETLINK_XFRM_SOCKET;
1131 case NETLINK_SELINUX:
1132 return SECCLASS_NETLINK_SELINUX_SOCKET;
1133 case NETLINK_AUDIT:
1134 return SECCLASS_NETLINK_AUDIT_SOCKET;
1135 case NETLINK_IP6_FW:
1136 return SECCLASS_NETLINK_IP6FW_SOCKET;
1137 case NETLINK_DNRTMSG:
1138 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001139 case NETLINK_KOBJECT_UEVENT:
1140 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 default:
1142 return SECCLASS_NETLINK_SOCKET;
1143 }
1144 case PF_PACKET:
1145 return SECCLASS_PACKET_SOCKET;
1146 case PF_KEY:
1147 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001148 case PF_APPLETALK:
1149 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
1152 return SECCLASS_SOCKET;
1153}
1154
1155#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001156static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 u16 tclass,
1158 u32 *sid)
1159{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001160 int rc;
1161 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Eric Paris828dfe12008-04-17 13:17:49 -04001163 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (!buffer)
1165 return -ENOMEM;
1166
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001167 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1168 if (IS_ERR(path))
1169 rc = PTR_ERR(path);
1170 else {
1171 /* each process gets a /proc/PID/ entry. Strip off the
1172 * PID part to get a valid selinux labeling.
1173 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1174 while (path[1] >= '0' && path[1] <= '9') {
1175 path[1] = '/';
1176 path++;
1177 }
1178 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 free_page((unsigned long)buffer);
1181 return rc;
1182}
1183#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001184static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 u16 tclass,
1186 u32 *sid)
1187{
1188 return -EINVAL;
1189}
1190#endif
1191
1192/* The inode's security attributes must be initialized before first use. */
1193static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1194{
1195 struct superblock_security_struct *sbsec = NULL;
1196 struct inode_security_struct *isec = inode->i_security;
1197 u32 sid;
1198 struct dentry *dentry;
1199#define INITCONTEXTLEN 255
1200 char *context = NULL;
1201 unsigned len = 0;
1202 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (isec->initialized)
1205 goto out;
1206
Eric Paris23970742006-09-25 23:32:01 -07001207 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001209 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001212 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /* Defer initialization until selinux_complete_init,
1214 after the initial policy is loaded and the security
1215 server is ready to handle calls. */
1216 spin_lock(&sbsec->isec_lock);
1217 if (list_empty(&isec->list))
1218 list_add(&isec->list, &sbsec->isec_head);
1219 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001220 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 switch (sbsec->behavior) {
1224 case SECURITY_FS_USE_XATTR:
1225 if (!inode->i_op->getxattr) {
1226 isec->sid = sbsec->def_sid;
1227 break;
1228 }
1229
1230 /* Need a dentry, since the xattr API requires one.
1231 Life would be simpler if we could just pass the inode. */
1232 if (opt_dentry) {
1233 /* Called from d_instantiate or d_splice_alias. */
1234 dentry = dget(opt_dentry);
1235 } else {
1236 /* Called from selinux_complete_init, try to find a dentry. */
1237 dentry = d_find_alias(inode);
1238 }
1239 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001240 /*
1241 * this is can be hit on boot when a file is accessed
1242 * before the policy is loaded. When we load policy we
1243 * may find inodes that have no dentry on the
1244 * sbsec->isec_head list. No reason to complain as these
1245 * will get fixed up the next time we go through
1246 * inode_doinit with a dentry, before these inodes could
1247 * be used again by userspace.
1248 */
Eric Paris23970742006-09-25 23:32:01 -07001249 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
1252 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001253 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!context) {
1255 rc = -ENOMEM;
1256 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001257 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001259 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1261 context, len);
1262 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001263 kfree(context);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /* Need a larger buffer. Query for the right size. */
1266 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1267 NULL, 0);
1268 if (rc < 0) {
1269 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001270 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001273 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!context) {
1275 rc = -ENOMEM;
1276 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001277 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001279 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 rc = inode->i_op->getxattr(dentry,
1281 XATTR_NAME_SELINUX,
1282 context, len);
1283 }
1284 dput(dentry);
1285 if (rc < 0) {
1286 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001287 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001288 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 -rc, inode->i_sb->s_id, inode->i_ino);
1290 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001291 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 /* Map ENODATA to the default file SID */
1294 sid = sbsec->def_sid;
1295 rc = 0;
1296 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001297 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001298 sbsec->def_sid,
1299 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001301 char *dev = inode->i_sb->s_id;
1302 unsigned long ino = inode->i_ino;
1303
1304 if (rc == -EINVAL) {
1305 if (printk_ratelimit())
1306 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1307 "context=%s. This indicates you may need to relabel the inode or the "
1308 "filesystem in question.\n", ino, dev, context);
1309 } else {
1310 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1311 "returned %d for dev=%s ino=%ld\n",
1312 __func__, context, -rc, dev, ino);
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 kfree(context);
1315 /* Leave with the unlabeled SID */
1316 rc = 0;
1317 break;
1318 }
1319 }
1320 kfree(context);
1321 isec->sid = sid;
1322 break;
1323 case SECURITY_FS_USE_TASK:
1324 isec->sid = isec->task_sid;
1325 break;
1326 case SECURITY_FS_USE_TRANS:
1327 /* Default to the fs SID. */
1328 isec->sid = sbsec->sid;
1329
1330 /* Try to obtain a transition SID. */
1331 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001332 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1333 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001335 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 isec->sid = sid;
1337 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001338 case SECURITY_FS_USE_MNTPOINT:
1339 isec->sid = sbsec->mntpoint_sid;
1340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001342 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 isec->sid = sbsec->sid;
1344
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001345 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001346 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001348 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 isec->sclass,
1350 &sid);
1351 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001352 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 isec->sid = sid;
1354 }
1355 }
1356 break;
1357 }
1358
1359 isec->initialized = 1;
1360
Eric Paris23970742006-09-25 23:32:01 -07001361out_unlock:
1362 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363out:
1364 if (isec->sclass == SECCLASS_FILE)
1365 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return rc;
1367}
1368
1369/* Convert a Linux signal to an access vector. */
1370static inline u32 signal_to_av(int sig)
1371{
1372 u32 perm = 0;
1373
1374 switch (sig) {
1375 case SIGCHLD:
1376 /* Commonly granted from child to parent. */
1377 perm = PROCESS__SIGCHLD;
1378 break;
1379 case SIGKILL:
1380 /* Cannot be caught or ignored */
1381 perm = PROCESS__SIGKILL;
1382 break;
1383 case SIGSTOP:
1384 /* Cannot be caught or ignored */
1385 perm = PROCESS__SIGSTOP;
1386 break;
1387 default:
1388 /* All other signals. */
1389 perm = PROCESS__SIGNAL;
1390 break;
1391 }
1392
1393 return perm;
1394}
1395
David Howells275bb412008-11-14 10:39:19 +11001396/*
David Howellsd84f4f92008-11-14 10:39:23 +11001397 * Check permission between a pair of credentials
1398 * fork check, ptrace check, etc.
1399 */
1400static int cred_has_perm(const struct cred *actor,
1401 const struct cred *target,
1402 u32 perms)
1403{
1404 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1405
1406 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1407}
1408
1409/*
David Howells88e67f32008-11-14 10:39:21 +11001410 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001411 * fork check, ptrace check, etc.
1412 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001413 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001414 */
1415static int task_has_perm(const struct task_struct *tsk1,
1416 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 u32 perms)
1418{
David Howells275bb412008-11-14 10:39:19 +11001419 const struct task_security_struct *__tsec1, *__tsec2;
1420 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
David Howells275bb412008-11-14 10:39:19 +11001422 rcu_read_lock();
1423 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1424 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1425 rcu_read_unlock();
1426 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
David Howells3b11a1d2008-11-14 10:39:26 +11001429/*
1430 * Check permission between current and another task, e.g. signal checks,
1431 * fork check, ptrace check, etc.
1432 * current is the actor and tsk2 is the target
1433 * - this uses current's subjective creds
1434 */
1435static int current_has_perm(const struct task_struct *tsk,
1436 u32 perms)
1437{
1438 u32 sid, tsid;
1439
1440 sid = current_sid();
1441 tsid = task_sid(tsk);
1442 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1443}
1444
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001445#if CAP_LAST_CAP > 63
1446#error Fix SELinux to handle capabilities > 63.
1447#endif
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001450static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001451 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Thomas Liu2bf49692009-07-14 12:14:09 -04001453 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001454 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001455 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001456 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001457 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001458 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Eric Paris50c205f2012-04-04 15:01:43 -04001460 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 ad.u.cap = cap;
1462
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001463 switch (CAP_TO_INDEX(cap)) {
1464 case 0:
1465 sclass = SECCLASS_CAPABILITY;
1466 break;
1467 case 1:
1468 sclass = SECCLASS_CAPABILITY2;
1469 break;
1470 default:
1471 printk(KERN_ERR
1472 "SELinux: out of range capability %d\n", cap);
1473 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001474 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001475 }
Eric Paris06112162008-11-11 22:02:50 +11001476
David Howells275bb412008-11-14 10:39:19 +11001477 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001478 if (audit == SECURITY_CAP_AUDIT) {
1479 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1480 if (rc2)
1481 return rc2;
1482 }
Eric Paris06112162008-11-11 22:02:50 +11001483 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486/* Check whether a task is allowed to use a system operation. */
1487static int task_has_system(struct task_struct *tsk,
1488 u32 perms)
1489{
David Howells275bb412008-11-14 10:39:19 +11001490 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
David Howells275bb412008-11-14 10:39:19 +11001492 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 SECCLASS_SYSTEM, perms, NULL);
1494}
1495
1496/* Check whether a task has a particular permission to an inode.
1497 The 'adp' parameter is optional and allows other audit
1498 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001499static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct inode *inode,
1501 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001502 struct common_audit_data *adp,
1503 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001506 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
David Howellse0e81732009-09-02 09:13:40 +01001508 validate_creds(cred);
1509
Eric Paris828dfe12008-04-17 13:17:49 -04001510 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001511 return 0;
1512
David Howells88e67f32008-11-14 10:39:21 +11001513 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 isec = inode->i_security;
1515
Eric Paris9ade0cf2011-04-25 16:26:29 -04001516 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519/* Same as inode_has_perm, but pass explicit audit data containing
1520 the dentry to help the auditing code to more easily generate the
1521 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001522static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct dentry *dentry,
1524 u32 av)
1525{
1526 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001527 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001528
Eric Paris50c205f2012-04-04 15:01:43 -04001529 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001530 ad.u.dentry = dentry;
1531 return inode_has_perm(cred, inode, av, &ad, 0);
1532}
1533
1534/* Same as inode_has_perm, but pass explicit audit data containing
1535 the path to help the auditing code to more easily generate the
1536 pathname if needed. */
1537static inline int path_has_perm(const struct cred *cred,
1538 struct path *path,
1539 u32 av)
1540{
1541 struct inode *inode = path->dentry->d_inode;
1542 struct common_audit_data ad;
1543
Eric Paris50c205f2012-04-04 15:01:43 -04001544 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001545 ad.u.path = *path;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001546 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549/* Check whether a task can use an open file descriptor to
1550 access an inode in a given way. Check access to the
1551 descriptor itself, and then use dentry_has_perm to
1552 check a particular permission to the file.
1553 Access to the descriptor is implicitly granted if it
1554 has the same SID as the process. If av is zero, then
1555 access to the file is not checked, e.g. for cases
1556 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001557static int file_has_perm(const struct cred *cred,
1558 struct file *file,
1559 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001562 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001563 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001564 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int rc;
1566
Eric Paris50c205f2012-04-04 15:01:43 -04001567 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04001568 ad.u.path = file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
David Howells275bb412008-11-14 10:39:19 +11001570 if (sid != fsec->sid) {
1571 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 SECCLASS_FD,
1573 FD__USE,
1574 &ad);
1575 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578
1579 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001580 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001582 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
David Howells88e67f32008-11-14 10:39:21 +11001584out:
1585 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588/* Check whether a task can create a file. */
1589static int may_create(struct inode *dir,
1590 struct dentry *dentry,
1591 u16 tclass)
1592{
Paul Moore5fb49872010-04-22 14:46:19 -04001593 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct inode_security_struct *dsec;
1595 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001596 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001597 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int rc;
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 dsec = dir->i_security;
1601 sbsec = dir->i_sb->s_security;
1602
David Howells275bb412008-11-14 10:39:19 +11001603 sid = tsec->sid;
1604 newsid = tsec->create_sid;
1605
Eric Paris50c205f2012-04-04 15:01:43 -04001606 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001607 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
David Howells275bb412008-11-14 10:39:19 +11001609 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 DIR__ADD_NAME | DIR__SEARCH,
1611 &ad);
1612 if (rc)
1613 return rc;
1614
David P. Quigleycd895962009-01-16 09:22:04 -05001615 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001616 rc = security_transition_sid(sid, dsec->sid, tclass,
1617 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (rc)
1619 return rc;
1620 }
1621
David Howells275bb412008-11-14 10:39:19 +11001622 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (rc)
1624 return rc;
1625
1626 return avc_has_perm(newsid, sbsec->sid,
1627 SECCLASS_FILESYSTEM,
1628 FILESYSTEM__ASSOCIATE, &ad);
1629}
1630
Michael LeMay4eb582c2006-06-26 00:24:57 -07001631/* Check whether a task can create a key. */
1632static int may_create_key(u32 ksid,
1633 struct task_struct *ctx)
1634{
David Howells275bb412008-11-14 10:39:19 +11001635 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001636
David Howells275bb412008-11-14 10:39:19 +11001637 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001638}
1639
Eric Paris828dfe12008-04-17 13:17:49 -04001640#define MAY_LINK 0
1641#define MAY_UNLINK 1
1642#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644/* Check whether a task can link, unlink, or rmdir a file/directory. */
1645static int may_link(struct inode *dir,
1646 struct dentry *dentry,
1647 int kind)
1648
1649{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001651 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001652 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 u32 av;
1654 int rc;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 dsec = dir->i_security;
1657 isec = dentry->d_inode->i_security;
1658
Eric Paris50c205f2012-04-04 15:01:43 -04001659 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001660 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 av = DIR__SEARCH;
1663 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001664 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (rc)
1666 return rc;
1667
1668 switch (kind) {
1669 case MAY_LINK:
1670 av = FILE__LINK;
1671 break;
1672 case MAY_UNLINK:
1673 av = FILE__UNLINK;
1674 break;
1675 case MAY_RMDIR:
1676 av = DIR__RMDIR;
1677 break;
1678 default:
Eric Paris744ba352008-04-17 11:52:44 -04001679 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1680 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return 0;
1682 }
1683
David Howells275bb412008-11-14 10:39:19 +11001684 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return rc;
1686}
1687
1688static inline int may_rename(struct inode *old_dir,
1689 struct dentry *old_dentry,
1690 struct inode *new_dir,
1691 struct dentry *new_dentry)
1692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001694 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001695 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 u32 av;
1697 int old_is_dir, new_is_dir;
1698 int rc;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 old_dsec = old_dir->i_security;
1701 old_isec = old_dentry->d_inode->i_security;
1702 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1703 new_dsec = new_dir->i_security;
1704
Eric Paris50c205f2012-04-04 15:01:43 -04001705 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Eric Parisa2694342011-04-25 13:10:27 -04001707 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001708 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1710 if (rc)
1711 return rc;
David Howells275bb412008-11-14 10:39:19 +11001712 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 old_isec->sclass, FILE__RENAME, &ad);
1714 if (rc)
1715 return rc;
1716 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001717 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 old_isec->sclass, DIR__REPARENT, &ad);
1719 if (rc)
1720 return rc;
1721 }
1722
Eric Parisa2694342011-04-25 13:10:27 -04001723 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 av = DIR__ADD_NAME | DIR__SEARCH;
1725 if (new_dentry->d_inode)
1726 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001727 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (rc)
1729 return rc;
1730 if (new_dentry->d_inode) {
1731 new_isec = new_dentry->d_inode->i_security;
1732 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001733 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 new_isec->sclass,
1735 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1736 if (rc)
1737 return rc;
1738 }
1739
1740 return 0;
1741}
1742
1743/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001744static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct super_block *sb,
1746 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001747 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001750 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001753 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756/* Convert a Linux mode and permission mask to an access vector. */
1757static inline u32 file_mask_to_av(int mode, int mask)
1758{
1759 u32 av = 0;
1760
Al Virodba19c62011-07-25 20:49:29 -04001761 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (mask & MAY_EXEC)
1763 av |= FILE__EXECUTE;
1764 if (mask & MAY_READ)
1765 av |= FILE__READ;
1766
1767 if (mask & MAY_APPEND)
1768 av |= FILE__APPEND;
1769 else if (mask & MAY_WRITE)
1770 av |= FILE__WRITE;
1771
1772 } else {
1773 if (mask & MAY_EXEC)
1774 av |= DIR__SEARCH;
1775 if (mask & MAY_WRITE)
1776 av |= DIR__WRITE;
1777 if (mask & MAY_READ)
1778 av |= DIR__READ;
1779 }
1780
1781 return av;
1782}
1783
1784/* Convert a Linux file to an access vector. */
1785static inline u32 file_to_av(struct file *file)
1786{
1787 u32 av = 0;
1788
1789 if (file->f_mode & FMODE_READ)
1790 av |= FILE__READ;
1791 if (file->f_mode & FMODE_WRITE) {
1792 if (file->f_flags & O_APPEND)
1793 av |= FILE__APPEND;
1794 else
1795 av |= FILE__WRITE;
1796 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001797 if (!av) {
1798 /*
1799 * Special file opened with flags 3 for ioctl-only use.
1800 */
1801 av = FILE__IOCTL;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 return av;
1805}
1806
Eric Paris8b6a5a32008-10-29 17:06:46 -04001807/*
1808 * Convert a file to an access vector and include the correct open
1809 * open permission.
1810 */
1811static inline u32 open_file_to_av(struct file *file)
1812{
1813 u32 av = file_to_av(file);
1814
Eric Paris49b7b8d2010-07-23 11:44:09 -04001815 if (selinux_policycap_openperm)
1816 av |= FILE__OPEN;
1817
Eric Paris8b6a5a32008-10-29 17:06:46 -04001818 return av;
1819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821/* Hook functions begin here. */
1822
Ingo Molnar9e488582009-05-07 19:26:19 +10001823static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001824 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int rc;
1827
Ingo Molnar9e488582009-05-07 19:26:19 +10001828 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (rc)
1830 return rc;
1831
Eric Paris69f594a2012-01-03 12:25:15 -05001832 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001833 u32 sid = current_sid();
1834 u32 csid = task_sid(child);
1835 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001836 }
1837
David Howells3b11a1d2008-11-14 10:39:26 +11001838 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001839}
1840
1841static int selinux_ptrace_traceme(struct task_struct *parent)
1842{
1843 int rc;
1844
Eric Paris200ac532009-02-12 15:01:04 -05001845 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001846 if (rc)
1847 return rc;
1848
1849 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001853 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 int error;
1856
David Howells3b11a1d2008-11-14 10:39:26 +11001857 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (error)
1859 return error;
1860
Eric Paris200ac532009-02-12 15:01:04 -05001861 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
David Howellsd84f4f92008-11-14 10:39:23 +11001864static int selinux_capset(struct cred *new, const struct cred *old,
1865 const kernel_cap_t *effective,
1866 const kernel_cap_t *inheritable,
1867 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 int error;
1870
Eric Paris200ac532009-02-12 15:01:04 -05001871 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001872 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (error)
1874 return error;
1875
David Howellsd84f4f92008-11-14 10:39:23 +11001876 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
James Morris5626d3e2009-01-30 10:05:06 +11001879/*
1880 * (This comment used to live with the selinux_task_setuid hook,
1881 * which was removed).
1882 *
1883 * Since setuid only affects the current process, and since the SELinux
1884 * controls are not based on the Linux identity attributes, SELinux does not
1885 * need to control this operation. However, SELinux does control the use of
1886 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1887 */
1888
Eric Paris6a9de492012-01-03 12:25:14 -05001889static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1890 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 int rc;
1893
Eric Paris6a9de492012-01-03 12:25:14 -05001894 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (rc)
1896 return rc;
1897
Eric Paris6a9de492012-01-03 12:25:14 -05001898 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1902{
David Howells88e67f32008-11-14 10:39:21 +11001903 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 int rc = 0;
1905
1906 if (!sb)
1907 return 0;
1908
1909 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001910 case Q_SYNC:
1911 case Q_QUOTAON:
1912 case Q_QUOTAOFF:
1913 case Q_SETINFO:
1914 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001915 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001916 break;
1917 case Q_GETFMT:
1918 case Q_GETINFO:
1919 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001920 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001921 break;
1922 default:
1923 rc = 0; /* let the kernel handle invalid cmds */
1924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926 return rc;
1927}
1928
1929static int selinux_quota_on(struct dentry *dentry)
1930{
David Howells88e67f32008-11-14 10:39:21 +11001931 const struct cred *cred = current_cred();
1932
Eric Paris2875fa02011-04-28 16:04:24 -04001933 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Eric Paris12b30522010-11-15 18:36:29 -05001936static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 int rc;
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001941 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
1942 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001943 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
1944 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001945 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
1946 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
1947 /* Set level of messages printed to console */
1948 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04001949 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
1950 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001951 case SYSLOG_ACTION_CLOSE: /* Close log */
1952 case SYSLOG_ACTION_OPEN: /* Open log */
1953 case SYSLOG_ACTION_READ: /* Read from log */
1954 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
1955 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001956 default:
1957 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
1958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960 return rc;
1961}
1962
1963/*
1964 * Check that a process has enough memory to allocate a new virtual
1965 * mapping. 0 means there is enough memory for the allocation to
1966 * succeed and -ENOMEM implies there is not.
1967 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 * Do not audit the selinux permission check, as this is applied to all
1969 * processes that allocate mappings.
1970 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001971static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Eric Paris6a9de492012-01-03 12:25:14 -05001975 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00001976 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (rc == 0)
1978 cap_sys_admin = 1;
1979
Alan Cox34b4e4a2007-08-22 14:01:28 -07001980 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983/* binprm security operations */
1984
David Howellsa6f76f22008-11-14 10:39:24 +11001985static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
David Howellsa6f76f22008-11-14 10:39:24 +11001987 const struct task_security_struct *old_tsec;
1988 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001990 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05001991 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int rc;
1993
Eric Paris200ac532009-02-12 15:01:04 -05001994 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (rc)
1996 return rc;
1997
David Howellsa6f76f22008-11-14 10:39:24 +11001998 /* SELinux context only depends on initial program or script and not
1999 * the script interpreter */
2000 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return 0;
2002
David Howellsa6f76f22008-11-14 10:39:24 +11002003 old_tsec = current_security();
2004 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 isec = inode->i_security;
2006
2007 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002008 new_tsec->sid = old_tsec->sid;
2009 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Michael LeMay28eba5b2006-06-27 02:53:42 -07002011 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002012 new_tsec->create_sid = 0;
2013 new_tsec->keycreate_sid = 0;
2014 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
David Howellsa6f76f22008-11-14 10:39:24 +11002016 if (old_tsec->exec_sid) {
2017 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002019 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002020
2021 /*
2022 * Minimize confusion: if no_new_privs and a transition is
2023 * explicitly requested, then fail the exec.
2024 */
2025 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2026 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 } else {
2028 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002029 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002030 SECCLASS_PROCESS, NULL,
2031 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (rc)
2033 return rc;
2034 }
2035
Eric Paris50c205f2012-04-04 15:01:43 -04002036 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04002037 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002039 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2040 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002041 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
David Howellsa6f76f22008-11-14 10:39:24 +11002043 if (new_tsec->sid == old_tsec->sid) {
2044 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2046 if (rc)
2047 return rc;
2048 } else {
2049 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002050 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2052 if (rc)
2053 return rc;
2054
David Howellsa6f76f22008-11-14 10:39:24 +11002055 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2057 if (rc)
2058 return rc;
2059
David Howellsa6f76f22008-11-14 10:39:24 +11002060 /* Check for shared state */
2061 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2062 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2063 SECCLASS_PROCESS, PROCESS__SHARE,
2064 NULL);
2065 if (rc)
2066 return -EPERM;
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
David Howellsa6f76f22008-11-14 10:39:24 +11002069 /* Make sure that anyone attempting to ptrace over a task that
2070 * changes its SID has the appropriate permit */
2071 if (bprm->unsafe &
2072 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2073 struct task_struct *tracer;
2074 struct task_security_struct *sec;
2075 u32 ptsid = 0;
2076
2077 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002078 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002079 if (likely(tracer != NULL)) {
2080 sec = __task_cred(tracer)->security;
2081 ptsid = sec->sid;
2082 }
2083 rcu_read_unlock();
2084
2085 if (ptsid != 0) {
2086 rc = avc_has_perm(ptsid, new_tsec->sid,
2087 SECCLASS_PROCESS,
2088 PROCESS__PTRACE, NULL);
2089 if (rc)
2090 return -EPERM;
2091 }
2092 }
2093
2094 /* Clear any possibly unsafe personality bits on exec: */
2095 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return 0;
2099}
2100
Eric Paris828dfe12008-04-17 13:17:49 -04002101static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Paul Moore5fb49872010-04-22 14:46:19 -04002103 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002104 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int atsecure = 0;
2106
David Howells275bb412008-11-14 10:39:19 +11002107 sid = tsec->sid;
2108 osid = tsec->osid;
2109
2110 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /* Enable secure mode for SIDs transitions unless
2112 the noatsecure permission is granted between
2113 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002114 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002115 SECCLASS_PROCESS,
2116 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118
Eric Paris200ac532009-02-12 15:01:04 -05002119 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Al Viroc3c073f2012-08-21 22:32:06 -04002122static int match_file(const void *p, struct file *file, unsigned fd)
2123{
2124 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2125}
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002128static inline void flush_unauthorized_files(const struct cred *cred,
2129 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002132 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002133 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002134 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002136 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002138 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002139 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002140 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /* Revalidate access to controlling tty.
Eric Paris602a8dd2012-04-04 15:01:42 -04002143 Use path_has_perm on the tty path directly rather
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 than using file_has_perm, as this particular open
2145 file may belong to another process and we are only
2146 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002147 file_priv = list_first_entry(&tty->tty_files,
2148 struct tty_file_private, list);
2149 file = file_priv->file;
Eric Paris602a8dd2012-04-04 15:01:42 -04002150 if (path_has_perm(cred, &file->f_path, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002151 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002153 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002154 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002156 /* Reset controlling tty. */
2157 if (drop_tty)
2158 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002161 n = iterate_fd(files, 0, match_file, cred);
2162 if (!n) /* none found? */
2163 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Al Viroc3c073f2012-08-21 22:32:06 -04002165 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002166 if (IS_ERR(devnull))
2167 devnull = NULL;
2168 /* replace all the matching ones with this */
2169 do {
2170 replace_fd(n - 1, devnull, 0);
2171 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2172 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002173 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/*
David Howellsa6f76f22008-11-14 10:39:24 +11002177 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 */
David Howellsa6f76f22008-11-14 10:39:24 +11002179static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
David Howellsa6f76f22008-11-14 10:39:24 +11002181 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 int rc, i;
2184
David Howellsa6f76f22008-11-14 10:39:24 +11002185 new_tsec = bprm->cred->security;
2186 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return;
2188
2189 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002190 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
David Howellsa6f76f22008-11-14 10:39:24 +11002192 /* Always clear parent death signal on SID transitions. */
2193 current->pdeath_signal = 0;
2194
2195 /* Check whether the new SID can inherit resource limits from the old
2196 * SID. If not, reset all soft limits to the lower of the current
2197 * task's hard limit and the init task's soft limit.
2198 *
2199 * Note that the setting of hard limits (even to lower them) can be
2200 * controlled by the setrlimit check. The inclusion of the init task's
2201 * soft limit into the computation is to avoid resetting soft limits
2202 * higher than the default soft limit for cases where the default is
2203 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2204 */
2205 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2206 PROCESS__RLIMITINH, NULL);
2207 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002208 /* protect against do_prlimit() */
2209 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002210 for (i = 0; i < RLIM_NLIMITS; i++) {
2211 rlim = current->signal->rlim + i;
2212 initrlim = init_task.signal->rlim + i;
2213 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2214 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002215 task_unlock(current);
2216 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002217 }
2218}
2219
2220/*
2221 * Clean up the process immediately after the installation of new credentials
2222 * due to exec
2223 */
2224static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2225{
2226 const struct task_security_struct *tsec = current_security();
2227 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002228 u32 osid, sid;
2229 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002230
David Howellsa6f76f22008-11-14 10:39:24 +11002231 osid = tsec->osid;
2232 sid = tsec->sid;
2233
2234 if (sid == osid)
2235 return;
2236
2237 /* Check whether the new SID can inherit signal state from the old SID.
2238 * If not, clear itimers to avoid subsequent signal generation and
2239 * flush and unblock signals.
2240 *
2241 * This must occur _after_ the task SID has been updated so that any
2242 * kill done after the flush will be checked against the new SID.
2243 */
2244 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (rc) {
2246 memset(&itimer, 0, sizeof itimer);
2247 for (i = 0; i < 3; i++)
2248 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002250 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2251 __flush_signals(current);
2252 flush_signal_handlers(current, 1);
2253 sigemptyset(&current->blocked);
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 spin_unlock_irq(&current->sighand->siglock);
2256 }
2257
David Howellsa6f76f22008-11-14 10:39:24 +11002258 /* Wake up the parent if it is waiting so that it can recheck
2259 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002260 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002261 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002262 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265/* superblock security operations */
2266
2267static int selinux_sb_alloc_security(struct super_block *sb)
2268{
2269 return superblock_alloc_security(sb);
2270}
2271
2272static void selinux_sb_free_security(struct super_block *sb)
2273{
2274 superblock_free_security(sb);
2275}
2276
2277static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2278{
2279 if (plen > olen)
2280 return 0;
2281
2282 return !memcmp(prefix, option, plen);
2283}
2284
2285static inline int selinux_option(char *option, int len)
2286{
Eric Paris832cbd92008-04-01 13:24:09 -04002287 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2288 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2289 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002290 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2291 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294static inline void take_option(char **to, char *from, int *first, int len)
2295{
2296 if (!*first) {
2297 **to = ',';
2298 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002299 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 *first = 0;
2301 memcpy(*to, from, len);
2302 *to += len;
2303}
2304
Eric Paris828dfe12008-04-17 13:17:49 -04002305static inline void take_selinux_option(char **to, char *from, int *first,
2306 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002307{
2308 int current_size = 0;
2309
2310 if (!*first) {
2311 **to = '|';
2312 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002313 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002314 *first = 0;
2315
2316 while (current_size < len) {
2317 if (*from != '"') {
2318 **to = *from;
2319 *to += 1;
2320 }
2321 from += 1;
2322 current_size += 1;
2323 }
2324}
2325
Eric Parise0007522008-03-05 10:31:54 -05002326static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 int fnosec, fsec, rc = 0;
2329 char *in_save, *in_curr, *in_end;
2330 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002331 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 in_curr = orig;
2334 sec_curr = copy;
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2337 if (!nosec) {
2338 rc = -ENOMEM;
2339 goto out;
2340 }
2341
2342 nosec_save = nosec;
2343 fnosec = fsec = 1;
2344 in_save = in_end = orig;
2345
2346 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002347 if (*in_end == '"')
2348 open_quote = !open_quote;
2349 if ((*in_end == ',' && open_quote == 0) ||
2350 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int len = in_end - in_curr;
2352
2353 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002354 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 else
2356 take_option(&nosec, in_curr, &fnosec, len);
2357
2358 in_curr = in_end + 1;
2359 }
2360 } while (*in_end++);
2361
Eric Paris6931dfc2005-06-30 02:58:51 -07002362 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002363 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364out:
2365 return rc;
2366}
2367
Eric Paris026eb162011-03-03 16:09:14 -05002368static int selinux_sb_remount(struct super_block *sb, void *data)
2369{
2370 int rc, i, *flags;
2371 struct security_mnt_opts opts;
2372 char *secdata, **mount_options;
2373 struct superblock_security_struct *sbsec = sb->s_security;
2374
2375 if (!(sbsec->flags & SE_SBINITIALIZED))
2376 return 0;
2377
2378 if (!data)
2379 return 0;
2380
2381 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2382 return 0;
2383
2384 security_init_mnt_opts(&opts);
2385 secdata = alloc_secdata();
2386 if (!secdata)
2387 return -ENOMEM;
2388 rc = selinux_sb_copy_data(data, secdata);
2389 if (rc)
2390 goto out_free_secdata;
2391
2392 rc = selinux_parse_opts_str(secdata, &opts);
2393 if (rc)
2394 goto out_free_secdata;
2395
2396 mount_options = opts.mnt_opts;
2397 flags = opts.mnt_opts_flags;
2398
2399 for (i = 0; i < opts.num_mnt_opts; i++) {
2400 u32 sid;
2401 size_t len;
2402
2403 if (flags[i] == SE_SBLABELSUPP)
2404 continue;
2405 len = strlen(mount_options[i]);
2406 rc = security_context_to_sid(mount_options[i], len, &sid);
2407 if (rc) {
2408 printk(KERN_WARNING "SELinux: security_context_to_sid"
2409 "(%s) failed for (dev %s, type %s) errno=%d\n",
2410 mount_options[i], sb->s_id, sb->s_type->name, rc);
2411 goto out_free_opts;
2412 }
2413 rc = -EINVAL;
2414 switch (flags[i]) {
2415 case FSCONTEXT_MNT:
2416 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2417 goto out_bad_option;
2418 break;
2419 case CONTEXT_MNT:
2420 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2421 goto out_bad_option;
2422 break;
2423 case ROOTCONTEXT_MNT: {
2424 struct inode_security_struct *root_isec;
2425 root_isec = sb->s_root->d_inode->i_security;
2426
2427 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2428 goto out_bad_option;
2429 break;
2430 }
2431 case DEFCONTEXT_MNT:
2432 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2433 goto out_bad_option;
2434 break;
2435 default:
2436 goto out_free_opts;
2437 }
2438 }
2439
2440 rc = 0;
2441out_free_opts:
2442 security_free_mnt_opts(&opts);
2443out_free_secdata:
2444 free_secdata(secdata);
2445 return rc;
2446out_bad_option:
2447 printk(KERN_WARNING "SELinux: unable to change security options "
2448 "during remount (dev %s, type=%s)\n", sb->s_id,
2449 sb->s_type->name);
2450 goto out_free_opts;
2451}
2452
James Morris12204e22008-12-19 10:44:42 +11002453static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
David Howells88e67f32008-11-14 10:39:21 +11002455 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002456 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 int rc;
2458
2459 rc = superblock_doinit(sb, data);
2460 if (rc)
2461 return rc;
2462
James Morris74192242008-12-19 11:41:10 +11002463 /* Allow all mounts performed by the kernel */
2464 if (flags & MS_KERNMOUNT)
2465 return 0;
2466
Eric Paris50c205f2012-04-04 15:01:43 -04002467 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002468 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002469 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
David Howells726c3342006-06-23 02:02:58 -07002472static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
David Howells88e67f32008-11-14 10:39:21 +11002474 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002475 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Eric Paris50c205f2012-04-04 15:01:43 -04002477 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002478 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002479 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Al Viro808d4e32012-10-11 11:42:01 -04002482static int selinux_mount(const char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002483 struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002484 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002485 unsigned long flags,
2486 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
David Howells88e67f32008-11-14 10:39:21 +11002488 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002491 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002492 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 else
Eric Paris2875fa02011-04-28 16:04:24 -04002494 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497static int selinux_umount(struct vfsmount *mnt, int flags)
2498{
David Howells88e67f32008-11-14 10:39:21 +11002499 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
David Howells88e67f32008-11-14 10:39:21 +11002501 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002502 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505/* inode security operations */
2506
2507static int selinux_inode_alloc_security(struct inode *inode)
2508{
2509 return inode_alloc_security(inode);
2510}
2511
2512static void selinux_inode_free_security(struct inode *inode)
2513{
2514 inode_free_security(inode);
2515}
2516
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002517static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002518 const struct qstr *qstr, char **name,
2519 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002520{
Paul Moore5fb49872010-04-22 14:46:19 -04002521 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002522 struct inode_security_struct *dsec;
2523 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002524 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002525 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002526 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002527
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002528 dsec = dir->i_security;
2529 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002530
David Howells275bb412008-11-14 10:39:19 +11002531 sid = tsec->sid;
2532 newsid = tsec->create_sid;
2533
Eric Paris415103f2010-12-02 16:13:40 -05002534 if ((sbsec->flags & SE_SBINITIALIZED) &&
2535 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2536 newsid = sbsec->mntpoint_sid;
2537 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002538 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002539 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002540 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002541 if (rc) {
2542 printk(KERN_WARNING "%s: "
2543 "security_transition_sid failed, rc=%d (dev=%s "
2544 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002545 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002546 -rc, inode->i_sb->s_id, inode->i_ino);
2547 return rc;
2548 }
2549 }
2550
Eric Paris296fddf2006-09-25 23:32:00 -07002551 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002552 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002553 struct inode_security_struct *isec = inode->i_security;
2554 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2555 isec->sid = newsid;
2556 isec->initialized = 1;
2557 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002558
David P. Quigleycd895962009-01-16 09:22:04 -05002559 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002560 return -EOPNOTSUPP;
2561
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002562 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002563 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002564 if (!namep)
2565 return -ENOMEM;
2566 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002567 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002568
2569 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002570 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002571 if (rc) {
2572 kfree(namep);
2573 return rc;
2574 }
2575 *value = context;
2576 *len = clen;
2577 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002578
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002579 return 0;
2580}
2581
Al Viro4acdaf22011-07-26 01:42:34 -04002582static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
2584 return may_create(dir, dentry, SECCLASS_FILE);
2585}
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return may_link(dir, old_dentry, MAY_LINK);
2590}
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return may_link(dir, dentry, MAY_UNLINK);
2595}
2596
2597static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2598{
2599 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2600}
2601
Al Viro18bb1db2011-07-26 01:41:39 -04002602static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 return may_create(dir, dentry, SECCLASS_DIR);
2605}
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2608{
2609 return may_link(dir, dentry, MAY_RMDIR);
2610}
2611
Al Viro1a67aaf2011-07-26 01:52:52 -04002612static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002618 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2621}
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623static int selinux_inode_readlink(struct dentry *dentry)
2624{
David Howells88e67f32008-11-14 10:39:21 +11002625 const struct cred *cred = current_cred();
2626
Eric Paris2875fa02011-04-28 16:04:24 -04002627 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
2630static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2631{
David Howells88e67f32008-11-14 10:39:21 +11002632 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Eric Paris2875fa02011-04-28 16:04:24 -04002634 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Eric Parisd4cf970d2012-04-04 15:01:42 -04002637static noinline int audit_inode_permission(struct inode *inode,
2638 u32 perms, u32 audited, u32 denied,
2639 unsigned flags)
2640{
2641 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002642 struct inode_security_struct *isec = inode->i_security;
2643 int rc;
2644
Eric Paris50c205f2012-04-04 15:01:43 -04002645 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002646 ad.u.inode = inode;
2647
2648 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2649 audited, denied, &ad, flags);
2650 if (rc)
2651 return rc;
2652 return 0;
2653}
2654
Al Viroe74f71e2011-06-20 19:38:15 -04002655static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
David Howells88e67f32008-11-14 10:39:21 +11002657 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002658 u32 perms;
2659 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002660 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002661 struct inode_security_struct *isec;
2662 u32 sid;
2663 struct av_decision avd;
2664 int rc, rc2;
2665 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Eric Parisb782e0a2010-07-23 11:44:03 -04002667 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002668 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2669
Eric Parisb782e0a2010-07-23 11:44:03 -04002670 /* No permission to check. Existence test. */
2671 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Eric Paris2e334052012-04-04 15:01:42 -04002674 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002675
Eric Paris2e334052012-04-04 15:01:42 -04002676 if (unlikely(IS_PRIVATE(inode)))
2677 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002678
2679 perms = file_mask_to_av(inode->i_mode, mask);
2680
Eric Paris2e334052012-04-04 15:01:42 -04002681 sid = cred_sid(cred);
2682 isec = inode->i_security;
2683
2684 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2685 audited = avc_audit_required(perms, &avd, rc,
2686 from_access ? FILE__AUDIT_ACCESS : 0,
2687 &denied);
2688 if (likely(!audited))
2689 return rc;
2690
Eric Parisd4cf970d2012-04-04 15:01:42 -04002691 rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002692 if (rc2)
2693 return rc2;
2694 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2698{
David Howells88e67f32008-11-14 10:39:21 +11002699 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002700 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04002701 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002703 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2704 if (ia_valid & ATTR_FORCE) {
2705 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2706 ATTR_FORCE);
2707 if (!ia_valid)
2708 return 0;
2709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002711 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2712 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002713 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Eric Paris3d2195c2012-07-06 14:13:30 -04002715 if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
Eric Paris95dbf732012-04-04 13:45:34 -04002716 av |= FILE__OPEN;
2717
2718 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
2721static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2722{
David Howells88e67f32008-11-14 10:39:21 +11002723 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002724 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002725
Eric Paris2875fa02011-04-28 16:04:24 -04002726 path.dentry = dentry;
2727 path.mnt = mnt;
2728
2729 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
David Howells8f0cfa52008-04-29 00:59:41 -07002732static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002733{
David Howells88e67f32008-11-14 10:39:21 +11002734 const struct cred *cred = current_cred();
2735
Serge E. Hallynb5376772007-10-16 23:31:36 -07002736 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2737 sizeof XATTR_SECURITY_PREFIX - 1)) {
2738 if (!strcmp(name, XATTR_NAME_CAPS)) {
2739 if (!capable(CAP_SETFCAP))
2740 return -EPERM;
2741 } else if (!capable(CAP_SYS_ADMIN)) {
2742 /* A different attribute in the security namespace.
2743 Restrict to administrator. */
2744 return -EPERM;
2745 }
2746 }
2747
2748 /* Not an attribute we recognize, so just check the
2749 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002750 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002751}
2752
David Howells8f0cfa52008-04-29 00:59:41 -07002753static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2754 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct inode *inode = dentry->d_inode;
2757 struct inode_security_struct *isec = inode->i_security;
2758 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002759 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002760 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 int rc = 0;
2762
Serge E. Hallynb5376772007-10-16 23:31:36 -07002763 if (strcmp(name, XATTR_NAME_SELINUX))
2764 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002767 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return -EOPNOTSUPP;
2769
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002770 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return -EPERM;
2772
Eric Paris50c205f2012-04-04 15:01:43 -04002773 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002774 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
David Howells275bb412008-11-14 10:39:19 +11002776 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 FILE__RELABELFROM, &ad);
2778 if (rc)
2779 return rc;
2780
2781 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002782 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04002783 if (!capable(CAP_MAC_ADMIN)) {
2784 struct audit_buffer *ab;
2785 size_t audit_size;
2786 const char *str;
2787
2788 /* We strip a nul only if it is at the end, otherwise the
2789 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01002790 if (value) {
2791 str = value;
2792 if (str[size - 1] == '\0')
2793 audit_size = size - 1;
2794 else
2795 audit_size = size;
2796 } else {
2797 str = "";
2798 audit_size = 0;
2799 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04002800 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2801 audit_log_format(ab, "op=setxattr invalid_context=");
2802 audit_log_n_untrustedstring(ab, value, audit_size);
2803 audit_log_end(ab);
2804
Stephen Smalley12b29f32008-05-07 13:03:20 -04002805 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04002806 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04002807 rc = security_context_to_sid_force(value, size, &newsid);
2808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (rc)
2810 return rc;
2811
David Howells275bb412008-11-14 10:39:19 +11002812 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 FILE__RELABELTO, &ad);
2814 if (rc)
2815 return rc;
2816
David Howells275bb412008-11-14 10:39:19 +11002817 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002818 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (rc)
2820 return rc;
2821
2822 return avc_has_perm(newsid,
2823 sbsec->sid,
2824 SECCLASS_FILESYSTEM,
2825 FILESYSTEM__ASSOCIATE,
2826 &ad);
2827}
2828
David Howells8f0cfa52008-04-29 00:59:41 -07002829static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002830 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002831 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct inode *inode = dentry->d_inode;
2834 struct inode_security_struct *isec = inode->i_security;
2835 u32 newsid;
2836 int rc;
2837
2838 if (strcmp(name, XATTR_NAME_SELINUX)) {
2839 /* Not an attribute we recognize, so nothing to do. */
2840 return;
2841 }
2842
Stephen Smalley12b29f32008-05-07 13:03:20 -04002843 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002845 printk(KERN_ERR "SELinux: unable to map context to SID"
2846 "for (%s, %lu), rc=%d\n",
2847 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return;
2849 }
2850
2851 isec->sid = newsid;
2852 return;
2853}
2854
David Howells8f0cfa52008-04-29 00:59:41 -07002855static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
David Howells88e67f32008-11-14 10:39:21 +11002857 const struct cred *cred = current_cred();
2858
Eric Paris2875fa02011-04-28 16:04:24 -04002859 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Eric Paris828dfe12008-04-17 13:17:49 -04002862static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
David Howells88e67f32008-11-14 10:39:21 +11002864 const struct cred *cred = current_cred();
2865
Eric Paris2875fa02011-04-28 16:04:24 -04002866 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867}
2868
David Howells8f0cfa52008-04-29 00:59:41 -07002869static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002871 if (strcmp(name, XATTR_NAME_SELINUX))
2872 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 /* No one is allowed to remove a SELinux security label.
2875 You can change the label, but all data must be labeled. */
2876 return -EACCES;
2877}
2878
James Morrisd381d8a2005-10-30 14:59:22 -08002879/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002880 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002881 *
2882 * Permission check is handled by selinux_inode_getxattr hook.
2883 */
David P. Quigley42492592008-02-04 22:29:39 -08002884static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
David P. Quigley42492592008-02-04 22:29:39 -08002886 u32 size;
2887 int error;
2888 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002891 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2892 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002894 /*
2895 * If the caller has CAP_MAC_ADMIN, then get the raw context
2896 * value even if it is not defined by current policy; otherwise,
2897 * use the in-core value under current policy.
2898 * Use the non-auditing forms of the permission checks since
2899 * getxattr may be called by unprivileged processes commonly
2900 * and lack of permission just means that we fall back to the
2901 * in-core context value, not a denial.
2902 */
Eric Paris6a9de492012-01-03 12:25:14 -05002903 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002904 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002905 if (!error)
2906 error = security_sid_to_context_force(isec->sid, &context,
2907 &size);
2908 else
2909 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002910 if (error)
2911 return error;
2912 error = size;
2913 if (alloc) {
2914 *buffer = context;
2915 goto out_nofree;
2916 }
2917 kfree(context);
2918out_nofree:
2919 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04002923 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 struct inode_security_struct *isec = inode->i_security;
2926 u32 newsid;
2927 int rc;
2928
2929 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2930 return -EOPNOTSUPP;
2931
2932 if (!value || !size)
2933 return -EACCES;
2934
Eric Paris828dfe12008-04-17 13:17:49 -04002935 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (rc)
2937 return rc;
2938
2939 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002940 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return 0;
2942}
2943
2944static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2945{
2946 const int len = sizeof(XATTR_NAME_SELINUX);
2947 if (buffer && len <= buffer_size)
2948 memcpy(buffer, XATTR_NAME_SELINUX, len);
2949 return len;
2950}
2951
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02002952static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2953{
2954 struct inode_security_struct *isec = inode->i_security;
2955 *secid = isec->sid;
2956}
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958/* file security operations */
2959
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002960static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
David Howells88e67f32008-11-14 10:39:21 +11002962 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05002963 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2966 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
2967 mask |= MAY_APPEND;
2968
Paul Moore389fb8002009-03-27 17:10:34 -04002969 return file_has_perm(cred, file,
2970 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002973static int selinux_file_permission(struct file *file, int mask)
2974{
Al Viro496ad9a2013-01-23 17:07:38 -05002975 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04002976 struct file_security_struct *fsec = file->f_security;
2977 struct inode_security_struct *isec = inode->i_security;
2978 u32 sid = current_sid();
2979
Paul Moore389fb8002009-03-27 17:10:34 -04002980 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002981 /* No permission to check. Existence test. */
2982 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002983
Stephen Smalley20dda182009-06-22 14:54:53 -04002984 if (sid == fsec->sid && fsec->isid == isec->sid &&
2985 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04002986 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04002987 return 0;
2988
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002989 return selinux_revalidate_file_permission(file, mask);
2990}
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992static int selinux_file_alloc_security(struct file *file)
2993{
2994 return file_alloc_security(file);
2995}
2996
2997static void selinux_file_free_security(struct file *file)
2998{
2999 file_free_security(file);
3000}
3001
3002static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3003 unsigned long arg)
3004{
David Howells88e67f32008-11-14 10:39:21 +11003005 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003006 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Eric Paris0b24dcb2011-02-25 15:39:20 -05003008 switch (cmd) {
3009 case FIONREAD:
3010 /* fall through */
3011 case FIBMAP:
3012 /* fall through */
3013 case FIGETBSZ:
3014 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003015 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003016 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003017 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003018 error = file_has_perm(cred, file, FILE__GETATTR);
3019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Al Viro2f99c362012-03-23 16:04:05 -04003021 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003022 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003023 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003024 error = file_has_perm(cred, file, FILE__SETATTR);
3025 break;
3026
3027 /* sys_ioctl() checks */
3028 case FIONBIO:
3029 /* fall through */
3030 case FIOASYNC:
3031 error = file_has_perm(cred, file, 0);
3032 break;
3033
3034 case KDSKBENT:
3035 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003036 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3037 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003038 break;
3039
3040 /* default case assumes that the command will go
3041 * to the file's ioctl() function.
3042 */
3043 default:
3044 error = file_has_perm(cred, file, FILE__IOCTL);
3045 }
3046 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003049static int default_noexec;
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3052{
David Howells88e67f32008-11-14 10:39:21 +11003053 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003054 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003055
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003056 if (default_noexec &&
3057 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 /*
3059 * We are making executable an anonymous mapping or a
3060 * private file mapping that will also be writable.
3061 * This has an additional check.
3062 */
David Howellsd84f4f92008-11-14 10:39:23 +11003063 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003065 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 if (file) {
3069 /* read access is always possible with a mapping */
3070 u32 av = FILE__READ;
3071
3072 /* write access only matters if the mapping is shared */
3073 if (shared && (prot & PROT_WRITE))
3074 av |= FILE__WRITE;
3075
3076 if (prot & PROT_EXEC)
3077 av |= FILE__EXECUTE;
3078
David Howells88e67f32008-11-14 10:39:21 +11003079 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
David Howellsd84f4f92008-11-14 10:39:23 +11003081
3082error:
3083 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Al Viroe5467852012-05-30 13:30:51 -04003086static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Eric Parised032182007-06-28 15:55:21 -04003088 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003089 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Eric Paris84336d1a2009-07-31 12:54:05 -04003091 /*
3092 * notice that we are intentionally putting the SELinux check before
3093 * the secondary cap_file_mmap check. This is such a likely attempt
3094 * at bad behaviour/exploit that we always want to get the AVC, even
3095 * if DAC would have also denied the operation.
3096 */
Eric Parisa2551df2009-07-31 12:54:11 -04003097 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003098 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3099 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003100 if (rc)
3101 return rc;
3102 }
3103
3104 /* do DAC check on address space usage */
Al Viroe5467852012-05-30 13:30:51 -04003105 return cap_mmap_addr(addr);
3106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Al Viroe5467852012-05-30 13:30:51 -04003108static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3109 unsigned long prot, unsigned long flags)
3110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (selinux_checkreqprot)
3112 prot = reqprot;
3113
3114 return file_map_prot_check(file, prot,
3115 (flags & MAP_TYPE) == MAP_SHARED);
3116}
3117
3118static int selinux_file_mprotect(struct vm_area_struct *vma,
3119 unsigned long reqprot,
3120 unsigned long prot)
3121{
David Howells88e67f32008-11-14 10:39:21 +11003122 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 if (selinux_checkreqprot)
3125 prot = reqprot;
3126
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003127 if (default_noexec &&
3128 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003129 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003130 if (vma->vm_start >= vma->vm_mm->start_brk &&
3131 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003132 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003133 } else if (!vma->vm_file &&
3134 vma->vm_start <= vma->vm_mm->start_stack &&
3135 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003136 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003137 } else if (vma->vm_file && vma->anon_vma) {
3138 /*
3139 * We are making executable a file mapping that has
3140 * had some COW done. Since pages might have been
3141 * written, check ability to execute the possibly
3142 * modified content. This typically should only
3143 * occur for text relocations.
3144 */
David Howellsd84f4f92008-11-14 10:39:23 +11003145 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003146 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003147 if (rc)
3148 return rc;
3149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3152}
3153
3154static int selinux_file_lock(struct file *file, unsigned int cmd)
3155{
David Howells88e67f32008-11-14 10:39:21 +11003156 const struct cred *cred = current_cred();
3157
3158 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3162 unsigned long arg)
3163{
David Howells88e67f32008-11-14 10:39:21 +11003164 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 int err = 0;
3166
3167 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003168 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003169 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003170 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003172 }
3173 /* fall through */
3174 case F_SETOWN:
3175 case F_SETSIG:
3176 case F_GETFL:
3177 case F_GETOWN:
3178 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003179 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003180 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003181 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003182 break;
3183 case F_GETLK:
3184 case F_SETLK:
3185 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003187 case F_GETLK64:
3188 case F_SETLK64:
3189 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190#endif
David Howells88e67f32008-11-14 10:39:21 +11003191 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194
3195 return err;
3196}
3197
3198static int selinux_file_set_fowner(struct file *file)
3199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 struct file_security_struct *fsec;
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003203 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 return 0;
3206}
3207
3208static int selinux_file_send_sigiotask(struct task_struct *tsk,
3209 struct fown_struct *fown, int signum)
3210{
Eric Paris828dfe12008-04-17 13:17:49 -04003211 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003212 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct file_security_struct *fsec;
3215
3216 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003217 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 fsec = file->f_security;
3220
3221 if (!signum)
3222 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3223 else
3224 perm = signal_to_av(signum);
3225
David Howells275bb412008-11-14 10:39:19 +11003226 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 SECCLASS_PROCESS, perm, NULL);
3228}
3229
3230static int selinux_file_receive(struct file *file)
3231{
David Howells88e67f32008-11-14 10:39:21 +11003232 const struct cred *cred = current_cred();
3233
3234 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Eric Paris83d49852012-04-04 13:45:40 -04003237static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003238{
3239 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003240 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003241
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003242 fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05003243 isec = file_inode(file)->i_security;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003244 /*
3245 * Save inode label and policy sequence number
3246 * at open-time so that selinux_file_permission
3247 * can determine whether revalidation is necessary.
3248 * Task label is already saved in the file security
3249 * struct as its SID.
3250 */
3251 fsec->isid = isec->sid;
3252 fsec->pseqno = avc_policy_seqno();
3253 /*
3254 * Since the inode label or policy seqno may have changed
3255 * between the selinux_inode_permission check and the saving
3256 * of state above, recheck that access is still permitted.
3257 * Otherwise, access might never be revalidated against the
3258 * new inode label or new policy.
3259 * This check is not redundant - do not remove.
3260 */
Eric Paris602a8dd2012-04-04 15:01:42 -04003261 return path_has_perm(cred, &file->f_path, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003262}
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264/* task security operations */
3265
3266static int selinux_task_create(unsigned long clone_flags)
3267{
David Howells3b11a1d2008-11-14 10:39:26 +11003268 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
David Howellsf1752ee2008-11-14 10:39:17 +11003271/*
David Howellsee18d642009-09-02 09:14:21 +01003272 * allocate the SELinux part of blank credentials
3273 */
3274static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3275{
3276 struct task_security_struct *tsec;
3277
3278 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3279 if (!tsec)
3280 return -ENOMEM;
3281
3282 cred->security = tsec;
3283 return 0;
3284}
3285
3286/*
David Howellsf1752ee2008-11-14 10:39:17 +11003287 * detach and free the LSM part of a set of credentials
3288 */
3289static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
David Howellsf1752ee2008-11-14 10:39:17 +11003291 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003292
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003293 /*
3294 * cred->security == NULL if security_cred_alloc_blank() or
3295 * security_prepare_creds() returned an error.
3296 */
3297 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003298 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003299 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
David Howellsd84f4f92008-11-14 10:39:23 +11003302/*
3303 * prepare a new set of credentials for modification
3304 */
3305static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3306 gfp_t gfp)
3307{
3308 const struct task_security_struct *old_tsec;
3309 struct task_security_struct *tsec;
3310
3311 old_tsec = old->security;
3312
3313 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3314 if (!tsec)
3315 return -ENOMEM;
3316
3317 new->security = tsec;
3318 return 0;
3319}
3320
3321/*
David Howellsee18d642009-09-02 09:14:21 +01003322 * transfer the SELinux data to a blank set of creds
3323 */
3324static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3325{
3326 const struct task_security_struct *old_tsec = old->security;
3327 struct task_security_struct *tsec = new->security;
3328
3329 *tsec = *old_tsec;
3330}
3331
3332/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003333 * set the security data for a kernel service
3334 * - all the creation contexts are set to unlabelled
3335 */
3336static int selinux_kernel_act_as(struct cred *new, u32 secid)
3337{
3338 struct task_security_struct *tsec = new->security;
3339 u32 sid = current_sid();
3340 int ret;
3341
3342 ret = avc_has_perm(sid, secid,
3343 SECCLASS_KERNEL_SERVICE,
3344 KERNEL_SERVICE__USE_AS_OVERRIDE,
3345 NULL);
3346 if (ret == 0) {
3347 tsec->sid = secid;
3348 tsec->create_sid = 0;
3349 tsec->keycreate_sid = 0;
3350 tsec->sockcreate_sid = 0;
3351 }
3352 return ret;
3353}
3354
3355/*
3356 * set the file creation context in a security record to the same as the
3357 * objective context of the specified inode
3358 */
3359static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3360{
3361 struct inode_security_struct *isec = inode->i_security;
3362 struct task_security_struct *tsec = new->security;
3363 u32 sid = current_sid();
3364 int ret;
3365
3366 ret = avc_has_perm(sid, isec->sid,
3367 SECCLASS_KERNEL_SERVICE,
3368 KERNEL_SERVICE__CREATE_FILES_AS,
3369 NULL);
3370
3371 if (ret == 0)
3372 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003373 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003374}
3375
Eric Parisdd8dbf22009-11-03 16:35:32 +11003376static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003377{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003378 u32 sid;
3379 struct common_audit_data ad;
3380
3381 sid = task_sid(current);
3382
Eric Paris50c205f2012-04-04 15:01:43 -04003383 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003384 ad.u.kmod_name = kmod_name;
3385
3386 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3387 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003388}
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3391{
David Howells3b11a1d2008-11-14 10:39:26 +11003392 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
3395static int selinux_task_getpgid(struct task_struct *p)
3396{
David Howells3b11a1d2008-11-14 10:39:26 +11003397 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
3400static int selinux_task_getsid(struct task_struct *p)
3401{
David Howells3b11a1d2008-11-14 10:39:26 +11003402 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
David Quigleyf9008e42006-06-30 01:55:46 -07003405static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3406{
David Howells275bb412008-11-14 10:39:19 +11003407 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003408}
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410static int selinux_task_setnice(struct task_struct *p, int nice)
3411{
3412 int rc;
3413
Eric Paris200ac532009-02-12 15:01:04 -05003414 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 if (rc)
3416 return rc;
3417
David Howells3b11a1d2008-11-14 10:39:26 +11003418 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
James Morris03e68062006-06-23 02:03:58 -07003421static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3422{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003423 int rc;
3424
Eric Paris200ac532009-02-12 15:01:04 -05003425 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003426 if (rc)
3427 return rc;
3428
David Howells3b11a1d2008-11-14 10:39:26 +11003429 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003430}
3431
David Quigleya1836a42006-06-30 01:55:49 -07003432static int selinux_task_getioprio(struct task_struct *p)
3433{
David Howells3b11a1d2008-11-14 10:39:26 +11003434 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003435}
3436
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003437static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3438 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003440 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 /* Control the ability to change the hard limit (whether
3443 lowering or raising it), so that the hard limit can
3444 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003445 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003447 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 return 0;
3450}
3451
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003452static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003454 int rc;
3455
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003456 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003457 if (rc)
3458 return rc;
3459
David Howells3b11a1d2008-11-14 10:39:26 +11003460 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
3463static int selinux_task_getscheduler(struct task_struct *p)
3464{
David Howells3b11a1d2008-11-14 10:39:26 +11003465 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466}
3467
David Quigley35601542006-06-23 02:04:01 -07003468static int selinux_task_movememory(struct task_struct *p)
3469{
David Howells3b11a1d2008-11-14 10:39:26 +11003470 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003471}
3472
David Quigleyf9008e42006-06-30 01:55:46 -07003473static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3474 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
3476 u32 perm;
3477 int rc;
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (!sig)
3480 perm = PROCESS__SIGNULL; /* null signal; existence test */
3481 else
3482 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003483 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003484 rc = avc_has_perm(secid, task_sid(p),
3485 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003486 else
David Howells3b11a1d2008-11-14 10:39:26 +11003487 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003488 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491static int selinux_task_wait(struct task_struct *p)
3492{
Eric Paris8a535142007-10-22 16:10:31 -04003493 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496static void selinux_task_to_inode(struct task_struct *p,
3497 struct inode *inode)
3498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003500 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
David Howells275bb412008-11-14 10:39:19 +11003502 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003507static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003508 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
3510 int offset, ihlen, ret = -EINVAL;
3511 struct iphdr _iph, *ih;
3512
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003513 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3515 if (ih == NULL)
3516 goto out;
3517
3518 ihlen = ih->ihl * 4;
3519 if (ihlen < sizeof(_iph))
3520 goto out;
3521
Eric Paris48c62af2012-04-02 13:15:44 -04003522 ad->u.net->v4info.saddr = ih->saddr;
3523 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 ret = 0;
3525
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003526 if (proto)
3527 *proto = ih->protocol;
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003530 case IPPROTO_TCP: {
3531 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Eric Paris828dfe12008-04-17 13:17:49 -04003533 if (ntohs(ih->frag_off) & IP_OFFSET)
3534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 offset += ihlen;
3537 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3538 if (th == NULL)
3539 break;
3540
Eric Paris48c62af2012-04-02 13:15:44 -04003541 ad->u.net->sport = th->source;
3542 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Eric Paris828dfe12008-04-17 13:17:49 -04003546 case IPPROTO_UDP: {
3547 struct udphdr _udph, *uh;
3548
3549 if (ntohs(ih->frag_off) & IP_OFFSET)
3550 break;
3551
3552 offset += ihlen;
3553 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3554 if (uh == NULL)
3555 break;
3556
Eric Paris48c62af2012-04-02 13:15:44 -04003557 ad->u.net->sport = uh->source;
3558 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003559 break;
3560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
James Morris2ee92d42006-11-13 16:09:01 -08003562 case IPPROTO_DCCP: {
3563 struct dccp_hdr _dccph, *dh;
3564
3565 if (ntohs(ih->frag_off) & IP_OFFSET)
3566 break;
3567
3568 offset += ihlen;
3569 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3570 if (dh == NULL)
3571 break;
3572
Eric Paris48c62af2012-04-02 13:15:44 -04003573 ad->u.net->sport = dh->dccph_sport;
3574 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003575 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003576 }
James Morris2ee92d42006-11-13 16:09:01 -08003577
Eric Paris828dfe12008-04-17 13:17:49 -04003578 default:
3579 break;
3580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581out:
3582 return ret;
3583}
3584
3585#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3586
3587/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003588static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003589 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
3591 u8 nexthdr;
3592 int ret = -EINVAL, offset;
3593 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003594 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003596 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3598 if (ip6 == NULL)
3599 goto out;
3600
Eric Paris48c62af2012-04-02 13:15:44 -04003601 ad->u.net->v6info.saddr = ip6->saddr;
3602 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 ret = 0;
3604
3605 nexthdr = ip6->nexthdr;
3606 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003607 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 if (offset < 0)
3609 goto out;
3610
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003611 if (proto)
3612 *proto = nexthdr;
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 switch (nexthdr) {
3615 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003616 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3619 if (th == NULL)
3620 break;
3621
Eric Paris48c62af2012-04-02 13:15:44 -04003622 ad->u.net->sport = th->source;
3623 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 break;
3625 }
3626
3627 case IPPROTO_UDP: {
3628 struct udphdr _udph, *uh;
3629
3630 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3631 if (uh == NULL)
3632 break;
3633
Eric Paris48c62af2012-04-02 13:15:44 -04003634 ad->u.net->sport = uh->source;
3635 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 break;
3637 }
3638
James Morris2ee92d42006-11-13 16:09:01 -08003639 case IPPROTO_DCCP: {
3640 struct dccp_hdr _dccph, *dh;
3641
3642 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3643 if (dh == NULL)
3644 break;
3645
Eric Paris48c62af2012-04-02 13:15:44 -04003646 ad->u.net->sport = dh->dccph_sport;
3647 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003648 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003649 }
James Morris2ee92d42006-11-13 16:09:01 -08003650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 /* includes fragments */
3652 default:
3653 break;
3654 }
3655out:
3656 return ret;
3657}
3658
3659#endif /* IPV6 */
3660
Thomas Liu2bf49692009-07-14 12:14:09 -04003661static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003662 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
David Howellscf9481e2008-07-27 21:31:07 +10003664 char *addrp;
3665 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Eric Paris48c62af2012-04-02 13:15:44 -04003667 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003669 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003670 if (ret)
3671 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003672 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3673 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003674 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3677 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003678 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003679 if (ret)
3680 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003681 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3682 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003683 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684#endif /* IPV6 */
3685 default:
David Howellscf9481e2008-07-27 21:31:07 +10003686 addrp = NULL;
3687 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
3689
David Howellscf9481e2008-07-27 21:31:07 +10003690parse_error:
3691 printk(KERN_WARNING
3692 "SELinux: failure in selinux_parse_skb(),"
3693 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003695
3696okay:
3697 if (_addrp)
3698 *_addrp = addrp;
3699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
Paul Moore4f6a9932007-03-01 14:35:22 -05003702/**
Paul Moore220deb92008-01-29 08:38:23 -05003703 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003704 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003705 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003706 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003707 *
3708 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003709 * Check the various different forms of network peer labeling and determine
3710 * the peer label/SID for the packet; most of the magic actually occurs in
3711 * the security server function security_net_peersid_cmp(). The function
3712 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3713 * or -EACCES if @sid is invalid due to inconsistencies with the different
3714 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003715 *
3716 */
Paul Moore220deb92008-01-29 08:38:23 -05003717static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003718{
Paul Moore71f1cb02008-01-29 08:51:16 -05003719 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003720 u32 xfrm_sid;
3721 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003722 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003723
3724 selinux_skb_xfrm_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003725 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003726
Paul Moore71f1cb02008-01-29 08:51:16 -05003727 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3728 if (unlikely(err)) {
3729 printk(KERN_WARNING
3730 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3731 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003732 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003733 }
Paul Moore220deb92008-01-29 08:38:23 -05003734
3735 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003736}
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003739
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003740static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3741 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003742{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003743 if (tsec->sockcreate_sid > SECSID_NULL) {
3744 *socksid = tsec->sockcreate_sid;
3745 return 0;
3746 }
3747
3748 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3749 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003750}
3751
Paul Moore253bfae2010-04-22 14:46:19 -04003752static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
Paul Moore253bfae2010-04-22 14:46:19 -04003754 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003755 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003756 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003757 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Paul Moore253bfae2010-04-22 14:46:19 -04003759 if (sksec->sid == SECINITSID_KERNEL)
3760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Eric Paris50c205f2012-04-04 15:01:43 -04003762 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003763 ad.u.net = &net;
3764 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Paul Moore253bfae2010-04-22 14:46:19 -04003766 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
3769static int selinux_socket_create(int family, int type,
3770 int protocol, int kern)
3771{
Paul Moore5fb49872010-04-22 14:46:19 -04003772 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003773 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003774 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003775 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
David Howells275bb412008-11-14 10:39:19 +11003780 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003781 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3782 if (rc)
3783 return rc;
3784
Paul Moored4f2d972010-04-22 14:46:18 -04003785 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003788static int selinux_socket_post_create(struct socket *sock, int family,
3789 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Paul Moore5fb49872010-04-22 14:46:19 -04003791 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003792 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003793 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003794 int err = 0;
3795
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003796 isec->sclass = socket_type_to_security_class(family, type, protocol);
3797
David Howells275bb412008-11-14 10:39:19 +11003798 if (kern)
3799 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003800 else {
3801 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3802 if (err)
3803 return err;
3804 }
David Howells275bb412008-11-14 10:39:19 +11003805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 isec->initialized = 1;
3807
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003808 if (sock->sk) {
3809 sksec = sock->sk->sk_security;
3810 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003811 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04003812 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003813 }
3814
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
3818/* Range of port numbers used to automatically bind.
3819 Need to determine whether we should perform a name_bind
3820 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3823{
Paul Moore253bfae2010-04-22 14:46:19 -04003824 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 u16 family;
3826 int err;
3827
Paul Moore253bfae2010-04-22 14:46:19 -04003828 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (err)
3830 goto out;
3831
3832 /*
3833 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003834 * Multiple address binding for SCTP is not supported yet: we just
3835 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 */
Paul Moore253bfae2010-04-22 14:46:19 -04003837 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 if (family == PF_INET || family == PF_INET6) {
3839 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003840 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003841 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003842 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 struct sockaddr_in *addr4 = NULL;
3844 struct sockaddr_in6 *addr6 = NULL;
3845 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003846 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 if (family == PF_INET) {
3849 addr4 = (struct sockaddr_in *)address;
3850 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 addrp = (char *)&addr4->sin_addr.s_addr;
3852 } else {
3853 addr6 = (struct sockaddr_in6 *)address;
3854 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 addrp = (char *)&addr6->sin6_addr.s6_addr;
3856 }
3857
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003858 if (snum) {
3859 int low, high;
3860
3861 inet_get_local_port_range(&low, &high);
3862
3863 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003864 err = sel_netport_sid(sk->sk_protocol,
3865 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003866 if (err)
3867 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04003868 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003869 ad.u.net = &net;
3870 ad.u.net->sport = htons(snum);
3871 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04003872 err = avc_has_perm(sksec->sid, sid,
3873 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003874 SOCKET__NAME_BIND, &ad);
3875 if (err)
3876 goto out;
3877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 }
Eric Paris828dfe12008-04-17 13:17:49 -04003879
Paul Moore253bfae2010-04-22 14:46:19 -04003880 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04003881 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 node_perm = TCP_SOCKET__NODE_BIND;
3883 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003884
James Morris13402582005-09-30 14:24:34 -04003885 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 node_perm = UDP_SOCKET__NODE_BIND;
3887 break;
James Morris2ee92d42006-11-13 16:09:01 -08003888
3889 case SECCLASS_DCCP_SOCKET:
3890 node_perm = DCCP_SOCKET__NODE_BIND;
3891 break;
3892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 default:
3894 node_perm = RAWIP_SOCKET__NODE_BIND;
3895 break;
3896 }
Eric Paris828dfe12008-04-17 13:17:49 -04003897
Paul Moore224dfbd2008-01-29 08:38:13 -05003898 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (err)
3900 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04003901
Eric Paris50c205f2012-04-04 15:01:43 -04003902 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003903 ad.u.net = &net;
3904 ad.u.net->sport = htons(snum);
3905 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
3907 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04003908 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 else
Eric Paris48c62af2012-04-02 13:15:44 -04003910 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Paul Moore253bfae2010-04-22 14:46:19 -04003912 err = avc_has_perm(sksec->sid, sid,
3913 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (err)
3915 goto out;
3916 }
3917out:
3918 return err;
3919}
3920
3921static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3922{
Paul Moore014ab192008-10-10 10:16:33 -04003923 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04003924 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 int err;
3926
Paul Moore253bfae2010-04-22 14:46:19 -04003927 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 if (err)
3929 return err;
3930
3931 /*
James Morris2ee92d42006-11-13 16:09:01 -08003932 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 */
Paul Moore253bfae2010-04-22 14:46:19 -04003934 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
3935 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04003936 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003937 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 struct sockaddr_in *addr4 = NULL;
3939 struct sockaddr_in6 *addr6 = NULL;
3940 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08003941 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 if (sk->sk_family == PF_INET) {
3944 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003945 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 return -EINVAL;
3947 snum = ntohs(addr4->sin_port);
3948 } else {
3949 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003950 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return -EINVAL;
3952 snum = ntohs(addr6->sin6_port);
3953 }
3954
Paul Moore3e112172008-04-10 10:48:14 -04003955 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (err)
3957 goto out;
3958
Paul Moore253bfae2010-04-22 14:46:19 -04003959 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08003960 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
3961
Eric Paris50c205f2012-04-04 15:01:43 -04003962 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003963 ad.u.net = &net;
3964 ad.u.net->dport = htons(snum);
3965 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04003966 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 if (err)
3968 goto out;
3969 }
3970
Paul Moore014ab192008-10-10 10:16:33 -04003971 err = selinux_netlbl_socket_connect(sk, address);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973out:
3974 return err;
3975}
3976
3977static int selinux_socket_listen(struct socket *sock, int backlog)
3978{
Paul Moore253bfae2010-04-22 14:46:19 -04003979 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
3982static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
3983{
3984 int err;
3985 struct inode_security_struct *isec;
3986 struct inode_security_struct *newisec;
3987
Paul Moore253bfae2010-04-22 14:46:19 -04003988 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (err)
3990 return err;
3991
3992 newisec = SOCK_INODE(newsock)->i_security;
3993
3994 isec = SOCK_INODE(sock)->i_security;
3995 newisec->sclass = isec->sclass;
3996 newisec->sid = isec->sid;
3997 newisec->initialized = 1;
3998
3999 return 0;
4000}
4001
4002static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004003 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Paul Moore253bfae2010-04-22 14:46:19 -04004005 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
4008static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4009 int size, int flags)
4010{
Paul Moore253bfae2010-04-22 14:46:19 -04004011 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
4014static int selinux_socket_getsockname(struct socket *sock)
4015{
Paul Moore253bfae2010-04-22 14:46:19 -04004016 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019static int selinux_socket_getpeername(struct socket *sock)
4020{
Paul Moore253bfae2010-04-22 14:46:19 -04004021 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022}
4023
Eric Paris828dfe12008-04-17 13:17:49 -04004024static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Paul Mooref8687af2006-10-30 15:22:15 -08004026 int err;
4027
Paul Moore253bfae2010-04-22 14:46:19 -04004028 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004029 if (err)
4030 return err;
4031
4032 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
4035static int selinux_socket_getsockopt(struct socket *sock, int level,
4036 int optname)
4037{
Paul Moore253bfae2010-04-22 14:46:19 -04004038 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
4041static int selinux_socket_shutdown(struct socket *sock, int how)
4042{
Paul Moore253bfae2010-04-22 14:46:19 -04004043 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
David S. Miller3610cda2011-01-05 15:38:53 -08004046static int selinux_socket_unix_stream_connect(struct sock *sock,
4047 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 struct sock *newsk)
4049{
David S. Miller3610cda2011-01-05 15:38:53 -08004050 struct sk_security_struct *sksec_sock = sock->sk_security;
4051 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004052 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004053 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004054 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 int err;
4056
Eric Paris50c205f2012-04-04 15:01:43 -04004057 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004058 ad.u.net = &net;
4059 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Paul Moore4d1e2452010-04-22 14:46:18 -04004061 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4062 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4064 if (err)
4065 return err;
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004068 sksec_new->peer_sid = sksec_sock->sid;
4069 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4070 &sksec_new->sid);
4071 if (err)
4072 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004073
Paul Moore4d1e2452010-04-22 14:46:18 -04004074 /* connecting socket */
4075 sksec_sock->peer_sid = sksec_new->sid;
4076
4077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
4080static int selinux_socket_unix_may_send(struct socket *sock,
4081 struct socket *other)
4082{
Paul Moore253bfae2010-04-22 14:46:19 -04004083 struct sk_security_struct *ssec = sock->sk->sk_security;
4084 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004085 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004086 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Eric Paris50c205f2012-04-04 15:01:43 -04004088 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004089 ad.u.net = &net;
4090 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Paul Moore253bfae2010-04-22 14:46:19 -04004092 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4093 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094}
4095
Paul Mooreeffad8d2008-01-29 08:49:27 -05004096static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4097 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004098 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004099{
4100 int err;
4101 u32 if_sid;
4102 u32 node_sid;
4103
4104 err = sel_netif_sid(ifindex, &if_sid);
4105 if (err)
4106 return err;
4107 err = avc_has_perm(peer_sid, if_sid,
4108 SECCLASS_NETIF, NETIF__INGRESS, ad);
4109 if (err)
4110 return err;
4111
4112 err = sel_netnode_sid(addrp, family, &node_sid);
4113 if (err)
4114 return err;
4115 return avc_has_perm(peer_sid, node_sid,
4116 SECCLASS_NODE, NODE__RECVFROM, ad);
4117}
4118
Paul Moore220deb92008-01-29 08:38:23 -05004119static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004120 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004121{
Paul Moore277d3422008-12-31 12:54:11 -05004122 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004123 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004124 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004125 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004126 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004127 char *addrp;
4128
Eric Paris50c205f2012-04-04 15:01:43 -04004129 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004130 ad.u.net = &net;
4131 ad.u.net->netif = skb->skb_iif;
4132 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004133 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4134 if (err)
4135 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004136
Paul Moore58bfbb52009-03-27 17:10:41 -04004137 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004138 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004139 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004140 if (err)
4141 return err;
4142 }
Paul Moore220deb92008-01-29 08:38:23 -05004143
Steffen Klassertb9679a72011-02-23 12:55:21 +01004144 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4145 if (err)
4146 return err;
4147 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004148
James Morris4e5ab4c2006-06-09 00:33:33 -07004149 return err;
4150}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004151
James Morris4e5ab4c2006-06-09 00:33:33 -07004152static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4153{
Paul Moore220deb92008-01-29 08:38:23 -05004154 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004155 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004156 u16 family = sk->sk_family;
4157 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004158 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004159 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004160 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004161 u8 secmark_active;
4162 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004163
James Morris4e5ab4c2006-06-09 00:33:33 -07004164 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004165 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004166
4167 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004168 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004169 family = PF_INET;
4170
Paul Moored8395c82008-10-10 10:16:30 -04004171 /* If any sort of compatibility mode is enabled then handoff processing
4172 * to the selinux_sock_rcv_skb_compat() function to deal with the
4173 * special handling. We do this in an attempt to keep this function
4174 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004175 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004176 return selinux_sock_rcv_skb_compat(sk, skb, family);
4177
4178 secmark_active = selinux_secmark_enabled();
4179 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4180 if (!secmark_active && !peerlbl_active)
4181 return 0;
4182
Eric Paris50c205f2012-04-04 15:01:43 -04004183 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004184 ad.u.net = &net;
4185 ad.u.net->netif = skb->skb_iif;
4186 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004187 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004188 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004189 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004190
Paul Moored8395c82008-10-10 10:16:30 -04004191 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004192 u32 peer_sid;
4193
4194 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4195 if (err)
4196 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004197 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004198 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004199 if (err) {
4200 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004201 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004202 }
Paul Moored621d352008-01-29 08:43:36 -05004203 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4204 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004205 if (err)
4206 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004207 }
4208
Paul Moored8395c82008-10-10 10:16:30 -04004209 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004210 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4211 PACKET__RECV, &ad);
4212 if (err)
4213 return err;
4214 }
4215
Paul Moored621d352008-01-29 08:43:36 -05004216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004219static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4220 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 int err = 0;
4223 char *scontext;
4224 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004225 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004226 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Paul Moore253bfae2010-04-22 14:46:19 -04004228 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4229 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004230 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004231 if (peer_sid == SECSID_NULL)
4232 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004234 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 if (scontext_len > len) {
4239 err = -ERANGE;
4240 goto out_len;
4241 }
4242
4243 if (copy_to_user(optval, scontext, scontext_len))
4244 err = -EFAULT;
4245
4246out_len:
4247 if (put_user(scontext_len, optlen))
4248 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return err;
4251}
4252
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004253static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004254{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004255 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004256 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004257
Paul Mooreaa862902008-10-10 10:16:29 -04004258 if (skb && skb->protocol == htons(ETH_P_IP))
4259 family = PF_INET;
4260 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4261 family = PF_INET6;
4262 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004263 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004264 else
4265 goto out;
4266
4267 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004268 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004269 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004270 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004271
Paul Moore75e22912008-01-29 08:38:04 -05004272out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004273 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004274 if (peer_secid == SECSID_NULL)
4275 return -EINVAL;
4276 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004277}
4278
Al Viro7d877f32005-10-21 03:20:43 -04004279static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Paul Moore84914b72010-04-22 14:46:18 -04004281 struct sk_security_struct *sksec;
4282
4283 sksec = kzalloc(sizeof(*sksec), priority);
4284 if (!sksec)
4285 return -ENOMEM;
4286
4287 sksec->peer_sid = SECINITSID_UNLABELED;
4288 sksec->sid = SECINITSID_UNLABELED;
4289 selinux_netlbl_sk_security_reset(sksec);
4290 sk->sk_security = sksec;
4291
4292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
4294
4295static void selinux_sk_free_security(struct sock *sk)
4296{
Paul Moore84914b72010-04-22 14:46:18 -04004297 struct sk_security_struct *sksec = sk->sk_security;
4298
4299 sk->sk_security = NULL;
4300 selinux_netlbl_sk_security_free(sksec);
4301 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004304static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4305{
Eric Parisdd3e7832010-04-07 15:08:46 -04004306 struct sk_security_struct *sksec = sk->sk_security;
4307 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004308
Eric Parisdd3e7832010-04-07 15:08:46 -04004309 newsksec->sid = sksec->sid;
4310 newsksec->peer_sid = sksec->peer_sid;
4311 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004312
Eric Parisdd3e7832010-04-07 15:08:46 -04004313 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004314}
4315
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004316static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004317{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004318 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004319 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004320 else {
4321 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004322
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004323 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004324 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004325}
4326
Eric Paris828dfe12008-04-17 13:17:49 -04004327static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004328{
4329 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4330 struct sk_security_struct *sksec = sk->sk_security;
4331
David Woodhouse2148ccc2006-09-29 15:50:25 -07004332 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4333 sk->sk_family == PF_UNIX)
4334 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004335 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004336}
4337
Adrian Bunk9a673e52006-08-15 00:03:53 -07004338static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4339 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004340{
4341 struct sk_security_struct *sksec = sk->sk_security;
4342 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004343 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004344 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004345 u32 peersid;
4346
Paul Mooreaa862902008-10-10 10:16:29 -04004347 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4348 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4349 family = PF_INET;
4350
4351 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004352 if (err)
4353 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004354 if (peersid == SECSID_NULL) {
4355 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004356 req->peer_secid = SECSID_NULL;
Paul Moore389fb8002009-03-27 17:10:34 -04004357 } else {
4358 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4359 if (err)
4360 return err;
4361 req->secid = newsid;
4362 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004363 }
4364
Paul Moore389fb8002009-03-27 17:10:34 -04004365 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004366}
4367
Adrian Bunk9a673e52006-08-15 00:03:53 -07004368static void selinux_inet_csk_clone(struct sock *newsk,
4369 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004370{
4371 struct sk_security_struct *newsksec = newsk->sk_security;
4372
4373 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004374 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004375 /* NOTE: Ideally, we should also get the isec->sid for the
4376 new socket in sync, but we don't have the isec available yet.
4377 So we will wait until sock_graft to do it, by which
4378 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004379
Paul Moore9f2ad662006-11-17 17:38:53 -05004380 /* We don't need to take any sort of lock here as we are the only
4381 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004382 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004383}
4384
Paul Moore014ab192008-10-10 10:16:33 -04004385static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004386{
Paul Mooreaa862902008-10-10 10:16:29 -04004387 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004388 struct sk_security_struct *sksec = sk->sk_security;
4389
Paul Mooreaa862902008-10-10 10:16:29 -04004390 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4391 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4392 family = PF_INET;
4393
4394 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004395}
4396
Eric Paris2606fd12010-10-13 16:24:41 -04004397static int selinux_secmark_relabel_packet(u32 sid)
4398{
4399 const struct task_security_struct *__tsec;
4400 u32 tsid;
4401
4402 __tsec = current_security();
4403 tsid = __tsec->sid;
4404
4405 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4406}
4407
4408static void selinux_secmark_refcount_inc(void)
4409{
4410 atomic_inc(&selinux_secmark_refcount);
4411}
4412
4413static void selinux_secmark_refcount_dec(void)
4414{
4415 atomic_dec(&selinux_secmark_refcount);
4416}
4417
Adrian Bunk9a673e52006-08-15 00:03:53 -07004418static void selinux_req_classify_flow(const struct request_sock *req,
4419 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004420{
David S. Miller1d28f422011-03-12 00:29:39 -05004421 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004422}
4423
Paul Moore5dbbaf22013-01-14 07:12:19 +00004424static int selinux_tun_dev_alloc_security(void **security)
4425{
4426 struct tun_security_struct *tunsec;
4427
4428 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4429 if (!tunsec)
4430 return -ENOMEM;
4431 tunsec->sid = current_sid();
4432
4433 *security = tunsec;
4434 return 0;
4435}
4436
4437static void selinux_tun_dev_free_security(void *security)
4438{
4439 kfree(security);
4440}
4441
Paul Mooreed6d76e2009-08-28 18:12:49 -04004442static int selinux_tun_dev_create(void)
4443{
4444 u32 sid = current_sid();
4445
4446 /* we aren't taking into account the "sockcreate" SID since the socket
4447 * that is being created here is not a socket in the traditional sense,
4448 * instead it is a private sock, accessible only to the kernel, and
4449 * representing a wide range of network traffic spanning multiple
4450 * connections unlike traditional sockets - check the TUN driver to
4451 * get a better understanding of why this socket is special */
4452
4453 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4454 NULL);
4455}
4456
Paul Moore5dbbaf22013-01-14 07:12:19 +00004457static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004458{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004459 struct tun_security_struct *tunsec = security;
4460
4461 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4462 TUN_SOCKET__ATTACH_QUEUE, NULL);
4463}
4464
4465static int selinux_tun_dev_attach(struct sock *sk, void *security)
4466{
4467 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004468 struct sk_security_struct *sksec = sk->sk_security;
4469
4470 /* we don't currently perform any NetLabel based labeling here and it
4471 * isn't clear that we would want to do so anyway; while we could apply
4472 * labeling without the support of the TUN user the resulting labeled
4473 * traffic from the other end of the connection would almost certainly
4474 * cause confusion to the TUN user that had no idea network labeling
4475 * protocols were being used */
4476
Paul Moore5dbbaf22013-01-14 07:12:19 +00004477 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004478 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004479
4480 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004481}
4482
Paul Moore5dbbaf22013-01-14 07:12:19 +00004483static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004484{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004485 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004486 u32 sid = current_sid();
4487 int err;
4488
Paul Moore5dbbaf22013-01-14 07:12:19 +00004489 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04004490 TUN_SOCKET__RELABELFROM, NULL);
4491 if (err)
4492 return err;
4493 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4494 TUN_SOCKET__RELABELTO, NULL);
4495 if (err)
4496 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004497 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004498
4499 return 0;
4500}
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4503{
4504 int err = 0;
4505 u32 perm;
4506 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004507 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 if (skb->len < NLMSG_SPACE(0)) {
4510 err = -EINVAL;
4511 goto out;
4512 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004513 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004514
Paul Moore253bfae2010-04-22 14:46:19 -04004515 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 if (err) {
4517 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004518 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 "SELinux: unrecognized netlink message"
4520 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004521 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004522 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 err = 0;
4524 }
4525
4526 /* Ignore */
4527 if (err == -ENOENT)
4528 err = 0;
4529 goto out;
4530 }
4531
Paul Moore253bfae2010-04-22 14:46:19 -04004532 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533out:
4534 return err;
4535}
4536
4537#ifdef CONFIG_NETFILTER
4538
Paul Mooreeffad8d2008-01-29 08:49:27 -05004539static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4540 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Paul Mooredfaebe92008-10-10 10:16:31 -04004542 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004543 char *addrp;
4544 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004545 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004546 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004547 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004548 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004549 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004550
Paul Mooreeffad8d2008-01-29 08:49:27 -05004551 if (!selinux_policycap_netpeer)
4552 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004553
Paul Mooreeffad8d2008-01-29 08:49:27 -05004554 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004555 netlbl_active = netlbl_enabled();
4556 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004557 if (!secmark_active && !peerlbl_active)
4558 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004559
Paul Moored8395c82008-10-10 10:16:30 -04004560 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4561 return NF_DROP;
4562
Eric Paris50c205f2012-04-04 15:01:43 -04004563 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004564 ad.u.net = &net;
4565 ad.u.net->netif = ifindex;
4566 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004567 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4568 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Paul Mooredfaebe92008-10-10 10:16:31 -04004570 if (peerlbl_active) {
4571 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4572 peer_sid, &ad);
4573 if (err) {
4574 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004575 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004576 }
4577 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004578
4579 if (secmark_active)
4580 if (avc_has_perm(peer_sid, skb->secmark,
4581 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4582 return NF_DROP;
4583
Paul Moore948bf852008-10-10 10:16:32 -04004584 if (netlbl_active)
4585 /* we do this in the FORWARD path and not the POST_ROUTING
4586 * path because we want to make sure we apply the necessary
4587 * labeling before IPsec is applied so we can leverage AH
4588 * protection */
4589 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4590 return NF_DROP;
4591
Paul Mooreeffad8d2008-01-29 08:49:27 -05004592 return NF_ACCEPT;
4593}
4594
4595static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4596 struct sk_buff *skb,
4597 const struct net_device *in,
4598 const struct net_device *out,
4599 int (*okfn)(struct sk_buff *))
4600{
4601 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4602}
4603
4604#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4605static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4606 struct sk_buff *skb,
4607 const struct net_device *in,
4608 const struct net_device *out,
4609 int (*okfn)(struct sk_buff *))
4610{
4611 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4612}
4613#endif /* IPV6 */
4614
Paul Moore948bf852008-10-10 10:16:32 -04004615static unsigned int selinux_ip_output(struct sk_buff *skb,
4616 u16 family)
4617{
4618 u32 sid;
4619
4620 if (!netlbl_enabled())
4621 return NF_ACCEPT;
4622
4623 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4624 * because we want to make sure we apply the necessary labeling
4625 * before IPsec is applied so we can leverage AH protection */
4626 if (skb->sk) {
4627 struct sk_security_struct *sksec = skb->sk->sk_security;
4628 sid = sksec->sid;
4629 } else
4630 sid = SECINITSID_KERNEL;
4631 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4632 return NF_DROP;
4633
4634 return NF_ACCEPT;
4635}
4636
4637static unsigned int selinux_ipv4_output(unsigned int hooknum,
4638 struct sk_buff *skb,
4639 const struct net_device *in,
4640 const struct net_device *out,
4641 int (*okfn)(struct sk_buff *))
4642{
4643 return selinux_ip_output(skb, PF_INET);
4644}
4645
Paul Mooreeffad8d2008-01-29 08:49:27 -05004646static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4647 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004648 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004649{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004650 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004651 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004652 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004653 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004654 char *addrp;
4655 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004656
Paul Mooreeffad8d2008-01-29 08:49:27 -05004657 if (sk == NULL)
4658 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004659 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004660
Eric Paris50c205f2012-04-04 15:01:43 -04004661 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004662 ad.u.net = &net;
4663 ad.u.net->netif = ifindex;
4664 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004665 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4666 return NF_DROP;
4667
Paul Moore58bfbb52009-03-27 17:10:41 -04004668 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004669 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004670 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004671 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004672
Steffen Klassertb9679a72011-02-23 12:55:21 +01004673 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4674 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004675
Paul Mooreeffad8d2008-01-29 08:49:27 -05004676 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
Paul Mooreeffad8d2008-01-29 08:49:27 -05004679static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4680 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004682 u32 secmark_perm;
4683 u32 peer_sid;
4684 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004685 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004686 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004687 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004688 u8 secmark_active;
4689 u8 peerlbl_active;
4690
Paul Mooreeffad8d2008-01-29 08:49:27 -05004691 /* If any sort of compatibility mode is enabled then handoff processing
4692 * to the selinux_ip_postroute_compat() function to deal with the
4693 * special handling. We do this in an attempt to keep this function
4694 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004695 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004696 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004697#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004698 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4699 * packet transformation so allow the packet to pass without any checks
4700 * since we'll have another chance to perform access control checks
4701 * when the packet is on it's final way out.
4702 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4703 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004704 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004705 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004706#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004707 secmark_active = selinux_secmark_enabled();
4708 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4709 if (!secmark_active && !peerlbl_active)
4710 return NF_ACCEPT;
4711
Paul Moored8395c82008-10-10 10:16:30 -04004712 /* if the packet is being forwarded then get the peer label from the
4713 * packet itself; otherwise check to see if it is from a local
4714 * application or the kernel, if from an application get the peer label
4715 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004716 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004717 if (sk == NULL) {
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004718 if (skb->skb_iif) {
4719 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004720 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004721 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004722 } else {
4723 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004724 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004725 }
Paul Moored8395c82008-10-10 10:16:30 -04004726 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004727 struct sk_security_struct *sksec = sk->sk_security;
4728 peer_sid = sksec->sid;
4729 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004730 }
4731
Eric Paris50c205f2012-04-04 15:01:43 -04004732 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004733 ad.u.net = &net;
4734 ad.u.net->netif = ifindex;
4735 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004736 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004737 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004738
Paul Mooreeffad8d2008-01-29 08:49:27 -05004739 if (secmark_active)
4740 if (avc_has_perm(peer_sid, skb->secmark,
4741 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004742 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004743
4744 if (peerlbl_active) {
4745 u32 if_sid;
4746 u32 node_sid;
4747
4748 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004749 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004750 if (avc_has_perm(peer_sid, if_sid,
4751 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004752 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004753
4754 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004755 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004756 if (avc_has_perm(peer_sid, node_sid,
4757 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004758 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004759 }
4760
4761 return NF_ACCEPT;
4762}
4763
4764static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4765 struct sk_buff *skb,
4766 const struct net_device *in,
4767 const struct net_device *out,
4768 int (*okfn)(struct sk_buff *))
4769{
4770 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
4773#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004774static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4775 struct sk_buff *skb,
4776 const struct net_device *in,
4777 const struct net_device *out,
4778 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004780 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782#endif /* IPV6 */
4783
4784#endif /* CONFIG_NETFILTER */
4785
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 int err;
4789
Eric Paris200ac532009-02-12 15:01:04 -05004790 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 if (err)
4792 return err;
4793
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004794 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797static int ipc_alloc_security(struct task_struct *task,
4798 struct kern_ipc_perm *perm,
4799 u16 sclass)
4800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004802 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
James Morris89d155e2005-10-30 14:59:21 -08004804 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 if (!isec)
4806 return -ENOMEM;
4807
David Howells275bb412008-11-14 10:39:19 +11004808 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004810 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 perm->security = isec;
4812
4813 return 0;
4814}
4815
4816static void ipc_free_security(struct kern_ipc_perm *perm)
4817{
4818 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 perm->security = NULL;
4820 kfree(isec);
4821}
4822
4823static int msg_msg_alloc_security(struct msg_msg *msg)
4824{
4825 struct msg_security_struct *msec;
4826
James Morris89d155e2005-10-30 14:59:21 -08004827 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 if (!msec)
4829 return -ENOMEM;
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 msec->sid = SECINITSID_UNLABELED;
4832 msg->security = msec;
4833
4834 return 0;
4835}
4836
4837static void msg_msg_free_security(struct msg_msg *msg)
4838{
4839 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 msg->security = NULL;
4842 kfree(msec);
4843}
4844
4845static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07004846 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004849 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004850 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 isec = ipc_perms->security;
4853
Eric Paris50c205f2012-04-04 15:01:43 -04004854 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 ad.u.ipc_id = ipc_perms->key;
4856
David Howells275bb412008-11-14 10:39:19 +11004857 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
4860static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
4861{
4862 return msg_msg_alloc_security(msg);
4863}
4864
4865static void selinux_msg_msg_free_security(struct msg_msg *msg)
4866{
4867 msg_msg_free_security(msg);
4868}
4869
4870/* message queue security operations */
4871static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
4872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004874 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004875 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 int rc;
4877
4878 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
4879 if (rc)
4880 return rc;
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 isec = msq->q_perm.security;
4883
Eric Paris50c205f2012-04-04 15:01:43 -04004884 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04004885 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
David Howells275bb412008-11-14 10:39:19 +11004887 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 MSGQ__CREATE, &ad);
4889 if (rc) {
4890 ipc_free_security(&msq->q_perm);
4891 return rc;
4892 }
4893 return 0;
4894}
4895
4896static void selinux_msg_queue_free_security(struct msg_queue *msq)
4897{
4898 ipc_free_security(&msq->q_perm);
4899}
4900
4901static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
4902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004904 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004905 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 isec = msq->q_perm.security;
4908
Eric Paris50c205f2012-04-04 15:01:43 -04004909 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 ad.u.ipc_id = msq->q_perm.key;
4911
David Howells275bb412008-11-14 10:39:19 +11004912 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 MSGQ__ASSOCIATE, &ad);
4914}
4915
4916static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
4917{
4918 int err;
4919 int perms;
4920
Eric Paris828dfe12008-04-17 13:17:49 -04004921 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 case IPC_INFO:
4923 case MSG_INFO:
4924 /* No specific object, just general system-wide information. */
4925 return task_has_system(current, SYSTEM__IPC_INFO);
4926 case IPC_STAT:
4927 case MSG_STAT:
4928 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
4929 break;
4930 case IPC_SET:
4931 perms = MSGQ__SETATTR;
4932 break;
4933 case IPC_RMID:
4934 perms = MSGQ__DESTROY;
4935 break;
4936 default:
4937 return 0;
4938 }
4939
Stephen Smalley6af963f2005-05-01 08:58:39 -07004940 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return err;
4942}
4943
4944static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
4945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 struct ipc_security_struct *isec;
4947 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004948 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004949 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 int rc;
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 isec = msq->q_perm.security;
4953 msec = msg->security;
4954
4955 /*
4956 * First time through, need to assign label to the message
4957 */
4958 if (msec->sid == SECINITSID_UNLABELED) {
4959 /*
4960 * Compute new sid based on current process and
4961 * message queue this message will be stored in
4962 */
David Howells275bb412008-11-14 10:39:19 +11004963 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05004964 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 if (rc)
4966 return rc;
4967 }
4968
Eric Paris50c205f2012-04-04 15:01:43 -04004969 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 ad.u.ipc_id = msq->q_perm.key;
4971
4972 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11004973 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 MSGQ__WRITE, &ad);
4975 if (!rc)
4976 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11004977 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
4978 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 if (!rc)
4980 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11004981 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
4982 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 return rc;
4985}
4986
4987static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
4988 struct task_struct *target,
4989 long type, int mode)
4990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 struct ipc_security_struct *isec;
4992 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004993 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004994 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 int rc;
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 isec = msq->q_perm.security;
4998 msec = msg->security;
4999
Eric Paris50c205f2012-04-04 15:01:43 -04005000 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005001 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
David Howells275bb412008-11-14 10:39:19 +11005003 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 SECCLASS_MSGQ, MSGQ__READ, &ad);
5005 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005006 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 SECCLASS_MSG, MSG__RECEIVE, &ad);
5008 return rc;
5009}
5010
5011/* Shared Memory security operations */
5012static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 struct ipc_security_struct *isec;
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
5019 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5020 if (rc)
5021 return rc;
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 isec = shp->shm_perm.security;
5024
Eric Paris50c205f2012-04-04 15:01:43 -04005025 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005026 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
David Howells275bb412008-11-14 10:39:19 +11005028 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 SHM__CREATE, &ad);
5030 if (rc) {
5031 ipc_free_security(&shp->shm_perm);
5032 return rc;
5033 }
5034 return 0;
5035}
5036
5037static void selinux_shm_free_security(struct shmid_kernel *shp)
5038{
5039 ipc_free_security(&shp->shm_perm);
5040}
5041
5042static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005045 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005046 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 isec = shp->shm_perm.security;
5049
Eric Paris50c205f2012-04-04 15:01:43 -04005050 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 ad.u.ipc_id = shp->shm_perm.key;
5052
David Howells275bb412008-11-14 10:39:19 +11005053 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 SHM__ASSOCIATE, &ad);
5055}
5056
5057/* Note, at this point, shp is locked down */
5058static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5059{
5060 int perms;
5061 int err;
5062
Eric Paris828dfe12008-04-17 13:17:49 -04005063 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 case IPC_INFO:
5065 case SHM_INFO:
5066 /* No specific object, just general system-wide information. */
5067 return task_has_system(current, SYSTEM__IPC_INFO);
5068 case IPC_STAT:
5069 case SHM_STAT:
5070 perms = SHM__GETATTR | SHM__ASSOCIATE;
5071 break;
5072 case IPC_SET:
5073 perms = SHM__SETATTR;
5074 break;
5075 case SHM_LOCK:
5076 case SHM_UNLOCK:
5077 perms = SHM__LOCK;
5078 break;
5079 case IPC_RMID:
5080 perms = SHM__DESTROY;
5081 break;
5082 default:
5083 return 0;
5084 }
5085
Stephen Smalley6af963f2005-05-01 08:58:39 -07005086 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 return err;
5088}
5089
5090static int selinux_shm_shmat(struct shmid_kernel *shp,
5091 char __user *shmaddr, int shmflg)
5092{
5093 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
5095 if (shmflg & SHM_RDONLY)
5096 perms = SHM__READ;
5097 else
5098 perms = SHM__READ | SHM__WRITE;
5099
Stephen Smalley6af963f2005-05-01 08:58:39 -07005100 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101}
5102
5103/* Semaphore security operations */
5104static int selinux_sem_alloc_security(struct sem_array *sma)
5105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005107 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005108 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 int rc;
5110
5111 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5112 if (rc)
5113 return rc;
5114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 isec = sma->sem_perm.security;
5116
Eric Paris50c205f2012-04-04 15:01:43 -04005117 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005118 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
David Howells275bb412008-11-14 10:39:19 +11005120 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 SEM__CREATE, &ad);
5122 if (rc) {
5123 ipc_free_security(&sma->sem_perm);
5124 return rc;
5125 }
5126 return 0;
5127}
5128
5129static void selinux_sem_free_security(struct sem_array *sma)
5130{
5131 ipc_free_security(&sma->sem_perm);
5132}
5133
5134static int selinux_sem_associate(struct sem_array *sma, int semflg)
5135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005137 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005138 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 isec = sma->sem_perm.security;
5141
Eric Paris50c205f2012-04-04 15:01:43 -04005142 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 ad.u.ipc_id = sma->sem_perm.key;
5144
David Howells275bb412008-11-14 10:39:19 +11005145 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 SEM__ASSOCIATE, &ad);
5147}
5148
5149/* Note, at this point, sma is locked down */
5150static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5151{
5152 int err;
5153 u32 perms;
5154
Eric Paris828dfe12008-04-17 13:17:49 -04005155 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 case IPC_INFO:
5157 case SEM_INFO:
5158 /* No specific object, just general system-wide information. */
5159 return task_has_system(current, SYSTEM__IPC_INFO);
5160 case GETPID:
5161 case GETNCNT:
5162 case GETZCNT:
5163 perms = SEM__GETATTR;
5164 break;
5165 case GETVAL:
5166 case GETALL:
5167 perms = SEM__READ;
5168 break;
5169 case SETVAL:
5170 case SETALL:
5171 perms = SEM__WRITE;
5172 break;
5173 case IPC_RMID:
5174 perms = SEM__DESTROY;
5175 break;
5176 case IPC_SET:
5177 perms = SEM__SETATTR;
5178 break;
5179 case IPC_STAT:
5180 case SEM_STAT:
5181 perms = SEM__GETATTR | SEM__ASSOCIATE;
5182 break;
5183 default:
5184 return 0;
5185 }
5186
Stephen Smalley6af963f2005-05-01 08:58:39 -07005187 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 return err;
5189}
5190
5191static int selinux_sem_semop(struct sem_array *sma,
5192 struct sembuf *sops, unsigned nsops, int alter)
5193{
5194 u32 perms;
5195
5196 if (alter)
5197 perms = SEM__READ | SEM__WRITE;
5198 else
5199 perms = SEM__READ;
5200
Stephen Smalley6af963f2005-05-01 08:58:39 -07005201 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202}
5203
5204static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 u32 av = 0;
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 av = 0;
5209 if (flag & S_IRUGO)
5210 av |= IPC__UNIX_READ;
5211 if (flag & S_IWUGO)
5212 av |= IPC__UNIX_WRITE;
5213
5214 if (av == 0)
5215 return 0;
5216
Stephen Smalley6af963f2005-05-01 08:58:39 -07005217 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218}
5219
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005220static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5221{
5222 struct ipc_security_struct *isec = ipcp->security;
5223 *secid = isec->sid;
5224}
5225
Eric Paris828dfe12008-04-17 13:17:49 -04005226static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227{
5228 if (inode)
5229 inode_doinit_with_dentry(inode, dentry);
5230}
5231
5232static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005233 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
David Howells275bb412008-11-14 10:39:19 +11005235 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005236 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005238 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
5240 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005241 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if (error)
5243 return error;
5244 }
5245
David Howells275bb412008-11-14 10:39:19 +11005246 rcu_read_lock();
5247 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
5249 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005250 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005252 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005254 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005256 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005257 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005258 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005259 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005260 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 else
David Howells275bb412008-11-14 10:39:19 +11005262 goto invalid;
5263 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
5265 if (!sid)
5266 return 0;
5267
Al Viro04ff9702007-03-12 16:17:58 +00005268 error = security_sid_to_context(sid, value, &len);
5269 if (error)
5270 return error;
5271 return len;
David Howells275bb412008-11-14 10:39:19 +11005272
5273invalid:
5274 rcu_read_unlock();
5275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276}
5277
5278static int selinux_setprocattr(struct task_struct *p,
5279 char *name, void *value, size_t size)
5280{
5281 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005282 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005283 struct cred *new;
5284 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 int error;
5286 char *str = value;
5287
5288 if (current != p) {
5289 /* SELinux only allows a process to change its own
5290 security attributes. */
5291 return -EACCES;
5292 }
5293
5294 /*
5295 * Basic control over ability to set these attributes at all.
5296 * current == p, but we'll pass them separately in case the
5297 * above restriction is ever removed.
5298 */
5299 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005300 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005302 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005303 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005304 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005305 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005306 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005308 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 else
5310 error = -EINVAL;
5311 if (error)
5312 return error;
5313
5314 /* Obtain a SID for the context, if one was specified. */
5315 if (size && str[1] && str[1] != '\n') {
5316 if (str[size-1] == '\n') {
5317 str[size-1] = 0;
5318 size--;
5319 }
5320 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005321 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005322 if (!capable(CAP_MAC_ADMIN)) {
5323 struct audit_buffer *ab;
5324 size_t audit_size;
5325
5326 /* We strip a nul only if it is at the end, otherwise the
5327 * context contains a nul and we should audit that */
5328 if (str[size - 1] == '\0')
5329 audit_size = size - 1;
5330 else
5331 audit_size = size;
5332 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5333 audit_log_format(ab, "op=fscreate invalid_context=");
5334 audit_log_n_untrustedstring(ab, value, audit_size);
5335 audit_log_end(ab);
5336
Stephen Smalley12b29f32008-05-07 13:03:20 -04005337 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005338 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005339 error = security_context_to_sid_force(value, size,
5340 &sid);
5341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 if (error)
5343 return error;
5344 }
5345
David Howellsd84f4f92008-11-14 10:39:23 +11005346 new = prepare_creds();
5347 if (!new)
5348 return -ENOMEM;
5349
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 /* Permission checking based on the specified context is
5351 performed during the actual operation (execve,
5352 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005353 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 checks and may_create for the file creation checks. The
5355 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005356 tsec = new->security;
5357 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005359 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005361 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005362 error = may_create_key(sid, p);
5363 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005364 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005365 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005366 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005367 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005368 } else if (!strcmp(name, "current")) {
5369 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005371 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005372
David Howellsd84f4f92008-11-14 10:39:23 +11005373 /* Only allow single threaded processes to change context */
5374 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005375 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005376 error = security_bounded_transition(tsec->sid, sid);
5377 if (error)
5378 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 /* Check permissions for the transition. */
5382 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005383 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005385 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 /* Check for ptracing, and update the task SID if ok.
5388 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005389 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005391 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005392 if (tracer)
5393 ptsid = task_sid(tracer);
5394 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
David Howellsd84f4f92008-11-14 10:39:23 +11005396 if (tracer) {
5397 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5398 PROCESS__PTRACE, NULL);
5399 if (error)
5400 goto abort_change;
5401 }
5402
5403 tsec->sid = sid;
5404 } else {
5405 error = -EINVAL;
5406 goto abort_change;
5407 }
5408
5409 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005411
5412abort_change:
5413 abort_creds(new);
5414 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415}
5416
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005417static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5418{
5419 return security_sid_to_context(secid, secdata, seclen);
5420}
5421
David Howells7bf570d2008-04-29 20:52:51 +01005422static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005423{
5424 return security_context_to_sid(secdata, seclen, secid);
5425}
5426
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005427static void selinux_release_secctx(char *secdata, u32 seclen)
5428{
Paul Moore088999e2007-08-01 11:12:58 -04005429 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005430}
5431
David P. Quigley1ee65e32009-09-03 14:25:57 -04005432/*
5433 * called with inode->i_mutex locked
5434 */
5435static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5436{
5437 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5438}
5439
5440/*
5441 * called with inode->i_mutex locked
5442 */
5443static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5444{
5445 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5446}
5447
5448static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5449{
5450 int len = 0;
5451 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5452 ctx, true);
5453 if (len < 0)
5454 return len;
5455 *ctxlen = len;
5456 return 0;
5457}
Michael LeMayd7200242006-06-22 14:47:17 -07005458#ifdef CONFIG_KEYS
5459
David Howellsd84f4f92008-11-14 10:39:23 +11005460static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005461 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005462{
David Howellsd84f4f92008-11-14 10:39:23 +11005463 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005464 struct key_security_struct *ksec;
5465
5466 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5467 if (!ksec)
5468 return -ENOMEM;
5469
David Howellsd84f4f92008-11-14 10:39:23 +11005470 tsec = cred->security;
5471 if (tsec->keycreate_sid)
5472 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005473 else
David Howellsd84f4f92008-11-14 10:39:23 +11005474 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005475
David Howells275bb412008-11-14 10:39:19 +11005476 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005477 return 0;
5478}
5479
5480static void selinux_key_free(struct key *k)
5481{
5482 struct key_security_struct *ksec = k->security;
5483
5484 k->security = NULL;
5485 kfree(ksec);
5486}
5487
5488static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005489 const struct cred *cred,
5490 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005491{
5492 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005493 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005494 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005495
5496 /* if no specific permissions are requested, we skip the
5497 permission check. No serious, additional covert channels
5498 appear to be created. */
5499 if (perm == 0)
5500 return 0;
5501
David Howellsd84f4f92008-11-14 10:39:23 +11005502 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005503
5504 key = key_ref_to_ptr(key_ref);
5505 ksec = key->security;
5506
5507 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005508}
5509
David Howells70a5bb72008-04-29 01:01:26 -07005510static int selinux_key_getsecurity(struct key *key, char **_buffer)
5511{
5512 struct key_security_struct *ksec = key->security;
5513 char *context = NULL;
5514 unsigned len;
5515 int rc;
5516
5517 rc = security_sid_to_context(ksec->sid, &context, &len);
5518 if (!rc)
5519 rc = len;
5520 *_buffer = context;
5521 return rc;
5522}
5523
Michael LeMayd7200242006-06-22 14:47:17 -07005524#endif
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005527 .name = "selinux",
5528
Ingo Molnar9e488582009-05-07 19:26:19 +10005529 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005530 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005532 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 .capable = selinux_capable,
5534 .quotactl = selinux_quotactl,
5535 .quota_on = selinux_quota_on,
5536 .syslog = selinux_syslog,
5537 .vm_enough_memory = selinux_vm_enough_memory,
5538
5539 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
David Howellsa6f76f22008-11-14 10:39:24 +11005541 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005542 .bprm_committing_creds = selinux_bprm_committing_creds,
5543 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 .bprm_secureexec = selinux_bprm_secureexec,
5545
5546 .sb_alloc_security = selinux_sb_alloc_security,
5547 .sb_free_security = selinux_sb_free_security,
5548 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005549 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005550 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005551 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 .sb_statfs = selinux_sb_statfs,
5553 .sb_mount = selinux_mount,
5554 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005555 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005556 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005557 .sb_parse_opts_str = selinux_parse_opts_str,
5558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 .inode_alloc_security = selinux_inode_alloc_security,
5561 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005562 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 .inode_unlink = selinux_inode_unlink,
5566 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 .inode_rmdir = selinux_inode_rmdir,
5569 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 .inode_readlink = selinux_inode_readlink,
5572 .inode_follow_link = selinux_inode_follow_link,
5573 .inode_permission = selinux_inode_permission,
5574 .inode_setattr = selinux_inode_setattr,
5575 .inode_getattr = selinux_inode_getattr,
5576 .inode_setxattr = selinux_inode_setxattr,
5577 .inode_post_setxattr = selinux_inode_post_setxattr,
5578 .inode_getxattr = selinux_inode_getxattr,
5579 .inode_listxattr = selinux_inode_listxattr,
5580 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005581 .inode_getsecurity = selinux_inode_getsecurity,
5582 .inode_setsecurity = selinux_inode_setsecurity,
5583 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005584 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 .file_permission = selinux_file_permission,
5587 .file_alloc_security = selinux_file_alloc_security,
5588 .file_free_security = selinux_file_free_security,
5589 .file_ioctl = selinux_file_ioctl,
Al Viroe5467852012-05-30 13:30:51 -04005590 .mmap_file = selinux_mmap_file,
5591 .mmap_addr = selinux_mmap_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 .file_mprotect = selinux_file_mprotect,
5593 .file_lock = selinux_file_lock,
5594 .file_fcntl = selinux_file_fcntl,
5595 .file_set_fowner = selinux_file_set_fowner,
5596 .file_send_sigiotask = selinux_file_send_sigiotask,
5597 .file_receive = selinux_file_receive,
5598
Eric Paris83d49852012-04-04 13:45:40 -04005599 .file_open = selinux_file_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005600
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005602 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005603 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005604 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005605 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005606 .kernel_act_as = selinux_kernel_act_as,
5607 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005608 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 .task_setpgid = selinux_task_setpgid,
5610 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005611 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005612 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005614 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005615 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 .task_setrlimit = selinux_task_setrlimit,
5617 .task_setscheduler = selinux_task_setscheduler,
5618 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005619 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 .task_kill = selinux_task_kill,
5621 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005622 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005625 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5628 .msg_msg_free_security = selinux_msg_msg_free_security,
5629
5630 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5631 .msg_queue_free_security = selinux_msg_queue_free_security,
5632 .msg_queue_associate = selinux_msg_queue_associate,
5633 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5634 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5635 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5636
5637 .shm_alloc_security = selinux_shm_alloc_security,
5638 .shm_free_security = selinux_shm_free_security,
5639 .shm_associate = selinux_shm_associate,
5640 .shm_shmctl = selinux_shm_shmctl,
5641 .shm_shmat = selinux_shm_shmat,
5642
Eric Paris828dfe12008-04-17 13:17:49 -04005643 .sem_alloc_security = selinux_sem_alloc_security,
5644 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 .sem_associate = selinux_sem_associate,
5646 .sem_semctl = selinux_sem_semctl,
5647 .sem_semop = selinux_sem_semop,
5648
Eric Paris828dfe12008-04-17 13:17:49 -04005649 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Eric Paris828dfe12008-04-17 13:17:49 -04005651 .getprocattr = selinux_getprocattr,
5652 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005654 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005655 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005656 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005657 .inode_notifysecctx = selinux_inode_notifysecctx,
5658 .inode_setsecctx = selinux_inode_setsecctx,
5659 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005660
Eric Paris828dfe12008-04-17 13:17:49 -04005661 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 .unix_may_send = selinux_socket_unix_may_send,
5663
5664 .socket_create = selinux_socket_create,
5665 .socket_post_create = selinux_socket_post_create,
5666 .socket_bind = selinux_socket_bind,
5667 .socket_connect = selinux_socket_connect,
5668 .socket_listen = selinux_socket_listen,
5669 .socket_accept = selinux_socket_accept,
5670 .socket_sendmsg = selinux_socket_sendmsg,
5671 .socket_recvmsg = selinux_socket_recvmsg,
5672 .socket_getsockname = selinux_socket_getsockname,
5673 .socket_getpeername = selinux_socket_getpeername,
5674 .socket_getsockopt = selinux_socket_getsockopt,
5675 .socket_setsockopt = selinux_socket_setsockopt,
5676 .socket_shutdown = selinux_socket_shutdown,
5677 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005678 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5679 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 .sk_alloc_security = selinux_sk_alloc_security,
5681 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005682 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005683 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005684 .sock_graft = selinux_sock_graft,
5685 .inet_conn_request = selinux_inet_conn_request,
5686 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005687 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005688 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5689 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5690 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005691 .req_classify_flow = selinux_req_classify_flow,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005692 .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5693 .tun_dev_free_security = selinux_tun_dev_free_security,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005694 .tun_dev_create = selinux_tun_dev_create,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005695 .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005696 .tun_dev_attach = selinux_tun_dev_attach,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005697 .tun_dev_open = selinux_tun_dev_open,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005698
5699#ifdef CONFIG_SECURITY_NETWORK_XFRM
5700 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5701 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5702 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005703 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005704 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5705 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005706 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005707 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005708 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005709 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005711
5712#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005713 .key_alloc = selinux_key_alloc,
5714 .key_free = selinux_key_free,
5715 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005716 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005717#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005718
5719#ifdef CONFIG_AUDIT
5720 .audit_rule_init = selinux_audit_rule_init,
5721 .audit_rule_known = selinux_audit_rule_known,
5722 .audit_rule_match = selinux_audit_rule_match,
5723 .audit_rule_free = selinux_audit_rule_free,
5724#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725};
5726
5727static __init int selinux_init(void)
5728{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005729 if (!security_module_enable(&selinux_ops)) {
5730 selinux_enabled = 0;
5731 return 0;
5732 }
5733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 if (!selinux_enabled) {
5735 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5736 return 0;
5737 }
5738
5739 printk(KERN_INFO "SELinux: Initializing.\n");
5740
5741 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005742 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005744 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5745
James Morris7cae7e22006-03-22 00:09:22 -08005746 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5747 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005748 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 avc_init();
5750
Eric Paris828dfe12008-04-17 13:17:49 -04005751 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 panic("SELinux: Unable to register with kernel.\n");
5753
Eric Paris828dfe12008-04-17 13:17:49 -04005754 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005755 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005756 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005757 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005758
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 return 0;
5760}
5761
Al Viroe8c26252010-03-23 06:36:54 -04005762static void delayed_superblock_init(struct super_block *sb, void *unused)
5763{
5764 superblock_doinit(sb, NULL);
5765}
5766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767void selinux_complete_init(void)
5768{
Eric Parisfadcdb42007-02-22 18:11:31 -05005769 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
5771 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005772 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005773 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774}
5775
5776/* SELinux requires early initialization in order to label
5777 all processes and objects when they are created. */
5778security_initcall(selinux_init);
5779
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005780#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
Paul Mooreeffad8d2008-01-29 08:49:27 -05005782static struct nf_hook_ops selinux_ipv4_ops[] = {
5783 {
5784 .hook = selinux_ipv4_postroute,
5785 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005786 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005787 .hooknum = NF_INET_POST_ROUTING,
5788 .priority = NF_IP_PRI_SELINUX_LAST,
5789 },
5790 {
5791 .hook = selinux_ipv4_forward,
5792 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005793 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005794 .hooknum = NF_INET_FORWARD,
5795 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005796 },
5797 {
5798 .hook = selinux_ipv4_output,
5799 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005800 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04005801 .hooknum = NF_INET_LOCAL_OUT,
5802 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804};
5805
5806#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5807
Paul Mooreeffad8d2008-01-29 08:49:27 -05005808static struct nf_hook_ops selinux_ipv6_ops[] = {
5809 {
5810 .hook = selinux_ipv6_postroute,
5811 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005812 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005813 .hooknum = NF_INET_POST_ROUTING,
5814 .priority = NF_IP6_PRI_SELINUX_LAST,
5815 },
5816 {
5817 .hook = selinux_ipv6_forward,
5818 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005819 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005820 .hooknum = NF_INET_FORWARD,
5821 .priority = NF_IP6_PRI_SELINUX_FIRST,
5822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823};
5824
5825#endif /* IPV6 */
5826
5827static int __init selinux_nf_ip_init(void)
5828{
5829 int err = 0;
5830
5831 if (!selinux_enabled)
5832 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05005833
5834 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
5835
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005836 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
5837 if (err)
5838 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
5840#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005841 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
5842 if (err)
5843 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005845
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846out:
5847 return err;
5848}
5849
5850__initcall(selinux_nf_ip_init);
5851
5852#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5853static void selinux_nf_ip_exit(void)
5854{
Eric Parisfadcdb42007-02-22 18:11:31 -05005855 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005857 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005859 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860#endif /* IPV6 */
5861}
5862#endif
5863
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005864#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5867#define selinux_nf_ip_exit()
5868#endif
5869
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005870#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04005873static int selinux_disabled;
5874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875int selinux_disable(void)
5876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 if (ss_initialized) {
5878 /* Not permitted after initial policy load. */
5879 return -EINVAL;
5880 }
5881
5882 if (selinux_disabled) {
5883 /* Only do this once. */
5884 return -EINVAL;
5885 }
5886
5887 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
5888
5889 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04005890 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08005892 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
Eric Parisaf8ff042009-09-20 21:23:01 -04005894 /* Try to destroy the avc node cache */
5895 avc_disable();
5896
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 /* Unregister netfilter hooks. */
5898 selinux_nf_ip_exit();
5899
5900 /* Unregister selinuxfs. */
5901 exit_sel_fs();
5902
5903 return 0;
5904}
5905#endif