blob: 7bb8073767fa111bb29bfe570ee7dffa86184fb8 [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 Sharma60063492011-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. Hallyn34867402011-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>
Amir Samuelov6a22e462014-05-26 11:44:06 +030085#include <linux/pft.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include "avc.h"
88#include "objsec.h"
89#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050090#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040091#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080092#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050093#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020094#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100095#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
David P. Quigley11689d42009-01-16 09:22:03 -050097#define NUM_SEL_MNT_OPTS 5
Eric Parisc9180a52007-11-30 13:00:35 -050098
James Morris20510f22007-10-16 23:31:32 -070099extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Paul Moored621d352008-01-29 08:43:36 -0500101/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000102static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400105int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int __init enforcing_setup(char *str)
108{
Eric Parisf5269712008-05-14 11:27:45 -0400109 unsigned long enforcing;
110 if (!strict_strtoul(str, 0, &enforcing))
111 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 1;
113}
114__setup("enforcing=", enforcing_setup);
115#endif
116
117#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
118int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
119
120static int __init selinux_enabled_setup(char *str)
121{
Eric Parisf5269712008-05-14 11:27:45 -0400122 unsigned long enabled;
123 if (!strict_strtoul(str, 0, &enabled))
124 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 1;
126}
127__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400128#else
129int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
131
Christoph Lametere18b8902006-12-06 20:33:20 -0800132static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800133
Paul Moored621d352008-01-29 08:43:36 -0500134/**
135 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
136 *
137 * Description:
138 * This function checks the SECMARK reference counter to see if any SECMARK
139 * targets are currently configured, if the reference counter is greater than
140 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
141 * enabled, false (0) if SECMARK is disabled.
142 *
143 */
144static int selinux_secmark_enabled(void)
145{
146 return (atomic_read(&selinux_secmark_refcount) > 0);
147}
148
David Howellsd84f4f92008-11-14 10:39:23 +1100149/*
150 * initialise the security for the init task
151 */
152static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Howells3b11a1d2008-11-14 10:39:26 +1100154 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct task_security_struct *tsec;
156
James Morris89d155e2005-10-30 14:59:21 -0800157 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100159 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsd84f4f92008-11-14 10:39:23 +1100161 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100162 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
David Howells275bb412008-11-14 10:39:19 +1100165/*
David Howells88e67f32008-11-14 10:39:21 +1100166 * get the security ID of a set of credentials
167 */
168static inline u32 cred_sid(const struct cred *cred)
169{
170 const struct task_security_struct *tsec;
171
172 tsec = cred->security;
173 return tsec->sid;
174}
175
176/*
David Howells3b11a1d2008-11-14 10:39:26 +1100177 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100178 */
179static inline u32 task_sid(const struct task_struct *task)
180{
David Howells275bb412008-11-14 10:39:19 +1100181 u32 sid;
182
183 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100184 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100185 rcu_read_unlock();
186 return sid;
187}
188
189/*
David Howells3b11a1d2008-11-14 10:39:26 +1100190 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100191 */
192static inline u32 current_sid(void)
193{
Paul Moore5fb49872010-04-22 14:46:19 -0400194 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100195
196 return tsec->sid;
197}
198
David Howells88e67f32008-11-14 10:39:21 +1100199/* Allocate and free functions for each kind of security blob. */
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int inode_alloc_security(struct inode *inode)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100204 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Josef Bacika02fe132008-04-04 09:35:05 +1100206 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!isec)
208 return -ENOMEM;
209
Eric Paris23970742006-09-25 23:32:01 -0700210 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 isec->inode = inode;
213 isec->sid = SECINITSID_UNLABELED;
214 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100215 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 inode->i_security = isec;
217
218 return 0;
219}
220
221static void inode_free_security(struct inode *inode)
222{
223 struct inode_security_struct *isec = inode->i_security;
224 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spin_lock(&sbsec->isec_lock);
227 if (!list_empty(&isec->list))
228 list_del_init(&isec->list);
229 spin_unlock(&sbsec->isec_lock);
230
231 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800232 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static int file_alloc_security(struct file *file)
236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100238 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800240 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!fsec)
242 return -ENOMEM;
243
David Howells275bb412008-11-14 10:39:19 +1100244 fsec->sid = sid;
245 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 file->f_security = fsec;
247
248 return 0;
249}
250
251static void file_free_security(struct file *file)
252{
253 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 file->f_security = NULL;
255 kfree(fsec);
256}
257
258static int superblock_alloc_security(struct super_block *sb)
259{
260 struct superblock_security_struct *sbsec;
261
James Morris89d155e2005-10-30 14:59:21 -0800262 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!sbsec)
264 return -ENOMEM;
265
Eric Parisbc7e9822006-09-25 23:32:02 -0700266 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 INIT_LIST_HEAD(&sbsec->isec_head);
268 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 sbsec->sb = sb;
270 sbsec->sid = SECINITSID_UNLABELED;
271 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700272 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 sb->s_security = sbsec;
274
275 return 0;
276}
277
278static void superblock_free_security(struct super_block *sb)
279{
280 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sb->s_security = NULL;
282 kfree(sbsec);
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* The file system's label must be initialized prior to use. */
286
Stephen Hemminger634a5392010-03-04 21:59:03 -0800287static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "uses xattr",
289 "uses transition SIDs",
290 "uses task SIDs",
291 "uses genfs_contexts",
292 "not configured for labeling",
293 "uses mountpoint labeling",
294};
295
296static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
297
298static inline int inode_doinit(struct inode *inode)
299{
300 return inode_doinit_with_dentry(inode, NULL);
301}
302
303enum {
Eric Paris31e87932007-09-19 17:19:12 -0400304 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 Opt_context = 1,
306 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500307 Opt_defcontext = 3,
308 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500309 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Steven Whitehousea447c092008-10-13 10:46:57 +0100312static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400313 {Opt_context, CONTEXT_STR "%s"},
314 {Opt_fscontext, FSCONTEXT_STR "%s"},
315 {Opt_defcontext, DEFCONTEXT_STR "%s"},
316 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500317 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400318 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
321#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
322
Eric Parisc312feb2006-07-10 04:43:53 -0700323static int may_context_mount_sb_relabel(u32 sid,
324 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100325 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700326{
David Howells275bb412008-11-14 10:39:19 +1100327 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700328 int rc;
329
330 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
331 FILESYSTEM__RELABELFROM, NULL);
332 if (rc)
333 return rc;
334
335 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
336 FILESYSTEM__RELABELTO, NULL);
337 return rc;
338}
339
Eric Paris08089252006-07-10 04:43:55 -0700340static int may_context_mount_inode_relabel(u32 sid,
341 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100342 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700343{
David Howells275bb412008-11-14 10:39:19 +1100344 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700345 int rc;
346 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
347 FILESYSTEM__RELABELFROM, NULL);
348 if (rc)
349 return rc;
350
351 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
352 FILESYSTEM__ASSOCIATE, NULL);
353 return rc;
354}
355
Eric Parisc9180a52007-11-30 13:00:35 -0500356static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct superblock_security_struct *sbsec = sb->s_security;
359 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500360 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int rc = 0;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
364 /* Make sure that the xattr handler exists and that no
365 error other than -ENODATA is returned by getxattr on
366 the root directory. -ENODATA is ok, as this may be
367 the first boot of the SELinux kernel before we have
368 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500369 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
371 "xattr support\n", sb->s_id, sb->s_type->name);
372 rc = -EOPNOTSUPP;
373 goto out;
374 }
Eric Parisc9180a52007-11-30 13:00:35 -0500375 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (rc < 0 && rc != -ENODATA) {
377 if (rc == -EOPNOTSUPP)
378 printk(KERN_WARNING "SELinux: (dev %s, type "
379 "%s) has no security xattr handler\n",
380 sb->s_id, sb->s_type->name);
381 else
382 printk(KERN_WARNING "SELinux: (dev %s, type "
383 "%s) getxattr errno %d\n", sb->s_id,
384 sb->s_type->name, -rc);
385 goto out;
386 }
387 }
388
David P. Quigley11689d42009-01-16 09:22:03 -0500389 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Eric Parisc9180a52007-11-30 13:00:35 -0500391 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500392 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500394 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500395 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sb->s_id, sb->s_type->name,
397 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David P. Quigley11689d42009-01-16 09:22:03 -0500399 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
400 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
401 sbsec->behavior == SECURITY_FS_USE_NONE ||
402 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
403 sbsec->flags &= ~SE_SBLABELSUPP;
404
Mark Salyzyn2c088052015-01-07 09:27:15 -0800405 /* Special handling. Is genfs but also has in-core setxattr handler*/
406 if (!strcmp(sb->s_type->name, "sysfs") ||
407 !strcmp(sb->s_type->name, "pstore") ||
408 !strcmp(sb->s_type->name, "debugfs") ||
409 !strcmp(sb->s_type->name, "rootfs"))
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400410 sbsec->flags |= SE_SBLABELSUPP;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500413 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Initialize any other inodes associated with the superblock, e.g.
416 inodes created prior to initial policy load or inodes created
417 during get_sb by a pseudo filesystem that directly
418 populates itself. */
419 spin_lock(&sbsec->isec_lock);
420next_inode:
421 if (!list_empty(&sbsec->isec_head)) {
422 struct inode_security_struct *isec =
423 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500424 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct inode *inode = isec->inode;
426 spin_unlock(&sbsec->isec_lock);
427 inode = igrab(inode);
428 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500429 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 inode_doinit(inode);
431 iput(inode);
432 }
433 spin_lock(&sbsec->isec_lock);
434 list_del_init(&isec->list);
435 goto next_inode;
436 }
437 spin_unlock(&sbsec->isec_lock);
438out:
Eric Parisc9180a52007-11-30 13:00:35 -0500439 return rc;
440}
441
442/*
443 * This function should allow an FS to ask what it's mount security
444 * options were so it can use those later for submounts, displaying
445 * mount options, or whatever.
446 */
447static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500448 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500449{
450 int rc = 0, i;
451 struct superblock_security_struct *sbsec = sb->s_security;
452 char *context = NULL;
453 u32 len;
454 char tmp;
455
Eric Parise0007522008-03-05 10:31:54 -0500456 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500457
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500458 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500459 return -EINVAL;
460
461 if (!ss_initialized)
462 return -EINVAL;
463
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500464 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500465 /* count the number of mount options for this sb */
466 for (i = 0; i < 8; i++) {
467 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500468 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500469 tmp >>= 1;
470 }
David P. Quigley11689d42009-01-16 09:22:03 -0500471 /* Check if the Label support flag is set */
472 if (sbsec->flags & SE_SBLABELSUPP)
473 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500474
Eric Parise0007522008-03-05 10:31:54 -0500475 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
476 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500477 rc = -ENOMEM;
478 goto out_free;
479 }
480
Eric Parise0007522008-03-05 10:31:54 -0500481 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
482 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500483 rc = -ENOMEM;
484 goto out_free;
485 }
486
487 i = 0;
488 if (sbsec->flags & FSCONTEXT_MNT) {
489 rc = security_sid_to_context(sbsec->sid, &context, &len);
490 if (rc)
491 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500492 opts->mnt_opts[i] = context;
493 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500494 }
495 if (sbsec->flags & CONTEXT_MNT) {
496 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
497 if (rc)
498 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500499 opts->mnt_opts[i] = context;
500 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500501 }
502 if (sbsec->flags & DEFCONTEXT_MNT) {
503 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
504 if (rc)
505 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500506 opts->mnt_opts[i] = context;
507 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500508 }
509 if (sbsec->flags & ROOTCONTEXT_MNT) {
510 struct inode *root = sbsec->sb->s_root->d_inode;
511 struct inode_security_struct *isec = root->i_security;
512
513 rc = security_sid_to_context(isec->sid, &context, &len);
514 if (rc)
515 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500516 opts->mnt_opts[i] = context;
517 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500518 }
David P. Quigley11689d42009-01-16 09:22:03 -0500519 if (sbsec->flags & SE_SBLABELSUPP) {
520 opts->mnt_opts[i] = NULL;
521 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
522 }
Eric Parisc9180a52007-11-30 13:00:35 -0500523
Eric Parise0007522008-03-05 10:31:54 -0500524 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500525
526 return 0;
527
528out_free:
Eric Parise0007522008-03-05 10:31:54 -0500529 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500530 return rc;
531}
532
533static int bad_option(struct superblock_security_struct *sbsec, char flag,
534 u32 old_sid, u32 new_sid)
535{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500536 char mnt_flags = sbsec->flags & SE_MNTMASK;
537
Eric Parisc9180a52007-11-30 13:00:35 -0500538 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500539 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500540 if (!(sbsec->flags & flag) ||
541 (old_sid != new_sid))
542 return 1;
543
544 /* check if we were passed the same options twice,
545 * aka someone passed context=a,context=b
546 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500547 if (!(sbsec->flags & SE_SBINITIALIZED))
548 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500549 return 1;
550 return 0;
551}
Eric Parise0007522008-03-05 10:31:54 -0500552
Eric Parisc9180a52007-11-30 13:00:35 -0500553/*
554 * Allow filesystems with binary mount data to explicitly set mount point
555 * labeling information.
556 */
Eric Parise0007522008-03-05 10:31:54 -0500557static int selinux_set_mnt_opts(struct super_block *sb,
558 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500559{
David Howells275bb412008-11-14 10:39:19 +1100560 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500561 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500562 struct superblock_security_struct *sbsec = sb->s_security;
563 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000564 struct inode *inode = sbsec->sb->s_root->d_inode;
565 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500566 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
567 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500568 char **mount_options = opts->mnt_opts;
569 int *flags = opts->mnt_opts_flags;
570 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500571
572 mutex_lock(&sbsec->lock);
573
574 if (!ss_initialized) {
575 if (!num_opts) {
576 /* Defer initialization until selinux_complete_init,
577 after the initial policy is loaded and the security
578 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500579 goto out;
580 }
581 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400582 printk(KERN_WARNING "SELinux: Unable to set superblock options "
583 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500584 goto out;
585 }
586
587 /*
Eric Parise0007522008-03-05 10:31:54 -0500588 * Binary mount data FS will come through this function twice. Once
589 * from an explicit call and once from the generic calls from the vfs.
590 * Since the generic VFS calls will not contain any security mount data
591 * we need to skip the double mount verification.
592 *
593 * This does open a hole in which we will not notice if the first
594 * mount using this sb set explict options and a second mount using
595 * this sb does not set any security options. (The first options
596 * will be used for both mounts)
597 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500598 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500599 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400600 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500601
602 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500603 * parse the mount options, check if they are valid sids.
604 * also check if someone is trying to mount the same sb more
605 * than once with different security options.
606 */
607 for (i = 0; i < num_opts; i++) {
608 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500609
610 if (flags[i] == SE_SBLABELSUPP)
611 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500612 rc = security_context_to_sid(mount_options[i],
613 strlen(mount_options[i]), &sid);
614 if (rc) {
615 printk(KERN_WARNING "SELinux: security_context_to_sid"
616 "(%s) failed for (dev %s, type %s) errno=%d\n",
617 mount_options[i], sb->s_id, name, rc);
618 goto out;
619 }
620 switch (flags[i]) {
621 case FSCONTEXT_MNT:
622 fscontext_sid = sid;
623
624 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
625 fscontext_sid))
626 goto out_double_mount;
627
628 sbsec->flags |= FSCONTEXT_MNT;
629 break;
630 case CONTEXT_MNT:
631 context_sid = sid;
632
633 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
634 context_sid))
635 goto out_double_mount;
636
637 sbsec->flags |= CONTEXT_MNT;
638 break;
639 case ROOTCONTEXT_MNT:
640 rootcontext_sid = sid;
641
642 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
643 rootcontext_sid))
644 goto out_double_mount;
645
646 sbsec->flags |= ROOTCONTEXT_MNT;
647
648 break;
649 case DEFCONTEXT_MNT:
650 defcontext_sid = sid;
651
652 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
653 defcontext_sid))
654 goto out_double_mount;
655
656 sbsec->flags |= DEFCONTEXT_MNT;
657
658 break;
659 default:
660 rc = -EINVAL;
661 goto out;
662 }
663 }
664
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500665 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500666 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500667 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500668 goto out_double_mount;
669 rc = 0;
670 goto out;
671 }
672
James Morris089be432008-07-15 18:32:49 +1000673 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500674 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500675
676 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500677 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500678 if (rc) {
679 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000680 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500681 goto out;
682 }
683
684 /* sets the context of the superblock for the fs being mounted. */
685 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100686 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500687 if (rc)
688 goto out;
689
690 sbsec->sid = fscontext_sid;
691 }
692
693 /*
694 * Switch to using mount point labeling behavior.
695 * sets the label used on all file below the mountpoint, and will set
696 * the superblock context if not already set.
697 */
698 if (context_sid) {
699 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100700 rc = may_context_mount_sb_relabel(context_sid, sbsec,
701 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500702 if (rc)
703 goto out;
704 sbsec->sid = context_sid;
705 } else {
David Howells275bb412008-11-14 10:39:19 +1100706 rc = may_context_mount_inode_relabel(context_sid, sbsec,
707 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500708 if (rc)
709 goto out;
710 }
711 if (!rootcontext_sid)
712 rootcontext_sid = context_sid;
713
714 sbsec->mntpoint_sid = context_sid;
715 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
716 }
717
718 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100719 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
720 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500721 if (rc)
722 goto out;
723
724 root_isec->sid = rootcontext_sid;
725 root_isec->initialized = 1;
726 }
727
728 if (defcontext_sid) {
729 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
730 rc = -EINVAL;
731 printk(KERN_WARNING "SELinux: defcontext option is "
732 "invalid for this filesystem type\n");
733 goto out;
734 }
735
736 if (defcontext_sid != sbsec->def_sid) {
737 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100738 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500739 if (rc)
740 goto out;
741 }
742
743 sbsec->def_sid = defcontext_sid;
744 }
745
746 rc = sb_finish_set_opts(sb);
747out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700748 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500750out_double_mount:
751 rc = -EINVAL;
752 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
753 "security settings for (dev %s, type %s)\n", sb->s_id, name);
754 goto out;
755}
756
757static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
758 struct super_block *newsb)
759{
760 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
761 struct superblock_security_struct *newsbsec = newsb->s_security;
762
763 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
764 int set_context = (oldsbsec->flags & CONTEXT_MNT);
765 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
766
Eric Paris0f5e6422008-04-21 16:24:11 -0400767 /*
768 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400769 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400770 */
Al Viroe8c26252010-03-23 06:36:54 -0400771 if (!ss_initialized)
Eric Paris0f5e6422008-04-21 16:24:11 -0400772 return;
Eric Parisc9180a52007-11-30 13:00:35 -0500773
Eric Parisc9180a52007-11-30 13:00:35 -0500774 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500775 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500776
Eric Paris5a552612008-04-09 14:08:35 -0400777 /* if fs is reusing a sb, just let its options stand... */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500778 if (newsbsec->flags & SE_SBINITIALIZED)
Eric Paris5a552612008-04-09 14:08:35 -0400779 return;
780
Eric Parisc9180a52007-11-30 13:00:35 -0500781 mutex_lock(&newsbsec->lock);
782
783 newsbsec->flags = oldsbsec->flags;
784
785 newsbsec->sid = oldsbsec->sid;
786 newsbsec->def_sid = oldsbsec->def_sid;
787 newsbsec->behavior = oldsbsec->behavior;
788
789 if (set_context) {
790 u32 sid = oldsbsec->mntpoint_sid;
791
792 if (!set_fscontext)
793 newsbsec->sid = sid;
794 if (!set_rootcontext) {
795 struct inode *newinode = newsb->s_root->d_inode;
796 struct inode_security_struct *newisec = newinode->i_security;
797 newisec->sid = sid;
798 }
799 newsbsec->mntpoint_sid = sid;
800 }
801 if (set_rootcontext) {
802 const struct inode *oldinode = oldsb->s_root->d_inode;
803 const struct inode_security_struct *oldisec = oldinode->i_security;
804 struct inode *newinode = newsb->s_root->d_inode;
805 struct inode_security_struct *newisec = newinode->i_security;
806
807 newisec->sid = oldisec->sid;
808 }
809
810 sb_finish_set_opts(newsb);
811 mutex_unlock(&newsbsec->lock);
812}
813
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200814static int selinux_parse_opts_str(char *options,
815 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500816{
Eric Parise0007522008-03-05 10:31:54 -0500817 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500818 char *context = NULL, *defcontext = NULL;
819 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500820 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500821
Eric Parise0007522008-03-05 10:31:54 -0500822 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500823
824 /* Standard string-based options. */
825 while ((p = strsep(&options, "|")) != NULL) {
826 int token;
827 substring_t args[MAX_OPT_ARGS];
828
829 if (!*p)
830 continue;
831
832 token = match_token(p, tokens, args);
833
834 switch (token) {
835 case Opt_context:
836 if (context || defcontext) {
837 rc = -EINVAL;
838 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
839 goto out_err;
840 }
841 context = match_strdup(&args[0]);
842 if (!context) {
843 rc = -ENOMEM;
844 goto out_err;
845 }
846 break;
847
848 case Opt_fscontext:
849 if (fscontext) {
850 rc = -EINVAL;
851 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
852 goto out_err;
853 }
854 fscontext = match_strdup(&args[0]);
855 if (!fscontext) {
856 rc = -ENOMEM;
857 goto out_err;
858 }
859 break;
860
861 case Opt_rootcontext:
862 if (rootcontext) {
863 rc = -EINVAL;
864 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
865 goto out_err;
866 }
867 rootcontext = match_strdup(&args[0]);
868 if (!rootcontext) {
869 rc = -ENOMEM;
870 goto out_err;
871 }
872 break;
873
874 case Opt_defcontext:
875 if (context || defcontext) {
876 rc = -EINVAL;
877 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
878 goto out_err;
879 }
880 defcontext = match_strdup(&args[0]);
881 if (!defcontext) {
882 rc = -ENOMEM;
883 goto out_err;
884 }
885 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500886 case Opt_labelsupport:
887 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500888 default:
889 rc = -EINVAL;
890 printk(KERN_WARNING "SELinux: unknown mount option\n");
891 goto out_err;
892
893 }
894 }
895
Eric Parise0007522008-03-05 10:31:54 -0500896 rc = -ENOMEM;
897 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
898 if (!opts->mnt_opts)
899 goto out_err;
900
901 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
902 if (!opts->mnt_opts_flags) {
903 kfree(opts->mnt_opts);
904 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500905 }
906
Eric Parise0007522008-03-05 10:31:54 -0500907 if (fscontext) {
908 opts->mnt_opts[num_mnt_opts] = fscontext;
909 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
910 }
911 if (context) {
912 opts->mnt_opts[num_mnt_opts] = context;
913 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
914 }
915 if (rootcontext) {
916 opts->mnt_opts[num_mnt_opts] = rootcontext;
917 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
918 }
919 if (defcontext) {
920 opts->mnt_opts[num_mnt_opts] = defcontext;
921 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
922 }
923
924 opts->num_mnt_opts = num_mnt_opts;
925 return 0;
926
Eric Parisc9180a52007-11-30 13:00:35 -0500927out_err:
928 kfree(context);
929 kfree(defcontext);
930 kfree(fscontext);
931 kfree(rootcontext);
932 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
Eric Parise0007522008-03-05 10:31:54 -0500934/*
935 * string mount options parsing and call set the sbsec
936 */
937static int superblock_doinit(struct super_block *sb, void *data)
938{
939 int rc = 0;
940 char *options = data;
941 struct security_mnt_opts opts;
942
943 security_init_mnt_opts(&opts);
944
945 if (!data)
946 goto out;
947
948 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
949
950 rc = selinux_parse_opts_str(options, &opts);
951 if (rc)
952 goto out_err;
953
954out:
955 rc = selinux_set_mnt_opts(sb, &opts);
956
957out_err:
958 security_free_mnt_opts(&opts);
959 return rc;
960}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Adrian Bunk3583a712008-07-22 20:21:23 +0300962static void selinux_write_opts(struct seq_file *m,
963 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000964{
965 int i;
966 char *prefix;
967
968 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500969 char *has_comma;
970
971 if (opts->mnt_opts[i])
972 has_comma = strchr(opts->mnt_opts[i], ',');
973 else
974 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +1000975
976 switch (opts->mnt_opts_flags[i]) {
977 case CONTEXT_MNT:
978 prefix = CONTEXT_STR;
979 break;
980 case FSCONTEXT_MNT:
981 prefix = FSCONTEXT_STR;
982 break;
983 case ROOTCONTEXT_MNT:
984 prefix = ROOTCONTEXT_STR;
985 break;
986 case DEFCONTEXT_MNT:
987 prefix = DEFCONTEXT_STR;
988 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500989 case SE_SBLABELSUPP:
990 seq_putc(m, ',');
991 seq_puts(m, LABELSUPP_STR);
992 continue;
Eric Paris2069f452008-07-04 09:47:13 +1000993 default:
994 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -0400995 return;
Eric Paris2069f452008-07-04 09:47:13 +1000996 };
997 /* we need a comma before each option */
998 seq_putc(m, ',');
999 seq_puts(m, prefix);
1000 if (has_comma)
1001 seq_putc(m, '\"');
1002 seq_puts(m, opts->mnt_opts[i]);
1003 if (has_comma)
1004 seq_putc(m, '\"');
1005 }
1006}
1007
1008static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1009{
1010 struct security_mnt_opts opts;
1011 int rc;
1012
1013 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001014 if (rc) {
1015 /* before policy load we may get EINVAL, don't show anything */
1016 if (rc == -EINVAL)
1017 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001018 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001019 }
Eric Paris2069f452008-07-04 09:47:13 +10001020
1021 selinux_write_opts(m, &opts);
1022
1023 security_free_mnt_opts(&opts);
1024
1025 return rc;
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static inline u16 inode_mode_to_security_class(umode_t mode)
1029{
1030 switch (mode & S_IFMT) {
1031 case S_IFSOCK:
1032 return SECCLASS_SOCK_FILE;
1033 case S_IFLNK:
1034 return SECCLASS_LNK_FILE;
1035 case S_IFREG:
1036 return SECCLASS_FILE;
1037 case S_IFBLK:
1038 return SECCLASS_BLK_FILE;
1039 case S_IFDIR:
1040 return SECCLASS_DIR;
1041 case S_IFCHR:
1042 return SECCLASS_CHR_FILE;
1043 case S_IFIFO:
1044 return SECCLASS_FIFO_FILE;
1045
1046 }
1047
1048 return SECCLASS_FILE;
1049}
1050
James Morris13402582005-09-30 14:24:34 -04001051static inline int default_protocol_stream(int protocol)
1052{
1053 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1054}
1055
1056static inline int default_protocol_dgram(int protocol)
1057{
1058 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1062{
1063 switch (family) {
1064 case PF_UNIX:
1065 switch (type) {
1066 case SOCK_STREAM:
1067 case SOCK_SEQPACKET:
1068 return SECCLASS_UNIX_STREAM_SOCKET;
1069 case SOCK_DGRAM:
1070 return SECCLASS_UNIX_DGRAM_SOCKET;
1071 }
1072 break;
1073 case PF_INET:
1074 case PF_INET6:
1075 switch (type) {
1076 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001077 if (default_protocol_stream(protocol))
1078 return SECCLASS_TCP_SOCKET;
1079 else
1080 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001082 if (default_protocol_dgram(protocol))
1083 return SECCLASS_UDP_SOCKET;
1084 else
1085 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001086 case SOCK_DCCP:
1087 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001088 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return SECCLASS_RAWIP_SOCKET;
1090 }
1091 break;
1092 case PF_NETLINK:
1093 switch (protocol) {
1094 case NETLINK_ROUTE:
1095 return SECCLASS_NETLINK_ROUTE_SOCKET;
1096 case NETLINK_FIREWALL:
1097 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001098 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1100 case NETLINK_NFLOG:
1101 return SECCLASS_NETLINK_NFLOG_SOCKET;
1102 case NETLINK_XFRM:
1103 return SECCLASS_NETLINK_XFRM_SOCKET;
1104 case NETLINK_SELINUX:
1105 return SECCLASS_NETLINK_SELINUX_SOCKET;
1106 case NETLINK_AUDIT:
1107 return SECCLASS_NETLINK_AUDIT_SOCKET;
1108 case NETLINK_IP6_FW:
1109 return SECCLASS_NETLINK_IP6FW_SOCKET;
1110 case NETLINK_DNRTMSG:
1111 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001112 case NETLINK_KOBJECT_UEVENT:
1113 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 default:
1115 return SECCLASS_NETLINK_SOCKET;
1116 }
1117 case PF_PACKET:
1118 return SECCLASS_PACKET_SOCKET;
1119 case PF_KEY:
1120 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001121 case PF_APPLETALK:
1122 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 return SECCLASS_SOCKET;
1126}
1127
1128#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001129static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 u16 tclass,
1131 u32 *sid)
1132{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001133 int rc;
1134 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Eric Paris828dfe12008-04-17 13:17:49 -04001136 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!buffer)
1138 return -ENOMEM;
1139
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001140 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1141 if (IS_ERR(path))
1142 rc = PTR_ERR(path);
1143 else {
1144 /* each process gets a /proc/PID/ entry. Strip off the
1145 * PID part to get a valid selinux labeling.
1146 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1147 while (path[1] >= '0' && path[1] <= '9') {
1148 path[1] = '/';
1149 path++;
1150 }
1151 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 free_page((unsigned long)buffer);
1154 return rc;
1155}
1156#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001157static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 u16 tclass,
1159 u32 *sid)
1160{
1161 return -EINVAL;
1162}
1163#endif
1164
1165/* The inode's security attributes must be initialized before first use. */
1166static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1167{
1168 struct superblock_security_struct *sbsec = NULL;
1169 struct inode_security_struct *isec = inode->i_security;
1170 u32 sid;
1171 struct dentry *dentry;
1172#define INITCONTEXTLEN 255
1173 char *context = NULL;
1174 unsigned len = 0;
1175 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (isec->initialized)
1178 goto out;
1179
Eric Paris23970742006-09-25 23:32:01 -07001180 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001182 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001185 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* Defer initialization until selinux_complete_init,
1187 after the initial policy is loaded and the security
1188 server is ready to handle calls. */
1189 spin_lock(&sbsec->isec_lock);
1190 if (list_empty(&isec->list))
1191 list_add(&isec->list, &sbsec->isec_head);
1192 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001193 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 switch (sbsec->behavior) {
1197 case SECURITY_FS_USE_XATTR:
1198 if (!inode->i_op->getxattr) {
1199 isec->sid = sbsec->def_sid;
1200 break;
1201 }
1202
1203 /* Need a dentry, since the xattr API requires one.
1204 Life would be simpler if we could just pass the inode. */
1205 if (opt_dentry) {
1206 /* Called from d_instantiate or d_splice_alias. */
1207 dentry = dget(opt_dentry);
1208 } else {
1209 /* Called from selinux_complete_init, try to find a dentry. */
1210 dentry = d_find_alias(inode);
1211 }
1212 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001213 /*
1214 * this is can be hit on boot when a file is accessed
1215 * before the policy is loaded. When we load policy we
1216 * may find inodes that have no dentry on the
1217 * sbsec->isec_head list. No reason to complain as these
1218 * will get fixed up the next time we go through
1219 * inode_doinit with a dentry, before these inodes could
1220 * be used again by userspace.
1221 */
Eric Paris23970742006-09-25 23:32:01 -07001222 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001226 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!context) {
1228 rc = -ENOMEM;
1229 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001230 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001232 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1234 context, len);
1235 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001236 kfree(context);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /* Need a larger buffer. Query for the right size. */
1239 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1240 NULL, 0);
1241 if (rc < 0) {
1242 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001243 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001246 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!context) {
1248 rc = -ENOMEM;
1249 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001250 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001252 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 rc = inode->i_op->getxattr(dentry,
1254 XATTR_NAME_SELINUX,
1255 context, len);
1256 }
1257 dput(dentry);
1258 if (rc < 0) {
1259 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001260 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001261 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 -rc, inode->i_sb->s_id, inode->i_ino);
1263 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001264 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 /* Map ENODATA to the default file SID */
1267 sid = sbsec->def_sid;
1268 rc = 0;
1269 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001270 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001271 sbsec->def_sid,
1272 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001274 char *dev = inode->i_sb->s_id;
1275 unsigned long ino = inode->i_ino;
1276
1277 if (rc == -EINVAL) {
1278 if (printk_ratelimit())
1279 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1280 "context=%s. This indicates you may need to relabel the inode or the "
1281 "filesystem in question.\n", ino, dev, context);
1282 } else {
1283 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1284 "returned %d for dev=%s ino=%ld\n",
1285 __func__, context, -rc, dev, ino);
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 kfree(context);
1288 /* Leave with the unlabeled SID */
1289 rc = 0;
1290 break;
1291 }
1292 }
1293 kfree(context);
1294 isec->sid = sid;
1295 break;
1296 case SECURITY_FS_USE_TASK:
1297 isec->sid = isec->task_sid;
1298 break;
1299 case SECURITY_FS_USE_TRANS:
1300 /* Default to the fs SID. */
1301 isec->sid = sbsec->sid;
1302
1303 /* Try to obtain a transition SID. */
1304 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001305 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1306 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001308 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 isec->sid = sid;
1310 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001311 case SECURITY_FS_USE_MNTPOINT:
1312 isec->sid = sbsec->mntpoint_sid;
1313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001315 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 isec->sid = sbsec->sid;
1317
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001318 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001319 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001321 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 isec->sclass,
1323 &sid);
1324 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001325 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 isec->sid = sid;
1327 }
1328 }
1329 break;
1330 }
1331
1332 isec->initialized = 1;
1333
Eric Paris23970742006-09-25 23:32:01 -07001334out_unlock:
1335 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336out:
1337 if (isec->sclass == SECCLASS_FILE)
1338 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return rc;
1340}
1341
1342/* Convert a Linux signal to an access vector. */
1343static inline u32 signal_to_av(int sig)
1344{
1345 u32 perm = 0;
1346
1347 switch (sig) {
1348 case SIGCHLD:
1349 /* Commonly granted from child to parent. */
1350 perm = PROCESS__SIGCHLD;
1351 break;
1352 case SIGKILL:
1353 /* Cannot be caught or ignored */
1354 perm = PROCESS__SIGKILL;
1355 break;
1356 case SIGSTOP:
1357 /* Cannot be caught or ignored */
1358 perm = PROCESS__SIGSTOP;
1359 break;
1360 default:
1361 /* All other signals. */
1362 perm = PROCESS__SIGNAL;
1363 break;
1364 }
1365
1366 return perm;
1367}
1368
David Howells275bb412008-11-14 10:39:19 +11001369/*
David Howellsd84f4f92008-11-14 10:39:23 +11001370 * Check permission between a pair of credentials
1371 * fork check, ptrace check, etc.
1372 */
1373static int cred_has_perm(const struct cred *actor,
1374 const struct cred *target,
1375 u32 perms)
1376{
1377 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1378
1379 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1380}
1381
1382/*
David Howells88e67f32008-11-14 10:39:21 +11001383 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001384 * fork check, ptrace check, etc.
1385 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001386 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001387 */
1388static int task_has_perm(const struct task_struct *tsk1,
1389 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 u32 perms)
1391{
David Howells275bb412008-11-14 10:39:19 +11001392 const struct task_security_struct *__tsec1, *__tsec2;
1393 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
David Howells275bb412008-11-14 10:39:19 +11001395 rcu_read_lock();
1396 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1397 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1398 rcu_read_unlock();
1399 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
David Howells3b11a1d2008-11-14 10:39:26 +11001402/*
1403 * Check permission between current and another task, e.g. signal checks,
1404 * fork check, ptrace check, etc.
1405 * current is the actor and tsk2 is the target
1406 * - this uses current's subjective creds
1407 */
1408static int current_has_perm(const struct task_struct *tsk,
1409 u32 perms)
1410{
1411 u32 sid, tsid;
1412
1413 sid = current_sid();
1414 tsid = task_sid(tsk);
1415 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1416}
1417
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001418#if CAP_LAST_CAP > 63
1419#error Fix SELinux to handle capabilities > 63.
1420#endif
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001423static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001424 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Thomas Liu2bf49692009-07-14 12:14:09 -04001426 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001427 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001428 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001429 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001430 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001431 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001432 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Thomas Liu2bf49692009-07-14 12:14:09 -04001434 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001435 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001436 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 ad.u.cap = cap;
1438
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001439 switch (CAP_TO_INDEX(cap)) {
1440 case 0:
1441 sclass = SECCLASS_CAPABILITY;
1442 break;
1443 case 1:
1444 sclass = SECCLASS_CAPABILITY2;
1445 break;
1446 default:
1447 printk(KERN_ERR
1448 "SELinux: out of range capability %d\n", cap);
1449 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001450 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001451 }
Eric Paris06112162008-11-11 22:02:50 +11001452
David Howells275bb412008-11-14 10:39:19 +11001453 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001454 if (audit == SECURITY_CAP_AUDIT) {
1455 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1456 if (rc2)
1457 return rc2;
1458 }
Eric Paris06112162008-11-11 22:02:50 +11001459 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462/* Check whether a task is allowed to use a system operation. */
1463static int task_has_system(struct task_struct *tsk,
1464 u32 perms)
1465{
David Howells275bb412008-11-14 10:39:19 +11001466 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
David Howells275bb412008-11-14 10:39:19 +11001468 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 SECCLASS_SYSTEM, perms, NULL);
1470}
1471
1472/* Check whether a task has a particular permission to an inode.
1473 The 'adp' parameter is optional and allows other audit
1474 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001475static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct inode *inode,
1477 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001478 struct common_audit_data *adp,
1479 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001482 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
David Howellse0e81732009-09-02 09:13:40 +01001484 validate_creds(cred);
1485
Eric Paris828dfe12008-04-17 13:17:49 -04001486 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001487 return 0;
1488
David Howells88e67f32008-11-14 10:39:21 +11001489 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 isec = inode->i_security;
1491
Eric Paris9ade0cf2011-04-25 16:26:29 -04001492 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001495static int inode_has_perm_noadp(const struct cred *cred,
1496 struct inode *inode,
1497 u32 perms,
1498 unsigned flags)
1499{
1500 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001501 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001502
1503 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1504 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001505 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001506 return inode_has_perm(cred, inode, perms, &ad, flags);
1507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* Same as inode_has_perm, but pass explicit audit data containing
1510 the dentry to help the auditing code to more easily generate the
1511 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001512static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct dentry *dentry,
1514 u32 av)
1515{
1516 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001517 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001518 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001519
Eric Paris2875fa02011-04-28 16:04:24 -04001520 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1521 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001522 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001523 return inode_has_perm(cred, inode, av, &ad, 0);
1524}
1525
1526/* Same as inode_has_perm, but pass explicit audit data containing
1527 the path to help the auditing code to more easily generate the
1528 pathname if needed. */
1529static inline int path_has_perm(const struct cred *cred,
1530 struct path *path,
1531 u32 av)
1532{
1533 struct inode *inode = path->dentry->d_inode;
1534 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001535 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001536
Eric Parisf48b7392011-04-25 12:54:27 -04001537 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001538 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001539 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001540 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543/* Check whether a task can use an open file descriptor to
1544 access an inode in a given way. Check access to the
1545 descriptor itself, and then use dentry_has_perm to
1546 check a particular permission to the file.
1547 Access to the descriptor is implicitly granted if it
1548 has the same SID as the process. If av is zero, then
1549 access to the file is not checked, e.g. for cases
1550 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001551static int file_has_perm(const struct cred *cred,
1552 struct file *file,
1553 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001556 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001557 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001558 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001559 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int rc;
1561
Eric Parisf48b7392011-04-25 12:54:27 -04001562 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1563 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001564 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
David Howells275bb412008-11-14 10:39:19 +11001566 if (sid != fsec->sid) {
1567 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 SECCLASS_FD,
1569 FD__USE,
1570 &ad);
1571 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
1575 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001576 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001578 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
David Howells88e67f32008-11-14 10:39:21 +11001580out:
1581 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584/* Check whether a task can create a file. */
1585static int may_create(struct inode *dir,
1586 struct dentry *dentry,
1587 u16 tclass)
1588{
Paul Moore5fb49872010-04-22 14:46:19 -04001589 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 struct inode_security_struct *dsec;
1591 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001592 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001593 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001594 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int rc;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 dsec = dir->i_security;
1598 sbsec = dir->i_sb->s_security;
1599
David Howells275bb412008-11-14 10:39:19 +11001600 sid = tsec->sid;
1601 newsid = tsec->create_sid;
1602
Eric Parisa2694342011-04-25 13:10:27 -04001603 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1604 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001605 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
David Howells275bb412008-11-14 10:39:19 +11001607 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 DIR__ADD_NAME | DIR__SEARCH,
1609 &ad);
1610 if (rc)
1611 return rc;
1612
David P. Quigleycd895962009-01-16 09:22:04 -05001613 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001614 rc = security_transition_sid(sid, dsec->sid, tclass,
1615 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (rc)
1617 return rc;
1618 }
1619
David Howells275bb412008-11-14 10:39:19 +11001620 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (rc)
1622 return rc;
1623
Amir Samuelov6a22e462014-05-26 11:44:06 +03001624 rc = avc_has_perm(newsid, sbsec->sid,
1625 SECCLASS_FILESYSTEM,
1626 FILESYSTEM__ASSOCIATE, &ad);
1627 if (rc)
1628 return rc;
1629
1630 rc = pft_inode_mknod(dir, dentry, 0, 0);
1631
1632 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Michael LeMay4eb582c2006-06-26 00:24:57 -07001635/* Check whether a task can create a key. */
1636static int may_create_key(u32 ksid,
1637 struct task_struct *ctx)
1638{
David Howells275bb412008-11-14 10:39:19 +11001639 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001640
David Howells275bb412008-11-14 10:39:19 +11001641 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001642}
1643
Eric Paris828dfe12008-04-17 13:17:49 -04001644#define MAY_LINK 0
1645#define MAY_UNLINK 1
1646#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648/* Check whether a task can link, unlink, or rmdir a file/directory. */
1649static int may_link(struct inode *dir,
1650 struct dentry *dentry,
1651 int kind)
1652
1653{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001655 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001656 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001657 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 u32 av;
1659 int rc;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 dsec = dir->i_security;
1662 isec = dentry->d_inode->i_security;
1663
Eric Parisa2694342011-04-25 13:10:27 -04001664 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1665 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001666 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 av = DIR__SEARCH;
1669 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001670 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (rc)
1672 return rc;
1673
1674 switch (kind) {
1675 case MAY_LINK:
1676 av = FILE__LINK;
1677 break;
1678 case MAY_UNLINK:
1679 av = FILE__UNLINK;
1680 break;
1681 case MAY_RMDIR:
1682 av = DIR__RMDIR;
1683 break;
1684 default:
Eric Paris744ba352008-04-17 11:52:44 -04001685 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1686 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688 }
1689
David Howells275bb412008-11-14 10:39:19 +11001690 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Amir Samuelov6a22e462014-05-26 11:44:06 +03001691 if (rc)
1692 return rc;
1693
1694 if (kind == MAY_UNLINK)
1695 rc = pft_inode_unlink(dir, dentry);
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return rc;
1698}
1699
1700static inline int may_rename(struct inode *old_dir,
1701 struct dentry *old_dentry,
1702 struct inode *new_dir,
1703 struct dentry *new_dentry)
1704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001706 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001707 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001708 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 u32 av;
1710 int old_is_dir, new_is_dir;
1711 int rc;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 old_dsec = old_dir->i_security;
1714 old_isec = old_dentry->d_inode->i_security;
1715 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1716 new_dsec = new_dir->i_security;
1717
Eric Parisa2694342011-04-25 13:10:27 -04001718 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001719 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Eric Parisa2694342011-04-25 13:10:27 -04001721 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001722 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1724 if (rc)
1725 return rc;
David Howells275bb412008-11-14 10:39:19 +11001726 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 old_isec->sclass, FILE__RENAME, &ad);
1728 if (rc)
1729 return rc;
1730 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001731 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 old_isec->sclass, DIR__REPARENT, &ad);
1733 if (rc)
1734 return rc;
1735 }
1736
Eric Parisa2694342011-04-25 13:10:27 -04001737 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 av = DIR__ADD_NAME | DIR__SEARCH;
1739 if (new_dentry->d_inode)
1740 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001741 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (rc)
1743 return rc;
1744 if (new_dentry->d_inode) {
1745 new_isec = new_dentry->d_inode->i_security;
1746 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001747 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 new_isec->sclass,
1749 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1750 if (rc)
1751 return rc;
1752 }
1753
1754 return 0;
1755}
1756
1757/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001758static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 struct super_block *sb,
1760 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001761 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001764 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001767 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
1770/* Convert a Linux mode and permission mask to an access vector. */
1771static inline u32 file_mask_to_av(int mode, int mask)
1772{
1773 u32 av = 0;
1774
Al Virodba19c62011-07-25 20:49:29 -04001775 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (mask & MAY_EXEC)
1777 av |= FILE__EXECUTE;
1778 if (mask & MAY_READ)
1779 av |= FILE__READ;
1780
1781 if (mask & MAY_APPEND)
1782 av |= FILE__APPEND;
1783 else if (mask & MAY_WRITE)
1784 av |= FILE__WRITE;
1785
1786 } else {
1787 if (mask & MAY_EXEC)
1788 av |= DIR__SEARCH;
1789 if (mask & MAY_WRITE)
1790 av |= DIR__WRITE;
1791 if (mask & MAY_READ)
1792 av |= DIR__READ;
1793 }
1794
1795 return av;
1796}
1797
1798/* Convert a Linux file to an access vector. */
1799static inline u32 file_to_av(struct file *file)
1800{
1801 u32 av = 0;
1802
1803 if (file->f_mode & FMODE_READ)
1804 av |= FILE__READ;
1805 if (file->f_mode & FMODE_WRITE) {
1806 if (file->f_flags & O_APPEND)
1807 av |= FILE__APPEND;
1808 else
1809 av |= FILE__WRITE;
1810 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001811 if (!av) {
1812 /*
1813 * Special file opened with flags 3 for ioctl-only use.
1814 */
1815 av = FILE__IOCTL;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 return av;
1819}
1820
Eric Paris8b6a5a32008-10-29 17:06:46 -04001821/*
1822 * Convert a file to an access vector and include the correct open
1823 * open permission.
1824 */
1825static inline u32 open_file_to_av(struct file *file)
1826{
1827 u32 av = file_to_av(file);
1828
Eric Paris49b7b8d2010-07-23 11:44:09 -04001829 if (selinux_policycap_openperm)
1830 av |= FILE__OPEN;
1831
Eric Paris8b6a5a32008-10-29 17:06:46 -04001832 return av;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/* Hook functions begin here. */
1836
Stephen Smalley48a23702012-11-05 08:15:34 -05001837static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1838{
1839 u32 mysid = current_sid();
1840 u32 mgrsid = task_sid(mgr);
1841
1842 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1843}
1844
1845static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1846{
1847 u32 mysid = current_sid();
1848 u32 fromsid = task_sid(from);
1849 u32 tosid = task_sid(to);
1850 int rc;
1851
1852 if (mysid != fromsid) {
1853 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1854 if (rc)
1855 return rc;
1856 }
1857
1858 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1859}
1860
1861static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1862{
1863 u32 fromsid = task_sid(from);
1864 u32 tosid = task_sid(to);
1865 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1866}
1867
1868static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1869{
1870 u32 sid = task_sid(to);
1871 struct file_security_struct *fsec = file->f_security;
1872 struct inode *inode = file->f_path.dentry->d_inode;
1873 struct inode_security_struct *isec = inode->i_security;
1874 struct common_audit_data ad;
1875 struct selinux_audit_data sad = {0,};
1876 int rc;
1877
1878 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1879 ad.u.path = file->f_path;
1880 ad.selinux_audit_data = &sad;
1881
1882 if (sid != fsec->sid) {
1883 rc = avc_has_perm(sid, fsec->sid,
1884 SECCLASS_FD,
1885 FD__USE,
1886 &ad);
1887 if (rc)
1888 return rc;
1889 }
1890
1891 if (unlikely(IS_PRIVATE(inode)))
1892 return 0;
1893
1894 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1895 &ad);
1896}
1897
Ingo Molnar9e488582009-05-07 19:26:19 +10001898static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001899 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int rc;
1902
Ingo Molnar9e488582009-05-07 19:26:19 +10001903 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (rc)
1905 return rc;
1906
Eric Paris69f594a2012-01-03 12:25:15 -05001907 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001908 u32 sid = current_sid();
1909 u32 csid = task_sid(child);
1910 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001911 }
1912
David Howells3b11a1d2008-11-14 10:39:26 +11001913 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001914}
1915
1916static int selinux_ptrace_traceme(struct task_struct *parent)
1917{
1918 int rc;
1919
Eric Paris200ac532009-02-12 15:01:04 -05001920 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001921 if (rc)
1922 return rc;
1923
1924 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001928 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int error;
1931
David Howells3b11a1d2008-11-14 10:39:26 +11001932 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (error)
1934 return error;
1935
Eric Paris200ac532009-02-12 15:01:04 -05001936 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
David Howellsd84f4f92008-11-14 10:39:23 +11001939static int selinux_capset(struct cred *new, const struct cred *old,
1940 const kernel_cap_t *effective,
1941 const kernel_cap_t *inheritable,
1942 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 int error;
1945
Eric Paris200ac532009-02-12 15:01:04 -05001946 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001947 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (error)
1949 return error;
1950
David Howellsd84f4f92008-11-14 10:39:23 +11001951 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
James Morris5626d3e2009-01-30 10:05:06 +11001954/*
1955 * (This comment used to live with the selinux_task_setuid hook,
1956 * which was removed).
1957 *
1958 * Since setuid only affects the current process, and since the SELinux
1959 * controls are not based on the Linux identity attributes, SELinux does not
1960 * need to control this operation. However, SELinux does control the use of
1961 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1962 */
1963
Eric Paris6a9de492012-01-03 12:25:14 -05001964static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1965 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 int rc;
1968
Eric Paris6a9de492012-01-03 12:25:14 -05001969 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (rc)
1971 return rc;
1972
Eric Paris6a9de492012-01-03 12:25:14 -05001973 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1977{
David Howells88e67f32008-11-14 10:39:21 +11001978 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 int rc = 0;
1980
1981 if (!sb)
1982 return 0;
1983
1984 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001985 case Q_SYNC:
1986 case Q_QUOTAON:
1987 case Q_QUOTAOFF:
1988 case Q_SETINFO:
1989 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001990 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001991 break;
1992 case Q_GETFMT:
1993 case Q_GETINFO:
1994 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001995 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001996 break;
1997 default:
1998 rc = 0; /* let the kernel handle invalid cmds */
1999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 return rc;
2002}
2003
2004static int selinux_quota_on(struct dentry *dentry)
2005{
David Howells88e67f32008-11-14 10:39:21 +11002006 const struct cred *cred = current_cred();
2007
Eric Paris2875fa02011-04-28 16:04:24 -04002008 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Eric Paris12b30522010-11-15 18:36:29 -05002011static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 int rc;
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002016 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2017 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002018 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2019 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002020 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2021 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2022 /* Set level of messages printed to console */
2023 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002024 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2025 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002026 case SYSLOG_ACTION_CLOSE: /* Close log */
2027 case SYSLOG_ACTION_OPEN: /* Open log */
2028 case SYSLOG_ACTION_READ: /* Read from log */
2029 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2030 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002031 default:
2032 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035 return rc;
2036}
2037
2038/*
2039 * Check that a process has enough memory to allocate a new virtual
2040 * mapping. 0 means there is enough memory for the allocation to
2041 * succeed and -ENOMEM implies there is not.
2042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 * Do not audit the selinux permission check, as this is applied to all
2044 * processes that allocate mappings.
2045 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002046static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Eric Paris6a9de492012-01-03 12:25:14 -05002050 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002051 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (rc == 0)
2053 cap_sys_admin = 1;
2054
Alan Cox34b4e4a2007-08-22 14:01:28 -07002055 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058/* binprm security operations */
2059
David Howellsa6f76f22008-11-14 10:39:24 +11002060static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
David Howellsa6f76f22008-11-14 10:39:24 +11002062 const struct task_security_struct *old_tsec;
2063 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002065 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002066 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002067 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int rc;
2069
Eric Paris200ac532009-02-12 15:01:04 -05002070 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (rc)
2072 return rc;
2073
David Howellsa6f76f22008-11-14 10:39:24 +11002074 /* SELinux context only depends on initial program or script and not
2075 * the script interpreter */
2076 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078
David Howellsa6f76f22008-11-14 10:39:24 +11002079 old_tsec = current_security();
2080 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 isec = inode->i_security;
2082
2083 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002084 new_tsec->sid = old_tsec->sid;
2085 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Michael LeMay28eba5b2006-06-27 02:53:42 -07002087 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002088 new_tsec->create_sid = 0;
2089 new_tsec->keycreate_sid = 0;
2090 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
David Howellsa6f76f22008-11-14 10:39:24 +11002092 if (old_tsec->exec_sid) {
2093 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002095 new_tsec->exec_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 } else {
2097 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002098 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002099 SECCLASS_PROCESS, NULL,
2100 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (rc)
2102 return rc;
2103 }
2104
Eric Parisf48b7392011-04-25 12:54:27 -04002105 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002106 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002107 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Josef Sipek3d5ff522006-12-08 02:37:38 -08002109 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
David Howellsa6f76f22008-11-14 10:39:24 +11002110 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
David Howellsa6f76f22008-11-14 10:39:24 +11002112 if (new_tsec->sid == old_tsec->sid) {
2113 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2115 if (rc)
2116 return rc;
2117 } else {
2118 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002119 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2121 if (rc)
2122 return rc;
2123
David Howellsa6f76f22008-11-14 10:39:24 +11002124 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2126 if (rc)
2127 return rc;
2128
David Howellsa6f76f22008-11-14 10:39:24 +11002129 /* Check for shared state */
2130 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2131 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2132 SECCLASS_PROCESS, PROCESS__SHARE,
2133 NULL);
2134 if (rc)
2135 return -EPERM;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
David Howellsa6f76f22008-11-14 10:39:24 +11002138 /* Make sure that anyone attempting to ptrace over a task that
2139 * changes its SID has the appropriate permit */
2140 if (bprm->unsafe &
2141 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2142 struct task_struct *tracer;
2143 struct task_security_struct *sec;
2144 u32 ptsid = 0;
2145
2146 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002147 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002148 if (likely(tracer != NULL)) {
2149 sec = __task_cred(tracer)->security;
2150 ptsid = sec->sid;
2151 }
2152 rcu_read_unlock();
2153
2154 if (ptsid != 0) {
2155 rc = avc_has_perm(ptsid, new_tsec->sid,
2156 SECCLASS_PROCESS,
2157 PROCESS__PTRACE, NULL);
2158 if (rc)
2159 return -EPERM;
2160 }
2161 }
2162
2163 /* Clear any possibly unsafe personality bits on exec: */
2164 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return 0;
2168}
2169
Eric Paris828dfe12008-04-17 13:17:49 -04002170static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Paul Moore5fb49872010-04-22 14:46:19 -04002172 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002173 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int atsecure = 0;
2175
David Howells275bb412008-11-14 10:39:19 +11002176 sid = tsec->sid;
2177 osid = tsec->osid;
2178
2179 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* Enable secure mode for SIDs transitions unless
2181 the noatsecure permission is granted between
2182 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002183 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002184 SECCLASS_PROCESS,
2185 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187
Eric Paris200ac532009-02-12 15:01:04 -05002188 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002192static inline void flush_unauthorized_files(const struct cred *cred,
2193 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Thomas Liu2bf49692009-07-14 12:14:09 -04002195 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002196 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002198 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002199 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002201 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002203 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002205 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002206 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002207 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002208 struct inode *inode;
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* Revalidate access to controlling tty.
2211 Use inode_has_perm on the tty inode directly rather
2212 than using file_has_perm, as this particular open
2213 file may belong to another process and we are only
2214 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002215 file_priv = list_first_entry(&tty->tty_files,
2216 struct tty_file_private, list);
2217 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002218 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002219 if (inode_has_perm_noadp(cred, inode,
2220 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002221 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002224 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002225 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002227 /* Reset controlling tty. */
2228 if (drop_tty)
2229 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Revalidate access to inherited open files. */
2232
Eric Parisf48b7392011-04-25 12:54:27 -04002233 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002234 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 spin_lock(&files->file_lock);
2237 for (;;) {
2238 unsigned long set, i;
2239 int fd;
2240
2241 j++;
2242 i = j * __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002243 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002244 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002246 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!set)
2248 continue;
2249 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002250 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (set & 1) {
2252 file = fget(i);
2253 if (!file)
2254 continue;
David Howells88e67f32008-11-14 10:39:21 +11002255 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 file,
2257 file_to_av(file))) {
2258 sys_close(i);
2259 fd = get_unused_fd();
2260 if (fd != i) {
2261 if (fd >= 0)
2262 put_unused_fd(fd);
2263 fput(file);
2264 continue;
2265 }
2266 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002267 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 } else {
David Howells745ca242008-11-14 10:39:22 +11002269 devnull = dentry_open(
2270 dget(selinux_null),
2271 mntget(selinuxfs_mount),
2272 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002273 if (IS_ERR(devnull)) {
2274 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 put_unused_fd(fd);
2276 fput(file);
2277 continue;
2278 }
2279 }
2280 fd_install(fd, devnull);
2281 }
2282 fput(file);
2283 }
2284 }
2285 spin_lock(&files->file_lock);
2286
2287 }
2288 spin_unlock(&files->file_lock);
2289}
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/*
David Howellsa6f76f22008-11-14 10:39:24 +11002292 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
David Howellsa6f76f22008-11-14 10:39:24 +11002294static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
David Howellsa6f76f22008-11-14 10:39:24 +11002296 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int rc, i;
2299
David Howellsa6f76f22008-11-14 10:39:24 +11002300 new_tsec = bprm->cred->security;
2301 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return;
2303
2304 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002305 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
David Howellsa6f76f22008-11-14 10:39:24 +11002307 /* Always clear parent death signal on SID transitions. */
2308 current->pdeath_signal = 0;
2309
2310 /* Check whether the new SID can inherit resource limits from the old
2311 * SID. If not, reset all soft limits to the lower of the current
2312 * task's hard limit and the init task's soft limit.
2313 *
2314 * Note that the setting of hard limits (even to lower them) can be
2315 * controlled by the setrlimit check. The inclusion of the init task's
2316 * soft limit into the computation is to avoid resetting soft limits
2317 * higher than the default soft limit for cases where the default is
2318 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2319 */
2320 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2321 PROCESS__RLIMITINH, NULL);
2322 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002323 /* protect against do_prlimit() */
2324 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002325 for (i = 0; i < RLIM_NLIMITS; i++) {
2326 rlim = current->signal->rlim + i;
2327 initrlim = init_task.signal->rlim + i;
2328 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2329 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002330 task_unlock(current);
2331 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002332 }
2333}
2334
2335/*
2336 * Clean up the process immediately after the installation of new credentials
2337 * due to exec
2338 */
2339static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2340{
2341 const struct task_security_struct *tsec = current_security();
2342 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002343 u32 osid, sid;
2344 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002345
David Howellsa6f76f22008-11-14 10:39:24 +11002346 osid = tsec->osid;
2347 sid = tsec->sid;
2348
2349 if (sid == osid)
2350 return;
2351
2352 /* Check whether the new SID can inherit signal state from the old SID.
2353 * If not, clear itimers to avoid subsequent signal generation and
2354 * flush and unblock signals.
2355 *
2356 * This must occur _after_ the task SID has been updated so that any
2357 * kill done after the flush will be checked against the new SID.
2358 */
2359 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (rc) {
2361 memset(&itimer, 0, sizeof itimer);
2362 for (i = 0; i < 3; i++)
2363 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002365 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2366 __flush_signals(current);
2367 flush_signal_handlers(current, 1);
2368 sigemptyset(&current->blocked);
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 spin_unlock_irq(&current->sighand->siglock);
2371 }
2372
David Howellsa6f76f22008-11-14 10:39:24 +11002373 /* Wake up the parent if it is waiting so that it can recheck
2374 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002375 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002376 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002377 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380/* superblock security operations */
2381
2382static int selinux_sb_alloc_security(struct super_block *sb)
2383{
2384 return superblock_alloc_security(sb);
2385}
2386
2387static void selinux_sb_free_security(struct super_block *sb)
2388{
2389 superblock_free_security(sb);
2390}
2391
2392static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2393{
2394 if (plen > olen)
2395 return 0;
2396
2397 return !memcmp(prefix, option, plen);
2398}
2399
2400static inline int selinux_option(char *option, int len)
2401{
Eric Paris832cbd92008-04-01 13:24:09 -04002402 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2403 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2404 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002405 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2406 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409static inline void take_option(char **to, char *from, int *first, int len)
2410{
2411 if (!*first) {
2412 **to = ',';
2413 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002414 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 *first = 0;
2416 memcpy(*to, from, len);
2417 *to += len;
2418}
2419
Eric Paris828dfe12008-04-17 13:17:49 -04002420static inline void take_selinux_option(char **to, char *from, int *first,
2421 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002422{
2423 int current_size = 0;
2424
2425 if (!*first) {
2426 **to = '|';
2427 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002428 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002429 *first = 0;
2430
2431 while (current_size < len) {
2432 if (*from != '"') {
2433 **to = *from;
2434 *to += 1;
2435 }
2436 from += 1;
2437 current_size += 1;
2438 }
2439}
2440
Eric Parise0007522008-03-05 10:31:54 -05002441static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 int fnosec, fsec, rc = 0;
2444 char *in_save, *in_curr, *in_end;
2445 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002446 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 in_curr = orig;
2449 sec_curr = copy;
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2452 if (!nosec) {
2453 rc = -ENOMEM;
2454 goto out;
2455 }
2456
2457 nosec_save = nosec;
2458 fnosec = fsec = 1;
2459 in_save = in_end = orig;
2460
2461 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002462 if (*in_end == '"')
2463 open_quote = !open_quote;
2464 if ((*in_end == ',' && open_quote == 0) ||
2465 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int len = in_end - in_curr;
2467
2468 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002469 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 else
2471 take_option(&nosec, in_curr, &fnosec, len);
2472
2473 in_curr = in_end + 1;
2474 }
2475 } while (*in_end++);
2476
Eric Paris6931dfc2005-06-30 02:58:51 -07002477 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002478 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479out:
2480 return rc;
2481}
2482
Eric Paris026eb162011-03-03 16:09:14 -05002483static int selinux_sb_remount(struct super_block *sb, void *data)
2484{
2485 int rc, i, *flags;
2486 struct security_mnt_opts opts;
2487 char *secdata, **mount_options;
2488 struct superblock_security_struct *sbsec = sb->s_security;
2489
2490 if (!(sbsec->flags & SE_SBINITIALIZED))
2491 return 0;
2492
2493 if (!data)
2494 return 0;
2495
2496 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2497 return 0;
2498
2499 security_init_mnt_opts(&opts);
2500 secdata = alloc_secdata();
2501 if (!secdata)
2502 return -ENOMEM;
2503 rc = selinux_sb_copy_data(data, secdata);
2504 if (rc)
2505 goto out_free_secdata;
2506
2507 rc = selinux_parse_opts_str(secdata, &opts);
2508 if (rc)
2509 goto out_free_secdata;
2510
2511 mount_options = opts.mnt_opts;
2512 flags = opts.mnt_opts_flags;
2513
2514 for (i = 0; i < opts.num_mnt_opts; i++) {
2515 u32 sid;
2516 size_t len;
2517
2518 if (flags[i] == SE_SBLABELSUPP)
2519 continue;
2520 len = strlen(mount_options[i]);
2521 rc = security_context_to_sid(mount_options[i], len, &sid);
2522 if (rc) {
2523 printk(KERN_WARNING "SELinux: security_context_to_sid"
2524 "(%s) failed for (dev %s, type %s) errno=%d\n",
2525 mount_options[i], sb->s_id, sb->s_type->name, rc);
2526 goto out_free_opts;
2527 }
2528 rc = -EINVAL;
2529 switch (flags[i]) {
2530 case FSCONTEXT_MNT:
2531 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2532 goto out_bad_option;
2533 break;
2534 case CONTEXT_MNT:
2535 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2536 goto out_bad_option;
2537 break;
2538 case ROOTCONTEXT_MNT: {
2539 struct inode_security_struct *root_isec;
2540 root_isec = sb->s_root->d_inode->i_security;
2541
2542 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2543 goto out_bad_option;
2544 break;
2545 }
2546 case DEFCONTEXT_MNT:
2547 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2548 goto out_bad_option;
2549 break;
2550 default:
2551 goto out_free_opts;
2552 }
2553 }
2554
2555 rc = 0;
2556out_free_opts:
2557 security_free_mnt_opts(&opts);
2558out_free_secdata:
2559 free_secdata(secdata);
2560 return rc;
2561out_bad_option:
2562 printk(KERN_WARNING "SELinux: unable to change security options "
2563 "during remount (dev %s, type=%s)\n", sb->s_id,
2564 sb->s_type->name);
2565 goto out_free_opts;
2566}
2567
James Morris12204e22008-12-19 10:44:42 +11002568static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
David Howells88e67f32008-11-14 10:39:21 +11002570 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002571 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002572 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 int rc;
2574
2575 rc = superblock_doinit(sb, data);
2576 if (rc)
2577 return rc;
2578
James Morris74192242008-12-19 11:41:10 +11002579 /* Allow all mounts performed by the kernel */
2580 if (flags & MS_KERNMOUNT)
2581 return 0;
2582
Eric Parisa2694342011-04-25 13:10:27 -04002583 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002584 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002585 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002586 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
David Howells726c3342006-06-23 02:02:58 -07002589static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
David Howells88e67f32008-11-14 10:39:21 +11002591 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002592 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002593 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Eric Parisa2694342011-04-25 13:10:27 -04002595 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002596 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002597 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002598 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Eric Paris828dfe12008-04-17 13:17:49 -04002601static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002602 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002603 char *type,
2604 unsigned long flags,
2605 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
David Howells88e67f32008-11-14 10:39:21 +11002607 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002610 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002611 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 else
Eric Paris2875fa02011-04-28 16:04:24 -04002613 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
2616static int selinux_umount(struct vfsmount *mnt, int flags)
2617{
David Howells88e67f32008-11-14 10:39:21 +11002618 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
David Howells88e67f32008-11-14 10:39:21 +11002620 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002621 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
2624/* inode security operations */
2625
2626static int selinux_inode_alloc_security(struct inode *inode)
2627{
2628 return inode_alloc_security(inode);
2629}
2630
2631static void selinux_inode_free_security(struct inode *inode)
2632{
2633 inode_free_security(inode);
2634}
2635
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002636static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002637 const struct qstr *qstr, char **name,
2638 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002639{
Paul Moore5fb49872010-04-22 14:46:19 -04002640 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002641 struct inode_security_struct *dsec;
2642 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002643 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002644 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002645 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002646
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002647 dsec = dir->i_security;
2648 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002649
David Howells275bb412008-11-14 10:39:19 +11002650 sid = tsec->sid;
2651 newsid = tsec->create_sid;
2652
Eric Paris415103f2010-12-02 16:13:40 -05002653 if ((sbsec->flags & SE_SBINITIALIZED) &&
2654 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2655 newsid = sbsec->mntpoint_sid;
2656 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002657 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002658 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002659 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002660 if (rc) {
2661 printk(KERN_WARNING "%s: "
2662 "security_transition_sid failed, rc=%d (dev=%s "
2663 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002664 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002665 -rc, inode->i_sb->s_id, inode->i_ino);
2666 return rc;
2667 }
2668 }
2669
Eric Paris296fddf2006-09-25 23:32:00 -07002670 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002671 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002672 struct inode_security_struct *isec = inode->i_security;
2673 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2674 isec->sid = newsid;
2675 isec->initialized = 1;
2676 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002677
David P. Quigleycd895962009-01-16 09:22:04 -05002678 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002679 return -EOPNOTSUPP;
2680
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002681 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002682 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002683 if (!namep)
2684 return -ENOMEM;
2685 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002686 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002687
2688 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002689 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002690 if (rc) {
2691 kfree(namep);
2692 return rc;
2693 }
2694 *value = context;
2695 *len = clen;
2696 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002697
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002698 return 0;
2699}
2700
Al Viro4acdaf22011-07-26 01:42:34 -04002701static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Amir Samuelov6a22e462014-05-26 11:44:06 +03002703 int ret;
2704
2705 ret = pft_inode_create(dir, dentry, mode);
2706 if (ret < 0)
2707 return ret;
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return may_create(dir, dentry, SECCLASS_FILE);
2710}
2711
Amir Samuelov6a22e462014-05-26 11:44:06 +03002712static int selinux_inode_post_create(struct inode *dir, struct dentry *dentry,
2713 umode_t mode)
2714{
2715 int ret;
2716
2717 ret = pft_inode_post_create(dir, dentry, mode);
2718
2719 return ret;
2720}
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return may_link(dir, old_dentry, MAY_LINK);
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return may_link(dir, dentry, MAY_UNLINK);
2730}
2731
2732static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2733{
2734 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2735}
2736
Al Viro18bb1db2011-07-26 01:41:39 -04002737static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 return may_create(dir, dentry, SECCLASS_DIR);
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2743{
2744 return may_link(dir, dentry, MAY_RMDIR);
2745}
2746
Al Viro1a67aaf2011-07-26 01:52:52 -04002747static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002753 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Amir Samuelov6a22e462014-05-26 11:44:06 +03002755 int rc;
2756
2757 rc = pft_inode_rename(old_inode, old_dentry, new_inode, new_dentry);
2758 if (rc)
2759 return rc;
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764static int selinux_inode_readlink(struct dentry *dentry)
2765{
David Howells88e67f32008-11-14 10:39:21 +11002766 const struct cred *cred = current_cred();
2767
Eric Paris2875fa02011-04-28 16:04:24 -04002768 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2772{
David Howells88e67f32008-11-14 10:39:21 +11002773 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Eric Paris2875fa02011-04-28 16:04:24 -04002775 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Al Viroe74f71e2011-06-20 19:38:15 -04002778static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
David Howells88e67f32008-11-14 10:39:21 +11002780 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002781 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002782 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002783 u32 perms;
2784 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002785 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Eric Parisb782e0a2010-07-23 11:44:03 -04002787 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002788 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2789
Eric Parisb782e0a2010-07-23 11:44:03 -04002790 /* No permission to check. Existence test. */
2791 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Eric Parisf48b7392011-04-25 12:54:27 -04002794 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002795 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002796 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002797
2798 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002799 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002800
2801 perms = file_mask_to_av(inode->i_mode, mask);
2802
Eric Paris9ade0cf2011-04-25 16:26:29 -04002803 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
2806static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2807{
David Howells88e67f32008-11-14 10:39:21 +11002808 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002809 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002811 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2812 if (ia_valid & ATTR_FORCE) {
2813 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2814 ATTR_FORCE);
2815 if (!ia_valid)
2816 return 0;
2817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002819 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2820 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002821 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Eric Paris2875fa02011-04-28 16:04:24 -04002823 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
2826static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2827{
David Howells88e67f32008-11-14 10:39:21 +11002828 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002829 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002830
Eric Paris2875fa02011-04-28 16:04:24 -04002831 path.dentry = dentry;
2832 path.mnt = mnt;
2833
2834 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
David Howells8f0cfa52008-04-29 00:59:41 -07002837static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002838{
David Howells88e67f32008-11-14 10:39:21 +11002839 const struct cred *cred = current_cred();
2840
Amir Samuelov6a22e462014-05-26 11:44:06 +03002841 if (pft_inode_set_xattr(dentry, name) < 0)
2842 return -EACCES;
2843
2844
Serge E. Hallynb5376772007-10-16 23:31:36 -07002845 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2846 sizeof XATTR_SECURITY_PREFIX - 1)) {
2847 if (!strcmp(name, XATTR_NAME_CAPS)) {
2848 if (!capable(CAP_SETFCAP))
2849 return -EPERM;
2850 } else if (!capable(CAP_SYS_ADMIN)) {
2851 /* A different attribute in the security namespace.
2852 Restrict to administrator. */
2853 return -EPERM;
2854 }
2855 }
2856
2857 /* Not an attribute we recognize, so just check the
2858 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002859 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002860}
2861
David Howells8f0cfa52008-04-29 00:59:41 -07002862static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2863 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct inode *inode = dentry->d_inode;
2866 struct inode_security_struct *isec = inode->i_security;
2867 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002868 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002869 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002870 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 int rc = 0;
2872
Serge E. Hallynb5376772007-10-16 23:31:36 -07002873 if (strcmp(name, XATTR_NAME_SELINUX))
2874 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002877 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return -EOPNOTSUPP;
2879
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002880 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return -EPERM;
2882
Eric Parisa2694342011-04-25 13:10:27 -04002883 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002884 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002885 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
David Howells275bb412008-11-14 10:39:19 +11002887 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 FILE__RELABELFROM, &ad);
2889 if (rc)
2890 return rc;
2891
2892 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002893 if (rc == -EINVAL) {
2894 if (!capable(CAP_MAC_ADMIN))
2895 return rc;
2896 rc = security_context_to_sid_force(value, size, &newsid);
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (rc)
2899 return rc;
2900
David Howells275bb412008-11-14 10:39:19 +11002901 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 FILE__RELABELTO, &ad);
2903 if (rc)
2904 return rc;
2905
David Howells275bb412008-11-14 10:39:19 +11002906 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002907 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (rc)
2909 return rc;
2910
2911 return avc_has_perm(newsid,
2912 sbsec->sid,
2913 SECCLASS_FILESYSTEM,
2914 FILESYSTEM__ASSOCIATE,
2915 &ad);
2916}
2917
David Howells8f0cfa52008-04-29 00:59:41 -07002918static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002919 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002920 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 struct inode *inode = dentry->d_inode;
2923 struct inode_security_struct *isec = inode->i_security;
2924 u32 newsid;
2925 int rc;
2926
2927 if (strcmp(name, XATTR_NAME_SELINUX)) {
2928 /* Not an attribute we recognize, so nothing to do. */
2929 return;
2930 }
2931
Stephen Smalley12b29f32008-05-07 13:03:20 -04002932 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002934 printk(KERN_ERR "SELinux: unable to map context to SID"
2935 "for (%s, %lu), rc=%d\n",
2936 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return;
2938 }
2939
2940 isec->sid = newsid;
2941 return;
2942}
2943
David Howells8f0cfa52008-04-29 00:59:41 -07002944static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
David Howells88e67f32008-11-14 10:39:21 +11002946 const struct cred *cred = current_cred();
2947
Eric Paris2875fa02011-04-28 16:04:24 -04002948 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Eric Paris828dfe12008-04-17 13:17:49 -04002951static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
David Howells88e67f32008-11-14 10:39:21 +11002953 const struct cred *cred = current_cred();
2954
Eric Paris2875fa02011-04-28 16:04:24 -04002955 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
David Howells8f0cfa52008-04-29 00:59:41 -07002958static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002960 if (strcmp(name, XATTR_NAME_SELINUX))
2961 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /* No one is allowed to remove a SELinux security label.
2964 You can change the label, but all data must be labeled. */
2965 return -EACCES;
2966}
2967
James Morrisd381d8a2005-10-30 14:59:22 -08002968/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002969 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002970 *
2971 * Permission check is handled by selinux_inode_getxattr hook.
2972 */
David P. Quigley42492592008-02-04 22:29:39 -08002973static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
David P. Quigley42492592008-02-04 22:29:39 -08002975 u32 size;
2976 int error;
2977 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002980 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2981 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002983 /*
2984 * If the caller has CAP_MAC_ADMIN, then get the raw context
2985 * value even if it is not defined by current policy; otherwise,
2986 * use the in-core value under current policy.
2987 * Use the non-auditing forms of the permission checks since
2988 * getxattr may be called by unprivileged processes commonly
2989 * and lack of permission just means that we fall back to the
2990 * in-core context value, not a denial.
2991 */
Eric Paris6a9de492012-01-03 12:25:14 -05002992 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002993 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002994 if (!error)
2995 error = security_sid_to_context_force(isec->sid, &context,
2996 &size);
2997 else
2998 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002999 if (error)
3000 return error;
3001 error = size;
3002 if (alloc) {
3003 *buffer = context;
3004 goto out_nofree;
3005 }
3006 kfree(context);
3007out_nofree:
3008 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
3011static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003012 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
3014 struct inode_security_struct *isec = inode->i_security;
3015 u32 newsid;
3016 int rc;
3017
3018 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3019 return -EOPNOTSUPP;
3020
3021 if (!value || !size)
3022 return -EACCES;
3023
Eric Paris828dfe12008-04-17 13:17:49 -04003024 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (rc)
3026 return rc;
3027
3028 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003029 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return 0;
3031}
3032
3033static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3034{
3035 const int len = sizeof(XATTR_NAME_SELINUX);
3036 if (buffer && len <= buffer_size)
3037 memcpy(buffer, XATTR_NAME_SELINUX, len);
3038 return len;
3039}
3040
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003041static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3042{
3043 struct inode_security_struct *isec = inode->i_security;
3044 *secid = isec->sid;
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047/* file security operations */
3048
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003049static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
David Howells88e67f32008-11-14 10:39:21 +11003051 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003052 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3055 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3056 mask |= MAY_APPEND;
3057
Paul Moore389fb802009-03-27 17:10:34 -04003058 return file_has_perm(cred, file,
3059 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003062static int selinux_file_permission(struct file *file, int mask)
3063{
Stephen Smalley20dda182009-06-22 14:54:53 -04003064 struct inode *inode = file->f_path.dentry->d_inode;
3065 struct file_security_struct *fsec = file->f_security;
3066 struct inode_security_struct *isec = inode->i_security;
3067 u32 sid = current_sid();
Amir Samuelov6a22e462014-05-26 11:44:06 +03003068 int ret;
Stephen Smalley20dda182009-06-22 14:54:53 -04003069
Paul Moore389fb802009-03-27 17:10:34 -04003070 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003071 /* No permission to check. Existence test. */
3072 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003073
Amir Samuelov6a22e462014-05-26 11:44:06 +03003074 ret = pft_file_permission(file, mask);
3075 if (ret < 0)
3076 return ret;
3077
Stephen Smalley20dda182009-06-22 14:54:53 -04003078 if (sid == fsec->sid && fsec->isid == isec->sid &&
3079 fsec->pseqno == avc_policy_seqno())
3080 /* No change since dentry_open check. */
3081 return 0;
3082
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003083 return selinux_revalidate_file_permission(file, mask);
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086static int selinux_file_alloc_security(struct file *file)
3087{
3088 return file_alloc_security(file);
3089}
3090
3091static void selinux_file_free_security(struct file *file)
3092{
3093 file_free_security(file);
3094}
3095
3096static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3097 unsigned long arg)
3098{
David Howells88e67f32008-11-14 10:39:21 +11003099 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003100 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Eric Paris0b24dcb2011-02-25 15:39:20 -05003102 switch (cmd) {
3103 case FIONREAD:
3104 /* fall through */
3105 case FIBMAP:
3106 /* fall through */
3107 case FIGETBSZ:
3108 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003109 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003110 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003111 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003112 error = file_has_perm(cred, file, FILE__GETATTR);
3113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Al Viro2f99c362012-03-23 16:04:05 -04003115 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003116 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003117 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003118 error = file_has_perm(cred, file, FILE__SETATTR);
3119 break;
3120
3121 /* sys_ioctl() checks */
3122 case FIONBIO:
3123 /* fall through */
3124 case FIOASYNC:
3125 error = file_has_perm(cred, file, 0);
3126 break;
3127
3128 case KDSKBENT:
3129 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003130 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3131 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003132 break;
3133
3134 /* default case assumes that the command will go
3135 * to the file's ioctl() function.
3136 */
3137 default:
Jeff Vander Stoep1db0d292015-07-29 18:36:41 -07003138 error = file_has_perm(cred, file, FILE__IOCTL);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003139 }
3140 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003143static int default_noexec;
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3146{
David Howells88e67f32008-11-14 10:39:21 +11003147 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003148 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003149
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003150 if (default_noexec &&
3151 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /*
3153 * We are making executable an anonymous mapping or a
3154 * private file mapping that will also be writable.
3155 * This has an additional check.
3156 */
David Howellsd84f4f92008-11-14 10:39:23 +11003157 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003159 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 if (file) {
3163 /* read access is always possible with a mapping */
3164 u32 av = FILE__READ;
3165
3166 /* write access only matters if the mapping is shared */
3167 if (shared && (prot & PROT_WRITE))
3168 av |= FILE__WRITE;
3169
3170 if (prot & PROT_EXEC)
3171 av |= FILE__EXECUTE;
3172
David Howells88e67f32008-11-14 10:39:21 +11003173 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
David Howellsd84f4f92008-11-14 10:39:23 +11003175
3176error:
3177 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
3180static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003181 unsigned long prot, unsigned long flags,
3182 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Eric Parised032182007-06-28 15:55:21 -04003184 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003185 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Eric Paris84336d1a2009-07-31 12:54:05 -04003187 /*
3188 * notice that we are intentionally putting the SELinux check before
3189 * the secondary cap_file_mmap check. This is such a likely attempt
3190 * at bad behaviour/exploit that we always want to get the AVC, even
3191 * if DAC would have also denied the operation.
3192 */
Eric Parisa2551df2009-07-31 12:54:11 -04003193 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003194 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3195 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003196 if (rc)
3197 return rc;
3198 }
3199
3200 /* do DAC check on address space usage */
3201 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003202 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return rc;
3204
3205 if (selinux_checkreqprot)
3206 prot = reqprot;
3207
3208 return file_map_prot_check(file, prot,
3209 (flags & MAP_TYPE) == MAP_SHARED);
3210}
3211
3212static int selinux_file_mprotect(struct vm_area_struct *vma,
3213 unsigned long reqprot,
3214 unsigned long prot)
3215{
David Howells88e67f32008-11-14 10:39:21 +11003216 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 if (selinux_checkreqprot)
3219 prot = reqprot;
3220
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003221 if (default_noexec &&
3222 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003223 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003224 if (vma->vm_start >= vma->vm_mm->start_brk &&
3225 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003226 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003227 } else if (!vma->vm_file &&
3228 vma->vm_start <= vma->vm_mm->start_stack &&
3229 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003230 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003231 } else if (vma->vm_file && vma->anon_vma) {
3232 /*
3233 * We are making executable a file mapping that has
3234 * had some COW done. Since pages might have been
3235 * written, check ability to execute the possibly
3236 * modified content. This typically should only
3237 * occur for text relocations.
3238 */
David Howellsd84f4f92008-11-14 10:39:23 +11003239 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003240 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003241 if (rc)
3242 return rc;
3243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3246}
3247
3248static int selinux_file_lock(struct file *file, unsigned int cmd)
3249{
David Howells88e67f32008-11-14 10:39:21 +11003250 const struct cred *cred = current_cred();
3251
3252 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
3255static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3256 unsigned long arg)
3257{
David Howells88e67f32008-11-14 10:39:21 +11003258 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 int err = 0;
3260
3261 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003262 case F_SETFL:
3263 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3264 err = -EINVAL;
3265 break;
3266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Eric Paris828dfe12008-04-17 13:17:49 -04003268 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003269 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003271 }
3272 /* fall through */
3273 case F_SETOWN:
3274 case F_SETSIG:
3275 case F_GETFL:
3276 case F_GETOWN:
3277 case F_GETSIG:
3278 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003279 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003280 break;
3281 case F_GETLK:
3282 case F_SETLK:
3283 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003285 case F_GETLK64:
3286 case F_SETLK64:
3287 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003289 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3290 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003292 }
David Howells88e67f32008-11-14 10:39:21 +11003293 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296
3297 return err;
3298}
3299
3300static int selinux_file_set_fowner(struct file *file)
3301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 struct file_security_struct *fsec;
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003305 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 return 0;
3308}
3309
3310static int selinux_file_send_sigiotask(struct task_struct *tsk,
3311 struct fown_struct *fown, int signum)
3312{
Eric Paris828dfe12008-04-17 13:17:49 -04003313 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003314 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 struct file_security_struct *fsec;
3317
3318 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003319 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 fsec = file->f_security;
3322
3323 if (!signum)
3324 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3325 else
3326 perm = signal_to_av(signum);
3327
David Howells275bb412008-11-14 10:39:19 +11003328 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 SECCLASS_PROCESS, perm, NULL);
3330}
3331
3332static int selinux_file_receive(struct file *file)
3333{
David Howells88e67f32008-11-14 10:39:21 +11003334 const struct cred *cred = current_cred();
3335
3336 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337}
3338
David Howells745ca242008-11-14 10:39:22 +11003339static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003340{
3341 struct file_security_struct *fsec;
3342 struct inode *inode;
3343 struct inode_security_struct *isec;
Amir Samuelov6a22e462014-05-26 11:44:06 +03003344 int ret;
3345
3346 ret = pft_file_open(file, cred);
3347 if (ret < 0)
3348 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +11003349
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003350 inode = file->f_path.dentry->d_inode;
3351 fsec = file->f_security;
3352 isec = inode->i_security;
3353 /*
3354 * Save inode label and policy sequence number
3355 * at open-time so that selinux_file_permission
3356 * can determine whether revalidation is necessary.
3357 * Task label is already saved in the file security
3358 * struct as its SID.
3359 */
3360 fsec->isid = isec->sid;
3361 fsec->pseqno = avc_policy_seqno();
3362 /*
3363 * Since the inode label or policy seqno may have changed
3364 * between the selinux_inode_permission check and the saving
3365 * of state above, recheck that access is still permitted.
3366 * Otherwise, access might never be revalidated against the
3367 * new inode label or new policy.
3368 * This check is not redundant - do not remove.
3369 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003370 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003371}
3372
Amir Samuelov6a22e462014-05-26 11:44:06 +03003373static int selinux_file_close(struct file *file)
3374{
3375 return pft_file_close(file);
3376}
3377
3378static bool selinux_allow_merge_bio(struct bio *bio1, struct bio *bio2)
3379{
3380 return pft_allow_merge_bio(bio1, bio2);
3381}
3382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383/* task security operations */
3384
3385static int selinux_task_create(unsigned long clone_flags)
3386{
David Howells3b11a1d2008-11-14 10:39:26 +11003387 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
David Howellsf1752ee2008-11-14 10:39:17 +11003390/*
David Howellsee18d642009-09-02 09:14:21 +01003391 * allocate the SELinux part of blank credentials
3392 */
3393static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3394{
3395 struct task_security_struct *tsec;
3396
3397 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3398 if (!tsec)
3399 return -ENOMEM;
3400
3401 cred->security = tsec;
3402 return 0;
3403}
3404
3405/*
David Howellsf1752ee2008-11-14 10:39:17 +11003406 * detach and free the LSM part of a set of credentials
3407 */
3408static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
David Howellsf1752ee2008-11-14 10:39:17 +11003410 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003411
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003412 /*
3413 * cred->security == NULL if security_cred_alloc_blank() or
3414 * security_prepare_creds() returned an error.
3415 */
3416 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003417 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003418 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
David Howellsd84f4f92008-11-14 10:39:23 +11003421/*
3422 * prepare a new set of credentials for modification
3423 */
3424static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3425 gfp_t gfp)
3426{
3427 const struct task_security_struct *old_tsec;
3428 struct task_security_struct *tsec;
3429
3430 old_tsec = old->security;
3431
3432 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3433 if (!tsec)
3434 return -ENOMEM;
3435
3436 new->security = tsec;
3437 return 0;
3438}
3439
3440/*
David Howellsee18d642009-09-02 09:14:21 +01003441 * transfer the SELinux data to a blank set of creds
3442 */
3443static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3444{
3445 const struct task_security_struct *old_tsec = old->security;
3446 struct task_security_struct *tsec = new->security;
3447
3448 *tsec = *old_tsec;
3449}
3450
3451/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003452 * set the security data for a kernel service
3453 * - all the creation contexts are set to unlabelled
3454 */
3455static int selinux_kernel_act_as(struct cred *new, u32 secid)
3456{
3457 struct task_security_struct *tsec = new->security;
3458 u32 sid = current_sid();
3459 int ret;
3460
3461 ret = avc_has_perm(sid, secid,
3462 SECCLASS_KERNEL_SERVICE,
3463 KERNEL_SERVICE__USE_AS_OVERRIDE,
3464 NULL);
3465 if (ret == 0) {
3466 tsec->sid = secid;
3467 tsec->create_sid = 0;
3468 tsec->keycreate_sid = 0;
3469 tsec->sockcreate_sid = 0;
3470 }
3471 return ret;
3472}
3473
3474/*
3475 * set the file creation context in a security record to the same as the
3476 * objective context of the specified inode
3477 */
3478static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3479{
3480 struct inode_security_struct *isec = inode->i_security;
3481 struct task_security_struct *tsec = new->security;
3482 u32 sid = current_sid();
3483 int ret;
3484
3485 ret = avc_has_perm(sid, isec->sid,
3486 SECCLASS_KERNEL_SERVICE,
3487 KERNEL_SERVICE__CREATE_FILES_AS,
3488 NULL);
3489
3490 if (ret == 0)
3491 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003492 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003493}
3494
Eric Parisdd8dbf22009-11-03 16:35:32 +11003495static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003496{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003497 u32 sid;
3498 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003499 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003500
3501 sid = task_sid(current);
3502
3503 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003504 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003505 ad.u.kmod_name = kmod_name;
3506
3507 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3508 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003509}
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3512{
David Howells3b11a1d2008-11-14 10:39:26 +11003513 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
3516static int selinux_task_getpgid(struct task_struct *p)
3517{
David Howells3b11a1d2008-11-14 10:39:26 +11003518 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
3521static int selinux_task_getsid(struct task_struct *p)
3522{
David Howells3b11a1d2008-11-14 10:39:26 +11003523 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
David Quigleyf9008e42006-06-30 01:55:46 -07003526static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3527{
David Howells275bb412008-11-14 10:39:19 +11003528 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003529}
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531static int selinux_task_setnice(struct task_struct *p, int nice)
3532{
3533 int rc;
3534
Eric Paris200ac532009-02-12 15:01:04 -05003535 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (rc)
3537 return rc;
3538
David Howells3b11a1d2008-11-14 10:39:26 +11003539 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
James Morris03e68062006-06-23 02:03:58 -07003542static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3543{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003544 int rc;
3545
Eric Paris200ac532009-02-12 15:01:04 -05003546 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003547 if (rc)
3548 return rc;
3549
David Howells3b11a1d2008-11-14 10:39:26 +11003550 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003551}
3552
David Quigleya1836a42006-06-30 01:55:49 -07003553static int selinux_task_getioprio(struct task_struct *p)
3554{
David Howells3b11a1d2008-11-14 10:39:26 +11003555 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003556}
3557
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003558static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3559 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003561 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
3563 /* Control the ability to change the hard limit (whether
3564 lowering or raising it), so that the hard limit can
3565 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003566 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003568 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 return 0;
3571}
3572
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003573static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003575 int rc;
3576
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003577 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003578 if (rc)
3579 return rc;
3580
David Howells3b11a1d2008-11-14 10:39:26 +11003581 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
3584static int selinux_task_getscheduler(struct task_struct *p)
3585{
David Howells3b11a1d2008-11-14 10:39:26 +11003586 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
David Quigley35601542006-06-23 02:04:01 -07003589static int selinux_task_movememory(struct task_struct *p)
3590{
David Howells3b11a1d2008-11-14 10:39:26 +11003591 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003592}
3593
David Quigleyf9008e42006-06-30 01:55:46 -07003594static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3595 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
3597 u32 perm;
3598 int rc;
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (!sig)
3601 perm = PROCESS__SIGNULL; /* null signal; existence test */
3602 else
3603 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003604 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003605 rc = avc_has_perm(secid, task_sid(p),
3606 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003607 else
David Howells3b11a1d2008-11-14 10:39:26 +11003608 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003609 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612static int selinux_task_wait(struct task_struct *p)
3613{
Eric Paris8a535142007-10-22 16:10:31 -04003614 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617static void selinux_task_to_inode(struct task_struct *p,
3618 struct inode *inode)
3619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003621 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
David Howells275bb412008-11-14 10:39:19 +11003623 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003628static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003629 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 int offset, ihlen, ret = -EINVAL;
3632 struct iphdr _iph, *ih;
3633
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003634 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3636 if (ih == NULL)
3637 goto out;
3638
3639 ihlen = ih->ihl * 4;
3640 if (ihlen < sizeof(_iph))
3641 goto out;
3642
Eric Paris48c62af2012-04-02 13:15:44 -04003643 ad->u.net->v4info.saddr = ih->saddr;
3644 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 ret = 0;
3646
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003647 if (proto)
3648 *proto = ih->protocol;
3649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003651 case IPPROTO_TCP: {
3652 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Eric Paris828dfe12008-04-17 13:17:49 -04003654 if (ntohs(ih->frag_off) & IP_OFFSET)
3655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 offset += ihlen;
3658 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3659 if (th == NULL)
3660 break;
3661
Eric Paris48c62af2012-04-02 13:15:44 -04003662 ad->u.net->sport = th->source;
3663 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Eric Paris828dfe12008-04-17 13:17:49 -04003667 case IPPROTO_UDP: {
3668 struct udphdr _udph, *uh;
3669
3670 if (ntohs(ih->frag_off) & IP_OFFSET)
3671 break;
3672
3673 offset += ihlen;
3674 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3675 if (uh == NULL)
3676 break;
3677
Eric Paris48c62af2012-04-02 13:15:44 -04003678 ad->u.net->sport = uh->source;
3679 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003680 break;
3681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
James Morris2ee92d42006-11-13 16:09:01 -08003683 case IPPROTO_DCCP: {
3684 struct dccp_hdr _dccph, *dh;
3685
3686 if (ntohs(ih->frag_off) & IP_OFFSET)
3687 break;
3688
3689 offset += ihlen;
3690 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3691 if (dh == NULL)
3692 break;
3693
Eric Paris48c62af2012-04-02 13:15:44 -04003694 ad->u.net->sport = dh->dccph_sport;
3695 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003696 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003697 }
James Morris2ee92d42006-11-13 16:09:01 -08003698
Eric Paris828dfe12008-04-17 13:17:49 -04003699 default:
3700 break;
3701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702out:
3703 return ret;
3704}
3705
3706#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3707
3708/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003709static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003710 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 u8 nexthdr;
3713 int ret = -EINVAL, offset;
3714 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003715 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003717 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3719 if (ip6 == NULL)
3720 goto out;
3721
Eric Paris48c62af2012-04-02 13:15:44 -04003722 ad->u.net->v6info.saddr = ip6->saddr;
3723 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 ret = 0;
3725
3726 nexthdr = ip6->nexthdr;
3727 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003728 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (offset < 0)
3730 goto out;
3731
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003732 if (proto)
3733 *proto = nexthdr;
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 switch (nexthdr) {
3736 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003737 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
3739 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3740 if (th == NULL)
3741 break;
3742
Eric Paris48c62af2012-04-02 13:15:44 -04003743 ad->u.net->sport = th->source;
3744 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 break;
3746 }
3747
3748 case IPPROTO_UDP: {
3749 struct udphdr _udph, *uh;
3750
3751 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3752 if (uh == NULL)
3753 break;
3754
Eric Paris48c62af2012-04-02 13:15:44 -04003755 ad->u.net->sport = uh->source;
3756 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 break;
3758 }
3759
James Morris2ee92d42006-11-13 16:09:01 -08003760 case IPPROTO_DCCP: {
3761 struct dccp_hdr _dccph, *dh;
3762
3763 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3764 if (dh == NULL)
3765 break;
3766
Eric Paris48c62af2012-04-02 13:15:44 -04003767 ad->u.net->sport = dh->dccph_sport;
3768 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003769 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003770 }
James Morris2ee92d42006-11-13 16:09:01 -08003771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 /* includes fragments */
3773 default:
3774 break;
3775 }
3776out:
3777 return ret;
3778}
3779
3780#endif /* IPV6 */
3781
Thomas Liu2bf49692009-07-14 12:14:09 -04003782static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003783 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
David Howellscf9481e2008-07-27 21:31:07 +10003785 char *addrp;
3786 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Eric Paris48c62af2012-04-02 13:15:44 -04003788 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003790 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003791 if (ret)
3792 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003793 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3794 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003795 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3798 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003799 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003800 if (ret)
3801 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003802 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3803 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003804 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805#endif /* IPV6 */
3806 default:
David Howellscf9481e2008-07-27 21:31:07 +10003807 addrp = NULL;
3808 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810
David Howellscf9481e2008-07-27 21:31:07 +10003811parse_error:
3812 printk(KERN_WARNING
3813 "SELinux: failure in selinux_parse_skb(),"
3814 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003816
3817okay:
3818 if (_addrp)
3819 *_addrp = addrp;
3820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
Paul Moore4f6a9932007-03-01 14:35:22 -05003823/**
Paul Moore220deb92008-01-29 08:38:23 -05003824 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003825 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003826 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003827 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003828 *
3829 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003830 * Check the various different forms of network peer labeling and determine
3831 * the peer label/SID for the packet; most of the magic actually occurs in
3832 * the security server function security_net_peersid_cmp(). The function
3833 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3834 * or -EACCES if @sid is invalid due to inconsistencies with the different
3835 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003836 *
3837 */
Paul Moore220deb92008-01-29 08:38:23 -05003838static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003839{
Paul Moore71f1cb02008-01-29 08:51:16 -05003840 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003841 u32 xfrm_sid;
3842 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003843 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003844
3845 selinux_skb_xfrm_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003846 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003847
Paul Moore71f1cb02008-01-29 08:51:16 -05003848 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3849 if (unlikely(err)) {
3850 printk(KERN_WARNING
3851 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3852 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003853 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003854 }
Paul Moore220deb92008-01-29 08:38:23 -05003855
3856 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003857}
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003860
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003861static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3862 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003863{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003864 if (tsec->sockcreate_sid > SECSID_NULL) {
3865 *socksid = tsec->sockcreate_sid;
3866 return 0;
3867 }
3868
3869 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3870 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003871}
3872
Paul Moore253bfae2010-04-22 14:46:19 -04003873static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
Paul Moore253bfae2010-04-22 14:46:19 -04003875 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003876 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003877 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003878 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003879 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Satya Durga Srinivasu Prabhala90280652013-09-24 15:23:48 -07003881 if (unlikely(!sksec)) {
3882 pr_warn("SELinux: sksec is NULL, socket is already freed\n");
3883 return -EINVAL;
3884 }
3885
Paul Moore253bfae2010-04-22 14:46:19 -04003886 if (sksec->sid == SECINITSID_KERNEL)
3887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Thomas Liu2bf49692009-07-14 12:14:09 -04003889 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003890 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003891 ad.u.net = &net;
3892 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Paul Moore253bfae2010-04-22 14:46:19 -04003894 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
3896
3897static int selinux_socket_create(int family, int type,
3898 int protocol, int kern)
3899{
Paul Moore5fb49872010-04-22 14:46:19 -04003900 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003901 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003902 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003903 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
David Howells275bb412008-11-14 10:39:19 +11003908 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003909 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3910 if (rc)
3911 return rc;
3912
Paul Moored4f2d972010-04-22 14:46:18 -04003913 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
3915
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003916static int selinux_socket_post_create(struct socket *sock, int family,
3917 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
Paul Moore5fb49872010-04-22 14:46:19 -04003919 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003920 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003921 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003922 int err = 0;
3923
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003924 isec->sclass = socket_type_to_security_class(family, type, protocol);
3925
David Howells275bb412008-11-14 10:39:19 +11003926 if (kern)
3927 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003928 else {
3929 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3930 if (err)
3931 return err;
3932 }
David Howells275bb412008-11-14 10:39:19 +11003933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 isec->initialized = 1;
3935
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003936 if (sock->sk) {
3937 sksec = sock->sk->sk_security;
3938 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003939 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003940 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003941 }
3942
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
3946/* Range of port numbers used to automatically bind.
3947 Need to determine whether we should perform a name_bind
3948 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3951{
Paul Moore253bfae2010-04-22 14:46:19 -04003952 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 u16 family;
3954 int err;
3955
Paul Moore253bfae2010-04-22 14:46:19 -04003956 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 if (err)
3958 goto out;
3959
3960 /*
3961 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003962 * Multiple address binding for SCTP is not supported yet: we just
3963 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 */
Paul Moore253bfae2010-04-22 14:46:19 -04003965 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 if (family == PF_INET || family == PF_INET6) {
3967 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003968 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003969 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003970 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003971 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 struct sockaddr_in *addr4 = NULL;
3973 struct sockaddr_in6 *addr6 = NULL;
3974 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003975 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (family == PF_INET) {
3978 addr4 = (struct sockaddr_in *)address;
3979 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 addrp = (char *)&addr4->sin_addr.s_addr;
3981 } else {
3982 addr6 = (struct sockaddr_in6 *)address;
3983 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 addrp = (char *)&addr6->sin6_addr.s6_addr;
3985 }
3986
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003987 if (snum) {
3988 int low, high;
3989
3990 inet_get_local_port_range(&low, &high);
3991
3992 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003993 err = sel_netport_sid(sk->sk_protocol,
3994 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003995 if (err)
3996 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04003997 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003998 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003999 ad.u.net = &net;
4000 ad.u.net->sport = htons(snum);
4001 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004002 err = avc_has_perm(sksec->sid, sid,
4003 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004004 SOCKET__NAME_BIND, &ad);
4005 if (err)
4006 goto out;
4007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
Eric Paris828dfe12008-04-17 13:17:49 -04004009
Paul Moore253bfae2010-04-22 14:46:19 -04004010 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004011 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 node_perm = TCP_SOCKET__NODE_BIND;
4013 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004014
James Morris13402582005-09-30 14:24:34 -04004015 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 node_perm = UDP_SOCKET__NODE_BIND;
4017 break;
James Morris2ee92d42006-11-13 16:09:01 -08004018
4019 case SECCLASS_DCCP_SOCKET:
4020 node_perm = DCCP_SOCKET__NODE_BIND;
4021 break;
4022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 default:
4024 node_perm = RAWIP_SOCKET__NODE_BIND;
4025 break;
4026 }
Eric Paris828dfe12008-04-17 13:17:49 -04004027
Paul Moore224dfbd2008-01-29 08:38:13 -05004028 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (err)
4030 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004031
Thomas Liu2bf49692009-07-14 12:14:09 -04004032 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004033 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004034 ad.u.net = &net;
4035 ad.u.net->sport = htons(snum);
4036 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004039 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 else
Eric Paris48c62af2012-04-02 13:15:44 -04004041 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Paul Moore253bfae2010-04-22 14:46:19 -04004043 err = avc_has_perm(sksec->sid, sid,
4044 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 if (err)
4046 goto out;
4047 }
4048out:
4049 return err;
4050}
4051
4052static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4053{
Paul Moore014ab192008-10-10 10:16:33 -04004054 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004055 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 int err;
4057
Paul Moore253bfae2010-04-22 14:46:19 -04004058 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (err)
4060 return err;
4061
4062 /*
James Morris2ee92d42006-11-13 16:09:01 -08004063 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 */
Paul Moore253bfae2010-04-22 14:46:19 -04004065 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4066 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004067 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004068 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004069 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 struct sockaddr_in *addr4 = NULL;
4071 struct sockaddr_in6 *addr6 = NULL;
4072 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004073 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 if (sk->sk_family == PF_INET) {
4076 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004077 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return -EINVAL;
4079 snum = ntohs(addr4->sin_port);
4080 } else {
4081 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004082 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 return -EINVAL;
4084 snum = ntohs(addr6->sin6_port);
4085 }
4086
Paul Moore3e112172008-04-10 10:48:14 -04004087 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 if (err)
4089 goto out;
4090
Paul Moore253bfae2010-04-22 14:46:19 -04004091 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004092 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4093
Thomas Liu2bf49692009-07-14 12:14:09 -04004094 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004095 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004096 ad.u.net = &net;
4097 ad.u.net->dport = htons(snum);
4098 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004099 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (err)
4101 goto out;
4102 }
4103
Paul Moore014ab192008-10-10 10:16:33 -04004104 err = selinux_netlbl_socket_connect(sk, address);
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106out:
4107 return err;
4108}
4109
4110static int selinux_socket_listen(struct socket *sock, int backlog)
4111{
Paul Moore253bfae2010-04-22 14:46:19 -04004112 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113}
4114
4115static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4116{
4117 int err;
4118 struct inode_security_struct *isec;
4119 struct inode_security_struct *newisec;
4120
Paul Moore253bfae2010-04-22 14:46:19 -04004121 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 if (err)
4123 return err;
4124
4125 newisec = SOCK_INODE(newsock)->i_security;
4126
4127 isec = SOCK_INODE(sock)->i_security;
4128 newisec->sclass = isec->sclass;
4129 newisec->sid = isec->sid;
4130 newisec->initialized = 1;
4131
4132 return 0;
4133}
4134
4135static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004136 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Paul Moore253bfae2010-04-22 14:46:19 -04004138 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
4141static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4142 int size, int flags)
4143{
Paul Moore253bfae2010-04-22 14:46:19 -04004144 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
4147static int selinux_socket_getsockname(struct socket *sock)
4148{
Paul Moore253bfae2010-04-22 14:46:19 -04004149 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
4152static int selinux_socket_getpeername(struct socket *sock)
4153{
Paul Moore253bfae2010-04-22 14:46:19 -04004154 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
Eric Paris828dfe12008-04-17 13:17:49 -04004157static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
Paul Mooref8687af2006-10-30 15:22:15 -08004159 int err;
4160
Paul Moore253bfae2010-04-22 14:46:19 -04004161 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004162 if (err)
4163 return err;
4164
4165 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
4168static int selinux_socket_getsockopt(struct socket *sock, int level,
4169 int optname)
4170{
Paul Moore253bfae2010-04-22 14:46:19 -04004171 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
4174static int selinux_socket_shutdown(struct socket *sock, int how)
4175{
Paul Moore253bfae2010-04-22 14:46:19 -04004176 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
David S. Miller3610cda2011-01-05 15:38:53 -08004179static int selinux_socket_unix_stream_connect(struct sock *sock,
4180 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 struct sock *newsk)
4182{
David S. Miller3610cda2011-01-05 15:38:53 -08004183 struct sk_security_struct *sksec_sock = sock->sk_security;
4184 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004185 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004186 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004187 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004188 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 int err;
4190
Thomas Liu2bf49692009-07-14 12:14:09 -04004191 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004192 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004193 ad.u.net = &net;
4194 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Paul Moore4d1e2452010-04-22 14:46:18 -04004196 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4197 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4199 if (err)
4200 return err;
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004203 sksec_new->peer_sid = sksec_sock->sid;
4204 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4205 &sksec_new->sid);
4206 if (err)
4207 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004208
Paul Moore4d1e2452010-04-22 14:46:18 -04004209 /* connecting socket */
4210 sksec_sock->peer_sid = sksec_new->sid;
4211
4212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
4215static int selinux_socket_unix_may_send(struct socket *sock,
4216 struct socket *other)
4217{
Paul Moore253bfae2010-04-22 14:46:19 -04004218 struct sk_security_struct *ssec = sock->sk->sk_security;
4219 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004220 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004221 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004222 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Thomas Liu2bf49692009-07-14 12:14:09 -04004224 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004225 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004226 ad.u.net = &net;
4227 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Paul Moore253bfae2010-04-22 14:46:19 -04004229 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4230 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Paul Mooreeffad8d2008-01-29 08:49:27 -05004233static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4234 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004235 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004236{
4237 int err;
4238 u32 if_sid;
4239 u32 node_sid;
4240
4241 err = sel_netif_sid(ifindex, &if_sid);
4242 if (err)
4243 return err;
4244 err = avc_has_perm(peer_sid, if_sid,
4245 SECCLASS_NETIF, NETIF__INGRESS, ad);
4246 if (err)
4247 return err;
4248
4249 err = sel_netnode_sid(addrp, family, &node_sid);
4250 if (err)
4251 return err;
4252 return avc_has_perm(peer_sid, node_sid,
4253 SECCLASS_NODE, NODE__RECVFROM, ad);
4254}
4255
Paul Moore220deb92008-01-29 08:38:23 -05004256static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004257 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004258{
Paul Moore277d3422008-12-31 12:54:11 -05004259 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004260 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004261 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004262 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004263 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004264 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004265 char *addrp;
4266
Thomas Liu2bf49692009-07-14 12:14:09 -04004267 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004268 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004269 ad.u.net = &net;
4270 ad.u.net->netif = skb->skb_iif;
4271 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004272 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4273 if (err)
4274 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004275
Paul Moore58bfbb52009-03-27 17:10:41 -04004276 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004277 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004278 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004279 if (err)
4280 return err;
4281 }
Paul Moore220deb92008-01-29 08:38:23 -05004282
Steffen Klassertb9679a72011-02-23 12:55:21 +01004283 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4284 if (err)
4285 return err;
4286 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004287
James Morris4e5ab4c2006-06-09 00:33:33 -07004288 return err;
4289}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004290
James Morris4e5ab4c2006-06-09 00:33:33 -07004291static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4292{
Paul Moore220deb92008-01-29 08:38:23 -05004293 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004294 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004295 u16 family = sk->sk_family;
4296 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004297 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004298 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004299 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004300 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004301 u8 secmark_active;
4302 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004303
James Morris4e5ab4c2006-06-09 00:33:33 -07004304 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004305 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004306
4307 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004308 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004309 family = PF_INET;
4310
Paul Moored8395c82008-10-10 10:16:30 -04004311 /* If any sort of compatibility mode is enabled then handoff processing
4312 * to the selinux_sock_rcv_skb_compat() function to deal with the
4313 * special handling. We do this in an attempt to keep this function
4314 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004315 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004316 return selinux_sock_rcv_skb_compat(sk, skb, family);
4317
4318 secmark_active = selinux_secmark_enabled();
4319 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4320 if (!secmark_active && !peerlbl_active)
4321 return 0;
4322
Thomas Liu2bf49692009-07-14 12:14:09 -04004323 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004324 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004325 ad.u.net = &net;
4326 ad.u.net->netif = skb->skb_iif;
4327 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004328 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004329 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004330 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004331
Paul Moored8395c82008-10-10 10:16:30 -04004332 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004333 u32 peer_sid;
4334
4335 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4336 if (err)
4337 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004338 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004339 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004340 if (err) {
4341 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004342 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004343 }
Paul Moored621d352008-01-29 08:43:36 -05004344 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4345 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004346 if (err)
4347 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004348 }
4349
Paul Moored8395c82008-10-10 10:16:30 -04004350 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004351 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4352 PACKET__RECV, &ad);
4353 if (err)
4354 return err;
4355 }
4356
Paul Moored621d352008-01-29 08:43:36 -05004357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004360static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4361 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
4363 int err = 0;
4364 char *scontext;
4365 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004366 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004367 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Paul Moore253bfae2010-04-22 14:46:19 -04004369 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4370 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004371 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004372 if (peer_sid == SECSID_NULL)
4373 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004375 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
4379 if (scontext_len > len) {
4380 err = -ERANGE;
4381 goto out_len;
4382 }
4383
4384 if (copy_to_user(optval, scontext, scontext_len))
4385 err = -EFAULT;
4386
4387out_len:
4388 if (put_user(scontext_len, optlen))
4389 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return err;
4392}
4393
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004394static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004395{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004396 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004397 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004398
Paul Mooreaa862902008-10-10 10:16:29 -04004399 if (skb && skb->protocol == htons(ETH_P_IP))
4400 family = PF_INET;
4401 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4402 family = PF_INET6;
4403 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004404 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004405 else
4406 goto out;
4407
4408 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004409 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004410 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004411 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004412
Paul Moore75e22912008-01-29 08:38:04 -05004413out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004414 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004415 if (peer_secid == SECSID_NULL)
4416 return -EINVAL;
4417 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004418}
4419
Al Viro7d877f32005-10-21 03:20:43 -04004420static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
Paul Moore84914b72010-04-22 14:46:18 -04004422 struct sk_security_struct *sksec;
4423
4424 sksec = kzalloc(sizeof(*sksec), priority);
4425 if (!sksec)
4426 return -ENOMEM;
4427
4428 sksec->peer_sid = SECINITSID_UNLABELED;
4429 sksec->sid = SECINITSID_UNLABELED;
4430 selinux_netlbl_sk_security_reset(sksec);
4431 sk->sk_security = sksec;
4432
4433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
4436static void selinux_sk_free_security(struct sock *sk)
4437{
Paul Moore84914b72010-04-22 14:46:18 -04004438 struct sk_security_struct *sksec = sk->sk_security;
4439
4440 sk->sk_security = NULL;
4441 selinux_netlbl_sk_security_free(sksec);
4442 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004445static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4446{
Eric Parisdd3e7832010-04-07 15:08:46 -04004447 struct sk_security_struct *sksec = sk->sk_security;
4448 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004449
Eric Parisdd3e7832010-04-07 15:08:46 -04004450 newsksec->sid = sksec->sid;
4451 newsksec->peer_sid = sksec->peer_sid;
4452 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004453
Eric Parisdd3e7832010-04-07 15:08:46 -04004454 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004455}
4456
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004457static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004458{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004459 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004460 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004461 else {
4462 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004463
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004464 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004465 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004466}
4467
Eric Paris828dfe12008-04-17 13:17:49 -04004468static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004469{
4470 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4471 struct sk_security_struct *sksec = sk->sk_security;
4472
David Woodhouse2148ccc2006-09-29 15:50:25 -07004473 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4474 sk->sk_family == PF_UNIX)
4475 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004476 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004477}
4478
Adrian Bunk9a673e52006-08-15 00:03:53 -07004479static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4480 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004481{
4482 struct sk_security_struct *sksec = sk->sk_security;
4483 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004484 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004485 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004486 u32 peersid;
4487
Paul Mooreaa862902008-10-10 10:16:29 -04004488 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4489 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4490 family = PF_INET;
4491
4492 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004493 if (err)
4494 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004495 if (peersid == SECSID_NULL) {
4496 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004497 req->peer_secid = SECSID_NULL;
Paul Moore389fb802009-03-27 17:10:34 -04004498 } else {
4499 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4500 if (err)
4501 return err;
4502 req->secid = newsid;
4503 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004504 }
4505
Paul Moore389fb802009-03-27 17:10:34 -04004506 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004507}
4508
Adrian Bunk9a673e52006-08-15 00:03:53 -07004509static void selinux_inet_csk_clone(struct sock *newsk,
4510 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004511{
4512 struct sk_security_struct *newsksec = newsk->sk_security;
4513
4514 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004515 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004516 /* NOTE: Ideally, we should also get the isec->sid for the
4517 new socket in sync, but we don't have the isec available yet.
4518 So we will wait until sock_graft to do it, by which
4519 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004520
Paul Moore9f2ad662006-11-17 17:38:53 -05004521 /* We don't need to take any sort of lock here as we are the only
4522 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004523 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004524}
4525
Paul Moore014ab192008-10-10 10:16:33 -04004526static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004527{
Paul Mooreaa862902008-10-10 10:16:29 -04004528 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004529 struct sk_security_struct *sksec = sk->sk_security;
4530
Paul Mooreaa862902008-10-10 10:16:29 -04004531 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4532 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4533 family = PF_INET;
4534
4535 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004536}
4537
Eric Paris2606fd12010-10-13 16:24:41 -04004538static int selinux_secmark_relabel_packet(u32 sid)
4539{
4540 const struct task_security_struct *__tsec;
4541 u32 tsid;
4542
4543 __tsec = current_security();
4544 tsid = __tsec->sid;
4545
4546 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4547}
4548
4549static void selinux_secmark_refcount_inc(void)
4550{
4551 atomic_inc(&selinux_secmark_refcount);
4552}
4553
4554static void selinux_secmark_refcount_dec(void)
4555{
4556 atomic_dec(&selinux_secmark_refcount);
4557}
4558
Adrian Bunk9a673e52006-08-15 00:03:53 -07004559static void selinux_req_classify_flow(const struct request_sock *req,
4560 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004561{
David S. Miller1d28f422011-03-12 00:29:39 -05004562 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004563}
4564
Paul Mooreed6d76e2009-08-28 18:12:49 -04004565static int selinux_tun_dev_create(void)
4566{
4567 u32 sid = current_sid();
4568
4569 /* we aren't taking into account the "sockcreate" SID since the socket
4570 * that is being created here is not a socket in the traditional sense,
4571 * instead it is a private sock, accessible only to the kernel, and
4572 * representing a wide range of network traffic spanning multiple
4573 * connections unlike traditional sockets - check the TUN driver to
4574 * get a better understanding of why this socket is special */
4575
4576 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4577 NULL);
4578}
4579
4580static void selinux_tun_dev_post_create(struct sock *sk)
4581{
4582 struct sk_security_struct *sksec = sk->sk_security;
4583
4584 /* we don't currently perform any NetLabel based labeling here and it
4585 * isn't clear that we would want to do so anyway; while we could apply
4586 * labeling without the support of the TUN user the resulting labeled
4587 * traffic from the other end of the connection would almost certainly
4588 * cause confusion to the TUN user that had no idea network labeling
4589 * protocols were being used */
4590
4591 /* see the comments in selinux_tun_dev_create() about why we don't use
4592 * the sockcreate SID here */
4593
4594 sksec->sid = current_sid();
4595 sksec->sclass = SECCLASS_TUN_SOCKET;
4596}
4597
4598static int selinux_tun_dev_attach(struct sock *sk)
4599{
4600 struct sk_security_struct *sksec = sk->sk_security;
4601 u32 sid = current_sid();
4602 int err;
4603
4604 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4605 TUN_SOCKET__RELABELFROM, NULL);
4606 if (err)
4607 return err;
4608 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4609 TUN_SOCKET__RELABELTO, NULL);
4610 if (err)
4611 return err;
4612
4613 sksec->sid = sid;
4614
4615 return 0;
4616}
4617
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4619{
4620 int err = 0;
4621 u32 perm;
4622 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004623 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 if (skb->len < NLMSG_SPACE(0)) {
4626 err = -EINVAL;
4627 goto out;
4628 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004629 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004630
Paul Moore253bfae2010-04-22 14:46:19 -04004631 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 if (err) {
4633 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004634 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 "SELinux: unrecognized netlink message"
4636 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004637 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004638 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 err = 0;
4640 }
4641
4642 /* Ignore */
4643 if (err == -ENOENT)
4644 err = 0;
4645 goto out;
4646 }
4647
Paul Moore253bfae2010-04-22 14:46:19 -04004648 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649out:
4650 return err;
4651}
4652
4653#ifdef CONFIG_NETFILTER
4654
Paul Mooreeffad8d2008-01-29 08:49:27 -05004655static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4656 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Paul Mooredfaebe92008-10-10 10:16:31 -04004658 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004659 char *addrp;
4660 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004661 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004662 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004663 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004664 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004665 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004666 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004667
Paul Mooreeffad8d2008-01-29 08:49:27 -05004668 if (!selinux_policycap_netpeer)
4669 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004670
Paul Mooreeffad8d2008-01-29 08:49:27 -05004671 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004672 netlbl_active = netlbl_enabled();
4673 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004674 if (!secmark_active && !peerlbl_active)
4675 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004676
Paul Moored8395c82008-10-10 10:16:30 -04004677 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4678 return NF_DROP;
4679
Thomas Liu2bf49692009-07-14 12:14:09 -04004680 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004681 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004682 ad.u.net = &net;
4683 ad.u.net->netif = ifindex;
4684 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004685 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4686 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Paul Mooredfaebe92008-10-10 10:16:31 -04004688 if (peerlbl_active) {
4689 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4690 peer_sid, &ad);
4691 if (err) {
4692 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004693 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004694 }
4695 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004696
4697 if (secmark_active)
4698 if (avc_has_perm(peer_sid, skb->secmark,
4699 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4700 return NF_DROP;
4701
Paul Moore948bf852008-10-10 10:16:32 -04004702 if (netlbl_active)
4703 /* we do this in the FORWARD path and not the POST_ROUTING
4704 * path because we want to make sure we apply the necessary
4705 * labeling before IPsec is applied so we can leverage AH
4706 * protection */
4707 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4708 return NF_DROP;
4709
Paul Mooreeffad8d2008-01-29 08:49:27 -05004710 return NF_ACCEPT;
4711}
4712
4713static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4714 struct sk_buff *skb,
4715 const struct net_device *in,
4716 const struct net_device *out,
4717 int (*okfn)(struct sk_buff *))
4718{
4719 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4720}
4721
4722#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4723static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4724 struct sk_buff *skb,
4725 const struct net_device *in,
4726 const struct net_device *out,
4727 int (*okfn)(struct sk_buff *))
4728{
4729 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4730}
4731#endif /* IPV6 */
4732
Paul Moore948bf852008-10-10 10:16:32 -04004733static unsigned int selinux_ip_output(struct sk_buff *skb,
4734 u16 family)
4735{
4736 u32 sid;
4737
4738 if (!netlbl_enabled())
4739 return NF_ACCEPT;
4740
4741 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4742 * because we want to make sure we apply the necessary labeling
4743 * before IPsec is applied so we can leverage AH protection */
4744 if (skb->sk) {
4745 struct sk_security_struct *sksec = skb->sk->sk_security;
4746 sid = sksec->sid;
4747 } else
4748 sid = SECINITSID_KERNEL;
4749 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4750 return NF_DROP;
4751
4752 return NF_ACCEPT;
4753}
4754
4755static unsigned int selinux_ipv4_output(unsigned int hooknum,
4756 struct sk_buff *skb,
4757 const struct net_device *in,
4758 const struct net_device *out,
4759 int (*okfn)(struct sk_buff *))
4760{
4761 return selinux_ip_output(skb, PF_INET);
4762}
4763
Paul Mooreeffad8d2008-01-29 08:49:27 -05004764static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4765 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004766 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004767{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004768 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004769 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004770 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004771 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004772 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004773 char *addrp;
4774 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004775
Paul Mooreeffad8d2008-01-29 08:49:27 -05004776 if (sk == NULL)
4777 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004778 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004779
Thomas Liu2bf49692009-07-14 12:14:09 -04004780 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004781 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004782 ad.u.net = &net;
4783 ad.u.net->netif = ifindex;
4784 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004785 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4786 return NF_DROP;
4787
Paul Moore58bfbb52009-03-27 17:10:41 -04004788 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004789 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004790 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004791 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004792
Steffen Klassertb9679a72011-02-23 12:55:21 +01004793 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4794 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004795
Paul Mooreeffad8d2008-01-29 08:49:27 -05004796 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
Paul Mooreeffad8d2008-01-29 08:49:27 -05004799static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4800 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004802 u32 secmark_perm;
4803 u32 peer_sid;
4804 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004805 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004806 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004807 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004808 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004809 u8 secmark_active;
4810 u8 peerlbl_active;
4811
Paul Mooreeffad8d2008-01-29 08:49:27 -05004812 /* If any sort of compatibility mode is enabled then handoff processing
4813 * to the selinux_ip_postroute_compat() function to deal with the
4814 * special handling. We do this in an attempt to keep this function
4815 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004816 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004817 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004818#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004819 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4820 * packet transformation so allow the packet to pass without any checks
4821 * since we'll have another chance to perform access control checks
4822 * when the packet is on it's final way out.
4823 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4824 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004825 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004826 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004827#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004828 secmark_active = selinux_secmark_enabled();
4829 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4830 if (!secmark_active && !peerlbl_active)
4831 return NF_ACCEPT;
4832
Paul Moored8395c82008-10-10 10:16:30 -04004833 /* if the packet is being forwarded then get the peer label from the
4834 * packet itself; otherwise check to see if it is from a local
4835 * application or the kernel, if from an application get the peer label
4836 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004837 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004838 if (sk == NULL) {
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004839 if (skb->skb_iif) {
4840 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004841 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004842 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004843 } else {
4844 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004845 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004846 }
Paul Moored8395c82008-10-10 10:16:30 -04004847 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004848 struct sk_security_struct *sksec = sk->sk_security;
4849 peer_sid = sksec->sid;
4850 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004851 }
4852
Thomas Liu2bf49692009-07-14 12:14:09 -04004853 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004854 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004855 ad.u.net = &net;
4856 ad.u.net->netif = ifindex;
4857 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004858 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004859 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004860
Paul Mooreeffad8d2008-01-29 08:49:27 -05004861 if (secmark_active)
4862 if (avc_has_perm(peer_sid, skb->secmark,
4863 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004864 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004865
4866 if (peerlbl_active) {
4867 u32 if_sid;
4868 u32 node_sid;
4869
4870 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004871 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004872 if (avc_has_perm(peer_sid, if_sid,
4873 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004874 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004875
4876 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004877 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004878 if (avc_has_perm(peer_sid, node_sid,
4879 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004880 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004881 }
4882
4883 return NF_ACCEPT;
4884}
4885
4886static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4887 struct sk_buff *skb,
4888 const struct net_device *in,
4889 const struct net_device *out,
4890 int (*okfn)(struct sk_buff *))
4891{
4892 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893}
4894
4895#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004896static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4897 struct sk_buff *skb,
4898 const struct net_device *in,
4899 const struct net_device *out,
4900 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004902 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904#endif /* IPV6 */
4905
4906#endif /* CONFIG_NETFILTER */
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 int err;
4911
Eric Paris200ac532009-02-12 15:01:04 -05004912 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 if (err)
4914 return err;
4915
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004916 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919static int ipc_alloc_security(struct task_struct *task,
4920 struct kern_ipc_perm *perm,
4921 u16 sclass)
4922{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004924 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
James Morris89d155e2005-10-30 14:59:21 -08004926 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 if (!isec)
4928 return -ENOMEM;
4929
David Howells275bb412008-11-14 10:39:19 +11004930 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004932 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 perm->security = isec;
4934
4935 return 0;
4936}
4937
4938static void ipc_free_security(struct kern_ipc_perm *perm)
4939{
4940 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 perm->security = NULL;
4942 kfree(isec);
4943}
4944
4945static int msg_msg_alloc_security(struct msg_msg *msg)
4946{
4947 struct msg_security_struct *msec;
4948
James Morris89d155e2005-10-30 14:59:21 -08004949 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 if (!msec)
4951 return -ENOMEM;
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 msec->sid = SECINITSID_UNLABELED;
4954 msg->security = msec;
4955
4956 return 0;
4957}
4958
4959static void msg_msg_free_security(struct msg_msg *msg)
4960{
4961 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 msg->security = NULL;
4964 kfree(msec);
4965}
4966
4967static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07004968 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004971 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004972 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11004973 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 isec = ipc_perms->security;
4976
Thomas Liu2bf49692009-07-14 12:14:09 -04004977 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004978 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 ad.u.ipc_id = ipc_perms->key;
4980
David Howells275bb412008-11-14 10:39:19 +11004981 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
4984static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
4985{
4986 return msg_msg_alloc_security(msg);
4987}
4988
4989static void selinux_msg_msg_free_security(struct msg_msg *msg)
4990{
4991 msg_msg_free_security(msg);
4992}
4993
4994/* message queue security operations */
4995static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
4996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004998 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004999 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005000 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 int rc;
5002
5003 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5004 if (rc)
5005 return rc;
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 isec = msq->q_perm.security;
5008
Thomas Liu2bf49692009-07-14 12:14:09 -04005009 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005010 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005011 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
David Howells275bb412008-11-14 10:39:19 +11005013 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 MSGQ__CREATE, &ad);
5015 if (rc) {
5016 ipc_free_security(&msq->q_perm);
5017 return rc;
5018 }
5019 return 0;
5020}
5021
5022static void selinux_msg_queue_free_security(struct msg_queue *msq)
5023{
5024 ipc_free_security(&msq->q_perm);
5025}
5026
5027static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005030 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005031 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005032 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 isec = msq->q_perm.security;
5035
Thomas Liu2bf49692009-07-14 12:14:09 -04005036 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005037 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 ad.u.ipc_id = msq->q_perm.key;
5039
David Howells275bb412008-11-14 10:39:19 +11005040 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 MSGQ__ASSOCIATE, &ad);
5042}
5043
5044static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5045{
5046 int err;
5047 int perms;
5048
Eric Paris828dfe12008-04-17 13:17:49 -04005049 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 case IPC_INFO:
5051 case MSG_INFO:
5052 /* No specific object, just general system-wide information. */
5053 return task_has_system(current, SYSTEM__IPC_INFO);
5054 case IPC_STAT:
5055 case MSG_STAT:
5056 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5057 break;
5058 case IPC_SET:
5059 perms = MSGQ__SETATTR;
5060 break;
5061 case IPC_RMID:
5062 perms = MSGQ__DESTROY;
5063 break;
5064 default:
5065 return 0;
5066 }
5067
Stephen Smalley6af963f2005-05-01 08:58:39 -07005068 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 return err;
5070}
5071
5072static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 struct ipc_security_struct *isec;
5075 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005076 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005077 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005078 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 int rc;
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 isec = msq->q_perm.security;
5082 msec = msg->security;
5083
5084 /*
5085 * First time through, need to assign label to the message
5086 */
5087 if (msec->sid == SECINITSID_UNLABELED) {
5088 /*
5089 * Compute new sid based on current process and
5090 * message queue this message will be stored in
5091 */
David Howells275bb412008-11-14 10:39:19 +11005092 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005093 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 if (rc)
5095 return rc;
5096 }
5097
Thomas Liu2bf49692009-07-14 12:14:09 -04005098 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005099 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 ad.u.ipc_id = msq->q_perm.key;
5101
5102 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005103 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 MSGQ__WRITE, &ad);
5105 if (!rc)
5106 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005107 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5108 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (!rc)
5110 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005111 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5112 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 return rc;
5115}
5116
5117static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5118 struct task_struct *target,
5119 long type, int mode)
5120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 struct ipc_security_struct *isec;
5122 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005123 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005124 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005125 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 int rc;
5127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 isec = msq->q_perm.security;
5129 msec = msg->security;
5130
Thomas Liu2bf49692009-07-14 12:14:09 -04005131 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005132 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005133 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
David Howells275bb412008-11-14 10:39:19 +11005135 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 SECCLASS_MSGQ, MSGQ__READ, &ad);
5137 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005138 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 SECCLASS_MSG, MSG__RECEIVE, &ad);
5140 return rc;
5141}
5142
5143/* Shared Memory security operations */
5144static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005147 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005148 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005149 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 int rc;
5151
5152 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5153 if (rc)
5154 return rc;
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 isec = shp->shm_perm.security;
5157
Thomas Liu2bf49692009-07-14 12:14:09 -04005158 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005159 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005160 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
David Howells275bb412008-11-14 10:39:19 +11005162 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 SHM__CREATE, &ad);
5164 if (rc) {
5165 ipc_free_security(&shp->shm_perm);
5166 return rc;
5167 }
5168 return 0;
5169}
5170
5171static void selinux_shm_free_security(struct shmid_kernel *shp)
5172{
5173 ipc_free_security(&shp->shm_perm);
5174}
5175
5176static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005179 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005180 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005181 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 isec = shp->shm_perm.security;
5184
Thomas Liu2bf49692009-07-14 12:14:09 -04005185 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005186 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 ad.u.ipc_id = shp->shm_perm.key;
5188
David Howells275bb412008-11-14 10:39:19 +11005189 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 SHM__ASSOCIATE, &ad);
5191}
5192
5193/* Note, at this point, shp is locked down */
5194static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5195{
5196 int perms;
5197 int err;
5198
Eric Paris828dfe12008-04-17 13:17:49 -04005199 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 case IPC_INFO:
5201 case SHM_INFO:
5202 /* No specific object, just general system-wide information. */
5203 return task_has_system(current, SYSTEM__IPC_INFO);
5204 case IPC_STAT:
5205 case SHM_STAT:
5206 perms = SHM__GETATTR | SHM__ASSOCIATE;
5207 break;
5208 case IPC_SET:
5209 perms = SHM__SETATTR;
5210 break;
5211 case SHM_LOCK:
5212 case SHM_UNLOCK:
5213 perms = SHM__LOCK;
5214 break;
5215 case IPC_RMID:
5216 perms = SHM__DESTROY;
5217 break;
5218 default:
5219 return 0;
5220 }
5221
Stephen Smalley6af963f2005-05-01 08:58:39 -07005222 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 return err;
5224}
5225
5226static int selinux_shm_shmat(struct shmid_kernel *shp,
5227 char __user *shmaddr, int shmflg)
5228{
5229 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231 if (shmflg & SHM_RDONLY)
5232 perms = SHM__READ;
5233 else
5234 perms = SHM__READ | SHM__WRITE;
5235
Stephen Smalley6af963f2005-05-01 08:58:39 -07005236 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237}
5238
5239/* Semaphore security operations */
5240static int selinux_sem_alloc_security(struct sem_array *sma)
5241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005243 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005244 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005245 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 int rc;
5247
5248 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5249 if (rc)
5250 return rc;
5251
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 isec = sma->sem_perm.security;
5253
Thomas Liu2bf49692009-07-14 12:14:09 -04005254 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005255 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005256 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
David Howells275bb412008-11-14 10:39:19 +11005258 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 SEM__CREATE, &ad);
5260 if (rc) {
5261 ipc_free_security(&sma->sem_perm);
5262 return rc;
5263 }
5264 return 0;
5265}
5266
5267static void selinux_sem_free_security(struct sem_array *sma)
5268{
5269 ipc_free_security(&sma->sem_perm);
5270}
5271
5272static int selinux_sem_associate(struct sem_array *sma, int semflg)
5273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005275 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005276 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005277 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 isec = sma->sem_perm.security;
5280
Thomas Liu2bf49692009-07-14 12:14:09 -04005281 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005282 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 ad.u.ipc_id = sma->sem_perm.key;
5284
David Howells275bb412008-11-14 10:39:19 +11005285 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 SEM__ASSOCIATE, &ad);
5287}
5288
5289/* Note, at this point, sma is locked down */
5290static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5291{
5292 int err;
5293 u32 perms;
5294
Eric Paris828dfe12008-04-17 13:17:49 -04005295 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 case IPC_INFO:
5297 case SEM_INFO:
5298 /* No specific object, just general system-wide information. */
5299 return task_has_system(current, SYSTEM__IPC_INFO);
5300 case GETPID:
5301 case GETNCNT:
5302 case GETZCNT:
5303 perms = SEM__GETATTR;
5304 break;
5305 case GETVAL:
5306 case GETALL:
5307 perms = SEM__READ;
5308 break;
5309 case SETVAL:
5310 case SETALL:
5311 perms = SEM__WRITE;
5312 break;
5313 case IPC_RMID:
5314 perms = SEM__DESTROY;
5315 break;
5316 case IPC_SET:
5317 perms = SEM__SETATTR;
5318 break;
5319 case IPC_STAT:
5320 case SEM_STAT:
5321 perms = SEM__GETATTR | SEM__ASSOCIATE;
5322 break;
5323 default:
5324 return 0;
5325 }
5326
Stephen Smalley6af963f2005-05-01 08:58:39 -07005327 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 return err;
5329}
5330
5331static int selinux_sem_semop(struct sem_array *sma,
5332 struct sembuf *sops, unsigned nsops, int alter)
5333{
5334 u32 perms;
5335
5336 if (alter)
5337 perms = SEM__READ | SEM__WRITE;
5338 else
5339 perms = SEM__READ;
5340
Stephen Smalley6af963f2005-05-01 08:58:39 -07005341 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
5344static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 u32 av = 0;
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 av = 0;
5349 if (flag & S_IRUGO)
5350 av |= IPC__UNIX_READ;
5351 if (flag & S_IWUGO)
5352 av |= IPC__UNIX_WRITE;
5353
5354 if (av == 0)
5355 return 0;
5356
Stephen Smalley6af963f2005-05-01 08:58:39 -07005357 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005360static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5361{
5362 struct ipc_security_struct *isec = ipcp->security;
5363 *secid = isec->sid;
5364}
5365
Eric Paris828dfe12008-04-17 13:17:49 -04005366static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
5368 if (inode)
5369 inode_doinit_with_dentry(inode, dentry);
5370}
5371
5372static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005373 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
David Howells275bb412008-11-14 10:39:19 +11005375 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005376 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005378 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
5380 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005381 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 if (error)
5383 return error;
5384 }
5385
David Howells275bb412008-11-14 10:39:19 +11005386 rcu_read_lock();
5387 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
5389 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005390 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005392 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005394 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005396 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005397 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005398 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005399 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005400 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 else
David Howells275bb412008-11-14 10:39:19 +11005402 goto invalid;
5403 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
5405 if (!sid)
5406 return 0;
5407
Al Viro04ff9702007-03-12 16:17:58 +00005408 error = security_sid_to_context(sid, value, &len);
5409 if (error)
5410 return error;
5411 return len;
David Howells275bb412008-11-14 10:39:19 +11005412
5413invalid:
5414 rcu_read_unlock();
5415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416}
5417
5418static int selinux_setprocattr(struct task_struct *p,
5419 char *name, void *value, size_t size)
5420{
5421 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005422 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005423 struct cred *new;
5424 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 int error;
5426 char *str = value;
5427
5428 if (current != p) {
5429 /* SELinux only allows a process to change its own
5430 security attributes. */
5431 return -EACCES;
5432 }
5433
5434 /*
5435 * Basic control over ability to set these attributes at all.
5436 * current == p, but we'll pass them separately in case the
5437 * above restriction is ever removed.
5438 */
5439 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005440 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005442 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005443 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005444 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005445 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005446 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005448 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 else
5450 error = -EINVAL;
5451 if (error)
5452 return error;
5453
5454 /* Obtain a SID for the context, if one was specified. */
5455 if (size && str[1] && str[1] != '\n') {
5456 if (str[size-1] == '\n') {
5457 str[size-1] = 0;
5458 size--;
5459 }
5460 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005461 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5462 if (!capable(CAP_MAC_ADMIN))
5463 return error;
5464 error = security_context_to_sid_force(value, size,
5465 &sid);
5466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 if (error)
5468 return error;
5469 }
5470
David Howellsd84f4f92008-11-14 10:39:23 +11005471 new = prepare_creds();
5472 if (!new)
5473 return -ENOMEM;
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 /* Permission checking based on the specified context is
5476 performed during the actual operation (execve,
5477 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005478 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 checks and may_create for the file creation checks. The
5480 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005481 tsec = new->security;
5482 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005484 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005486 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005487 error = may_create_key(sid, p);
5488 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005489 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005490 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005491 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005492 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005493 } else if (!strcmp(name, "current")) {
5494 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005496 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005497
David Howellsd84f4f92008-11-14 10:39:23 +11005498 /* Only allow single threaded processes to change context */
5499 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005500 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005501 error = security_bounded_transition(tsec->sid, sid);
5502 if (error)
5503 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 /* Check permissions for the transition. */
5507 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005508 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005510 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512 /* Check for ptracing, and update the task SID if ok.
5513 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005514 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005516 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005517 if (tracer)
5518 ptsid = task_sid(tracer);
5519 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
David Howellsd84f4f92008-11-14 10:39:23 +11005521 if (tracer) {
5522 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5523 PROCESS__PTRACE, NULL);
5524 if (error)
5525 goto abort_change;
5526 }
5527
5528 tsec->sid = sid;
5529 } else {
5530 error = -EINVAL;
5531 goto abort_change;
5532 }
5533
5534 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005536
5537abort_change:
5538 abort_creds(new);
5539 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540}
5541
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005542static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5543{
5544 return security_sid_to_context(secid, secdata, seclen);
5545}
5546
David Howells7bf570d2008-04-29 20:52:51 +01005547static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005548{
5549 return security_context_to_sid(secdata, seclen, secid);
5550}
5551
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005552static void selinux_release_secctx(char *secdata, u32 seclen)
5553{
Paul Moore088999e2007-08-01 11:12:58 -04005554 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005555}
5556
David P. Quigley1ee65e32009-09-03 14:25:57 -04005557/*
5558 * called with inode->i_mutex locked
5559 */
5560static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5561{
5562 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5563}
5564
5565/*
5566 * called with inode->i_mutex locked
5567 */
5568static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5569{
5570 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5571}
5572
5573static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5574{
5575 int len = 0;
5576 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5577 ctx, true);
5578 if (len < 0)
5579 return len;
5580 *ctxlen = len;
5581 return 0;
5582}
Michael LeMayd7200242006-06-22 14:47:17 -07005583#ifdef CONFIG_KEYS
5584
David Howellsd84f4f92008-11-14 10:39:23 +11005585static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005586 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005587{
David Howellsd84f4f92008-11-14 10:39:23 +11005588 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005589 struct key_security_struct *ksec;
5590
5591 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5592 if (!ksec)
5593 return -ENOMEM;
5594
David Howellsd84f4f92008-11-14 10:39:23 +11005595 tsec = cred->security;
5596 if (tsec->keycreate_sid)
5597 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005598 else
David Howellsd84f4f92008-11-14 10:39:23 +11005599 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005600
David Howells275bb412008-11-14 10:39:19 +11005601 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005602 return 0;
5603}
5604
5605static void selinux_key_free(struct key *k)
5606{
5607 struct key_security_struct *ksec = k->security;
5608
5609 k->security = NULL;
5610 kfree(ksec);
5611}
5612
5613static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005614 const struct cred *cred,
5615 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005616{
5617 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005618 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005619 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005620
5621 /* if no specific permissions are requested, we skip the
5622 permission check. No serious, additional covert channels
5623 appear to be created. */
5624 if (perm == 0)
5625 return 0;
5626
David Howellsd84f4f92008-11-14 10:39:23 +11005627 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005628
5629 key = key_ref_to_ptr(key_ref);
5630 ksec = key->security;
5631
5632 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005633}
5634
David Howells70a5bb72008-04-29 01:01:26 -07005635static int selinux_key_getsecurity(struct key *key, char **_buffer)
5636{
5637 struct key_security_struct *ksec = key->security;
5638 char *context = NULL;
5639 unsigned len;
5640 int rc;
5641
5642 rc = security_sid_to_context(ksec->sid, &context, &len);
5643 if (!rc)
5644 rc = len;
5645 *_buffer = context;
5646 return rc;
5647}
5648
Michael LeMayd7200242006-06-22 14:47:17 -07005649#endif
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005652 .name = "selinux",
5653
Stephen Smalley48a23702012-11-05 08:15:34 -05005654 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5655 .binder_transaction = selinux_binder_transaction,
5656 .binder_transfer_binder = selinux_binder_transfer_binder,
5657 .binder_transfer_file = selinux_binder_transfer_file,
5658
Ingo Molnar9e488582009-05-07 19:26:19 +10005659 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005660 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005662 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 .capable = selinux_capable,
5664 .quotactl = selinux_quotactl,
5665 .quota_on = selinux_quota_on,
5666 .syslog = selinux_syslog,
5667 .vm_enough_memory = selinux_vm_enough_memory,
5668
5669 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
David Howellsa6f76f22008-11-14 10:39:24 +11005671 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005672 .bprm_committing_creds = selinux_bprm_committing_creds,
5673 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 .bprm_secureexec = selinux_bprm_secureexec,
5675
5676 .sb_alloc_security = selinux_sb_alloc_security,
5677 .sb_free_security = selinux_sb_free_security,
5678 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005679 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005680 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005681 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 .sb_statfs = selinux_sb_statfs,
5683 .sb_mount = selinux_mount,
5684 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005685 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005686 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005687 .sb_parse_opts_str = selinux_parse_opts_str,
5688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690 .inode_alloc_security = selinux_inode_alloc_security,
5691 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005692 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 .inode_create = selinux_inode_create,
Amir Samuelov6a22e462014-05-26 11:44:06 +03005694 .inode_post_create = selinux_inode_post_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 .inode_unlink = selinux_inode_unlink,
5697 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 .inode_rmdir = selinux_inode_rmdir,
5700 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 .inode_readlink = selinux_inode_readlink,
5703 .inode_follow_link = selinux_inode_follow_link,
5704 .inode_permission = selinux_inode_permission,
5705 .inode_setattr = selinux_inode_setattr,
5706 .inode_getattr = selinux_inode_getattr,
5707 .inode_setxattr = selinux_inode_setxattr,
5708 .inode_post_setxattr = selinux_inode_post_setxattr,
5709 .inode_getxattr = selinux_inode_getxattr,
5710 .inode_listxattr = selinux_inode_listxattr,
5711 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005712 .inode_getsecurity = selinux_inode_getsecurity,
5713 .inode_setsecurity = selinux_inode_setsecurity,
5714 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005715 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 .file_permission = selinux_file_permission,
5718 .file_alloc_security = selinux_file_alloc_security,
5719 .file_free_security = selinux_file_free_security,
5720 .file_ioctl = selinux_file_ioctl,
5721 .file_mmap = selinux_file_mmap,
5722 .file_mprotect = selinux_file_mprotect,
5723 .file_lock = selinux_file_lock,
5724 .file_fcntl = selinux_file_fcntl,
5725 .file_set_fowner = selinux_file_set_fowner,
5726 .file_send_sigiotask = selinux_file_send_sigiotask,
5727 .file_receive = selinux_file_receive,
5728
Eric Paris828dfe12008-04-17 13:17:49 -04005729 .dentry_open = selinux_dentry_open,
Amir Samuelov6a22e462014-05-26 11:44:06 +03005730 .file_close = selinux_file_close,
5731 .allow_merge_bio = selinux_allow_merge_bio,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005734 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005735 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005736 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005737 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005738 .kernel_act_as = selinux_kernel_act_as,
5739 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005740 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 .task_setpgid = selinux_task_setpgid,
5742 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005743 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005744 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005746 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005747 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 .task_setrlimit = selinux_task_setrlimit,
5749 .task_setscheduler = selinux_task_setscheduler,
5750 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005751 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 .task_kill = selinux_task_kill,
5753 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005754 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005757 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
5759 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5760 .msg_msg_free_security = selinux_msg_msg_free_security,
5761
5762 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5763 .msg_queue_free_security = selinux_msg_queue_free_security,
5764 .msg_queue_associate = selinux_msg_queue_associate,
5765 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5766 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5767 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5768
5769 .shm_alloc_security = selinux_shm_alloc_security,
5770 .shm_free_security = selinux_shm_free_security,
5771 .shm_associate = selinux_shm_associate,
5772 .shm_shmctl = selinux_shm_shmctl,
5773 .shm_shmat = selinux_shm_shmat,
5774
Eric Paris828dfe12008-04-17 13:17:49 -04005775 .sem_alloc_security = selinux_sem_alloc_security,
5776 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 .sem_associate = selinux_sem_associate,
5778 .sem_semctl = selinux_sem_semctl,
5779 .sem_semop = selinux_sem_semop,
5780
Eric Paris828dfe12008-04-17 13:17:49 -04005781 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Eric Paris828dfe12008-04-17 13:17:49 -04005783 .getprocattr = selinux_getprocattr,
5784 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005786 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005787 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005788 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005789 .inode_notifysecctx = selinux_inode_notifysecctx,
5790 .inode_setsecctx = selinux_inode_setsecctx,
5791 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005792
Eric Paris828dfe12008-04-17 13:17:49 -04005793 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 .unix_may_send = selinux_socket_unix_may_send,
5795
5796 .socket_create = selinux_socket_create,
5797 .socket_post_create = selinux_socket_post_create,
5798 .socket_bind = selinux_socket_bind,
5799 .socket_connect = selinux_socket_connect,
5800 .socket_listen = selinux_socket_listen,
5801 .socket_accept = selinux_socket_accept,
5802 .socket_sendmsg = selinux_socket_sendmsg,
5803 .socket_recvmsg = selinux_socket_recvmsg,
5804 .socket_getsockname = selinux_socket_getsockname,
5805 .socket_getpeername = selinux_socket_getpeername,
5806 .socket_getsockopt = selinux_socket_getsockopt,
5807 .socket_setsockopt = selinux_socket_setsockopt,
5808 .socket_shutdown = selinux_socket_shutdown,
5809 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005810 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5811 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 .sk_alloc_security = selinux_sk_alloc_security,
5813 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005814 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005815 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005816 .sock_graft = selinux_sock_graft,
5817 .inet_conn_request = selinux_inet_conn_request,
5818 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005819 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005820 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5821 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5822 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005823 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005824 .tun_dev_create = selinux_tun_dev_create,
5825 .tun_dev_post_create = selinux_tun_dev_post_create,
5826 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005827
5828#ifdef CONFIG_SECURITY_NETWORK_XFRM
5829 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5830 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5831 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005832 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005833 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5834 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005835 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005836 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005837 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005838 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005840
5841#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005842 .key_alloc = selinux_key_alloc,
5843 .key_free = selinux_key_free,
5844 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005845 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005846#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005847
5848#ifdef CONFIG_AUDIT
5849 .audit_rule_init = selinux_audit_rule_init,
5850 .audit_rule_known = selinux_audit_rule_known,
5851 .audit_rule_match = selinux_audit_rule_match,
5852 .audit_rule_free = selinux_audit_rule_free,
5853#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854};
5855
5856static __init int selinux_init(void)
5857{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005858 if (!security_module_enable(&selinux_ops)) {
5859 selinux_enabled = 0;
5860 return 0;
5861 }
5862
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 if (!selinux_enabled) {
5864 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5865 return 0;
5866 }
5867
5868 printk(KERN_INFO "SELinux: Initializing.\n");
5869
5870 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005871 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005873 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5874
James Morris7cae7e22006-03-22 00:09:22 -08005875 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5876 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005877 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 avc_init();
5879
Eric Paris828dfe12008-04-17 13:17:49 -04005880 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 panic("SELinux: Unable to register with kernel.\n");
5882
Eric Paris828dfe12008-04-17 13:17:49 -04005883 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005884 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005885 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005886 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005887
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return 0;
5889}
5890
Al Viroe8c26252010-03-23 06:36:54 -04005891static void delayed_superblock_init(struct super_block *sb, void *unused)
5892{
5893 superblock_doinit(sb, NULL);
5894}
5895
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896void selinux_complete_init(void)
5897{
Eric Parisfadcdb42007-02-22 18:11:31 -05005898 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
5900 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005901 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005902 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903}
5904
5905/* SELinux requires early initialization in order to label
5906 all processes and objects when they are created. */
5907security_initcall(selinux_init);
5908
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005909#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
Paul Mooreeffad8d2008-01-29 08:49:27 -05005911static struct nf_hook_ops selinux_ipv4_ops[] = {
5912 {
5913 .hook = selinux_ipv4_postroute,
5914 .owner = THIS_MODULE,
5915 .pf = PF_INET,
5916 .hooknum = NF_INET_POST_ROUTING,
5917 .priority = NF_IP_PRI_SELINUX_LAST,
5918 },
5919 {
5920 .hook = selinux_ipv4_forward,
5921 .owner = THIS_MODULE,
5922 .pf = PF_INET,
5923 .hooknum = NF_INET_FORWARD,
5924 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005925 },
5926 {
5927 .hook = selinux_ipv4_output,
5928 .owner = THIS_MODULE,
5929 .pf = PF_INET,
5930 .hooknum = NF_INET_LOCAL_OUT,
5931 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933};
5934
5935#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5936
Paul Mooreeffad8d2008-01-29 08:49:27 -05005937static struct nf_hook_ops selinux_ipv6_ops[] = {
5938 {
5939 .hook = selinux_ipv6_postroute,
5940 .owner = THIS_MODULE,
5941 .pf = PF_INET6,
5942 .hooknum = NF_INET_POST_ROUTING,
5943 .priority = NF_IP6_PRI_SELINUX_LAST,
5944 },
5945 {
5946 .hook = selinux_ipv6_forward,
5947 .owner = THIS_MODULE,
5948 .pf = PF_INET6,
5949 .hooknum = NF_INET_FORWARD,
5950 .priority = NF_IP6_PRI_SELINUX_FIRST,
5951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952};
5953
5954#endif /* IPV6 */
5955
5956static int __init selinux_nf_ip_init(void)
5957{
5958 int err = 0;
5959
5960 if (!selinux_enabled)
5961 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05005962
5963 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
5964
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005965 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
5966 if (err)
5967 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
5969#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005970 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
5971 if (err)
5972 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005974
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975out:
5976 return err;
5977}
5978
5979__initcall(selinux_nf_ip_init);
5980
5981#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5982static void selinux_nf_ip_exit(void)
5983{
Eric Parisfadcdb42007-02-22 18:11:31 -05005984 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005986 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005988 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989#endif /* IPV6 */
5990}
5991#endif
5992
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005993#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
5995#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5996#define selinux_nf_ip_exit()
5997#endif
5998
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005999#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
6001#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006002static int selinux_disabled;
6003
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004int selinux_disable(void)
6005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 if (ss_initialized) {
6007 /* Not permitted after initial policy load. */
6008 return -EINVAL;
6009 }
6010
6011 if (selinux_disabled) {
6012 /* Only do this once. */
6013 return -EINVAL;
6014 }
6015
6016 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6017
6018 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006019 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006021 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Eric Parisaf8ff042009-09-20 21:23:01 -04006023 /* Try to destroy the avc node cache */
6024 avc_disable();
6025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 /* Unregister netfilter hooks. */
6027 selinux_nf_ip_exit();
6028
6029 /* Unregister selinuxfs. */
6030 exit_sel_fs();
6031
6032 return 0;
6033}
6034#endif