blob: 706529bd1dd7a1537499daa5677ae90bfe11ce3a [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
Jeff Vander Stoep581be712015-07-10 17:19:56 -04003096/*
3097 * Check whether a task has the ioctl permission and cmd
3098 * operation to an inode.
3099 */
3100int ioctl_has_perm(const struct cred *cred, struct file *file,
3101 u32 requested, u16 cmd)
3102{
3103 struct common_audit_data ad;
3104 struct file_security_struct *fsec = file->f_security;
3105 struct inode *inode = file->f_path.dentry->d_inode;
3106 struct inode_security_struct *isec = inode->i_security;
3107 struct lsm_ioctlop_audit ioctl;
3108 u32 ssid = cred_sid(cred);
3109 struct selinux_audit_data sad = {0,};
3110 int rc;
3111 u8 driver = cmd >> 8;
3112 u8 xperm = cmd & 0xff;
3113
3114 COMMON_AUDIT_DATA_INIT(&ad, IOCTL_OP);
3115 ad.u.op = &ioctl;
3116 ad.u.op->cmd = cmd;
3117 ad.selinux_audit_data = &sad;
3118 ad.u.op->path = file->f_path;
3119
3120 if (ssid != fsec->sid) {
3121 rc = avc_has_perm(ssid, fsec->sid,
3122 SECCLASS_FD,
3123 FD__USE,
3124 &ad);
3125 if (rc)
3126 goto out;
3127 }
3128
3129 if (unlikely(IS_PRIVATE(inode)))
3130 return 0;
3131
3132 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
3133 requested, driver, xperm, &ad);
3134out:
3135 return rc;
3136}
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3139 unsigned long arg)
3140{
David Howells88e67f32008-11-14 10:39:21 +11003141 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003142 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Eric Paris0b24dcb2011-02-25 15:39:20 -05003144 switch (cmd) {
3145 case FIONREAD:
3146 /* fall through */
3147 case FIBMAP:
3148 /* fall through */
3149 case FIGETBSZ:
3150 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003151 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003152 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003153 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003154 error = file_has_perm(cred, file, FILE__GETATTR);
3155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Al Viro2f99c362012-03-23 16:04:05 -04003157 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003158 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003159 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003160 error = file_has_perm(cred, file, FILE__SETATTR);
3161 break;
3162
3163 /* sys_ioctl() checks */
3164 case FIONBIO:
3165 /* fall through */
3166 case FIOASYNC:
3167 error = file_has_perm(cred, file, 0);
3168 break;
3169
3170 case KDSKBENT:
3171 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003172 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3173 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003174 break;
3175
3176 /* default case assumes that the command will go
3177 * to the file's ioctl() function.
3178 */
3179 default:
Jeff Vander Stoep581be712015-07-10 17:19:56 -04003180 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003181 }
3182 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003185static int default_noexec;
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3188{
David Howells88e67f32008-11-14 10:39:21 +11003189 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003190 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003191
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003192 if (default_noexec &&
3193 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /*
3195 * We are making executable an anonymous mapping or a
3196 * private file mapping that will also be writable.
3197 * This has an additional check.
3198 */
David Howellsd84f4f92008-11-14 10:39:23 +11003199 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003201 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 if (file) {
3205 /* read access is always possible with a mapping */
3206 u32 av = FILE__READ;
3207
3208 /* write access only matters if the mapping is shared */
3209 if (shared && (prot & PROT_WRITE))
3210 av |= FILE__WRITE;
3211
3212 if (prot & PROT_EXEC)
3213 av |= FILE__EXECUTE;
3214
David Howells88e67f32008-11-14 10:39:21 +11003215 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
David Howellsd84f4f92008-11-14 10:39:23 +11003217
3218error:
3219 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
3222static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003223 unsigned long prot, unsigned long flags,
3224 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Eric Parised032182007-06-28 15:55:21 -04003226 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003227 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Eric Paris84336d1a2009-07-31 12:54:05 -04003229 /*
3230 * notice that we are intentionally putting the SELinux check before
3231 * the secondary cap_file_mmap check. This is such a likely attempt
3232 * at bad behaviour/exploit that we always want to get the AVC, even
3233 * if DAC would have also denied the operation.
3234 */
Eric Parisa2551df2009-07-31 12:54:11 -04003235 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003236 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3237 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003238 if (rc)
3239 return rc;
3240 }
3241
3242 /* do DAC check on address space usage */
3243 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003244 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return rc;
3246
3247 if (selinux_checkreqprot)
3248 prot = reqprot;
3249
3250 return file_map_prot_check(file, prot,
3251 (flags & MAP_TYPE) == MAP_SHARED);
3252}
3253
3254static int selinux_file_mprotect(struct vm_area_struct *vma,
3255 unsigned long reqprot,
3256 unsigned long prot)
3257{
David Howells88e67f32008-11-14 10:39:21 +11003258 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 if (selinux_checkreqprot)
3261 prot = reqprot;
3262
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003263 if (default_noexec &&
3264 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003265 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003266 if (vma->vm_start >= vma->vm_mm->start_brk &&
3267 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003268 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003269 } else if (!vma->vm_file &&
3270 vma->vm_start <= vma->vm_mm->start_stack &&
3271 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003272 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003273 } else if (vma->vm_file && vma->anon_vma) {
3274 /*
3275 * We are making executable a file mapping that has
3276 * had some COW done. Since pages might have been
3277 * written, check ability to execute the possibly
3278 * modified content. This typically should only
3279 * occur for text relocations.
3280 */
David Howellsd84f4f92008-11-14 10:39:23 +11003281 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003282 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003283 if (rc)
3284 return rc;
3285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3288}
3289
3290static int selinux_file_lock(struct file *file, unsigned int cmd)
3291{
David Howells88e67f32008-11-14 10:39:21 +11003292 const struct cred *cred = current_cred();
3293
3294 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
3297static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3298 unsigned long arg)
3299{
David Howells88e67f32008-11-14 10:39:21 +11003300 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 int err = 0;
3302
3303 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003304 case F_SETFL:
3305 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3306 err = -EINVAL;
3307 break;
3308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Eric Paris828dfe12008-04-17 13:17:49 -04003310 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003311 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003313 }
3314 /* fall through */
3315 case F_SETOWN:
3316 case F_SETSIG:
3317 case F_GETFL:
3318 case F_GETOWN:
3319 case F_GETSIG:
3320 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003321 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003322 break;
3323 case F_GETLK:
3324 case F_SETLK:
3325 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003327 case F_GETLK64:
3328 case F_SETLK64:
3329 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003331 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3332 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003334 }
David Howells88e67f32008-11-14 10:39:21 +11003335 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
3338
3339 return err;
3340}
3341
3342static int selinux_file_set_fowner(struct file *file)
3343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct file_security_struct *fsec;
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003347 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 return 0;
3350}
3351
3352static int selinux_file_send_sigiotask(struct task_struct *tsk,
3353 struct fown_struct *fown, int signum)
3354{
Eric Paris828dfe12008-04-17 13:17:49 -04003355 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003356 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct file_security_struct *fsec;
3359
3360 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003361 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 fsec = file->f_security;
3364
3365 if (!signum)
3366 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3367 else
3368 perm = signal_to_av(signum);
3369
David Howells275bb412008-11-14 10:39:19 +11003370 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 SECCLASS_PROCESS, perm, NULL);
3372}
3373
3374static int selinux_file_receive(struct file *file)
3375{
David Howells88e67f32008-11-14 10:39:21 +11003376 const struct cred *cred = current_cred();
3377
3378 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
David Howells745ca242008-11-14 10:39:22 +11003381static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003382{
3383 struct file_security_struct *fsec;
3384 struct inode *inode;
3385 struct inode_security_struct *isec;
Amir Samuelov6a22e462014-05-26 11:44:06 +03003386 int ret;
3387
3388 ret = pft_file_open(file, cred);
3389 if (ret < 0)
3390 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +11003391
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003392 inode = file->f_path.dentry->d_inode;
3393 fsec = file->f_security;
3394 isec = inode->i_security;
3395 /*
3396 * Save inode label and policy sequence number
3397 * at open-time so that selinux_file_permission
3398 * can determine whether revalidation is necessary.
3399 * Task label is already saved in the file security
3400 * struct as its SID.
3401 */
3402 fsec->isid = isec->sid;
3403 fsec->pseqno = avc_policy_seqno();
3404 /*
3405 * Since the inode label or policy seqno may have changed
3406 * between the selinux_inode_permission check and the saving
3407 * of state above, recheck that access is still permitted.
3408 * Otherwise, access might never be revalidated against the
3409 * new inode label or new policy.
3410 * This check is not redundant - do not remove.
3411 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003412 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003413}
3414
Amir Samuelov6a22e462014-05-26 11:44:06 +03003415static int selinux_file_close(struct file *file)
3416{
3417 return pft_file_close(file);
3418}
3419
3420static bool selinux_allow_merge_bio(struct bio *bio1, struct bio *bio2)
3421{
3422 return pft_allow_merge_bio(bio1, bio2);
3423}
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425/* task security operations */
3426
3427static int selinux_task_create(unsigned long clone_flags)
3428{
David Howells3b11a1d2008-11-14 10:39:26 +11003429 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
David Howellsf1752ee2008-11-14 10:39:17 +11003432/*
David Howellsee18d642009-09-02 09:14:21 +01003433 * allocate the SELinux part of blank credentials
3434 */
3435static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3436{
3437 struct task_security_struct *tsec;
3438
3439 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3440 if (!tsec)
3441 return -ENOMEM;
3442
3443 cred->security = tsec;
3444 return 0;
3445}
3446
3447/*
David Howellsf1752ee2008-11-14 10:39:17 +11003448 * detach and free the LSM part of a set of credentials
3449 */
3450static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
David Howellsf1752ee2008-11-14 10:39:17 +11003452 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003453
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003454 /*
3455 * cred->security == NULL if security_cred_alloc_blank() or
3456 * security_prepare_creds() returned an error.
3457 */
3458 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003459 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003460 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
David Howellsd84f4f92008-11-14 10:39:23 +11003463/*
3464 * prepare a new set of credentials for modification
3465 */
3466static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3467 gfp_t gfp)
3468{
3469 const struct task_security_struct *old_tsec;
3470 struct task_security_struct *tsec;
3471
3472 old_tsec = old->security;
3473
3474 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3475 if (!tsec)
3476 return -ENOMEM;
3477
3478 new->security = tsec;
3479 return 0;
3480}
3481
3482/*
David Howellsee18d642009-09-02 09:14:21 +01003483 * transfer the SELinux data to a blank set of creds
3484 */
3485static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3486{
3487 const struct task_security_struct *old_tsec = old->security;
3488 struct task_security_struct *tsec = new->security;
3489
3490 *tsec = *old_tsec;
3491}
3492
3493/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003494 * set the security data for a kernel service
3495 * - all the creation contexts are set to unlabelled
3496 */
3497static int selinux_kernel_act_as(struct cred *new, u32 secid)
3498{
3499 struct task_security_struct *tsec = new->security;
3500 u32 sid = current_sid();
3501 int ret;
3502
3503 ret = avc_has_perm(sid, secid,
3504 SECCLASS_KERNEL_SERVICE,
3505 KERNEL_SERVICE__USE_AS_OVERRIDE,
3506 NULL);
3507 if (ret == 0) {
3508 tsec->sid = secid;
3509 tsec->create_sid = 0;
3510 tsec->keycreate_sid = 0;
3511 tsec->sockcreate_sid = 0;
3512 }
3513 return ret;
3514}
3515
3516/*
3517 * set the file creation context in a security record to the same as the
3518 * objective context of the specified inode
3519 */
3520static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3521{
3522 struct inode_security_struct *isec = inode->i_security;
3523 struct task_security_struct *tsec = new->security;
3524 u32 sid = current_sid();
3525 int ret;
3526
3527 ret = avc_has_perm(sid, isec->sid,
3528 SECCLASS_KERNEL_SERVICE,
3529 KERNEL_SERVICE__CREATE_FILES_AS,
3530 NULL);
3531
3532 if (ret == 0)
3533 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003534 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003535}
3536
Eric Parisdd8dbf22009-11-03 16:35:32 +11003537static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003538{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003539 u32 sid;
3540 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003541 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003542
3543 sid = task_sid(current);
3544
3545 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003546 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003547 ad.u.kmod_name = kmod_name;
3548
3549 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3550 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003551}
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3554{
David Howells3b11a1d2008-11-14 10:39:26 +11003555 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
3558static int selinux_task_getpgid(struct task_struct *p)
3559{
David Howells3b11a1d2008-11-14 10:39:26 +11003560 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
3563static int selinux_task_getsid(struct task_struct *p)
3564{
David Howells3b11a1d2008-11-14 10:39:26 +11003565 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
David Quigleyf9008e42006-06-30 01:55:46 -07003568static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3569{
David Howells275bb412008-11-14 10:39:19 +11003570 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003571}
3572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573static int selinux_task_setnice(struct task_struct *p, int nice)
3574{
3575 int rc;
3576
Eric Paris200ac532009-02-12 15:01:04 -05003577 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20: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
James Morris03e68062006-06-23 02:03:58 -07003584static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3585{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003586 int rc;
3587
Eric Paris200ac532009-02-12 15:01:04 -05003588 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003589 if (rc)
3590 return rc;
3591
David Howells3b11a1d2008-11-14 10:39:26 +11003592 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003593}
3594
David Quigleya1836a42006-06-30 01:55:49 -07003595static int selinux_task_getioprio(struct task_struct *p)
3596{
David Howells3b11a1d2008-11-14 10:39:26 +11003597 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003598}
3599
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003600static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3601 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003603 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 /* Control the ability to change the hard limit (whether
3606 lowering or raising it), so that the hard limit can
3607 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003608 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003610 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
3612 return 0;
3613}
3614
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003615static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003617 int rc;
3618
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003619 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003620 if (rc)
3621 return rc;
3622
David Howells3b11a1d2008-11-14 10:39:26 +11003623 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
3626static int selinux_task_getscheduler(struct task_struct *p)
3627{
David Howells3b11a1d2008-11-14 10:39:26 +11003628 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
David Quigley35601542006-06-23 02:04:01 -07003631static int selinux_task_movememory(struct task_struct *p)
3632{
David Howells3b11a1d2008-11-14 10:39:26 +11003633 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003634}
3635
David Quigleyf9008e42006-06-30 01:55:46 -07003636static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3637 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
3639 u32 perm;
3640 int rc;
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (!sig)
3643 perm = PROCESS__SIGNULL; /* null signal; existence test */
3644 else
3645 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003646 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003647 rc = avc_has_perm(secid, task_sid(p),
3648 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003649 else
David Howells3b11a1d2008-11-14 10:39:26 +11003650 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003651 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654static int selinux_task_wait(struct task_struct *p)
3655{
Eric Paris8a535142007-10-22 16:10:31 -04003656 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659static void selinux_task_to_inode(struct task_struct *p,
3660 struct inode *inode)
3661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003663 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
David Howells275bb412008-11-14 10:39:19 +11003665 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003670static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003671 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
3673 int offset, ihlen, ret = -EINVAL;
3674 struct iphdr _iph, *ih;
3675
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003676 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3678 if (ih == NULL)
3679 goto out;
3680
3681 ihlen = ih->ihl * 4;
3682 if (ihlen < sizeof(_iph))
3683 goto out;
3684
Eric Paris48c62af2012-04-02 13:15:44 -04003685 ad->u.net->v4info.saddr = ih->saddr;
3686 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 ret = 0;
3688
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003689 if (proto)
3690 *proto = ih->protocol;
3691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003693 case IPPROTO_TCP: {
3694 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Eric Paris828dfe12008-04-17 13:17:49 -04003696 if (ntohs(ih->frag_off) & IP_OFFSET)
3697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 offset += ihlen;
3700 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3701 if (th == NULL)
3702 break;
3703
Eric Paris48c62af2012-04-02 13:15:44 -04003704 ad->u.net->sport = th->source;
3705 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Eric Paris828dfe12008-04-17 13:17:49 -04003709 case IPPROTO_UDP: {
3710 struct udphdr _udph, *uh;
3711
3712 if (ntohs(ih->frag_off) & IP_OFFSET)
3713 break;
3714
3715 offset += ihlen;
3716 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3717 if (uh == NULL)
3718 break;
3719
Eric Paris48c62af2012-04-02 13:15:44 -04003720 ad->u.net->sport = uh->source;
3721 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003722 break;
3723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
James Morris2ee92d42006-11-13 16:09:01 -08003725 case IPPROTO_DCCP: {
3726 struct dccp_hdr _dccph, *dh;
3727
3728 if (ntohs(ih->frag_off) & IP_OFFSET)
3729 break;
3730
3731 offset += ihlen;
3732 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3733 if (dh == NULL)
3734 break;
3735
Eric Paris48c62af2012-04-02 13:15:44 -04003736 ad->u.net->sport = dh->dccph_sport;
3737 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003738 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003739 }
James Morris2ee92d42006-11-13 16:09:01 -08003740
Eric Paris828dfe12008-04-17 13:17:49 -04003741 default:
3742 break;
3743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744out:
3745 return ret;
3746}
3747
3748#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3749
3750/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003751static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003752 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 u8 nexthdr;
3755 int ret = -EINVAL, offset;
3756 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003757 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003759 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3761 if (ip6 == NULL)
3762 goto out;
3763
Eric Paris48c62af2012-04-02 13:15:44 -04003764 ad->u.net->v6info.saddr = ip6->saddr;
3765 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 ret = 0;
3767
3768 nexthdr = ip6->nexthdr;
3769 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003770 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (offset < 0)
3772 goto out;
3773
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003774 if (proto)
3775 *proto = nexthdr;
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 switch (nexthdr) {
3778 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003779 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3782 if (th == NULL)
3783 break;
3784
Eric Paris48c62af2012-04-02 13:15:44 -04003785 ad->u.net->sport = th->source;
3786 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 break;
3788 }
3789
3790 case IPPROTO_UDP: {
3791 struct udphdr _udph, *uh;
3792
3793 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3794 if (uh == NULL)
3795 break;
3796
Eric Paris48c62af2012-04-02 13:15:44 -04003797 ad->u.net->sport = uh->source;
3798 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 break;
3800 }
3801
James Morris2ee92d42006-11-13 16:09:01 -08003802 case IPPROTO_DCCP: {
3803 struct dccp_hdr _dccph, *dh;
3804
3805 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3806 if (dh == NULL)
3807 break;
3808
Eric Paris48c62af2012-04-02 13:15:44 -04003809 ad->u.net->sport = dh->dccph_sport;
3810 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003811 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003812 }
James Morris2ee92d42006-11-13 16:09:01 -08003813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 /* includes fragments */
3815 default:
3816 break;
3817 }
3818out:
3819 return ret;
3820}
3821
3822#endif /* IPV6 */
3823
Thomas Liu2bf49692009-07-14 12:14:09 -04003824static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003825 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
David Howellscf9481e2008-07-27 21:31:07 +10003827 char *addrp;
3828 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Eric Paris48c62af2012-04-02 13:15:44 -04003830 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003832 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003833 if (ret)
3834 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003835 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3836 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003837 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3840 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003841 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003842 if (ret)
3843 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003844 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3845 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003846 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847#endif /* IPV6 */
3848 default:
David Howellscf9481e2008-07-27 21:31:07 +10003849 addrp = NULL;
3850 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
3852
David Howellscf9481e2008-07-27 21:31:07 +10003853parse_error:
3854 printk(KERN_WARNING
3855 "SELinux: failure in selinux_parse_skb(),"
3856 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003858
3859okay:
3860 if (_addrp)
3861 *_addrp = addrp;
3862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
Paul Moore4f6a9932007-03-01 14:35:22 -05003865/**
Paul Moore220deb92008-01-29 08:38:23 -05003866 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003867 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003868 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003869 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003870 *
3871 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003872 * Check the various different forms of network peer labeling and determine
3873 * the peer label/SID for the packet; most of the magic actually occurs in
3874 * the security server function security_net_peersid_cmp(). The function
3875 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3876 * or -EACCES if @sid is invalid due to inconsistencies with the different
3877 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003878 *
3879 */
Paul Moore220deb92008-01-29 08:38:23 -05003880static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003881{
Paul Moore71f1cb02008-01-29 08:51:16 -05003882 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003883 u32 xfrm_sid;
3884 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003885 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003886
3887 selinux_skb_xfrm_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003888 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003889
Paul Moore71f1cb02008-01-29 08:51:16 -05003890 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3891 if (unlikely(err)) {
3892 printk(KERN_WARNING
3893 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3894 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003895 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003896 }
Paul Moore220deb92008-01-29 08:38:23 -05003897
3898 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003899}
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003902
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003903static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3904 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003905{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003906 if (tsec->sockcreate_sid > SECSID_NULL) {
3907 *socksid = tsec->sockcreate_sid;
3908 return 0;
3909 }
3910
3911 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3912 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003913}
3914
Paul Moore253bfae2010-04-22 14:46:19 -04003915static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Paul Moore253bfae2010-04-22 14:46:19 -04003917 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003918 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003919 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003920 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003921 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Satya Durga Srinivasu Prabhala90280652013-09-24 15:23:48 -07003923 if (unlikely(!sksec)) {
3924 pr_warn("SELinux: sksec is NULL, socket is already freed\n");
3925 return -EINVAL;
3926 }
3927
Paul Moore253bfae2010-04-22 14:46:19 -04003928 if (sksec->sid == SECINITSID_KERNEL)
3929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Thomas Liu2bf49692009-07-14 12:14:09 -04003931 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003932 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003933 ad.u.net = &net;
3934 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Paul Moore253bfae2010-04-22 14:46:19 -04003936 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
3939static int selinux_socket_create(int family, int type,
3940 int protocol, int kern)
3941{
Paul Moore5fb49872010-04-22 14:46:19 -04003942 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003943 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003944 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003945 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
David Howells275bb412008-11-14 10:39:19 +11003950 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003951 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3952 if (rc)
3953 return rc;
3954
Paul Moored4f2d972010-04-22 14:46:18 -04003955 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003958static int selinux_socket_post_create(struct socket *sock, int family,
3959 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Paul Moore5fb49872010-04-22 14:46:19 -04003961 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003962 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003963 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003964 int err = 0;
3965
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003966 isec->sclass = socket_type_to_security_class(family, type, protocol);
3967
David Howells275bb412008-11-14 10:39:19 +11003968 if (kern)
3969 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003970 else {
3971 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3972 if (err)
3973 return err;
3974 }
David Howells275bb412008-11-14 10:39:19 +11003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 isec->initialized = 1;
3977
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003978 if (sock->sk) {
3979 sksec = sock->sk->sk_security;
3980 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003981 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003982 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003983 }
3984
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003985 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
3987
3988/* Range of port numbers used to automatically bind.
3989 Need to determine whether we should perform a name_bind
3990 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3993{
Paul Moore253bfae2010-04-22 14:46:19 -04003994 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 u16 family;
3996 int err;
3997
Paul Moore253bfae2010-04-22 14:46:19 -04003998 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (err)
4000 goto out;
4001
4002 /*
4003 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04004004 * Multiple address binding for SCTP is not supported yet: we just
4005 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 */
Paul Moore253bfae2010-04-22 14:46:19 -04004007 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 if (family == PF_INET || family == PF_INET6) {
4009 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04004010 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004011 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004012 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004013 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 struct sockaddr_in *addr4 = NULL;
4015 struct sockaddr_in6 *addr6 = NULL;
4016 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004017 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (family == PF_INET) {
4020 addr4 = (struct sockaddr_in *)address;
4021 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 addrp = (char *)&addr4->sin_addr.s_addr;
4023 } else {
4024 addr6 = (struct sockaddr_in6 *)address;
4025 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 addrp = (char *)&addr6->sin6_addr.s6_addr;
4027 }
4028
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004029 if (snum) {
4030 int low, high;
4031
4032 inet_get_local_port_range(&low, &high);
4033
4034 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004035 err = sel_netport_sid(sk->sk_protocol,
4036 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004037 if (err)
4038 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04004039 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004040 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004041 ad.u.net = &net;
4042 ad.u.net->sport = htons(snum);
4043 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004044 err = avc_has_perm(sksec->sid, sid,
4045 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004046 SOCKET__NAME_BIND, &ad);
4047 if (err)
4048 goto out;
4049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
Eric Paris828dfe12008-04-17 13:17:49 -04004051
Paul Moore253bfae2010-04-22 14:46:19 -04004052 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004053 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 node_perm = TCP_SOCKET__NODE_BIND;
4055 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004056
James Morris13402582005-09-30 14:24:34 -04004057 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 node_perm = UDP_SOCKET__NODE_BIND;
4059 break;
James Morris2ee92d42006-11-13 16:09:01 -08004060
4061 case SECCLASS_DCCP_SOCKET:
4062 node_perm = DCCP_SOCKET__NODE_BIND;
4063 break;
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 default:
4066 node_perm = RAWIP_SOCKET__NODE_BIND;
4067 break;
4068 }
Eric Paris828dfe12008-04-17 13:17:49 -04004069
Paul Moore224dfbd2008-01-29 08:38:13 -05004070 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 if (err)
4072 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004073
Thomas Liu2bf49692009-07-14 12:14:09 -04004074 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004075 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004076 ad.u.net = &net;
4077 ad.u.net->sport = htons(snum);
4078 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004081 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 else
Eric Paris48c62af2012-04-02 13:15:44 -04004083 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Paul Moore253bfae2010-04-22 14:46:19 -04004085 err = avc_has_perm(sksec->sid, sid,
4086 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (err)
4088 goto out;
4089 }
4090out:
4091 return err;
4092}
4093
4094static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4095{
Paul Moore014ab192008-10-10 10:16:33 -04004096 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004097 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 int err;
4099
Paul Moore253bfae2010-04-22 14:46:19 -04004100 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 if (err)
4102 return err;
4103
4104 /*
James Morris2ee92d42006-11-13 16:09:01 -08004105 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 */
Paul Moore253bfae2010-04-22 14:46:19 -04004107 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4108 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004109 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004110 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004111 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 struct sockaddr_in *addr4 = NULL;
4113 struct sockaddr_in6 *addr6 = NULL;
4114 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004115 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117 if (sk->sk_family == PF_INET) {
4118 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004119 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return -EINVAL;
4121 snum = ntohs(addr4->sin_port);
4122 } else {
4123 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004124 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return -EINVAL;
4126 snum = ntohs(addr6->sin6_port);
4127 }
4128
Paul Moore3e112172008-04-10 10:48:14 -04004129 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (err)
4131 goto out;
4132
Paul Moore253bfae2010-04-22 14:46:19 -04004133 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004134 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4135
Thomas Liu2bf49692009-07-14 12:14:09 -04004136 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004137 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004138 ad.u.net = &net;
4139 ad.u.net->dport = htons(snum);
4140 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004141 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (err)
4143 goto out;
4144 }
4145
Paul Moore014ab192008-10-10 10:16:33 -04004146 err = selinux_netlbl_socket_connect(sk, address);
4147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148out:
4149 return err;
4150}
4151
4152static int selinux_socket_listen(struct socket *sock, int backlog)
4153{
Paul Moore253bfae2010-04-22 14:46:19 -04004154 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
4157static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4158{
4159 int err;
4160 struct inode_security_struct *isec;
4161 struct inode_security_struct *newisec;
4162
Paul Moore253bfae2010-04-22 14:46:19 -04004163 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 if (err)
4165 return err;
4166
4167 newisec = SOCK_INODE(newsock)->i_security;
4168
4169 isec = SOCK_INODE(sock)->i_security;
4170 newisec->sclass = isec->sclass;
4171 newisec->sid = isec->sid;
4172 newisec->initialized = 1;
4173
4174 return 0;
4175}
4176
4177static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004178 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Paul Moore253bfae2010-04-22 14:46:19 -04004180 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
4183static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4184 int size, int flags)
4185{
Paul Moore253bfae2010-04-22 14:46:19 -04004186 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
4189static int selinux_socket_getsockname(struct socket *sock)
4190{
Paul Moore253bfae2010-04-22 14:46:19 -04004191 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
4194static int selinux_socket_getpeername(struct socket *sock)
4195{
Paul Moore253bfae2010-04-22 14:46:19 -04004196 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
Eric Paris828dfe12008-04-17 13:17:49 -04004199static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Paul Mooref8687af2006-10-30 15:22:15 -08004201 int err;
4202
Paul Moore253bfae2010-04-22 14:46:19 -04004203 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004204 if (err)
4205 return err;
4206
4207 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
4210static int selinux_socket_getsockopt(struct socket *sock, int level,
4211 int optname)
4212{
Paul Moore253bfae2010-04-22 14:46:19 -04004213 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
4216static int selinux_socket_shutdown(struct socket *sock, int how)
4217{
Paul Moore253bfae2010-04-22 14:46:19 -04004218 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219}
4220
David S. Miller3610cda2011-01-05 15:38:53 -08004221static int selinux_socket_unix_stream_connect(struct sock *sock,
4222 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 struct sock *newsk)
4224{
David S. Miller3610cda2011-01-05 15:38:53 -08004225 struct sk_security_struct *sksec_sock = sock->sk_security;
4226 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004227 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004228 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004229 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004230 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 int err;
4232
Thomas Liu2bf49692009-07-14 12:14:09 -04004233 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004234 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004235 ad.u.net = &net;
4236 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Paul Moore4d1e2452010-04-22 14:46:18 -04004238 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4239 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4241 if (err)
4242 return err;
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004245 sksec_new->peer_sid = sksec_sock->sid;
4246 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4247 &sksec_new->sid);
4248 if (err)
4249 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004250
Paul Moore4d1e2452010-04-22 14:46:18 -04004251 /* connecting socket */
4252 sksec_sock->peer_sid = sksec_new->sid;
4253
4254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
4257static int selinux_socket_unix_may_send(struct socket *sock,
4258 struct socket *other)
4259{
Paul Moore253bfae2010-04-22 14:46:19 -04004260 struct sk_security_struct *ssec = sock->sk->sk_security;
4261 struct sk_security_struct *osec = other->sk->sk_security;
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,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Thomas Liu2bf49692009-07-14 12:14:09 -04004266 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004267 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004268 ad.u.net = &net;
4269 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Paul Moore253bfae2010-04-22 14:46:19 -04004271 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4272 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
Paul Mooreeffad8d2008-01-29 08:49:27 -05004275static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4276 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004277 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004278{
4279 int err;
4280 u32 if_sid;
4281 u32 node_sid;
4282
4283 err = sel_netif_sid(ifindex, &if_sid);
4284 if (err)
4285 return err;
4286 err = avc_has_perm(peer_sid, if_sid,
4287 SECCLASS_NETIF, NETIF__INGRESS, ad);
4288 if (err)
4289 return err;
4290
4291 err = sel_netnode_sid(addrp, family, &node_sid);
4292 if (err)
4293 return err;
4294 return avc_has_perm(peer_sid, node_sid,
4295 SECCLASS_NODE, NODE__RECVFROM, ad);
4296}
4297
Paul Moore220deb92008-01-29 08:38:23 -05004298static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004299 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004300{
Paul Moore277d3422008-12-31 12:54:11 -05004301 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004302 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004303 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004304 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004305 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004306 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004307 char *addrp;
4308
Thomas Liu2bf49692009-07-14 12:14:09 -04004309 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004310 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004311 ad.u.net = &net;
4312 ad.u.net->netif = skb->skb_iif;
4313 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004314 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4315 if (err)
4316 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004317
Paul Moore58bfbb52009-03-27 17:10:41 -04004318 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004319 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004320 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004321 if (err)
4322 return err;
4323 }
Paul Moore220deb92008-01-29 08:38:23 -05004324
Steffen Klassertb9679a72011-02-23 12:55:21 +01004325 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4326 if (err)
4327 return err;
4328 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004329
James Morris4e5ab4c2006-06-09 00:33:33 -07004330 return err;
4331}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004332
James Morris4e5ab4c2006-06-09 00:33:33 -07004333static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4334{
Paul Moore220deb92008-01-29 08:38:23 -05004335 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004336 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004337 u16 family = sk->sk_family;
4338 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004339 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004340 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004341 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004342 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004343 u8 secmark_active;
4344 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004345
James Morris4e5ab4c2006-06-09 00:33:33 -07004346 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004347 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004348
4349 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004350 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004351 family = PF_INET;
4352
Paul Moored8395c82008-10-10 10:16:30 -04004353 /* If any sort of compatibility mode is enabled then handoff processing
4354 * to the selinux_sock_rcv_skb_compat() function to deal with the
4355 * special handling. We do this in an attempt to keep this function
4356 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004357 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004358 return selinux_sock_rcv_skb_compat(sk, skb, family);
4359
4360 secmark_active = selinux_secmark_enabled();
4361 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4362 if (!secmark_active && !peerlbl_active)
4363 return 0;
4364
Thomas Liu2bf49692009-07-14 12:14:09 -04004365 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004366 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004367 ad.u.net = &net;
4368 ad.u.net->netif = skb->skb_iif;
4369 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004370 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004371 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004372 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004373
Paul Moored8395c82008-10-10 10:16:30 -04004374 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004375 u32 peer_sid;
4376
4377 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4378 if (err)
4379 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004380 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004381 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004382 if (err) {
4383 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004384 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004385 }
Paul Moored621d352008-01-29 08:43:36 -05004386 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4387 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004388 if (err)
4389 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004390 }
4391
Paul Moored8395c82008-10-10 10:16:30 -04004392 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004393 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4394 PACKET__RECV, &ad);
4395 if (err)
4396 return err;
4397 }
4398
Paul Moored621d352008-01-29 08:43:36 -05004399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400}
4401
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004402static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4403 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404{
4405 int err = 0;
4406 char *scontext;
4407 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004408 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004409 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Paul Moore253bfae2010-04-22 14:46:19 -04004411 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4412 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004413 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004414 if (peer_sid == SECSID_NULL)
4415 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004417 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 if (scontext_len > len) {
4422 err = -ERANGE;
4423 goto out_len;
4424 }
4425
4426 if (copy_to_user(optval, scontext, scontext_len))
4427 err = -EFAULT;
4428
4429out_len:
4430 if (put_user(scontext_len, optlen))
4431 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 return err;
4434}
4435
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004436static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004437{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004438 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004439 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004440
Paul Mooreaa862902008-10-10 10:16:29 -04004441 if (skb && skb->protocol == htons(ETH_P_IP))
4442 family = PF_INET;
4443 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4444 family = PF_INET6;
4445 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004446 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004447 else
4448 goto out;
4449
4450 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004451 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004452 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004453 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004454
Paul Moore75e22912008-01-29 08:38:04 -05004455out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004456 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004457 if (peer_secid == SECSID_NULL)
4458 return -EINVAL;
4459 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004460}
4461
Al Viro7d877f32005-10-21 03:20:43 -04004462static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
Paul Moore84914b72010-04-22 14:46:18 -04004464 struct sk_security_struct *sksec;
4465
4466 sksec = kzalloc(sizeof(*sksec), priority);
4467 if (!sksec)
4468 return -ENOMEM;
4469
4470 sksec->peer_sid = SECINITSID_UNLABELED;
4471 sksec->sid = SECINITSID_UNLABELED;
4472 selinux_netlbl_sk_security_reset(sksec);
4473 sk->sk_security = sksec;
4474
4475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476}
4477
4478static void selinux_sk_free_security(struct sock *sk)
4479{
Paul Moore84914b72010-04-22 14:46:18 -04004480 struct sk_security_struct *sksec = sk->sk_security;
4481
4482 sk->sk_security = NULL;
4483 selinux_netlbl_sk_security_free(sksec);
4484 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004487static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4488{
Eric Parisdd3e7832010-04-07 15:08:46 -04004489 struct sk_security_struct *sksec = sk->sk_security;
4490 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004491
Eric Parisdd3e7832010-04-07 15:08:46 -04004492 newsksec->sid = sksec->sid;
4493 newsksec->peer_sid = sksec->peer_sid;
4494 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004495
Eric Parisdd3e7832010-04-07 15:08:46 -04004496 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004497}
4498
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004499static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004500{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004501 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004502 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004503 else {
4504 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004505
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004506 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004507 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004508}
4509
Eric Paris828dfe12008-04-17 13:17:49 -04004510static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004511{
4512 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4513 struct sk_security_struct *sksec = sk->sk_security;
4514
David Woodhouse2148ccc2006-09-29 15:50:25 -07004515 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4516 sk->sk_family == PF_UNIX)
4517 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004518 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004519}
4520
Adrian Bunk9a673e52006-08-15 00:03:53 -07004521static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4522 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004523{
4524 struct sk_security_struct *sksec = sk->sk_security;
4525 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004526 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004527 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004528 u32 peersid;
4529
Paul Mooreaa862902008-10-10 10:16:29 -04004530 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4531 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4532 family = PF_INET;
4533
4534 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004535 if (err)
4536 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004537 if (peersid == SECSID_NULL) {
4538 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004539 req->peer_secid = SECSID_NULL;
Paul Moore389fb802009-03-27 17:10:34 -04004540 } else {
4541 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4542 if (err)
4543 return err;
4544 req->secid = newsid;
4545 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004546 }
4547
Paul Moore389fb802009-03-27 17:10:34 -04004548 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004549}
4550
Adrian Bunk9a673e52006-08-15 00:03:53 -07004551static void selinux_inet_csk_clone(struct sock *newsk,
4552 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004553{
4554 struct sk_security_struct *newsksec = newsk->sk_security;
4555
4556 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004557 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004558 /* NOTE: Ideally, we should also get the isec->sid for the
4559 new socket in sync, but we don't have the isec available yet.
4560 So we will wait until sock_graft to do it, by which
4561 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004562
Paul Moore9f2ad662006-11-17 17:38:53 -05004563 /* We don't need to take any sort of lock here as we are the only
4564 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004565 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004566}
4567
Paul Moore014ab192008-10-10 10:16:33 -04004568static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004569{
Paul Mooreaa862902008-10-10 10:16:29 -04004570 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004571 struct sk_security_struct *sksec = sk->sk_security;
4572
Paul Mooreaa862902008-10-10 10:16:29 -04004573 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4574 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4575 family = PF_INET;
4576
4577 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004578}
4579
Eric Paris2606fd12010-10-13 16:24:41 -04004580static int selinux_secmark_relabel_packet(u32 sid)
4581{
4582 const struct task_security_struct *__tsec;
4583 u32 tsid;
4584
4585 __tsec = current_security();
4586 tsid = __tsec->sid;
4587
4588 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4589}
4590
4591static void selinux_secmark_refcount_inc(void)
4592{
4593 atomic_inc(&selinux_secmark_refcount);
4594}
4595
4596static void selinux_secmark_refcount_dec(void)
4597{
4598 atomic_dec(&selinux_secmark_refcount);
4599}
4600
Adrian Bunk9a673e52006-08-15 00:03:53 -07004601static void selinux_req_classify_flow(const struct request_sock *req,
4602 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004603{
David S. Miller1d28f422011-03-12 00:29:39 -05004604 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004605}
4606
Paul Mooreed6d76e2009-08-28 18:12:49 -04004607static int selinux_tun_dev_create(void)
4608{
4609 u32 sid = current_sid();
4610
4611 /* we aren't taking into account the "sockcreate" SID since the socket
4612 * that is being created here is not a socket in the traditional sense,
4613 * instead it is a private sock, accessible only to the kernel, and
4614 * representing a wide range of network traffic spanning multiple
4615 * connections unlike traditional sockets - check the TUN driver to
4616 * get a better understanding of why this socket is special */
4617
4618 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4619 NULL);
4620}
4621
4622static void selinux_tun_dev_post_create(struct sock *sk)
4623{
4624 struct sk_security_struct *sksec = sk->sk_security;
4625
4626 /* we don't currently perform any NetLabel based labeling here and it
4627 * isn't clear that we would want to do so anyway; while we could apply
4628 * labeling without the support of the TUN user the resulting labeled
4629 * traffic from the other end of the connection would almost certainly
4630 * cause confusion to the TUN user that had no idea network labeling
4631 * protocols were being used */
4632
4633 /* see the comments in selinux_tun_dev_create() about why we don't use
4634 * the sockcreate SID here */
4635
4636 sksec->sid = current_sid();
4637 sksec->sclass = SECCLASS_TUN_SOCKET;
4638}
4639
4640static int selinux_tun_dev_attach(struct sock *sk)
4641{
4642 struct sk_security_struct *sksec = sk->sk_security;
4643 u32 sid = current_sid();
4644 int err;
4645
4646 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4647 TUN_SOCKET__RELABELFROM, NULL);
4648 if (err)
4649 return err;
4650 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4651 TUN_SOCKET__RELABELTO, NULL);
4652 if (err)
4653 return err;
4654
4655 sksec->sid = sid;
4656
4657 return 0;
4658}
4659
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4661{
4662 int err = 0;
4663 u32 perm;
4664 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004665 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 if (skb->len < NLMSG_SPACE(0)) {
4668 err = -EINVAL;
4669 goto out;
4670 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004671 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004672
Paul Moore253bfae2010-04-22 14:46:19 -04004673 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 if (err) {
4675 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004676 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 "SELinux: unrecognized netlink message"
4678 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004679 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004680 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 err = 0;
4682 }
4683
4684 /* Ignore */
4685 if (err == -ENOENT)
4686 err = 0;
4687 goto out;
4688 }
4689
Paul Moore253bfae2010-04-22 14:46:19 -04004690 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691out:
4692 return err;
4693}
4694
4695#ifdef CONFIG_NETFILTER
4696
Paul Mooreeffad8d2008-01-29 08:49:27 -05004697static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4698 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Paul Mooredfaebe92008-10-10 10:16:31 -04004700 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004701 char *addrp;
4702 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004703 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004704 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004705 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004706 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004707 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004708 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004709
Paul Mooreeffad8d2008-01-29 08:49:27 -05004710 if (!selinux_policycap_netpeer)
4711 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004712
Paul Mooreeffad8d2008-01-29 08:49:27 -05004713 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004714 netlbl_active = netlbl_enabled();
4715 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004716 if (!secmark_active && !peerlbl_active)
4717 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004718
Paul Moored8395c82008-10-10 10:16:30 -04004719 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4720 return NF_DROP;
4721
Thomas Liu2bf49692009-07-14 12:14:09 -04004722 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004723 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004724 ad.u.net = &net;
4725 ad.u.net->netif = ifindex;
4726 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004727 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4728 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Paul Mooredfaebe92008-10-10 10:16:31 -04004730 if (peerlbl_active) {
4731 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4732 peer_sid, &ad);
4733 if (err) {
4734 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004735 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004736 }
4737 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004738
4739 if (secmark_active)
4740 if (avc_has_perm(peer_sid, skb->secmark,
4741 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4742 return NF_DROP;
4743
Paul Moore948bf852008-10-10 10:16:32 -04004744 if (netlbl_active)
4745 /* we do this in the FORWARD path and not the POST_ROUTING
4746 * path because we want to make sure we apply the necessary
4747 * labeling before IPsec is applied so we can leverage AH
4748 * protection */
4749 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4750 return NF_DROP;
4751
Paul Mooreeffad8d2008-01-29 08:49:27 -05004752 return NF_ACCEPT;
4753}
4754
4755static unsigned int selinux_ipv4_forward(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_forward(skb, in->ifindex, PF_INET);
4762}
4763
4764#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4765static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4766 struct sk_buff *skb,
4767 const struct net_device *in,
4768 const struct net_device *out,
4769 int (*okfn)(struct sk_buff *))
4770{
4771 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4772}
4773#endif /* IPV6 */
4774
Paul Moore948bf852008-10-10 10:16:32 -04004775static unsigned int selinux_ip_output(struct sk_buff *skb,
4776 u16 family)
4777{
4778 u32 sid;
4779
4780 if (!netlbl_enabled())
4781 return NF_ACCEPT;
4782
4783 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4784 * because we want to make sure we apply the necessary labeling
4785 * before IPsec is applied so we can leverage AH protection */
4786 if (skb->sk) {
4787 struct sk_security_struct *sksec = skb->sk->sk_security;
4788 sid = sksec->sid;
4789 } else
4790 sid = SECINITSID_KERNEL;
4791 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4792 return NF_DROP;
4793
4794 return NF_ACCEPT;
4795}
4796
4797static unsigned int selinux_ipv4_output(unsigned int hooknum,
4798 struct sk_buff *skb,
4799 const struct net_device *in,
4800 const struct net_device *out,
4801 int (*okfn)(struct sk_buff *))
4802{
4803 return selinux_ip_output(skb, PF_INET);
4804}
4805
Paul Mooreeffad8d2008-01-29 08:49:27 -05004806static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4807 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004808 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004809{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004810 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004811 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004812 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004813 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004814 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004815 char *addrp;
4816 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004817
Paul Mooreeffad8d2008-01-29 08:49:27 -05004818 if (sk == NULL)
4819 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004820 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004821
Thomas Liu2bf49692009-07-14 12:14:09 -04004822 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004823 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004824 ad.u.net = &net;
4825 ad.u.net->netif = ifindex;
4826 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004827 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4828 return NF_DROP;
4829
Paul Moore58bfbb52009-03-27 17:10:41 -04004830 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004831 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004832 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004833 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004834
Steffen Klassertb9679a72011-02-23 12:55:21 +01004835 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4836 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004837
Paul Mooreeffad8d2008-01-29 08:49:27 -05004838 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839}
4840
Paul Mooreeffad8d2008-01-29 08:49:27 -05004841static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4842 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004844 u32 secmark_perm;
4845 u32 peer_sid;
4846 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004847 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004848 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004849 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004850 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004851 u8 secmark_active;
4852 u8 peerlbl_active;
4853
Paul Mooreeffad8d2008-01-29 08:49:27 -05004854 /* If any sort of compatibility mode is enabled then handoff processing
4855 * to the selinux_ip_postroute_compat() function to deal with the
4856 * special handling. We do this in an attempt to keep this function
4857 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004858 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004859 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004860#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004861 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4862 * packet transformation so allow the packet to pass without any checks
4863 * since we'll have another chance to perform access control checks
4864 * when the packet is on it's final way out.
4865 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4866 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004867 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004868 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004869#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004870 secmark_active = selinux_secmark_enabled();
4871 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4872 if (!secmark_active && !peerlbl_active)
4873 return NF_ACCEPT;
4874
Paul Moored8395c82008-10-10 10:16:30 -04004875 /* if the packet is being forwarded then get the peer label from the
4876 * packet itself; otherwise check to see if it is from a local
4877 * application or the kernel, if from an application get the peer label
4878 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004879 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004880 if (sk == NULL) {
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004881 if (skb->skb_iif) {
4882 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004883 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004884 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004885 } else {
4886 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004887 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004888 }
Paul Moored8395c82008-10-10 10:16:30 -04004889 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004890 struct sk_security_struct *sksec = sk->sk_security;
4891 peer_sid = sksec->sid;
4892 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004893 }
4894
Thomas Liu2bf49692009-07-14 12:14:09 -04004895 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004896 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004897 ad.u.net = &net;
4898 ad.u.net->netif = ifindex;
4899 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004900 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004901 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004902
Paul Mooreeffad8d2008-01-29 08:49:27 -05004903 if (secmark_active)
4904 if (avc_has_perm(peer_sid, skb->secmark,
4905 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004906 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004907
4908 if (peerlbl_active) {
4909 u32 if_sid;
4910 u32 node_sid;
4911
4912 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004913 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004914 if (avc_has_perm(peer_sid, if_sid,
4915 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004916 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004917
4918 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004919 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004920 if (avc_has_perm(peer_sid, node_sid,
4921 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004922 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004923 }
4924
4925 return NF_ACCEPT;
4926}
4927
4928static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4929 struct sk_buff *skb,
4930 const struct net_device *in,
4931 const struct net_device *out,
4932 int (*okfn)(struct sk_buff *))
4933{
4934 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
4937#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004938static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4939 struct sk_buff *skb,
4940 const struct net_device *in,
4941 const struct net_device *out,
4942 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004944 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946#endif /* IPV6 */
4947
4948#endif /* CONFIG_NETFILTER */
4949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 int err;
4953
Eric Paris200ac532009-02-12 15:01:04 -05004954 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 if (err)
4956 return err;
4957
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004958 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961static int ipc_alloc_security(struct task_struct *task,
4962 struct kern_ipc_perm *perm,
4963 u16 sclass)
4964{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004966 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
James Morris89d155e2005-10-30 14:59:21 -08004968 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 if (!isec)
4970 return -ENOMEM;
4971
David Howells275bb412008-11-14 10:39:19 +11004972 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004974 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 perm->security = isec;
4976
4977 return 0;
4978}
4979
4980static void ipc_free_security(struct kern_ipc_perm *perm)
4981{
4982 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 perm->security = NULL;
4984 kfree(isec);
4985}
4986
4987static int msg_msg_alloc_security(struct msg_msg *msg)
4988{
4989 struct msg_security_struct *msec;
4990
James Morris89d155e2005-10-30 14:59:21 -08004991 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (!msec)
4993 return -ENOMEM;
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 msec->sid = SECINITSID_UNLABELED;
4996 msg->security = msec;
4997
4998 return 0;
4999}
5000
5001static void msg_msg_free_security(struct msg_msg *msg)
5002{
5003 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
5005 msg->security = NULL;
5006 kfree(msec);
5007}
5008
5009static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005010 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005013 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005014 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005015 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 isec = ipc_perms->security;
5018
Thomas Liu2bf49692009-07-14 12:14:09 -04005019 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005020 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 ad.u.ipc_id = ipc_perms->key;
5022
David Howells275bb412008-11-14 10:39:19 +11005023 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
5025
5026static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5027{
5028 return msg_msg_alloc_security(msg);
5029}
5030
5031static void selinux_msg_msg_free_security(struct msg_msg *msg)
5032{
5033 msg_msg_free_security(msg);
5034}
5035
5036/* message queue security operations */
5037static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005040 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005041 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005042 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int rc;
5044
5045 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5046 if (rc)
5047 return rc;
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 isec = msq->q_perm.security;
5050
Thomas Liu2bf49692009-07-14 12:14:09 -04005051 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005052 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005053 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
David Howells275bb412008-11-14 10:39:19 +11005055 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 MSGQ__CREATE, &ad);
5057 if (rc) {
5058 ipc_free_security(&msq->q_perm);
5059 return rc;
5060 }
5061 return 0;
5062}
5063
5064static void selinux_msg_queue_free_security(struct msg_queue *msq)
5065{
5066 ipc_free_security(&msq->q_perm);
5067}
5068
5069static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005072 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005073 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005074 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 isec = msq->q_perm.security;
5077
Thomas Liu2bf49692009-07-14 12:14:09 -04005078 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005079 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 ad.u.ipc_id = msq->q_perm.key;
5081
David Howells275bb412008-11-14 10:39:19 +11005082 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 MSGQ__ASSOCIATE, &ad);
5084}
5085
5086static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5087{
5088 int err;
5089 int perms;
5090
Eric Paris828dfe12008-04-17 13:17:49 -04005091 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 case IPC_INFO:
5093 case MSG_INFO:
5094 /* No specific object, just general system-wide information. */
5095 return task_has_system(current, SYSTEM__IPC_INFO);
5096 case IPC_STAT:
5097 case MSG_STAT:
5098 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5099 break;
5100 case IPC_SET:
5101 perms = MSGQ__SETATTR;
5102 break;
5103 case IPC_RMID:
5104 perms = MSGQ__DESTROY;
5105 break;
5106 default:
5107 return 0;
5108 }
5109
Stephen Smalley6af963f2005-05-01 08:58:39 -07005110 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return err;
5112}
5113
5114static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 struct ipc_security_struct *isec;
5117 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005118 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005119 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005120 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 int rc;
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 isec = msq->q_perm.security;
5124 msec = msg->security;
5125
5126 /*
5127 * First time through, need to assign label to the message
5128 */
5129 if (msec->sid == SECINITSID_UNLABELED) {
5130 /*
5131 * Compute new sid based on current process and
5132 * message queue this message will be stored in
5133 */
David Howells275bb412008-11-14 10:39:19 +11005134 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005135 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (rc)
5137 return rc;
5138 }
5139
Thomas Liu2bf49692009-07-14 12:14:09 -04005140 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005141 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 ad.u.ipc_id = msq->q_perm.key;
5143
5144 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005145 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 MSGQ__WRITE, &ad);
5147 if (!rc)
5148 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005149 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5150 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 if (!rc)
5152 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005153 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5154 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
5156 return rc;
5157}
5158
5159static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5160 struct task_struct *target,
5161 long type, int mode)
5162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 struct ipc_security_struct *isec;
5164 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005165 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005166 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005167 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 int rc;
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 isec = msq->q_perm.security;
5171 msec = msg->security;
5172
Thomas Liu2bf49692009-07-14 12:14:09 -04005173 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005174 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005175 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
David Howells275bb412008-11-14 10:39:19 +11005177 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 SECCLASS_MSGQ, MSGQ__READ, &ad);
5179 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005180 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 SECCLASS_MSG, MSG__RECEIVE, &ad);
5182 return rc;
5183}
5184
5185/* Shared Memory security operations */
5186static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005189 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005190 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005191 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 int rc;
5193
5194 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5195 if (rc)
5196 return rc;
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 isec = shp->shm_perm.security;
5199
Thomas Liu2bf49692009-07-14 12:14:09 -04005200 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005201 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005202 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
David Howells275bb412008-11-14 10:39:19 +11005204 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 SHM__CREATE, &ad);
5206 if (rc) {
5207 ipc_free_security(&shp->shm_perm);
5208 return rc;
5209 }
5210 return 0;
5211}
5212
5213static void selinux_shm_free_security(struct shmid_kernel *shp)
5214{
5215 ipc_free_security(&shp->shm_perm);
5216}
5217
5218static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005221 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005222 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005223 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 isec = shp->shm_perm.security;
5226
Thomas Liu2bf49692009-07-14 12:14:09 -04005227 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005228 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 ad.u.ipc_id = shp->shm_perm.key;
5230
David Howells275bb412008-11-14 10:39:19 +11005231 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 SHM__ASSOCIATE, &ad);
5233}
5234
5235/* Note, at this point, shp is locked down */
5236static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5237{
5238 int perms;
5239 int err;
5240
Eric Paris828dfe12008-04-17 13:17:49 -04005241 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 case IPC_INFO:
5243 case SHM_INFO:
5244 /* No specific object, just general system-wide information. */
5245 return task_has_system(current, SYSTEM__IPC_INFO);
5246 case IPC_STAT:
5247 case SHM_STAT:
5248 perms = SHM__GETATTR | SHM__ASSOCIATE;
5249 break;
5250 case IPC_SET:
5251 perms = SHM__SETATTR;
5252 break;
5253 case SHM_LOCK:
5254 case SHM_UNLOCK:
5255 perms = SHM__LOCK;
5256 break;
5257 case IPC_RMID:
5258 perms = SHM__DESTROY;
5259 break;
5260 default:
5261 return 0;
5262 }
5263
Stephen Smalley6af963f2005-05-01 08:58:39 -07005264 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 return err;
5266}
5267
5268static int selinux_shm_shmat(struct shmid_kernel *shp,
5269 char __user *shmaddr, int shmflg)
5270{
5271 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
5273 if (shmflg & SHM_RDONLY)
5274 perms = SHM__READ;
5275 else
5276 perms = SHM__READ | SHM__WRITE;
5277
Stephen Smalley6af963f2005-05-01 08:58:39 -07005278 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
5281/* Semaphore security operations */
5282static int selinux_sem_alloc_security(struct sem_array *sma)
5283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005285 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005286 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005287 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 int rc;
5289
5290 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5291 if (rc)
5292 return rc;
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 isec = sma->sem_perm.security;
5295
Thomas Liu2bf49692009-07-14 12:14:09 -04005296 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005297 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005298 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
David Howells275bb412008-11-14 10:39:19 +11005300 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 SEM__CREATE, &ad);
5302 if (rc) {
5303 ipc_free_security(&sma->sem_perm);
5304 return rc;
5305 }
5306 return 0;
5307}
5308
5309static void selinux_sem_free_security(struct sem_array *sma)
5310{
5311 ipc_free_security(&sma->sem_perm);
5312}
5313
5314static int selinux_sem_associate(struct sem_array *sma, int semflg)
5315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005317 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005318 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005319 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 isec = sma->sem_perm.security;
5322
Thomas Liu2bf49692009-07-14 12:14:09 -04005323 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005324 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 ad.u.ipc_id = sma->sem_perm.key;
5326
David Howells275bb412008-11-14 10:39:19 +11005327 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 SEM__ASSOCIATE, &ad);
5329}
5330
5331/* Note, at this point, sma is locked down */
5332static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5333{
5334 int err;
5335 u32 perms;
5336
Eric Paris828dfe12008-04-17 13:17:49 -04005337 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 case IPC_INFO:
5339 case SEM_INFO:
5340 /* No specific object, just general system-wide information. */
5341 return task_has_system(current, SYSTEM__IPC_INFO);
5342 case GETPID:
5343 case GETNCNT:
5344 case GETZCNT:
5345 perms = SEM__GETATTR;
5346 break;
5347 case GETVAL:
5348 case GETALL:
5349 perms = SEM__READ;
5350 break;
5351 case SETVAL:
5352 case SETALL:
5353 perms = SEM__WRITE;
5354 break;
5355 case IPC_RMID:
5356 perms = SEM__DESTROY;
5357 break;
5358 case IPC_SET:
5359 perms = SEM__SETATTR;
5360 break;
5361 case IPC_STAT:
5362 case SEM_STAT:
5363 perms = SEM__GETATTR | SEM__ASSOCIATE;
5364 break;
5365 default:
5366 return 0;
5367 }
5368
Stephen Smalley6af963f2005-05-01 08:58:39 -07005369 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return err;
5371}
5372
5373static int selinux_sem_semop(struct sem_array *sma,
5374 struct sembuf *sops, unsigned nsops, int alter)
5375{
5376 u32 perms;
5377
5378 if (alter)
5379 perms = SEM__READ | SEM__WRITE;
5380 else
5381 perms = SEM__READ;
5382
Stephen Smalley6af963f2005-05-01 08:58:39 -07005383 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
5386static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5387{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 u32 av = 0;
5389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 av = 0;
5391 if (flag & S_IRUGO)
5392 av |= IPC__UNIX_READ;
5393 if (flag & S_IWUGO)
5394 av |= IPC__UNIX_WRITE;
5395
5396 if (av == 0)
5397 return 0;
5398
Stephen Smalley6af963f2005-05-01 08:58:39 -07005399 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400}
5401
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005402static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5403{
5404 struct ipc_security_struct *isec = ipcp->security;
5405 *secid = isec->sid;
5406}
5407
Eric Paris828dfe12008-04-17 13:17:49 -04005408static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
5410 if (inode)
5411 inode_doinit_with_dentry(inode, dentry);
5412}
5413
5414static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005415 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
David Howells275bb412008-11-14 10:39:19 +11005417 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005418 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005420 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
5422 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005423 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 if (error)
5425 return error;
5426 }
5427
David Howells275bb412008-11-14 10:39:19 +11005428 rcu_read_lock();
5429 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
5431 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005432 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005434 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005436 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005438 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005439 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005440 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005441 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005442 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 else
David Howells275bb412008-11-14 10:39:19 +11005444 goto invalid;
5445 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 if (!sid)
5448 return 0;
5449
Al Viro04ff9702007-03-12 16:17:58 +00005450 error = security_sid_to_context(sid, value, &len);
5451 if (error)
5452 return error;
5453 return len;
David Howells275bb412008-11-14 10:39:19 +11005454
5455invalid:
5456 rcu_read_unlock();
5457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458}
5459
5460static int selinux_setprocattr(struct task_struct *p,
5461 char *name, void *value, size_t size)
5462{
5463 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005464 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005465 struct cred *new;
5466 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 int error;
5468 char *str = value;
5469
5470 if (current != p) {
5471 /* SELinux only allows a process to change its own
5472 security attributes. */
5473 return -EACCES;
5474 }
5475
5476 /*
5477 * Basic control over ability to set these attributes at all.
5478 * current == p, but we'll pass them separately in case the
5479 * above restriction is ever removed.
5480 */
5481 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005482 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005484 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005485 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005486 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005487 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005488 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005490 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 else
5492 error = -EINVAL;
5493 if (error)
5494 return error;
5495
5496 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleye44b57e2017-01-31 11:54:04 -05005497 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 if (str[size-1] == '\n') {
5499 str[size-1] = 0;
5500 size--;
5501 }
5502 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005503 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5504 if (!capable(CAP_MAC_ADMIN))
5505 return error;
5506 error = security_context_to_sid_force(value, size,
5507 &sid);
5508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 if (error)
5510 return error;
5511 }
5512
David Howellsd84f4f92008-11-14 10:39:23 +11005513 new = prepare_creds();
5514 if (!new)
5515 return -ENOMEM;
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 /* Permission checking based on the specified context is
5518 performed during the actual operation (execve,
5519 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005520 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 checks and may_create for the file creation checks. The
5522 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005523 tsec = new->security;
5524 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005526 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005528 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005529 error = may_create_key(sid, p);
5530 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005531 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005532 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005533 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005534 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005535 } else if (!strcmp(name, "current")) {
5536 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005538 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005539
David Howellsd84f4f92008-11-14 10:39:23 +11005540 /* Only allow single threaded processes to change context */
5541 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005542 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005543 error = security_bounded_transition(tsec->sid, sid);
5544 if (error)
5545 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
5548 /* Check permissions for the transition. */
5549 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005550 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005552 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
5554 /* Check for ptracing, and update the task SID if ok.
5555 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005556 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005558 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005559 if (tracer)
5560 ptsid = task_sid(tracer);
5561 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
David Howellsd84f4f92008-11-14 10:39:23 +11005563 if (tracer) {
5564 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5565 PROCESS__PTRACE, NULL);
5566 if (error)
5567 goto abort_change;
5568 }
5569
5570 tsec->sid = sid;
5571 } else {
5572 error = -EINVAL;
5573 goto abort_change;
5574 }
5575
5576 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005578
5579abort_change:
5580 abort_creds(new);
5581 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582}
5583
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005584static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5585{
5586 return security_sid_to_context(secid, secdata, seclen);
5587}
5588
David Howells7bf570d2008-04-29 20:52:51 +01005589static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005590{
5591 return security_context_to_sid(secdata, seclen, secid);
5592}
5593
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005594static void selinux_release_secctx(char *secdata, u32 seclen)
5595{
Paul Moore088999e2007-08-01 11:12:58 -04005596 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005597}
5598
David P. Quigley1ee65e32009-09-03 14:25:57 -04005599/*
5600 * called with inode->i_mutex locked
5601 */
5602static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5603{
5604 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5605}
5606
5607/*
5608 * called with inode->i_mutex locked
5609 */
5610static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5611{
5612 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5613}
5614
5615static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5616{
5617 int len = 0;
5618 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5619 ctx, true);
5620 if (len < 0)
5621 return len;
5622 *ctxlen = len;
5623 return 0;
5624}
Michael LeMayd7200242006-06-22 14:47:17 -07005625#ifdef CONFIG_KEYS
5626
David Howellsd84f4f92008-11-14 10:39:23 +11005627static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005628 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005629{
David Howellsd84f4f92008-11-14 10:39:23 +11005630 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005631 struct key_security_struct *ksec;
5632
5633 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5634 if (!ksec)
5635 return -ENOMEM;
5636
David Howellsd84f4f92008-11-14 10:39:23 +11005637 tsec = cred->security;
5638 if (tsec->keycreate_sid)
5639 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005640 else
David Howellsd84f4f92008-11-14 10:39:23 +11005641 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005642
David Howells275bb412008-11-14 10:39:19 +11005643 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005644 return 0;
5645}
5646
5647static void selinux_key_free(struct key *k)
5648{
5649 struct key_security_struct *ksec = k->security;
5650
5651 k->security = NULL;
5652 kfree(ksec);
5653}
5654
5655static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005656 const struct cred *cred,
5657 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005658{
5659 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005660 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005661 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005662
5663 /* if no specific permissions are requested, we skip the
5664 permission check. No serious, additional covert channels
5665 appear to be created. */
5666 if (perm == 0)
5667 return 0;
5668
David Howellsd84f4f92008-11-14 10:39:23 +11005669 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005670
5671 key = key_ref_to_ptr(key_ref);
5672 ksec = key->security;
5673
5674 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005675}
5676
David Howells70a5bb72008-04-29 01:01:26 -07005677static int selinux_key_getsecurity(struct key *key, char **_buffer)
5678{
5679 struct key_security_struct *ksec = key->security;
5680 char *context = NULL;
5681 unsigned len;
5682 int rc;
5683
5684 rc = security_sid_to_context(ksec->sid, &context, &len);
5685 if (!rc)
5686 rc = len;
5687 *_buffer = context;
5688 return rc;
5689}
5690
Michael LeMayd7200242006-06-22 14:47:17 -07005691#endif
5692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005694 .name = "selinux",
5695
Stephen Smalley48a23702012-11-05 08:15:34 -05005696 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5697 .binder_transaction = selinux_binder_transaction,
5698 .binder_transfer_binder = selinux_binder_transfer_binder,
5699 .binder_transfer_file = selinux_binder_transfer_file,
5700
Ingo Molnar9e488582009-05-07 19:26:19 +10005701 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005702 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005704 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 .capable = selinux_capable,
5706 .quotactl = selinux_quotactl,
5707 .quota_on = selinux_quota_on,
5708 .syslog = selinux_syslog,
5709 .vm_enough_memory = selinux_vm_enough_memory,
5710
5711 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
David Howellsa6f76f22008-11-14 10:39:24 +11005713 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005714 .bprm_committing_creds = selinux_bprm_committing_creds,
5715 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 .bprm_secureexec = selinux_bprm_secureexec,
5717
5718 .sb_alloc_security = selinux_sb_alloc_security,
5719 .sb_free_security = selinux_sb_free_security,
5720 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005721 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005722 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005723 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 .sb_statfs = selinux_sb_statfs,
5725 .sb_mount = selinux_mount,
5726 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005727 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005728 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005729 .sb_parse_opts_str = selinux_parse_opts_str,
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 .inode_alloc_security = selinux_inode_alloc_security,
5733 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005734 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 .inode_create = selinux_inode_create,
Amir Samuelov6a22e462014-05-26 11:44:06 +03005736 .inode_post_create = selinux_inode_post_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 .inode_unlink = selinux_inode_unlink,
5739 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 .inode_rmdir = selinux_inode_rmdir,
5742 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 .inode_readlink = selinux_inode_readlink,
5745 .inode_follow_link = selinux_inode_follow_link,
5746 .inode_permission = selinux_inode_permission,
5747 .inode_setattr = selinux_inode_setattr,
5748 .inode_getattr = selinux_inode_getattr,
5749 .inode_setxattr = selinux_inode_setxattr,
5750 .inode_post_setxattr = selinux_inode_post_setxattr,
5751 .inode_getxattr = selinux_inode_getxattr,
5752 .inode_listxattr = selinux_inode_listxattr,
5753 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005754 .inode_getsecurity = selinux_inode_getsecurity,
5755 .inode_setsecurity = selinux_inode_setsecurity,
5756 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005757 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
5759 .file_permission = selinux_file_permission,
5760 .file_alloc_security = selinux_file_alloc_security,
5761 .file_free_security = selinux_file_free_security,
5762 .file_ioctl = selinux_file_ioctl,
5763 .file_mmap = selinux_file_mmap,
5764 .file_mprotect = selinux_file_mprotect,
5765 .file_lock = selinux_file_lock,
5766 .file_fcntl = selinux_file_fcntl,
5767 .file_set_fowner = selinux_file_set_fowner,
5768 .file_send_sigiotask = selinux_file_send_sigiotask,
5769 .file_receive = selinux_file_receive,
5770
Eric Paris828dfe12008-04-17 13:17:49 -04005771 .dentry_open = selinux_dentry_open,
Amir Samuelov6a22e462014-05-26 11:44:06 +03005772 .file_close = selinux_file_close,
5773 .allow_merge_bio = selinux_allow_merge_bio,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005776 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005777 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005778 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005779 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005780 .kernel_act_as = selinux_kernel_act_as,
5781 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005782 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 .task_setpgid = selinux_task_setpgid,
5784 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005785 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005786 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005788 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005789 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 .task_setrlimit = selinux_task_setrlimit,
5791 .task_setscheduler = selinux_task_setscheduler,
5792 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005793 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 .task_kill = selinux_task_kill,
5795 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005796 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
5798 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005799 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5802 .msg_msg_free_security = selinux_msg_msg_free_security,
5803
5804 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5805 .msg_queue_free_security = selinux_msg_queue_free_security,
5806 .msg_queue_associate = selinux_msg_queue_associate,
5807 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5808 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5809 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5810
5811 .shm_alloc_security = selinux_shm_alloc_security,
5812 .shm_free_security = selinux_shm_free_security,
5813 .shm_associate = selinux_shm_associate,
5814 .shm_shmctl = selinux_shm_shmctl,
5815 .shm_shmat = selinux_shm_shmat,
5816
Eric Paris828dfe12008-04-17 13:17:49 -04005817 .sem_alloc_security = selinux_sem_alloc_security,
5818 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 .sem_associate = selinux_sem_associate,
5820 .sem_semctl = selinux_sem_semctl,
5821 .sem_semop = selinux_sem_semop,
5822
Eric Paris828dfe12008-04-17 13:17:49 -04005823 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Eric Paris828dfe12008-04-17 13:17:49 -04005825 .getprocattr = selinux_getprocattr,
5826 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005828 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005829 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005830 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005831 .inode_notifysecctx = selinux_inode_notifysecctx,
5832 .inode_setsecctx = selinux_inode_setsecctx,
5833 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005834
Eric Paris828dfe12008-04-17 13:17:49 -04005835 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 .unix_may_send = selinux_socket_unix_may_send,
5837
5838 .socket_create = selinux_socket_create,
5839 .socket_post_create = selinux_socket_post_create,
5840 .socket_bind = selinux_socket_bind,
5841 .socket_connect = selinux_socket_connect,
5842 .socket_listen = selinux_socket_listen,
5843 .socket_accept = selinux_socket_accept,
5844 .socket_sendmsg = selinux_socket_sendmsg,
5845 .socket_recvmsg = selinux_socket_recvmsg,
5846 .socket_getsockname = selinux_socket_getsockname,
5847 .socket_getpeername = selinux_socket_getpeername,
5848 .socket_getsockopt = selinux_socket_getsockopt,
5849 .socket_setsockopt = selinux_socket_setsockopt,
5850 .socket_shutdown = selinux_socket_shutdown,
5851 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005852 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5853 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 .sk_alloc_security = selinux_sk_alloc_security,
5855 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005856 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005857 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005858 .sock_graft = selinux_sock_graft,
5859 .inet_conn_request = selinux_inet_conn_request,
5860 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005861 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005862 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5863 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5864 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005865 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005866 .tun_dev_create = selinux_tun_dev_create,
5867 .tun_dev_post_create = selinux_tun_dev_post_create,
5868 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005869
5870#ifdef CONFIG_SECURITY_NETWORK_XFRM
5871 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5872 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5873 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005874 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005875 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5876 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005877 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005878 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005879 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005880 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005882
5883#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005884 .key_alloc = selinux_key_alloc,
5885 .key_free = selinux_key_free,
5886 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005887 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005888#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005889
5890#ifdef CONFIG_AUDIT
5891 .audit_rule_init = selinux_audit_rule_init,
5892 .audit_rule_known = selinux_audit_rule_known,
5893 .audit_rule_match = selinux_audit_rule_match,
5894 .audit_rule_free = selinux_audit_rule_free,
5895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896};
5897
5898static __init int selinux_init(void)
5899{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005900 if (!security_module_enable(&selinux_ops)) {
5901 selinux_enabled = 0;
5902 return 0;
5903 }
5904
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 if (!selinux_enabled) {
5906 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5907 return 0;
5908 }
5909
5910 printk(KERN_INFO "SELinux: Initializing.\n");
5911
5912 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005913 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005915 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5916
James Morris7cae7e22006-03-22 00:09:22 -08005917 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5918 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005919 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 avc_init();
5921
Eric Paris828dfe12008-04-17 13:17:49 -04005922 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 panic("SELinux: Unable to register with kernel.\n");
5924
Eric Paris828dfe12008-04-17 13:17:49 -04005925 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005926 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005927 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005928 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 return 0;
5931}
5932
Al Viroe8c26252010-03-23 06:36:54 -04005933static void delayed_superblock_init(struct super_block *sb, void *unused)
5934{
5935 superblock_doinit(sb, NULL);
5936}
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938void selinux_complete_init(void)
5939{
Eric Parisfadcdb42007-02-22 18:11:31 -05005940 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005943 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005944 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945}
5946
5947/* SELinux requires early initialization in order to label
5948 all processes and objects when they are created. */
5949security_initcall(selinux_init);
5950
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005951#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Paul Mooreeffad8d2008-01-29 08:49:27 -05005953static struct nf_hook_ops selinux_ipv4_ops[] = {
5954 {
5955 .hook = selinux_ipv4_postroute,
5956 .owner = THIS_MODULE,
5957 .pf = PF_INET,
5958 .hooknum = NF_INET_POST_ROUTING,
5959 .priority = NF_IP_PRI_SELINUX_LAST,
5960 },
5961 {
5962 .hook = selinux_ipv4_forward,
5963 .owner = THIS_MODULE,
5964 .pf = PF_INET,
5965 .hooknum = NF_INET_FORWARD,
5966 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005967 },
5968 {
5969 .hook = selinux_ipv4_output,
5970 .owner = THIS_MODULE,
5971 .pf = PF_INET,
5972 .hooknum = NF_INET_LOCAL_OUT,
5973 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975};
5976
5977#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5978
Paul Mooreeffad8d2008-01-29 08:49:27 -05005979static struct nf_hook_ops selinux_ipv6_ops[] = {
5980 {
5981 .hook = selinux_ipv6_postroute,
5982 .owner = THIS_MODULE,
5983 .pf = PF_INET6,
5984 .hooknum = NF_INET_POST_ROUTING,
5985 .priority = NF_IP6_PRI_SELINUX_LAST,
5986 },
5987 {
5988 .hook = selinux_ipv6_forward,
5989 .owner = THIS_MODULE,
5990 .pf = PF_INET6,
5991 .hooknum = NF_INET_FORWARD,
5992 .priority = NF_IP6_PRI_SELINUX_FIRST,
5993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994};
5995
5996#endif /* IPV6 */
5997
5998static int __init selinux_nf_ip_init(void)
5999{
6000 int err = 0;
6001
6002 if (!selinux_enabled)
6003 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006004
6005 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6006
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006007 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6008 if (err)
6009 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006012 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6013 if (err)
6014 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006016
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017out:
6018 return err;
6019}
6020
6021__initcall(selinux_nf_ip_init);
6022
6023#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6024static void selinux_nf_ip_exit(void)
6025{
Eric Parisfadcdb42007-02-22 18:11:31 -05006026 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006028 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006030 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031#endif /* IPV6 */
6032}
6033#endif
6034
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006035#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
6037#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6038#define selinux_nf_ip_exit()
6039#endif
6040
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006041#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
6043#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006044static int selinux_disabled;
6045
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046int selinux_disable(void)
6047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 if (ss_initialized) {
6049 /* Not permitted after initial policy load. */
6050 return -EINVAL;
6051 }
6052
6053 if (selinux_disabled) {
6054 /* Only do this once. */
6055 return -EINVAL;
6056 }
6057
6058 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6059
6060 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006061 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006063 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
Eric Parisaf8ff042009-09-20 21:23:01 -04006065 /* Try to destroy the avc node cache */
6066 avc_disable();
6067
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 /* Unregister netfilter hooks. */
6069 selinux_nf_ip_exit();
6070
6071 /* Unregister selinuxfs. */
6072 exit_sel_fs();
6073
6074 return 0;
6075}
6076#endif