blob: aa287977f9c202a1ed92fc502a2a11641e40f55e [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 Moore1c5d9d12013-12-04 16:10:45 -050055#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050056#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/ioctls.h>
Arun Sharma60063492011-07-26 16:09:06 -070060#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62#include <linux/interrupt.h>
63#include <linux/netdevice.h> /* for network interface checks */
64#include <linux/netlink.h>
65#include <linux/tcp.h>
66#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080067#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/quota.h>
69#include <linux/un.h> /* for Unix socket types */
70#include <net/af_unix.h> /* for Unix socket types */
71#include <linux/parser.h>
72#include <linux/nfs_mount.h>
73#include <net/ipv6.h>
74#include <linux/hugetlb.h>
75#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070077#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070078#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070079#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070080#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080081#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070082#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040083#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000084#include <linux/msg.h>
85#include <linux/shm.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
Steven Rostedt9e74d932014-01-09 21:46:34 -0500221static void inode_free_rcu(struct rcu_head *head)
222{
223 struct inode_security_struct *isec;
224
225 isec = container_of(head, struct inode_security_struct, rcu);
226 kmem_cache_free(sel_inode_cache, isec);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void inode_free_security(struct inode *inode)
230{
231 struct inode_security_struct *isec = inode->i_security;
232 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_lock(&sbsec->isec_lock);
235 if (!list_empty(&isec->list))
236 list_del_init(&isec->list);
237 spin_unlock(&sbsec->isec_lock);
238
Steven Rostedt9e74d932014-01-09 21:46:34 -0500239 /*
240 * The inode may still be referenced in a path walk and
241 * a call to selinux_inode_permission() can be made
242 * after inode_free_security() is called. Ideally, the VFS
243 * wouldn't do this, but fixing that is a much harder
244 * job. For now, simply free the i_security via RCU, and
245 * leave the current inode->i_security pointer intact.
246 * The inode will be freed after the RCU grace period too.
247 */
248 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static int file_alloc_security(struct file *file)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800256 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!fsec)
258 return -ENOMEM;
259
David Howells275bb412008-11-14 10:39:19 +1100260 fsec->sid = sid;
261 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 file->f_security = fsec;
263
264 return 0;
265}
266
267static void file_free_security(struct file *file)
268{
269 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 file->f_security = NULL;
271 kfree(fsec);
272}
273
274static int superblock_alloc_security(struct super_block *sb)
275{
276 struct superblock_security_struct *sbsec;
277
James Morris89d155e2005-10-30 14:59:21 -0800278 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!sbsec)
280 return -ENOMEM;
281
Eric Parisbc7e9822006-09-25 23:32:02 -0700282 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 INIT_LIST_HEAD(&sbsec->isec_head);
284 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sbsec->sb = sb;
286 sbsec->sid = SECINITSID_UNLABELED;
287 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700288 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 sb->s_security = sbsec;
290
291 return 0;
292}
293
294static void superblock_free_security(struct super_block *sb)
295{
296 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 sb->s_security = NULL;
298 kfree(sbsec);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* The file system's label must be initialized prior to use. */
302
Stephen Hemminger634a5392010-03-04 21:59:03 -0800303static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "uses xattr",
305 "uses transition SIDs",
306 "uses task SIDs",
307 "uses genfs_contexts",
308 "not configured for labeling",
309 "uses mountpoint labeling",
310};
311
312static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
313
314static inline int inode_doinit(struct inode *inode)
315{
316 return inode_doinit_with_dentry(inode, NULL);
317}
318
319enum {
Eric Paris31e87932007-09-19 17:19:12 -0400320 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 Opt_context = 1,
322 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500323 Opt_defcontext = 3,
324 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500325 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
Steven Whitehousea447c092008-10-13 10:46:57 +0100328static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400329 {Opt_context, CONTEXT_STR "%s"},
330 {Opt_fscontext, FSCONTEXT_STR "%s"},
331 {Opt_defcontext, DEFCONTEXT_STR "%s"},
332 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500333 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400334 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
338
Eric Parisc312feb2006-07-10 04:43:53 -0700339static int may_context_mount_sb_relabel(u32 sid,
340 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100341 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700342{
David Howells275bb412008-11-14 10:39:19 +1100343 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700344 int rc;
345
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(tsec->sid, sid, SECCLASS_FILESYSTEM,
352 FILESYSTEM__RELABELTO, NULL);
353 return rc;
354}
355
Eric Paris08089252006-07-10 04:43:55 -0700356static int may_context_mount_inode_relabel(u32 sid,
357 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100358 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700359{
David Howells275bb412008-11-14 10:39:19 +1100360 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700361 int rc;
362 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
363 FILESYSTEM__RELABELFROM, NULL);
364 if (rc)
365 return rc;
366
367 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
368 FILESYSTEM__ASSOCIATE, NULL);
369 return rc;
370}
371
Eric Parisc9180a52007-11-30 13:00:35 -0500372static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct superblock_security_struct *sbsec = sb->s_security;
375 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500376 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int rc = 0;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
380 /* Make sure that the xattr handler exists and that no
381 error other than -ENODATA is returned by getxattr on
382 the root directory. -ENODATA is ok, as this may be
383 the first boot of the SELinux kernel before we have
384 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500385 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
387 "xattr support\n", sb->s_id, sb->s_type->name);
388 rc = -EOPNOTSUPP;
389 goto out;
390 }
Eric Parisc9180a52007-11-30 13:00:35 -0500391 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (rc < 0 && rc != -ENODATA) {
393 if (rc == -EOPNOTSUPP)
394 printk(KERN_WARNING "SELinux: (dev %s, type "
395 "%s) has no security xattr handler\n",
396 sb->s_id, sb->s_type->name);
397 else
398 printk(KERN_WARNING "SELinux: (dev %s, type "
399 "%s) getxattr errno %d\n", sb->s_id,
400 sb->s_type->name, -rc);
401 goto out;
402 }
403 }
404
David P. Quigley11689d42009-01-16 09:22:03 -0500405 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Eric Parisc9180a52007-11-30 13:00:35 -0500407 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500408 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500410 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500411 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sb->s_id, sb->s_type->name,
413 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David P. Quigley11689d42009-01-16 09:22:03 -0500415 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
416 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
417 sbsec->behavior == SECURITY_FS_USE_NONE ||
418 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
419 sbsec->flags &= ~SE_SBLABELSUPP;
420
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400421 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
422 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
423 sbsec->flags |= SE_SBLABELSUPP;
424
Stephen Smalley90078112013-05-10 10:16:19 -0400425 /*
426 * Special handling for rootfs. Is genfs but supports
427 * setting SELinux context on in-core inodes.
428 */
429 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
430 sbsec->flags |= SE_SBLABELSUPP;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500433 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Initialize any other inodes associated with the superblock, e.g.
436 inodes created prior to initial policy load or inodes created
437 during get_sb by a pseudo filesystem that directly
438 populates itself. */
439 spin_lock(&sbsec->isec_lock);
440next_inode:
441 if (!list_empty(&sbsec->isec_head)) {
442 struct inode_security_struct *isec =
443 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500444 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct inode *inode = isec->inode;
446 spin_unlock(&sbsec->isec_lock);
447 inode = igrab(inode);
448 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500449 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 inode_doinit(inode);
451 iput(inode);
452 }
453 spin_lock(&sbsec->isec_lock);
454 list_del_init(&isec->list);
455 goto next_inode;
456 }
457 spin_unlock(&sbsec->isec_lock);
458out:
Eric Parisc9180a52007-11-30 13:00:35 -0500459 return rc;
460}
461
462/*
463 * This function should allow an FS to ask what it's mount security
464 * options were so it can use those later for submounts, displaying
465 * mount options, or whatever.
466 */
467static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500468 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500469{
470 int rc = 0, i;
471 struct superblock_security_struct *sbsec = sb->s_security;
472 char *context = NULL;
473 u32 len;
474 char tmp;
475
Eric Parise0007522008-03-05 10:31:54 -0500476 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500477
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500478 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500479 return -EINVAL;
480
481 if (!ss_initialized)
482 return -EINVAL;
483
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500484 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500485 /* count the number of mount options for this sb */
486 for (i = 0; i < 8; i++) {
487 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500488 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500489 tmp >>= 1;
490 }
David P. Quigley11689d42009-01-16 09:22:03 -0500491 /* Check if the Label support flag is set */
492 if (sbsec->flags & SE_SBLABELSUPP)
493 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500494
Eric Parise0007522008-03-05 10:31:54 -0500495 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
496 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500497 rc = -ENOMEM;
498 goto out_free;
499 }
500
Eric Parise0007522008-03-05 10:31:54 -0500501 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
502 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500503 rc = -ENOMEM;
504 goto out_free;
505 }
506
507 i = 0;
508 if (sbsec->flags & FSCONTEXT_MNT) {
509 rc = security_sid_to_context(sbsec->sid, &context, &len);
510 if (rc)
511 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500512 opts->mnt_opts[i] = context;
513 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500514 }
515 if (sbsec->flags & CONTEXT_MNT) {
516 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
517 if (rc)
518 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500519 opts->mnt_opts[i] = context;
520 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500521 }
522 if (sbsec->flags & DEFCONTEXT_MNT) {
523 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
524 if (rc)
525 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500526 opts->mnt_opts[i] = context;
527 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500528 }
529 if (sbsec->flags & ROOTCONTEXT_MNT) {
530 struct inode *root = sbsec->sb->s_root->d_inode;
531 struct inode_security_struct *isec = root->i_security;
532
533 rc = security_sid_to_context(isec->sid, &context, &len);
534 if (rc)
535 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500536 opts->mnt_opts[i] = context;
537 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500538 }
David P. Quigley11689d42009-01-16 09:22:03 -0500539 if (sbsec->flags & SE_SBLABELSUPP) {
540 opts->mnt_opts[i] = NULL;
541 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
542 }
Eric Parisc9180a52007-11-30 13:00:35 -0500543
Eric Parise0007522008-03-05 10:31:54 -0500544 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500545
546 return 0;
547
548out_free:
Eric Parise0007522008-03-05 10:31:54 -0500549 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500550 return rc;
551}
552
553static int bad_option(struct superblock_security_struct *sbsec, char flag,
554 u32 old_sid, u32 new_sid)
555{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500556 char mnt_flags = sbsec->flags & SE_MNTMASK;
557
Eric Parisc9180a52007-11-30 13:00:35 -0500558 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500559 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500560 if (!(sbsec->flags & flag) ||
561 (old_sid != new_sid))
562 return 1;
563
564 /* check if we were passed the same options twice,
565 * aka someone passed context=a,context=b
566 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500567 if (!(sbsec->flags & SE_SBINITIALIZED))
568 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500569 return 1;
570 return 0;
571}
Eric Parise0007522008-03-05 10:31:54 -0500572
Eric Parisc9180a52007-11-30 13:00:35 -0500573/*
574 * Allow filesystems with binary mount data to explicitly set mount point
575 * labeling information.
576 */
Eric Parise0007522008-03-05 10:31:54 -0500577static int selinux_set_mnt_opts(struct super_block *sb,
578 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500579{
David Howells275bb412008-11-14 10:39:19 +1100580 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500581 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500582 struct superblock_security_struct *sbsec = sb->s_security;
583 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000584 struct inode *inode = sbsec->sb->s_root->d_inode;
585 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500586 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
587 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500588 char **mount_options = opts->mnt_opts;
589 int *flags = opts->mnt_opts_flags;
590 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500591
592 mutex_lock(&sbsec->lock);
593
594 if (!ss_initialized) {
595 if (!num_opts) {
596 /* Defer initialization until selinux_complete_init,
597 after the initial policy is loaded and the security
598 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500599 goto out;
600 }
601 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400602 printk(KERN_WARNING "SELinux: Unable to set superblock options "
603 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500604 goto out;
605 }
606
607 /*
Eric Parise0007522008-03-05 10:31:54 -0500608 * Binary mount data FS will come through this function twice. Once
609 * from an explicit call and once from the generic calls from the vfs.
610 * Since the generic VFS calls will not contain any security mount data
611 * we need to skip the double mount verification.
612 *
613 * This does open a hole in which we will not notice if the first
614 * mount using this sb set explict options and a second mount using
615 * this sb does not set any security options. (The first options
616 * will be used for both mounts)
617 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500618 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500619 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400620 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500621
622 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500623 * parse the mount options, check if they are valid sids.
624 * also check if someone is trying to mount the same sb more
625 * than once with different security options.
626 */
627 for (i = 0; i < num_opts; i++) {
628 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500629
630 if (flags[i] == SE_SBLABELSUPP)
631 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500632 rc = security_context_to_sid(mount_options[i],
633 strlen(mount_options[i]), &sid);
634 if (rc) {
635 printk(KERN_WARNING "SELinux: security_context_to_sid"
636 "(%s) failed for (dev %s, type %s) errno=%d\n",
637 mount_options[i], sb->s_id, name, rc);
638 goto out;
639 }
640 switch (flags[i]) {
641 case FSCONTEXT_MNT:
642 fscontext_sid = sid;
643
644 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
645 fscontext_sid))
646 goto out_double_mount;
647
648 sbsec->flags |= FSCONTEXT_MNT;
649 break;
650 case CONTEXT_MNT:
651 context_sid = sid;
652
653 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
654 context_sid))
655 goto out_double_mount;
656
657 sbsec->flags |= CONTEXT_MNT;
658 break;
659 case ROOTCONTEXT_MNT:
660 rootcontext_sid = sid;
661
662 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
663 rootcontext_sid))
664 goto out_double_mount;
665
666 sbsec->flags |= ROOTCONTEXT_MNT;
667
668 break;
669 case DEFCONTEXT_MNT:
670 defcontext_sid = sid;
671
672 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
673 defcontext_sid))
674 goto out_double_mount;
675
676 sbsec->flags |= DEFCONTEXT_MNT;
677
678 break;
679 default:
680 rc = -EINVAL;
681 goto out;
682 }
683 }
684
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500685 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500686 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500687 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500688 goto out_double_mount;
689 rc = 0;
690 goto out;
691 }
692
James Morris089be432008-07-15 18:32:49 +1000693 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500694 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500695
696 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500697 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500698 if (rc) {
699 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000700 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500701 goto out;
702 }
703
704 /* sets the context of the superblock for the fs being mounted. */
705 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100706 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500707 if (rc)
708 goto out;
709
710 sbsec->sid = fscontext_sid;
711 }
712
713 /*
714 * Switch to using mount point labeling behavior.
715 * sets the label used on all file below the mountpoint, and will set
716 * the superblock context if not already set.
717 */
718 if (context_sid) {
719 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100720 rc = may_context_mount_sb_relabel(context_sid, sbsec,
721 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500722 if (rc)
723 goto out;
724 sbsec->sid = context_sid;
725 } else {
David Howells275bb412008-11-14 10:39:19 +1100726 rc = may_context_mount_inode_relabel(context_sid, sbsec,
727 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500728 if (rc)
729 goto out;
730 }
731 if (!rootcontext_sid)
732 rootcontext_sid = context_sid;
733
734 sbsec->mntpoint_sid = context_sid;
735 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
736 }
737
738 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100739 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
740 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500741 if (rc)
742 goto out;
743
744 root_isec->sid = rootcontext_sid;
745 root_isec->initialized = 1;
746 }
747
748 if (defcontext_sid) {
749 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
750 rc = -EINVAL;
751 printk(KERN_WARNING "SELinux: defcontext option is "
752 "invalid for this filesystem type\n");
753 goto out;
754 }
755
756 if (defcontext_sid != sbsec->def_sid) {
757 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100758 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500759 if (rc)
760 goto out;
761 }
762
763 sbsec->def_sid = defcontext_sid;
764 }
765
766 rc = sb_finish_set_opts(sb);
767out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700768 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500770out_double_mount:
771 rc = -EINVAL;
772 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
773 "security settings for (dev %s, type %s)\n", sb->s_id, name);
774 goto out;
775}
776
777static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
778 struct super_block *newsb)
779{
780 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
781 struct superblock_security_struct *newsbsec = newsb->s_security;
782
783 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
784 int set_context = (oldsbsec->flags & CONTEXT_MNT);
785 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
786
Eric Paris0f5e6422008-04-21 16:24:11 -0400787 /*
788 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400789 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400790 */
Al Viroe8c26252010-03-23 06:36:54 -0400791 if (!ss_initialized)
Eric Paris0f5e6422008-04-21 16:24:11 -0400792 return;
Eric Parisc9180a52007-11-30 13:00:35 -0500793
Eric Parisc9180a52007-11-30 13:00:35 -0500794 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500795 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500796
Eric Paris5a552612008-04-09 14:08:35 -0400797 /* if fs is reusing a sb, just let its options stand... */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500798 if (newsbsec->flags & SE_SBINITIALIZED)
Eric Paris5a552612008-04-09 14:08:35 -0400799 return;
800
Eric Parisc9180a52007-11-30 13:00:35 -0500801 mutex_lock(&newsbsec->lock);
802
803 newsbsec->flags = oldsbsec->flags;
804
805 newsbsec->sid = oldsbsec->sid;
806 newsbsec->def_sid = oldsbsec->def_sid;
807 newsbsec->behavior = oldsbsec->behavior;
808
809 if (set_context) {
810 u32 sid = oldsbsec->mntpoint_sid;
811
812 if (!set_fscontext)
813 newsbsec->sid = sid;
814 if (!set_rootcontext) {
815 struct inode *newinode = newsb->s_root->d_inode;
816 struct inode_security_struct *newisec = newinode->i_security;
817 newisec->sid = sid;
818 }
819 newsbsec->mntpoint_sid = sid;
820 }
821 if (set_rootcontext) {
822 const struct inode *oldinode = oldsb->s_root->d_inode;
823 const struct inode_security_struct *oldisec = oldinode->i_security;
824 struct inode *newinode = newsb->s_root->d_inode;
825 struct inode_security_struct *newisec = newinode->i_security;
826
827 newisec->sid = oldisec->sid;
828 }
829
830 sb_finish_set_opts(newsb);
831 mutex_unlock(&newsbsec->lock);
832}
833
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200834static int selinux_parse_opts_str(char *options,
835 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500836{
Eric Parise0007522008-03-05 10:31:54 -0500837 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500838 char *context = NULL, *defcontext = NULL;
839 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500840 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500841
Eric Parise0007522008-03-05 10:31:54 -0500842 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500843
844 /* Standard string-based options. */
845 while ((p = strsep(&options, "|")) != NULL) {
846 int token;
847 substring_t args[MAX_OPT_ARGS];
848
849 if (!*p)
850 continue;
851
852 token = match_token(p, tokens, args);
853
854 switch (token) {
855 case Opt_context:
856 if (context || defcontext) {
857 rc = -EINVAL;
858 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
859 goto out_err;
860 }
861 context = match_strdup(&args[0]);
862 if (!context) {
863 rc = -ENOMEM;
864 goto out_err;
865 }
866 break;
867
868 case Opt_fscontext:
869 if (fscontext) {
870 rc = -EINVAL;
871 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
872 goto out_err;
873 }
874 fscontext = match_strdup(&args[0]);
875 if (!fscontext) {
876 rc = -ENOMEM;
877 goto out_err;
878 }
879 break;
880
881 case Opt_rootcontext:
882 if (rootcontext) {
883 rc = -EINVAL;
884 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
885 goto out_err;
886 }
887 rootcontext = match_strdup(&args[0]);
888 if (!rootcontext) {
889 rc = -ENOMEM;
890 goto out_err;
891 }
892 break;
893
894 case Opt_defcontext:
895 if (context || defcontext) {
896 rc = -EINVAL;
897 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
898 goto out_err;
899 }
900 defcontext = match_strdup(&args[0]);
901 if (!defcontext) {
902 rc = -ENOMEM;
903 goto out_err;
904 }
905 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500906 case Opt_labelsupport:
907 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500908 default:
909 rc = -EINVAL;
910 printk(KERN_WARNING "SELinux: unknown mount option\n");
911 goto out_err;
912
913 }
914 }
915
Eric Parise0007522008-03-05 10:31:54 -0500916 rc = -ENOMEM;
917 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
918 if (!opts->mnt_opts)
919 goto out_err;
920
921 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
922 if (!opts->mnt_opts_flags) {
923 kfree(opts->mnt_opts);
924 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500925 }
926
Eric Parise0007522008-03-05 10:31:54 -0500927 if (fscontext) {
928 opts->mnt_opts[num_mnt_opts] = fscontext;
929 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
930 }
931 if (context) {
932 opts->mnt_opts[num_mnt_opts] = context;
933 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
934 }
935 if (rootcontext) {
936 opts->mnt_opts[num_mnt_opts] = rootcontext;
937 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
938 }
939 if (defcontext) {
940 opts->mnt_opts[num_mnt_opts] = defcontext;
941 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
942 }
943
944 opts->num_mnt_opts = num_mnt_opts;
945 return 0;
946
Eric Parisc9180a52007-11-30 13:00:35 -0500947out_err:
948 kfree(context);
949 kfree(defcontext);
950 kfree(fscontext);
951 kfree(rootcontext);
952 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
Eric Parise0007522008-03-05 10:31:54 -0500954/*
955 * string mount options parsing and call set the sbsec
956 */
957static int superblock_doinit(struct super_block *sb, void *data)
958{
959 int rc = 0;
960 char *options = data;
961 struct security_mnt_opts opts;
962
963 security_init_mnt_opts(&opts);
964
965 if (!data)
966 goto out;
967
968 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
969
970 rc = selinux_parse_opts_str(options, &opts);
971 if (rc)
972 goto out_err;
973
974out:
975 rc = selinux_set_mnt_opts(sb, &opts);
976
977out_err:
978 security_free_mnt_opts(&opts);
979 return rc;
980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Adrian Bunk3583a712008-07-22 20:21:23 +0300982static void selinux_write_opts(struct seq_file *m,
983 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000984{
985 int i;
986 char *prefix;
987
988 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500989 char *has_comma;
990
991 if (opts->mnt_opts[i])
992 has_comma = strchr(opts->mnt_opts[i], ',');
993 else
994 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +1000995
996 switch (opts->mnt_opts_flags[i]) {
997 case CONTEXT_MNT:
998 prefix = CONTEXT_STR;
999 break;
1000 case FSCONTEXT_MNT:
1001 prefix = FSCONTEXT_STR;
1002 break;
1003 case ROOTCONTEXT_MNT:
1004 prefix = ROOTCONTEXT_STR;
1005 break;
1006 case DEFCONTEXT_MNT:
1007 prefix = DEFCONTEXT_STR;
1008 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001009 case SE_SBLABELSUPP:
1010 seq_putc(m, ',');
1011 seq_puts(m, LABELSUPP_STR);
1012 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001013 default:
1014 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001015 return;
Eric Paris2069f452008-07-04 09:47:13 +10001016 };
1017 /* we need a comma before each option */
1018 seq_putc(m, ',');
1019 seq_puts(m, prefix);
1020 if (has_comma)
1021 seq_putc(m, '\"');
1022 seq_puts(m, opts->mnt_opts[i]);
1023 if (has_comma)
1024 seq_putc(m, '\"');
1025 }
1026}
1027
1028static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1029{
1030 struct security_mnt_opts opts;
1031 int rc;
1032
1033 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001034 if (rc) {
1035 /* before policy load we may get EINVAL, don't show anything */
1036 if (rc == -EINVAL)
1037 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001038 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001039 }
Eric Paris2069f452008-07-04 09:47:13 +10001040
1041 selinux_write_opts(m, &opts);
1042
1043 security_free_mnt_opts(&opts);
1044
1045 return rc;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048static inline u16 inode_mode_to_security_class(umode_t mode)
1049{
1050 switch (mode & S_IFMT) {
1051 case S_IFSOCK:
1052 return SECCLASS_SOCK_FILE;
1053 case S_IFLNK:
1054 return SECCLASS_LNK_FILE;
1055 case S_IFREG:
1056 return SECCLASS_FILE;
1057 case S_IFBLK:
1058 return SECCLASS_BLK_FILE;
1059 case S_IFDIR:
1060 return SECCLASS_DIR;
1061 case S_IFCHR:
1062 return SECCLASS_CHR_FILE;
1063 case S_IFIFO:
1064 return SECCLASS_FIFO_FILE;
1065
1066 }
1067
1068 return SECCLASS_FILE;
1069}
1070
James Morris13402582005-09-30 14:24:34 -04001071static inline int default_protocol_stream(int protocol)
1072{
1073 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1074}
1075
1076static inline int default_protocol_dgram(int protocol)
1077{
1078 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1082{
1083 switch (family) {
1084 case PF_UNIX:
1085 switch (type) {
1086 case SOCK_STREAM:
1087 case SOCK_SEQPACKET:
1088 return SECCLASS_UNIX_STREAM_SOCKET;
1089 case SOCK_DGRAM:
1090 return SECCLASS_UNIX_DGRAM_SOCKET;
1091 }
1092 break;
1093 case PF_INET:
1094 case PF_INET6:
1095 switch (type) {
1096 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001097 if (default_protocol_stream(protocol))
1098 return SECCLASS_TCP_SOCKET;
1099 else
1100 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001102 if (default_protocol_dgram(protocol))
1103 return SECCLASS_UDP_SOCKET;
1104 else
1105 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001106 case SOCK_DCCP:
1107 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return SECCLASS_RAWIP_SOCKET;
1110 }
1111 break;
1112 case PF_NETLINK:
1113 switch (protocol) {
1114 case NETLINK_ROUTE:
1115 return SECCLASS_NETLINK_ROUTE_SOCKET;
1116 case NETLINK_FIREWALL:
1117 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001118 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1120 case NETLINK_NFLOG:
1121 return SECCLASS_NETLINK_NFLOG_SOCKET;
1122 case NETLINK_XFRM:
1123 return SECCLASS_NETLINK_XFRM_SOCKET;
1124 case NETLINK_SELINUX:
1125 return SECCLASS_NETLINK_SELINUX_SOCKET;
1126 case NETLINK_AUDIT:
1127 return SECCLASS_NETLINK_AUDIT_SOCKET;
1128 case NETLINK_IP6_FW:
1129 return SECCLASS_NETLINK_IP6FW_SOCKET;
1130 case NETLINK_DNRTMSG:
1131 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001132 case NETLINK_KOBJECT_UEVENT:
1133 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 default:
1135 return SECCLASS_NETLINK_SOCKET;
1136 }
1137 case PF_PACKET:
1138 return SECCLASS_PACKET_SOCKET;
1139 case PF_KEY:
1140 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001141 case PF_APPLETALK:
1142 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
1145 return SECCLASS_SOCKET;
1146}
1147
1148#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001149static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 u16 tclass,
1151 u32 *sid)
1152{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001153 int rc;
1154 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Paris828dfe12008-04-17 13:17:49 -04001156 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!buffer)
1158 return -ENOMEM;
1159
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001160 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1161 if (IS_ERR(path))
1162 rc = PTR_ERR(path);
1163 else {
1164 /* each process gets a /proc/PID/ entry. Strip off the
1165 * PID part to get a valid selinux labeling.
1166 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1167 while (path[1] >= '0' && path[1] <= '9') {
1168 path[1] = '/';
1169 path++;
1170 }
1171 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 free_page((unsigned long)buffer);
1174 return rc;
1175}
1176#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001177static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 u16 tclass,
1179 u32 *sid)
1180{
1181 return -EINVAL;
1182}
1183#endif
1184
1185/* The inode's security attributes must be initialized before first use. */
1186static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1187{
1188 struct superblock_security_struct *sbsec = NULL;
1189 struct inode_security_struct *isec = inode->i_security;
1190 u32 sid;
1191 struct dentry *dentry;
1192#define INITCONTEXTLEN 255
1193 char *context = NULL;
1194 unsigned len = 0;
1195 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (isec->initialized)
1198 goto out;
1199
Eric Paris23970742006-09-25 23:32:01 -07001200 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001202 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001205 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Defer initialization until selinux_complete_init,
1207 after the initial policy is loaded and the security
1208 server is ready to handle calls. */
1209 spin_lock(&sbsec->isec_lock);
1210 if (list_empty(&isec->list))
1211 list_add(&isec->list, &sbsec->isec_head);
1212 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001213 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 switch (sbsec->behavior) {
1217 case SECURITY_FS_USE_XATTR:
1218 if (!inode->i_op->getxattr) {
1219 isec->sid = sbsec->def_sid;
1220 break;
1221 }
1222
1223 /* Need a dentry, since the xattr API requires one.
1224 Life would be simpler if we could just pass the inode. */
1225 if (opt_dentry) {
1226 /* Called from d_instantiate or d_splice_alias. */
1227 dentry = dget(opt_dentry);
1228 } else {
1229 /* Called from selinux_complete_init, try to find a dentry. */
1230 dentry = d_find_alias(inode);
1231 }
1232 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001233 /*
1234 * this is can be hit on boot when a file is accessed
1235 * before the policy is loaded. When we load policy we
1236 * may find inodes that have no dentry on the
1237 * sbsec->isec_head list. No reason to complain as these
1238 * will get fixed up the next time we go through
1239 * inode_doinit with a dentry, before these inodes could
1240 * be used again by userspace.
1241 */
Eric Paris23970742006-09-25 23:32:01 -07001242 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245 len = INITCONTEXTLEN;
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, XATTR_NAME_SELINUX,
1254 context, len);
1255 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001256 kfree(context);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Need a larger buffer. Query for the right size. */
1259 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1260 NULL, 0);
1261 if (rc < 0) {
1262 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001263 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001266 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!context) {
1268 rc = -ENOMEM;
1269 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001270 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001272 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rc = inode->i_op->getxattr(dentry,
1274 XATTR_NAME_SELINUX,
1275 context, len);
1276 }
1277 dput(dentry);
1278 if (rc < 0) {
1279 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001280 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001281 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 -rc, inode->i_sb->s_id, inode->i_ino);
1283 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001284 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 /* Map ENODATA to the default file SID */
1287 sid = sbsec->def_sid;
1288 rc = 0;
1289 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001290 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001291 sbsec->def_sid,
1292 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001294 char *dev = inode->i_sb->s_id;
1295 unsigned long ino = inode->i_ino;
1296
1297 if (rc == -EINVAL) {
1298 if (printk_ratelimit())
1299 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1300 "context=%s. This indicates you may need to relabel the inode or the "
1301 "filesystem in question.\n", ino, dev, context);
1302 } else {
1303 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1304 "returned %d for dev=%s ino=%ld\n",
1305 __func__, context, -rc, dev, ino);
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 kfree(context);
1308 /* Leave with the unlabeled SID */
1309 rc = 0;
1310 break;
1311 }
1312 }
1313 kfree(context);
1314 isec->sid = sid;
1315 break;
1316 case SECURITY_FS_USE_TASK:
1317 isec->sid = isec->task_sid;
1318 break;
1319 case SECURITY_FS_USE_TRANS:
1320 /* Default to the fs SID. */
1321 isec->sid = sbsec->sid;
1322
1323 /* Try to obtain a transition SID. */
1324 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001325 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1326 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001328 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 isec->sid = sid;
1330 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001331 case SECURITY_FS_USE_MNTPOINT:
1332 isec->sid = sbsec->mntpoint_sid;
1333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001335 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 isec->sid = sbsec->sid;
1337
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001338 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001339 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001341 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 isec->sclass,
1343 &sid);
1344 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001345 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 isec->sid = sid;
1347 }
1348 }
1349 break;
1350 }
1351
1352 isec->initialized = 1;
1353
Eric Paris23970742006-09-25 23:32:01 -07001354out_unlock:
1355 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356out:
1357 if (isec->sclass == SECCLASS_FILE)
1358 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return rc;
1360}
1361
1362/* Convert a Linux signal to an access vector. */
1363static inline u32 signal_to_av(int sig)
1364{
1365 u32 perm = 0;
1366
1367 switch (sig) {
1368 case SIGCHLD:
1369 /* Commonly granted from child to parent. */
1370 perm = PROCESS__SIGCHLD;
1371 break;
1372 case SIGKILL:
1373 /* Cannot be caught or ignored */
1374 perm = PROCESS__SIGKILL;
1375 break;
1376 case SIGSTOP:
1377 /* Cannot be caught or ignored */
1378 perm = PROCESS__SIGSTOP;
1379 break;
1380 default:
1381 /* All other signals. */
1382 perm = PROCESS__SIGNAL;
1383 break;
1384 }
1385
1386 return perm;
1387}
1388
David Howells275bb412008-11-14 10:39:19 +11001389/*
David Howellsd84f4f92008-11-14 10:39:23 +11001390 * Check permission between a pair of credentials
1391 * fork check, ptrace check, etc.
1392 */
1393static int cred_has_perm(const struct cred *actor,
1394 const struct cred *target,
1395 u32 perms)
1396{
1397 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1398
1399 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1400}
1401
1402/*
David Howells88e67f32008-11-14 10:39:21 +11001403 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001404 * fork check, ptrace check, etc.
1405 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001406 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001407 */
1408static int task_has_perm(const struct task_struct *tsk1,
1409 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 u32 perms)
1411{
David Howells275bb412008-11-14 10:39:19 +11001412 const struct task_security_struct *__tsec1, *__tsec2;
1413 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
David Howells275bb412008-11-14 10:39:19 +11001415 rcu_read_lock();
1416 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1417 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1418 rcu_read_unlock();
1419 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
David Howells3b11a1d2008-11-14 10:39:26 +11001422/*
1423 * Check permission between current and another task, e.g. signal checks,
1424 * fork check, ptrace check, etc.
1425 * current is the actor and tsk2 is the target
1426 * - this uses current's subjective creds
1427 */
1428static int current_has_perm(const struct task_struct *tsk,
1429 u32 perms)
1430{
1431 u32 sid, tsid;
1432
1433 sid = current_sid();
1434 tsid = task_sid(tsk);
1435 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1436}
1437
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001438#if CAP_LAST_CAP > 63
1439#error Fix SELinux to handle capabilities > 63.
1440#endif
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001443static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001444 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Thomas Liu2bf49692009-07-14 12:14:09 -04001446 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001447 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001448 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001449 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001450 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001451 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001452 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Thomas Liu2bf49692009-07-14 12:14:09 -04001454 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001455 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001456 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 ad.u.cap = cap;
1458
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001459 switch (CAP_TO_INDEX(cap)) {
1460 case 0:
1461 sclass = SECCLASS_CAPABILITY;
1462 break;
1463 case 1:
1464 sclass = SECCLASS_CAPABILITY2;
1465 break;
1466 default:
1467 printk(KERN_ERR
1468 "SELinux: out of range capability %d\n", cap);
1469 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001470 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001471 }
Eric Paris06112162008-11-11 22:02:50 +11001472
David Howells275bb412008-11-14 10:39:19 +11001473 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001474 if (audit == SECURITY_CAP_AUDIT) {
1475 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1476 if (rc2)
1477 return rc2;
1478 }
Eric Paris06112162008-11-11 22:02:50 +11001479 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/* Check whether a task is allowed to use a system operation. */
1483static int task_has_system(struct task_struct *tsk,
1484 u32 perms)
1485{
David Howells275bb412008-11-14 10:39:19 +11001486 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
David Howells275bb412008-11-14 10:39:19 +11001488 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 SECCLASS_SYSTEM, perms, NULL);
1490}
1491
1492/* Check whether a task has a particular permission to an inode.
1493 The 'adp' parameter is optional and allows other audit
1494 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001495static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct inode *inode,
1497 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001498 struct common_audit_data *adp,
1499 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001502 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
David Howellse0e81732009-09-02 09:13:40 +01001504 validate_creds(cred);
1505
Eric Paris828dfe12008-04-17 13:17:49 -04001506 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001507 return 0;
1508
David Howells88e67f32008-11-14 10:39:21 +11001509 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 isec = inode->i_security;
1511
Eric Paris9ade0cf2011-04-25 16:26:29 -04001512 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001515static int inode_has_perm_noadp(const struct cred *cred,
1516 struct inode *inode,
1517 u32 perms,
1518 unsigned flags)
1519{
1520 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001521 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001522
1523 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1524 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001525 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001526 return inode_has_perm(cred, inode, perms, &ad, flags);
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/* Same as inode_has_perm, but pass explicit audit data containing
1530 the dentry to help the auditing code to more easily generate the
1531 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001532static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct dentry *dentry,
1534 u32 av)
1535{
1536 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001537 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001538 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001539
Eric Paris2875fa02011-04-28 16:04:24 -04001540 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1541 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001542 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001543 return inode_has_perm(cred, inode, av, &ad, 0);
1544}
1545
1546/* Same as inode_has_perm, but pass explicit audit data containing
1547 the path to help the auditing code to more easily generate the
1548 pathname if needed. */
1549static inline int path_has_perm(const struct cred *cred,
1550 struct path *path,
1551 u32 av)
1552{
1553 struct inode *inode = path->dentry->d_inode;
1554 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001555 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001556
Eric Parisf48b7392011-04-25 12:54:27 -04001557 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001558 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001559 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001560 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563/* Check whether a task can use an open file descriptor to
1564 access an inode in a given way. Check access to the
1565 descriptor itself, and then use dentry_has_perm to
1566 check a particular permission to the file.
1567 Access to the descriptor is implicitly granted if it
1568 has the same SID as the process. If av is zero, then
1569 access to the file is not checked, e.g. for cases
1570 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001571static int file_has_perm(const struct cred *cred,
1572 struct file *file,
1573 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001576 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001577 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001578 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001579 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int rc;
1581
Eric Parisf48b7392011-04-25 12:54:27 -04001582 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1583 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001584 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
David Howells275bb412008-11-14 10:39:19 +11001586 if (sid != fsec->sid) {
1587 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 SECCLASS_FD,
1589 FD__USE,
1590 &ad);
1591 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
1595 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001596 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001598 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David Howells88e67f32008-11-14 10:39:21 +11001600out:
1601 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604/* Check whether a task can create a file. */
1605static int may_create(struct inode *dir,
1606 struct dentry *dentry,
1607 u16 tclass)
1608{
Paul Moore5fb49872010-04-22 14:46:19 -04001609 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct inode_security_struct *dsec;
1611 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001612 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001613 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001614 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int rc;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 dsec = dir->i_security;
1618 sbsec = dir->i_sb->s_security;
1619
David Howells275bb412008-11-14 10:39:19 +11001620 sid = tsec->sid;
1621 newsid = tsec->create_sid;
1622
Eric Parisa2694342011-04-25 13:10:27 -04001623 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1624 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001625 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
David Howells275bb412008-11-14 10:39:19 +11001627 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 DIR__ADD_NAME | DIR__SEARCH,
1629 &ad);
1630 if (rc)
1631 return rc;
1632
David P. Quigleycd895962009-01-16 09:22:04 -05001633 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001634 rc = security_transition_sid(sid, dsec->sid, tclass,
1635 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (rc)
1637 return rc;
1638 }
1639
David Howells275bb412008-11-14 10:39:19 +11001640 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (rc)
1642 return rc;
1643
1644 return avc_has_perm(newsid, sbsec->sid,
1645 SECCLASS_FILESYSTEM,
1646 FILESYSTEM__ASSOCIATE, &ad);
1647}
1648
Michael LeMay4eb582c2006-06-26 00:24:57 -07001649/* Check whether a task can create a key. */
1650static int may_create_key(u32 ksid,
1651 struct task_struct *ctx)
1652{
David Howells275bb412008-11-14 10:39:19 +11001653 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001654
David Howells275bb412008-11-14 10:39:19 +11001655 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001656}
1657
Eric Paris828dfe12008-04-17 13:17:49 -04001658#define MAY_LINK 0
1659#define MAY_UNLINK 1
1660#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662/* Check whether a task can link, unlink, or rmdir a file/directory. */
1663static int may_link(struct inode *dir,
1664 struct dentry *dentry,
1665 int kind)
1666
1667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001669 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001670 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001671 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 u32 av;
1673 int rc;
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 dsec = dir->i_security;
1676 isec = dentry->d_inode->i_security;
1677
Eric Parisa2694342011-04-25 13:10:27 -04001678 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1679 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001680 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 av = DIR__SEARCH;
1683 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001684 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (rc)
1686 return rc;
1687
1688 switch (kind) {
1689 case MAY_LINK:
1690 av = FILE__LINK;
1691 break;
1692 case MAY_UNLINK:
1693 av = FILE__UNLINK;
1694 break;
1695 case MAY_RMDIR:
1696 av = DIR__RMDIR;
1697 break;
1698 default:
Eric Paris744ba352008-04-17 11:52:44 -04001699 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1700 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702 }
1703
David Howells275bb412008-11-14 10:39:19 +11001704 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return rc;
1706}
1707
1708static inline int may_rename(struct inode *old_dir,
1709 struct dentry *old_dentry,
1710 struct inode *new_dir,
1711 struct dentry *new_dentry)
1712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001714 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001715 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001716 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 u32 av;
1718 int old_is_dir, new_is_dir;
1719 int rc;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 old_dsec = old_dir->i_security;
1722 old_isec = old_dentry->d_inode->i_security;
1723 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1724 new_dsec = new_dir->i_security;
1725
Eric Parisa2694342011-04-25 13:10:27 -04001726 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001727 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Eric Parisa2694342011-04-25 13:10:27 -04001729 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001730 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1732 if (rc)
1733 return rc;
David Howells275bb412008-11-14 10:39:19 +11001734 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 old_isec->sclass, FILE__RENAME, &ad);
1736 if (rc)
1737 return rc;
1738 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001739 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 old_isec->sclass, DIR__REPARENT, &ad);
1741 if (rc)
1742 return rc;
1743 }
1744
Eric Parisa2694342011-04-25 13:10:27 -04001745 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 av = DIR__ADD_NAME | DIR__SEARCH;
1747 if (new_dentry->d_inode)
1748 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001749 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (rc)
1751 return rc;
1752 if (new_dentry->d_inode) {
1753 new_isec = new_dentry->d_inode->i_security;
1754 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001755 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 new_isec->sclass,
1757 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1758 if (rc)
1759 return rc;
1760 }
1761
1762 return 0;
1763}
1764
1765/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001766static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct super_block *sb,
1768 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001769 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001772 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001775 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778/* Convert a Linux mode and permission mask to an access vector. */
1779static inline u32 file_mask_to_av(int mode, int mask)
1780{
1781 u32 av = 0;
1782
Al Virodba19c62011-07-25 20:49:29 -04001783 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (mask & MAY_EXEC)
1785 av |= FILE__EXECUTE;
1786 if (mask & MAY_READ)
1787 av |= FILE__READ;
1788
1789 if (mask & MAY_APPEND)
1790 av |= FILE__APPEND;
1791 else if (mask & MAY_WRITE)
1792 av |= FILE__WRITE;
1793
1794 } else {
1795 if (mask & MAY_EXEC)
1796 av |= DIR__SEARCH;
1797 if (mask & MAY_WRITE)
1798 av |= DIR__WRITE;
1799 if (mask & MAY_READ)
1800 av |= DIR__READ;
1801 }
1802
1803 return av;
1804}
1805
1806/* Convert a Linux file to an access vector. */
1807static inline u32 file_to_av(struct file *file)
1808{
1809 u32 av = 0;
1810
1811 if (file->f_mode & FMODE_READ)
1812 av |= FILE__READ;
1813 if (file->f_mode & FMODE_WRITE) {
1814 if (file->f_flags & O_APPEND)
1815 av |= FILE__APPEND;
1816 else
1817 av |= FILE__WRITE;
1818 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001819 if (!av) {
1820 /*
1821 * Special file opened with flags 3 for ioctl-only use.
1822 */
1823 av = FILE__IOCTL;
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 return av;
1827}
1828
Eric Paris8b6a5a32008-10-29 17:06:46 -04001829/*
1830 * Convert a file to an access vector and include the correct open
1831 * open permission.
1832 */
1833static inline u32 open_file_to_av(struct file *file)
1834{
1835 u32 av = file_to_av(file);
1836
Eric Paris49b7b8d2010-07-23 11:44:09 -04001837 if (selinux_policycap_openperm)
1838 av |= FILE__OPEN;
1839
Eric Paris8b6a5a32008-10-29 17:06:46 -04001840 return av;
1841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843/* Hook functions begin here. */
1844
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05001845static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1846{
1847 u32 mysid = current_sid();
1848 u32 mgrsid = task_sid(mgr);
1849
1850 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1851}
1852
1853static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1854{
1855 u32 mysid = current_sid();
1856 u32 fromsid = task_sid(from);
1857 u32 tosid = task_sid(to);
1858 int rc;
1859
1860 if (mysid != fromsid) {
1861 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1862 if (rc)
1863 return rc;
1864 }
1865
1866 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1867}
1868
1869static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1870{
1871 u32 fromsid = task_sid(from);
1872 u32 tosid = task_sid(to);
1873 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1874}
1875
1876static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1877{
1878 u32 sid = task_sid(to);
1879 struct file_security_struct *fsec = file->f_security;
1880 struct inode *inode = file->f_path.dentry->d_inode;
1881 struct inode_security_struct *isec = inode->i_security;
1882 struct common_audit_data ad;
1883 struct selinux_audit_data sad = {0,};
1884 int rc;
1885
1886 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1887 ad.u.path = file->f_path;
1888 ad.selinux_audit_data = &sad;
1889
1890 if (sid != fsec->sid) {
1891 rc = avc_has_perm(sid, fsec->sid,
1892 SECCLASS_FD,
1893 FD__USE,
1894 &ad);
1895 if (rc)
1896 return rc;
1897 }
1898
1899 if (unlikely(IS_PRIVATE(inode)))
1900 return 0;
1901
1902 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1903 &ad);
1904}
1905
Ingo Molnar9e488582009-05-07 19:26:19 +10001906static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001907 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int rc;
1910
Ingo Molnar9e488582009-05-07 19:26:19 +10001911 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (rc)
1913 return rc;
1914
Eric Paris69f594a2012-01-03 12:25:15 -05001915 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001916 u32 sid = current_sid();
1917 u32 csid = task_sid(child);
1918 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001919 }
1920
David Howells3b11a1d2008-11-14 10:39:26 +11001921 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001922}
1923
1924static int selinux_ptrace_traceme(struct task_struct *parent)
1925{
1926 int rc;
1927
Eric Paris200ac532009-02-12 15:01:04 -05001928 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001929 if (rc)
1930 return rc;
1931
1932 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001936 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 int error;
1939
David Howells3b11a1d2008-11-14 10:39:26 +11001940 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (error)
1942 return error;
1943
Eric Paris200ac532009-02-12 15:01:04 -05001944 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
David Howellsd84f4f92008-11-14 10:39:23 +11001947static int selinux_capset(struct cred *new, const struct cred *old,
1948 const kernel_cap_t *effective,
1949 const kernel_cap_t *inheritable,
1950 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 int error;
1953
Eric Paris200ac532009-02-12 15:01:04 -05001954 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001955 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (error)
1957 return error;
1958
David Howellsd84f4f92008-11-14 10:39:23 +11001959 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
James Morris5626d3e2009-01-30 10:05:06 +11001962/*
1963 * (This comment used to live with the selinux_task_setuid hook,
1964 * which was removed).
1965 *
1966 * Since setuid only affects the current process, and since the SELinux
1967 * controls are not based on the Linux identity attributes, SELinux does not
1968 * need to control this operation. However, SELinux does control the use of
1969 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1970 */
1971
Eric Paris6a9de492012-01-03 12:25:14 -05001972static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1973 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 int rc;
1976
Eric Paris6a9de492012-01-03 12:25:14 -05001977 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (rc)
1979 return rc;
1980
Eric Paris6a9de492012-01-03 12:25:14 -05001981 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1985{
David Howells88e67f32008-11-14 10:39:21 +11001986 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 int rc = 0;
1988
1989 if (!sb)
1990 return 0;
1991
1992 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001993 case Q_SYNC:
1994 case Q_QUOTAON:
1995 case Q_QUOTAOFF:
1996 case Q_SETINFO:
1997 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001998 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001999 break;
2000 case Q_GETFMT:
2001 case Q_GETINFO:
2002 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002003 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002004 break;
2005 default:
2006 rc = 0; /* let the kernel handle invalid cmds */
2007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 return rc;
2010}
2011
2012static int selinux_quota_on(struct dentry *dentry)
2013{
David Howells88e67f32008-11-14 10:39:21 +11002014 const struct cred *cred = current_cred();
2015
Eric Paris2875fa02011-04-28 16:04:24 -04002016 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Eric Paris12b30522010-11-15 18:36:29 -05002019static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int rc;
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002024 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2025 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002026 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2027 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002028 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2029 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2030 /* Set level of messages printed to console */
2031 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002032 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2033 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002034 case SYSLOG_ACTION_CLOSE: /* Close log */
2035 case SYSLOG_ACTION_OPEN: /* Open log */
2036 case SYSLOG_ACTION_READ: /* Read from log */
2037 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2038 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002039 default:
2040 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 return rc;
2044}
2045
2046/*
2047 * Check that a process has enough memory to allocate a new virtual
2048 * mapping. 0 means there is enough memory for the allocation to
2049 * succeed and -ENOMEM implies there is not.
2050 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * Do not audit the selinux permission check, as this is applied to all
2052 * processes that allocate mappings.
2053 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002054static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Eric Paris6a9de492012-01-03 12:25:14 -05002058 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002059 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (rc == 0)
2061 cap_sys_admin = 1;
2062
Alan Cox34b4e4a2007-08-22 14:01:28 -07002063 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066/* binprm security operations */
2067
David Howellsa6f76f22008-11-14 10:39:24 +11002068static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
David Howellsa6f76f22008-11-14 10:39:24 +11002070 const struct task_security_struct *old_tsec;
2071 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002073 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002074 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002075 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int rc;
2077
Eric Paris200ac532009-02-12 15:01:04 -05002078 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (rc)
2080 return rc;
2081
David Howellsa6f76f22008-11-14 10:39:24 +11002082 /* SELinux context only depends on initial program or script and not
2083 * the script interpreter */
2084 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return 0;
2086
David Howellsa6f76f22008-11-14 10:39:24 +11002087 old_tsec = current_security();
2088 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 isec = inode->i_security;
2090
2091 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002092 new_tsec->sid = old_tsec->sid;
2093 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Michael LeMay28eba5b2006-06-27 02:53:42 -07002095 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002096 new_tsec->create_sid = 0;
2097 new_tsec->keycreate_sid = 0;
2098 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
David Howellsa6f76f22008-11-14 10:39:24 +11002100 if (old_tsec->exec_sid) {
2101 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002103 new_tsec->exec_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 } else {
2105 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002106 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002107 SECCLASS_PROCESS, NULL,
2108 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (rc)
2110 return rc;
2111 }
2112
Eric Parisf48b7392011-04-25 12:54:27 -04002113 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002114 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002115 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Josef Sipek3d5ff522006-12-08 02:37:38 -08002117 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
David Howellsa6f76f22008-11-14 10:39:24 +11002118 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
David Howellsa6f76f22008-11-14 10:39:24 +11002120 if (new_tsec->sid == old_tsec->sid) {
2121 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2123 if (rc)
2124 return rc;
2125 } else {
2126 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002127 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2129 if (rc)
2130 return rc;
2131
David Howellsa6f76f22008-11-14 10:39:24 +11002132 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2134 if (rc)
2135 return rc;
2136
David Howellsa6f76f22008-11-14 10:39:24 +11002137 /* Check for shared state */
2138 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2139 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2140 SECCLASS_PROCESS, PROCESS__SHARE,
2141 NULL);
2142 if (rc)
2143 return -EPERM;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
David Howellsa6f76f22008-11-14 10:39:24 +11002146 /* Make sure that anyone attempting to ptrace over a task that
2147 * changes its SID has the appropriate permit */
2148 if (bprm->unsafe &
2149 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2150 struct task_struct *tracer;
2151 struct task_security_struct *sec;
2152 u32 ptsid = 0;
2153
2154 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002155 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002156 if (likely(tracer != NULL)) {
2157 sec = __task_cred(tracer)->security;
2158 ptsid = sec->sid;
2159 }
2160 rcu_read_unlock();
2161
2162 if (ptsid != 0) {
2163 rc = avc_has_perm(ptsid, new_tsec->sid,
2164 SECCLASS_PROCESS,
2165 PROCESS__PTRACE, NULL);
2166 if (rc)
2167 return -EPERM;
2168 }
2169 }
2170
2171 /* Clear any possibly unsafe personality bits on exec: */
2172 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return 0;
2176}
2177
Eric Paris828dfe12008-04-17 13:17:49 -04002178static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Paul Moore5fb49872010-04-22 14:46:19 -04002180 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002181 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int atsecure = 0;
2183
David Howells275bb412008-11-14 10:39:19 +11002184 sid = tsec->sid;
2185 osid = tsec->osid;
2186
2187 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /* Enable secure mode for SIDs transitions unless
2189 the noatsecure permission is granted between
2190 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002191 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002192 SECCLASS_PROCESS,
2193 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195
Eric Paris200ac532009-02-12 15:01:04 -05002196 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002200static inline void flush_unauthorized_files(const struct cred *cred,
2201 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Thomas Liu2bf49692009-07-14 12:14:09 -04002203 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002204 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002206 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002207 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002209 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002211 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002213 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002214 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002215 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002216 struct inode *inode;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* Revalidate access to controlling tty.
2219 Use inode_has_perm on the tty inode directly rather
2220 than using file_has_perm, as this particular open
2221 file may belong to another process and we are only
2222 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002223 file_priv = list_first_entry(&tty->tty_files,
2224 struct tty_file_private, list);
2225 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002226 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002227 if (inode_has_perm_noadp(cred, inode,
2228 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002229 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002232 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002233 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002235 /* Reset controlling tty. */
2236 if (drop_tty)
2237 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Revalidate access to inherited open files. */
2240
Eric Parisf48b7392011-04-25 12:54:27 -04002241 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002242 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 spin_lock(&files->file_lock);
2245 for (;;) {
2246 unsigned long set, i;
2247 int fd;
2248
2249 j++;
Josh Boyer27cd8f52012-07-25 10:40:34 -04002250 i = j * BITS_PER_LONG;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002251 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002252 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002254 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (!set)
2256 continue;
2257 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002258 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (set & 1) {
2260 file = fget(i);
2261 if (!file)
2262 continue;
David Howells88e67f32008-11-14 10:39:21 +11002263 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 file,
2265 file_to_av(file))) {
2266 sys_close(i);
2267 fd = get_unused_fd();
2268 if (fd != i) {
2269 if (fd >= 0)
2270 put_unused_fd(fd);
2271 fput(file);
2272 continue;
2273 }
2274 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002275 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 } else {
David Howells745ca242008-11-14 10:39:22 +11002277 devnull = dentry_open(
2278 dget(selinux_null),
2279 mntget(selinuxfs_mount),
2280 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002281 if (IS_ERR(devnull)) {
2282 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 put_unused_fd(fd);
2284 fput(file);
2285 continue;
2286 }
2287 }
2288 fd_install(fd, devnull);
2289 }
2290 fput(file);
2291 }
2292 }
2293 spin_lock(&files->file_lock);
2294
2295 }
2296 spin_unlock(&files->file_lock);
2297}
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299/*
David Howellsa6f76f22008-11-14 10:39:24 +11002300 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 */
David Howellsa6f76f22008-11-14 10:39:24 +11002302static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
David Howellsa6f76f22008-11-14 10:39:24 +11002304 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int rc, i;
2307
David Howellsa6f76f22008-11-14 10:39:24 +11002308 new_tsec = bprm->cred->security;
2309 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return;
2311
2312 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002313 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
David Howellsa6f76f22008-11-14 10:39:24 +11002315 /* Always clear parent death signal on SID transitions. */
2316 current->pdeath_signal = 0;
2317
2318 /* Check whether the new SID can inherit resource limits from the old
2319 * SID. If not, reset all soft limits to the lower of the current
2320 * task's hard limit and the init task's soft limit.
2321 *
2322 * Note that the setting of hard limits (even to lower them) can be
2323 * controlled by the setrlimit check. The inclusion of the init task's
2324 * soft limit into the computation is to avoid resetting soft limits
2325 * higher than the default soft limit for cases where the default is
2326 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2327 */
2328 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2329 PROCESS__RLIMITINH, NULL);
2330 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002331 /* protect against do_prlimit() */
2332 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002333 for (i = 0; i < RLIM_NLIMITS; i++) {
2334 rlim = current->signal->rlim + i;
2335 initrlim = init_task.signal->rlim + i;
2336 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2337 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002338 task_unlock(current);
2339 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002340 }
2341}
2342
2343/*
2344 * Clean up the process immediately after the installation of new credentials
2345 * due to exec
2346 */
2347static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2348{
2349 const struct task_security_struct *tsec = current_security();
2350 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002351 u32 osid, sid;
2352 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002353
David Howellsa6f76f22008-11-14 10:39:24 +11002354 osid = tsec->osid;
2355 sid = tsec->sid;
2356
2357 if (sid == osid)
2358 return;
2359
2360 /* Check whether the new SID can inherit signal state from the old SID.
2361 * If not, clear itimers to avoid subsequent signal generation and
2362 * flush and unblock signals.
2363 *
2364 * This must occur _after_ the task SID has been updated so that any
2365 * kill done after the flush will be checked against the new SID.
2366 */
2367 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (rc) {
2369 memset(&itimer, 0, sizeof itimer);
2370 for (i = 0; i < 3; i++)
2371 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002373 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2374 __flush_signals(current);
2375 flush_signal_handlers(current, 1);
2376 sigemptyset(&current->blocked);
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 spin_unlock_irq(&current->sighand->siglock);
2379 }
2380
David Howellsa6f76f22008-11-14 10:39:24 +11002381 /* Wake up the parent if it is waiting so that it can recheck
2382 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002383 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002384 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002385 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
2388/* superblock security operations */
2389
2390static int selinux_sb_alloc_security(struct super_block *sb)
2391{
2392 return superblock_alloc_security(sb);
2393}
2394
2395static void selinux_sb_free_security(struct super_block *sb)
2396{
2397 superblock_free_security(sb);
2398}
2399
2400static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2401{
2402 if (plen > olen)
2403 return 0;
2404
2405 return !memcmp(prefix, option, plen);
2406}
2407
2408static inline int selinux_option(char *option, int len)
2409{
Eric Paris832cbd92008-04-01 13:24:09 -04002410 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2411 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2412 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002413 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2414 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417static inline void take_option(char **to, char *from, int *first, int len)
2418{
2419 if (!*first) {
2420 **to = ',';
2421 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002422 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 *first = 0;
2424 memcpy(*to, from, len);
2425 *to += len;
2426}
2427
Eric Paris828dfe12008-04-17 13:17:49 -04002428static inline void take_selinux_option(char **to, char *from, int *first,
2429 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002430{
2431 int current_size = 0;
2432
2433 if (!*first) {
2434 **to = '|';
2435 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002436 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002437 *first = 0;
2438
2439 while (current_size < len) {
2440 if (*from != '"') {
2441 **to = *from;
2442 *to += 1;
2443 }
2444 from += 1;
2445 current_size += 1;
2446 }
2447}
2448
Eric Parise0007522008-03-05 10:31:54 -05002449static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 int fnosec, fsec, rc = 0;
2452 char *in_save, *in_curr, *in_end;
2453 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002454 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 in_curr = orig;
2457 sec_curr = copy;
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2460 if (!nosec) {
2461 rc = -ENOMEM;
2462 goto out;
2463 }
2464
2465 nosec_save = nosec;
2466 fnosec = fsec = 1;
2467 in_save = in_end = orig;
2468
2469 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002470 if (*in_end == '"')
2471 open_quote = !open_quote;
2472 if ((*in_end == ',' && open_quote == 0) ||
2473 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int len = in_end - in_curr;
2475
2476 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002477 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 else
2479 take_option(&nosec, in_curr, &fnosec, len);
2480
2481 in_curr = in_end + 1;
2482 }
2483 } while (*in_end++);
2484
Eric Paris6931dfc2005-06-30 02:58:51 -07002485 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002486 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487out:
2488 return rc;
2489}
2490
Eric Paris026eb162011-03-03 16:09:14 -05002491static int selinux_sb_remount(struct super_block *sb, void *data)
2492{
2493 int rc, i, *flags;
2494 struct security_mnt_opts opts;
2495 char *secdata, **mount_options;
2496 struct superblock_security_struct *sbsec = sb->s_security;
2497
2498 if (!(sbsec->flags & SE_SBINITIALIZED))
2499 return 0;
2500
2501 if (!data)
2502 return 0;
2503
2504 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2505 return 0;
2506
2507 security_init_mnt_opts(&opts);
2508 secdata = alloc_secdata();
2509 if (!secdata)
2510 return -ENOMEM;
2511 rc = selinux_sb_copy_data(data, secdata);
2512 if (rc)
2513 goto out_free_secdata;
2514
2515 rc = selinux_parse_opts_str(secdata, &opts);
2516 if (rc)
2517 goto out_free_secdata;
2518
2519 mount_options = opts.mnt_opts;
2520 flags = opts.mnt_opts_flags;
2521
2522 for (i = 0; i < opts.num_mnt_opts; i++) {
2523 u32 sid;
2524 size_t len;
2525
2526 if (flags[i] == SE_SBLABELSUPP)
2527 continue;
2528 len = strlen(mount_options[i]);
2529 rc = security_context_to_sid(mount_options[i], len, &sid);
2530 if (rc) {
2531 printk(KERN_WARNING "SELinux: security_context_to_sid"
2532 "(%s) failed for (dev %s, type %s) errno=%d\n",
2533 mount_options[i], sb->s_id, sb->s_type->name, rc);
2534 goto out_free_opts;
2535 }
2536 rc = -EINVAL;
2537 switch (flags[i]) {
2538 case FSCONTEXT_MNT:
2539 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2540 goto out_bad_option;
2541 break;
2542 case CONTEXT_MNT:
2543 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2544 goto out_bad_option;
2545 break;
2546 case ROOTCONTEXT_MNT: {
2547 struct inode_security_struct *root_isec;
2548 root_isec = sb->s_root->d_inode->i_security;
2549
2550 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2551 goto out_bad_option;
2552 break;
2553 }
2554 case DEFCONTEXT_MNT:
2555 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2556 goto out_bad_option;
2557 break;
2558 default:
2559 goto out_free_opts;
2560 }
2561 }
2562
2563 rc = 0;
2564out_free_opts:
2565 security_free_mnt_opts(&opts);
2566out_free_secdata:
2567 free_secdata(secdata);
2568 return rc;
2569out_bad_option:
2570 printk(KERN_WARNING "SELinux: unable to change security options "
2571 "during remount (dev %s, type=%s)\n", sb->s_id,
2572 sb->s_type->name);
2573 goto out_free_opts;
2574}
2575
James Morris12204e22008-12-19 10:44:42 +11002576static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
David Howells88e67f32008-11-14 10:39:21 +11002578 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002579 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002580 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 int rc;
2582
2583 rc = superblock_doinit(sb, data);
2584 if (rc)
2585 return rc;
2586
James Morris74192242008-12-19 11:41:10 +11002587 /* Allow all mounts performed by the kernel */
2588 if (flags & MS_KERNMOUNT)
2589 return 0;
2590
Eric Parisa2694342011-04-25 13:10:27 -04002591 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002592 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002593 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002594 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
David Howells726c3342006-06-23 02:02:58 -07002597static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
David Howells88e67f32008-11-14 10:39:21 +11002599 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002600 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002601 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Eric Parisa2694342011-04-25 13:10:27 -04002603 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002604 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002605 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002606 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Eric Paris828dfe12008-04-17 13:17:49 -04002609static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002610 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002611 char *type,
2612 unsigned long flags,
2613 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
David Howells88e67f32008-11-14 10:39:21 +11002615 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002618 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002619 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 else
Eric Paris2875fa02011-04-28 16:04:24 -04002621 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
2624static int selinux_umount(struct vfsmount *mnt, int flags)
2625{
David Howells88e67f32008-11-14 10:39:21 +11002626 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
David Howells88e67f32008-11-14 10:39:21 +11002628 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002629 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632/* inode security operations */
2633
2634static int selinux_inode_alloc_security(struct inode *inode)
2635{
2636 return inode_alloc_security(inode);
2637}
2638
2639static void selinux_inode_free_security(struct inode *inode)
2640{
2641 inode_free_security(inode);
2642}
2643
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002644static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002645 const struct qstr *qstr, char **name,
2646 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002647{
Paul Moore5fb49872010-04-22 14:46:19 -04002648 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002649 struct inode_security_struct *dsec;
2650 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002651 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002652 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002653 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002654
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002655 dsec = dir->i_security;
2656 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002657
David Howells275bb412008-11-14 10:39:19 +11002658 sid = tsec->sid;
2659 newsid = tsec->create_sid;
2660
Eric Paris415103f2010-12-02 16:13:40 -05002661 if ((sbsec->flags & SE_SBINITIALIZED) &&
2662 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2663 newsid = sbsec->mntpoint_sid;
2664 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002665 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002666 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002667 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002668 if (rc) {
2669 printk(KERN_WARNING "%s: "
2670 "security_transition_sid failed, rc=%d (dev=%s "
2671 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002672 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002673 -rc, inode->i_sb->s_id, inode->i_ino);
2674 return rc;
2675 }
2676 }
2677
Eric Paris296fddf2006-09-25 23:32:00 -07002678 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002679 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002680 struct inode_security_struct *isec = inode->i_security;
2681 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2682 isec->sid = newsid;
2683 isec->initialized = 1;
2684 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002685
David P. Quigleycd895962009-01-16 09:22:04 -05002686 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002687 return -EOPNOTSUPP;
2688
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002689 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002690 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002691 if (!namep)
2692 return -ENOMEM;
2693 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002694 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002695
2696 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002697 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002698 if (rc) {
2699 kfree(namep);
2700 return rc;
2701 }
2702 *value = context;
2703 *len = clen;
2704 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002705
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002706 return 0;
2707}
2708
Al Viro4acdaf22011-07-26 01:42:34 -04002709static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 return may_create(dir, dentry, SECCLASS_FILE);
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return may_link(dir, old_dentry, MAY_LINK);
2717}
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return may_link(dir, dentry, MAY_UNLINK);
2722}
2723
2724static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2725{
2726 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2727}
2728
Al Viro18bb1db2011-07-26 01:41:39 -04002729static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 return may_create(dir, dentry, SECCLASS_DIR);
2732}
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2735{
2736 return may_link(dir, dentry, MAY_RMDIR);
2737}
2738
Al Viro1a67aaf2011-07-26 01:52:52 -04002739static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2742}
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002745 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750static int selinux_inode_readlink(struct dentry *dentry)
2751{
David Howells88e67f32008-11-14 10:39:21 +11002752 const struct cred *cred = current_cred();
2753
Eric Paris2875fa02011-04-28 16:04:24 -04002754 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
2757static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2758{
David Howells88e67f32008-11-14 10:39:21 +11002759 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Eric Paris2875fa02011-04-28 16:04:24 -04002761 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Al Viroe74f71e2011-06-20 19:38:15 -04002764static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
David Howells88e67f32008-11-14 10:39:21 +11002766 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002767 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002768 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002769 u32 perms;
2770 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002771 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Eric Parisb782e0a2010-07-23 11:44:03 -04002773 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002774 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2775
Eric Parisb782e0a2010-07-23 11:44:03 -04002776 /* No permission to check. Existence test. */
2777 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Eric Parisf48b7392011-04-25 12:54:27 -04002780 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002781 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002782 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002783
2784 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002785 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002786
2787 perms = file_mask_to_av(inode->i_mode, mask);
2788
Eric Paris9ade0cf2011-04-25 16:26:29 -04002789 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
2792static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2793{
David Howells88e67f32008-11-14 10:39:21 +11002794 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002795 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002797 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2798 if (ia_valid & ATTR_FORCE) {
2799 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2800 ATTR_FORCE);
2801 if (!ia_valid)
2802 return 0;
2803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002805 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2806 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002807 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Eric Paris2875fa02011-04-28 16:04:24 -04002809 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
2812static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2813{
David Howells88e67f32008-11-14 10:39:21 +11002814 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002815 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002816
Eric Paris2875fa02011-04-28 16:04:24 -04002817 path.dentry = dentry;
2818 path.mnt = mnt;
2819
2820 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
David Howells8f0cfa52008-04-29 00:59:41 -07002823static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002824{
David Howells88e67f32008-11-14 10:39:21 +11002825 const struct cred *cred = current_cred();
2826
Serge E. Hallynb5376772007-10-16 23:31:36 -07002827 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2828 sizeof XATTR_SECURITY_PREFIX - 1)) {
2829 if (!strcmp(name, XATTR_NAME_CAPS)) {
2830 if (!capable(CAP_SETFCAP))
2831 return -EPERM;
2832 } else if (!capable(CAP_SYS_ADMIN)) {
2833 /* A different attribute in the security namespace.
2834 Restrict to administrator. */
2835 return -EPERM;
2836 }
2837 }
2838
2839 /* Not an attribute we recognize, so just check the
2840 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002841 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002842}
2843
David Howells8f0cfa52008-04-29 00:59:41 -07002844static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2845 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 struct inode *inode = dentry->d_inode;
2848 struct inode_security_struct *isec = inode->i_security;
2849 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002850 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002851 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002852 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 int rc = 0;
2854
Serge E. Hallynb5376772007-10-16 23:31:36 -07002855 if (strcmp(name, XATTR_NAME_SELINUX))
2856 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002859 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return -EOPNOTSUPP;
2861
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002862 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return -EPERM;
2864
Eric Parisa2694342011-04-25 13:10:27 -04002865 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002866 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002867 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
David Howells275bb412008-11-14 10:39:19 +11002869 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 FILE__RELABELFROM, &ad);
2871 if (rc)
2872 return rc;
2873
2874 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002875 if (rc == -EINVAL) {
2876 if (!capable(CAP_MAC_ADMIN))
2877 return rc;
2878 rc = security_context_to_sid_force(value, size, &newsid);
2879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (rc)
2881 return rc;
2882
David Howells275bb412008-11-14 10:39:19 +11002883 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 FILE__RELABELTO, &ad);
2885 if (rc)
2886 return rc;
2887
David Howells275bb412008-11-14 10:39:19 +11002888 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002889 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (rc)
2891 return rc;
2892
2893 return avc_has_perm(newsid,
2894 sbsec->sid,
2895 SECCLASS_FILESYSTEM,
2896 FILESYSTEM__ASSOCIATE,
2897 &ad);
2898}
2899
David Howells8f0cfa52008-04-29 00:59:41 -07002900static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002901 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002902 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct inode *inode = dentry->d_inode;
2905 struct inode_security_struct *isec = inode->i_security;
2906 u32 newsid;
2907 int rc;
2908
2909 if (strcmp(name, XATTR_NAME_SELINUX)) {
2910 /* Not an attribute we recognize, so nothing to do. */
2911 return;
2912 }
2913
Stephen Smalley12b29f32008-05-07 13:03:20 -04002914 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002916 printk(KERN_ERR "SELinux: unable to map context to SID"
2917 "for (%s, %lu), rc=%d\n",
2918 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return;
2920 }
2921
2922 isec->sid = newsid;
2923 return;
2924}
2925
David Howells8f0cfa52008-04-29 00:59:41 -07002926static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
David Howells88e67f32008-11-14 10:39:21 +11002928 const struct cred *cred = current_cred();
2929
Eric Paris2875fa02011-04-28 16:04:24 -04002930 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Eric Paris828dfe12008-04-17 13:17:49 -04002933static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
David Howells88e67f32008-11-14 10:39:21 +11002935 const struct cred *cred = current_cred();
2936
Eric Paris2875fa02011-04-28 16:04:24 -04002937 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
David Howells8f0cfa52008-04-29 00:59:41 -07002940static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002942 if (strcmp(name, XATTR_NAME_SELINUX))
2943 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* No one is allowed to remove a SELinux security label.
2946 You can change the label, but all data must be labeled. */
2947 return -EACCES;
2948}
2949
James Morrisd381d8a2005-10-30 14:59:22 -08002950/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002951 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002952 *
2953 * Permission check is handled by selinux_inode_getxattr hook.
2954 */
David P. Quigley42492592008-02-04 22:29:39 -08002955static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
David P. Quigley42492592008-02-04 22:29:39 -08002957 u32 size;
2958 int error;
2959 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002962 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2963 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002965 /*
2966 * If the caller has CAP_MAC_ADMIN, then get the raw context
2967 * value even if it is not defined by current policy; otherwise,
2968 * use the in-core value under current policy.
2969 * Use the non-auditing forms of the permission checks since
2970 * getxattr may be called by unprivileged processes commonly
2971 * and lack of permission just means that we fall back to the
2972 * in-core context value, not a denial.
2973 */
Eric Paris6a9de492012-01-03 12:25:14 -05002974 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002975 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002976 if (!error)
2977 error = security_sid_to_context_force(isec->sid, &context,
2978 &size);
2979 else
2980 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002981 if (error)
2982 return error;
2983 error = size;
2984 if (alloc) {
2985 *buffer = context;
2986 goto out_nofree;
2987 }
2988 kfree(context);
2989out_nofree:
2990 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04002994 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct inode_security_struct *isec = inode->i_security;
2997 u32 newsid;
2998 int rc;
2999
3000 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3001 return -EOPNOTSUPP;
3002
3003 if (!value || !size)
3004 return -EACCES;
3005
Eric Paris828dfe12008-04-17 13:17:49 -04003006 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (rc)
3008 return rc;
3009
3010 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003011 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return 0;
3013}
3014
3015static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3016{
3017 const int len = sizeof(XATTR_NAME_SELINUX);
3018 if (buffer && len <= buffer_size)
3019 memcpy(buffer, XATTR_NAME_SELINUX, len);
3020 return len;
3021}
3022
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003023static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3024{
3025 struct inode_security_struct *isec = inode->i_security;
3026 *secid = isec->sid;
3027}
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029/* file security operations */
3030
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003031static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
David Howells88e67f32008-11-14 10:39:21 +11003033 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003034 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3037 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3038 mask |= MAY_APPEND;
3039
Paul Moore389fb802009-03-27 17:10:34 -04003040 return file_has_perm(cred, file,
3041 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003044static int selinux_file_permission(struct file *file, int mask)
3045{
Stephen Smalley20dda182009-06-22 14:54:53 -04003046 struct inode *inode = file->f_path.dentry->d_inode;
3047 struct file_security_struct *fsec = file->f_security;
3048 struct inode_security_struct *isec = inode->i_security;
3049 u32 sid = current_sid();
3050
Paul Moore389fb802009-03-27 17:10:34 -04003051 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003052 /* No permission to check. Existence test. */
3053 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003054
Stephen Smalley20dda182009-06-22 14:54:53 -04003055 if (sid == fsec->sid && fsec->isid == isec->sid &&
3056 fsec->pseqno == avc_policy_seqno())
3057 /* No change since dentry_open check. */
3058 return 0;
3059
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003060 return selinux_revalidate_file_permission(file, mask);
3061}
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063static int selinux_file_alloc_security(struct file *file)
3064{
3065 return file_alloc_security(file);
3066}
3067
3068static void selinux_file_free_security(struct file *file)
3069{
3070 file_free_security(file);
3071}
3072
3073static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3074 unsigned long arg)
3075{
David Howells88e67f32008-11-14 10:39:21 +11003076 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003077 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Eric Paris0b24dcb2011-02-25 15:39:20 -05003079 switch (cmd) {
3080 case FIONREAD:
3081 /* fall through */
3082 case FIBMAP:
3083 /* fall through */
3084 case FIGETBSZ:
3085 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003086 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003087 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003088 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003089 error = file_has_perm(cred, file, FILE__GETATTR);
3090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Al Viro2f99c362012-03-23 16:04:05 -04003092 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003093 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003094 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003095 error = file_has_perm(cred, file, FILE__SETATTR);
3096 break;
3097
3098 /* sys_ioctl() checks */
3099 case FIONBIO:
3100 /* fall through */
3101 case FIOASYNC:
3102 error = file_has_perm(cred, file, 0);
3103 break;
3104
3105 case KDSKBENT:
3106 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003107 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3108 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003109 break;
3110
3111 /* default case assumes that the command will go
3112 * to the file's ioctl() function.
3113 */
3114 default:
3115 error = file_has_perm(cred, file, FILE__IOCTL);
3116 }
3117 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003120static int default_noexec;
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3123{
David Howells88e67f32008-11-14 10:39:21 +11003124 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003125 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003126
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003127 if (default_noexec &&
3128 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /*
3130 * We are making executable an anonymous mapping or a
3131 * private file mapping that will also be writable.
3132 * This has an additional check.
3133 */
David Howellsd84f4f92008-11-14 10:39:23 +11003134 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003136 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 if (file) {
3140 /* read access is always possible with a mapping */
3141 u32 av = FILE__READ;
3142
3143 /* write access only matters if the mapping is shared */
3144 if (shared && (prot & PROT_WRITE))
3145 av |= FILE__WRITE;
3146
3147 if (prot & PROT_EXEC)
3148 av |= FILE__EXECUTE;
3149
David Howells88e67f32008-11-14 10:39:21 +11003150 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 }
David Howellsd84f4f92008-11-14 10:39:23 +11003152
3153error:
3154 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
3157static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003158 unsigned long prot, unsigned long flags,
3159 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Eric Parised032182007-06-28 15:55:21 -04003161 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003162 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Eric Paris84336d1a2009-07-31 12:54:05 -04003164 /*
3165 * notice that we are intentionally putting the SELinux check before
3166 * the secondary cap_file_mmap check. This is such a likely attempt
3167 * at bad behaviour/exploit that we always want to get the AVC, even
3168 * if DAC would have also denied the operation.
3169 */
Eric Parisa2551df2009-07-31 12:54:11 -04003170 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003171 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3172 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003173 if (rc)
3174 return rc;
3175 }
3176
3177 /* do DAC check on address space usage */
3178 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003179 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 return rc;
3181
3182 if (selinux_checkreqprot)
3183 prot = reqprot;
3184
3185 return file_map_prot_check(file, prot,
3186 (flags & MAP_TYPE) == MAP_SHARED);
3187}
3188
3189static int selinux_file_mprotect(struct vm_area_struct *vma,
3190 unsigned long reqprot,
3191 unsigned long prot)
3192{
David Howells88e67f32008-11-14 10:39:21 +11003193 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 if (selinux_checkreqprot)
3196 prot = reqprot;
3197
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003198 if (default_noexec &&
3199 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003200 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003201 if (vma->vm_start >= vma->vm_mm->start_brk &&
3202 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003203 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003204 } else if (!vma->vm_file &&
3205 vma->vm_start <= vma->vm_mm->start_stack &&
3206 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003207 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003208 } else if (vma->vm_file && vma->anon_vma) {
3209 /*
3210 * We are making executable a file mapping that has
3211 * had some COW done. Since pages might have been
3212 * written, check ability to execute the possibly
3213 * modified content. This typically should only
3214 * occur for text relocations.
3215 */
David Howellsd84f4f92008-11-14 10:39:23 +11003216 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003217 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003218 if (rc)
3219 return rc;
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3223}
3224
3225static int selinux_file_lock(struct file *file, unsigned int cmd)
3226{
David Howells88e67f32008-11-14 10:39:21 +11003227 const struct cred *cred = current_cred();
3228
3229 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
3232static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3233 unsigned long arg)
3234{
David Howells88e67f32008-11-14 10:39:21 +11003235 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 int err = 0;
3237
3238 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003239 case F_SETFL:
3240 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3241 err = -EINVAL;
3242 break;
3243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Eric Paris828dfe12008-04-17 13:17:49 -04003245 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003246 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003248 }
3249 /* fall through */
3250 case F_SETOWN:
3251 case F_SETSIG:
3252 case F_GETFL:
3253 case F_GETOWN:
3254 case F_GETSIG:
3255 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003256 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003257 break;
3258 case F_GETLK:
3259 case F_SETLK:
3260 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003262 case F_GETLK64:
3263 case F_SETLK64:
3264 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003266 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3267 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003269 }
David Howells88e67f32008-11-14 10:39:21 +11003270 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273
3274 return err;
3275}
3276
3277static int selinux_file_set_fowner(struct file *file)
3278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct file_security_struct *fsec;
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003282 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 return 0;
3285}
3286
3287static int selinux_file_send_sigiotask(struct task_struct *tsk,
3288 struct fown_struct *fown, int signum)
3289{
Eric Paris828dfe12008-04-17 13:17:49 -04003290 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003291 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct file_security_struct *fsec;
3294
3295 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003296 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 fsec = file->f_security;
3299
3300 if (!signum)
3301 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3302 else
3303 perm = signal_to_av(signum);
3304
David Howells275bb412008-11-14 10:39:19 +11003305 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 SECCLASS_PROCESS, perm, NULL);
3307}
3308
3309static int selinux_file_receive(struct file *file)
3310{
David Howells88e67f32008-11-14 10:39:21 +11003311 const struct cred *cred = current_cred();
3312
3313 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
David Howells745ca242008-11-14 10:39:22 +11003316static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003317{
3318 struct file_security_struct *fsec;
3319 struct inode *inode;
3320 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003321
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003322 inode = file->f_path.dentry->d_inode;
3323 fsec = file->f_security;
3324 isec = inode->i_security;
3325 /*
3326 * Save inode label and policy sequence number
3327 * at open-time so that selinux_file_permission
3328 * can determine whether revalidation is necessary.
3329 * Task label is already saved in the file security
3330 * struct as its SID.
3331 */
3332 fsec->isid = isec->sid;
3333 fsec->pseqno = avc_policy_seqno();
3334 /*
3335 * Since the inode label or policy seqno may have changed
3336 * between the selinux_inode_permission check and the saving
3337 * of state above, recheck that access is still permitted.
3338 * Otherwise, access might never be revalidated against the
3339 * new inode label or new policy.
3340 * This check is not redundant - do not remove.
3341 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003342 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003343}
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345/* task security operations */
3346
3347static int selinux_task_create(unsigned long clone_flags)
3348{
David Howells3b11a1d2008-11-14 10:39:26 +11003349 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
David Howellsf1752ee2008-11-14 10:39:17 +11003352/*
David Howellsee18d642009-09-02 09:14:21 +01003353 * allocate the SELinux part of blank credentials
3354 */
3355static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3356{
3357 struct task_security_struct *tsec;
3358
3359 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3360 if (!tsec)
3361 return -ENOMEM;
3362
3363 cred->security = tsec;
3364 return 0;
3365}
3366
3367/*
David Howellsf1752ee2008-11-14 10:39:17 +11003368 * detach and free the LSM part of a set of credentials
3369 */
3370static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
David Howellsf1752ee2008-11-14 10:39:17 +11003372 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003373
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003374 /*
3375 * cred->security == NULL if security_cred_alloc_blank() or
3376 * security_prepare_creds() returned an error.
3377 */
3378 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003379 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003380 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
David Howellsd84f4f92008-11-14 10:39:23 +11003383/*
3384 * prepare a new set of credentials for modification
3385 */
3386static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3387 gfp_t gfp)
3388{
3389 const struct task_security_struct *old_tsec;
3390 struct task_security_struct *tsec;
3391
3392 old_tsec = old->security;
3393
3394 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3395 if (!tsec)
3396 return -ENOMEM;
3397
3398 new->security = tsec;
3399 return 0;
3400}
3401
3402/*
David Howellsee18d642009-09-02 09:14:21 +01003403 * transfer the SELinux data to a blank set of creds
3404 */
3405static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3406{
3407 const struct task_security_struct *old_tsec = old->security;
3408 struct task_security_struct *tsec = new->security;
3409
3410 *tsec = *old_tsec;
3411}
3412
3413/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003414 * set the security data for a kernel service
3415 * - all the creation contexts are set to unlabelled
3416 */
3417static int selinux_kernel_act_as(struct cred *new, u32 secid)
3418{
3419 struct task_security_struct *tsec = new->security;
3420 u32 sid = current_sid();
3421 int ret;
3422
3423 ret = avc_has_perm(sid, secid,
3424 SECCLASS_KERNEL_SERVICE,
3425 KERNEL_SERVICE__USE_AS_OVERRIDE,
3426 NULL);
3427 if (ret == 0) {
3428 tsec->sid = secid;
3429 tsec->create_sid = 0;
3430 tsec->keycreate_sid = 0;
3431 tsec->sockcreate_sid = 0;
3432 }
3433 return ret;
3434}
3435
3436/*
3437 * set the file creation context in a security record to the same as the
3438 * objective context of the specified inode
3439 */
3440static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3441{
3442 struct inode_security_struct *isec = inode->i_security;
3443 struct task_security_struct *tsec = new->security;
3444 u32 sid = current_sid();
3445 int ret;
3446
3447 ret = avc_has_perm(sid, isec->sid,
3448 SECCLASS_KERNEL_SERVICE,
3449 KERNEL_SERVICE__CREATE_FILES_AS,
3450 NULL);
3451
3452 if (ret == 0)
3453 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003454 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003455}
3456
Eric Parisdd8dbf22009-11-03 16:35:32 +11003457static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003458{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003459 u32 sid;
3460 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003461 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003462
3463 sid = task_sid(current);
3464
3465 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003466 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003467 ad.u.kmod_name = kmod_name;
3468
3469 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3470 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003471}
3472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3474{
David Howells3b11a1d2008-11-14 10:39:26 +11003475 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
3478static int selinux_task_getpgid(struct task_struct *p)
3479{
David Howells3b11a1d2008-11-14 10:39:26 +11003480 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481}
3482
3483static int selinux_task_getsid(struct task_struct *p)
3484{
David Howells3b11a1d2008-11-14 10:39:26 +11003485 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
David Quigleyf9008e42006-06-30 01:55:46 -07003488static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3489{
David Howells275bb412008-11-14 10:39:19 +11003490 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003491}
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static int selinux_task_setnice(struct task_struct *p, int nice)
3494{
3495 int rc;
3496
Eric Paris200ac532009-02-12 15:01:04 -05003497 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 if (rc)
3499 return rc;
3500
David Howells3b11a1d2008-11-14 10:39:26 +11003501 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
James Morris03e68062006-06-23 02:03:58 -07003504static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3505{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003506 int rc;
3507
Eric Paris200ac532009-02-12 15:01:04 -05003508 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003509 if (rc)
3510 return rc;
3511
David Howells3b11a1d2008-11-14 10:39:26 +11003512 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003513}
3514
David Quigleya1836a42006-06-30 01:55:49 -07003515static int selinux_task_getioprio(struct task_struct *p)
3516{
David Howells3b11a1d2008-11-14 10:39:26 +11003517 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003518}
3519
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003520static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3521 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003523 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 /* Control the ability to change the hard limit (whether
3526 lowering or raising it), so that the hard limit can
3527 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003528 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003530 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 return 0;
3533}
3534
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003535static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003537 int rc;
3538
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003539 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003540 if (rc)
3541 return rc;
3542
David Howells3b11a1d2008-11-14 10:39:26 +11003543 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
3546static int selinux_task_getscheduler(struct task_struct *p)
3547{
David Howells3b11a1d2008-11-14 10:39:26 +11003548 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
David Quigley35601542006-06-23 02:04:01 -07003551static int selinux_task_movememory(struct task_struct *p)
3552{
David Howells3b11a1d2008-11-14 10:39:26 +11003553 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003554}
3555
David Quigleyf9008e42006-06-30 01:55:46 -07003556static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3557 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
3559 u32 perm;
3560 int rc;
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 if (!sig)
3563 perm = PROCESS__SIGNULL; /* null signal; existence test */
3564 else
3565 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003566 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003567 rc = avc_has_perm(secid, task_sid(p),
3568 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003569 else
David Howells3b11a1d2008-11-14 10:39:26 +11003570 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003571 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574static int selinux_task_wait(struct task_struct *p)
3575{
Eric Paris8a535142007-10-22 16:10:31 -04003576 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579static void selinux_task_to_inode(struct task_struct *p,
3580 struct inode *inode)
3581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003583 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
David Howells275bb412008-11-14 10:39:19 +11003585 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003590static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003591 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 int offset, ihlen, ret = -EINVAL;
3594 struct iphdr _iph, *ih;
3595
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003596 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3598 if (ih == NULL)
3599 goto out;
3600
3601 ihlen = ih->ihl * 4;
3602 if (ihlen < sizeof(_iph))
3603 goto out;
3604
Eric Paris48c62af2012-04-02 13:15:44 -04003605 ad->u.net->v4info.saddr = ih->saddr;
3606 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 ret = 0;
3608
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003609 if (proto)
3610 *proto = ih->protocol;
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003613 case IPPROTO_TCP: {
3614 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Eric Paris828dfe12008-04-17 13:17:49 -04003616 if (ntohs(ih->frag_off) & IP_OFFSET)
3617 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 offset += ihlen;
3620 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3621 if (th == NULL)
3622 break;
3623
Eric Paris48c62af2012-04-02 13:15:44 -04003624 ad->u.net->sport = th->source;
3625 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Eric Paris828dfe12008-04-17 13:17:49 -04003629 case IPPROTO_UDP: {
3630 struct udphdr _udph, *uh;
3631
3632 if (ntohs(ih->frag_off) & IP_OFFSET)
3633 break;
3634
3635 offset += ihlen;
3636 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3637 if (uh == NULL)
3638 break;
3639
Eric Paris48c62af2012-04-02 13:15:44 -04003640 ad->u.net->sport = uh->source;
3641 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003642 break;
3643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
James Morris2ee92d42006-11-13 16:09:01 -08003645 case IPPROTO_DCCP: {
3646 struct dccp_hdr _dccph, *dh;
3647
3648 if (ntohs(ih->frag_off) & IP_OFFSET)
3649 break;
3650
3651 offset += ihlen;
3652 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3653 if (dh == NULL)
3654 break;
3655
Eric Paris48c62af2012-04-02 13:15:44 -04003656 ad->u.net->sport = dh->dccph_sport;
3657 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003658 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003659 }
James Morris2ee92d42006-11-13 16:09:01 -08003660
Eric Paris828dfe12008-04-17 13:17:49 -04003661 default:
3662 break;
3663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664out:
3665 return ret;
3666}
3667
3668#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3669
3670/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003671static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003672 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674 u8 nexthdr;
3675 int ret = -EINVAL, offset;
3676 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003677 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003679 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3681 if (ip6 == NULL)
3682 goto out;
3683
Eric Paris48c62af2012-04-02 13:15:44 -04003684 ad->u.net->v6info.saddr = ip6->saddr;
3685 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 ret = 0;
3687
3688 nexthdr = ip6->nexthdr;
3689 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003690 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if (offset < 0)
3692 goto out;
3693
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003694 if (proto)
3695 *proto = nexthdr;
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 switch (nexthdr) {
3698 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003699 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3702 if (th == NULL)
3703 break;
3704
Eric Paris48c62af2012-04-02 13:15:44 -04003705 ad->u.net->sport = th->source;
3706 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 break;
3708 }
3709
3710 case IPPROTO_UDP: {
3711 struct udphdr _udph, *uh;
3712
3713 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3714 if (uh == NULL)
3715 break;
3716
Eric Paris48c62af2012-04-02 13:15:44 -04003717 ad->u.net->sport = uh->source;
3718 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 break;
3720 }
3721
James Morris2ee92d42006-11-13 16:09:01 -08003722 case IPPROTO_DCCP: {
3723 struct dccp_hdr _dccph, *dh;
3724
3725 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3726 if (dh == NULL)
3727 break;
3728
Eric Paris48c62af2012-04-02 13:15:44 -04003729 ad->u.net->sport = dh->dccph_sport;
3730 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003731 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003732 }
James Morris2ee92d42006-11-13 16:09:01 -08003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 /* includes fragments */
3735 default:
3736 break;
3737 }
3738out:
3739 return ret;
3740}
3741
3742#endif /* IPV6 */
3743
Thomas Liu2bf49692009-07-14 12:14:09 -04003744static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003745 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
David Howellscf9481e2008-07-27 21:31:07 +10003747 char *addrp;
3748 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Eric Paris48c62af2012-04-02 13:15:44 -04003750 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003752 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003753 if (ret)
3754 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003755 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3756 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003757 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
3759#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3760 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003761 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003762 if (ret)
3763 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003764 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3765 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003766 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767#endif /* IPV6 */
3768 default:
David Howellscf9481e2008-07-27 21:31:07 +10003769 addrp = NULL;
3770 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
3772
David Howellscf9481e2008-07-27 21:31:07 +10003773parse_error:
3774 printk(KERN_WARNING
3775 "SELinux: failure in selinux_parse_skb(),"
3776 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003778
3779okay:
3780 if (_addrp)
3781 *_addrp = addrp;
3782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784
Paul Moore4f6a9932007-03-01 14:35:22 -05003785/**
Paul Moore220deb92008-01-29 08:38:23 -05003786 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003787 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003788 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003789 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003790 *
3791 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003792 * Check the various different forms of network peer labeling and determine
3793 * the peer label/SID for the packet; most of the magic actually occurs in
3794 * the security server function security_net_peersid_cmp(). The function
3795 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3796 * or -EACCES if @sid is invalid due to inconsistencies with the different
3797 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003798 *
3799 */
Paul Moore220deb92008-01-29 08:38:23 -05003800static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003801{
Paul Moore71f1cb02008-01-29 08:51:16 -05003802 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003803 u32 xfrm_sid;
3804 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003805 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003806
Paul Moore73ec9552013-12-10 14:57:54 -05003807 selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003808 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003809
Paul Moore71f1cb02008-01-29 08:51:16 -05003810 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3811 if (unlikely(err)) {
3812 printk(KERN_WARNING
3813 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3814 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003815 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003816 }
Paul Moore220deb92008-01-29 08:38:23 -05003817
3818 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003819}
3820
Paul Moore2ea04e52013-12-04 16:10:51 -05003821/**
3822 * selinux_conn_sid - Determine the child socket label for a connection
3823 * @sk_sid: the parent socket's SID
3824 * @skb_sid: the packet's SID
3825 * @conn_sid: the resulting connection SID
3826 *
3827 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3828 * combined with the MLS information from @skb_sid in order to create
3829 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3830 * of @sk_sid. Returns zero on success, negative values on failure.
3831 *
3832 */
3833static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3834{
3835 int err = 0;
3836
3837 if (skb_sid != SECSID_NULL)
3838 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3839 else
3840 *conn_sid = sk_sid;
3841
3842 return err;
3843}
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003846
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003847static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3848 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003849{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003850 if (tsec->sockcreate_sid > SECSID_NULL) {
3851 *socksid = tsec->sockcreate_sid;
3852 return 0;
3853 }
3854
3855 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3856 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003857}
3858
Paul Moore253bfae2010-04-22 14:46:19 -04003859static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
Paul Moore253bfae2010-04-22 14:46:19 -04003861 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003862 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003863 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003864 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003865 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Paul Moore253bfae2010-04-22 14:46:19 -04003867 if (sksec->sid == SECINITSID_KERNEL)
3868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Thomas Liu2bf49692009-07-14 12:14:09 -04003870 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003871 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003872 ad.u.net = &net;
3873 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Paul Moore253bfae2010-04-22 14:46:19 -04003875 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876}
3877
3878static int selinux_socket_create(int family, int type,
3879 int protocol, int kern)
3880{
Paul Moore5fb49872010-04-22 14:46:19 -04003881 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003882 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003883 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003884 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
David Howells275bb412008-11-14 10:39:19 +11003889 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003890 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3891 if (rc)
3892 return rc;
3893
Paul Moored4f2d972010-04-22 14:46:18 -04003894 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
3896
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003897static int selinux_socket_post_create(struct socket *sock, int family,
3898 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
Paul Moore5fb49872010-04-22 14:46:19 -04003900 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003901 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003902 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003903 int err = 0;
3904
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003905 isec->sclass = socket_type_to_security_class(family, type, protocol);
3906
David Howells275bb412008-11-14 10:39:19 +11003907 if (kern)
3908 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003909 else {
3910 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3911 if (err)
3912 return err;
3913 }
David Howells275bb412008-11-14 10:39:19 +11003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 isec->initialized = 1;
3916
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003917 if (sock->sk) {
3918 sksec = sock->sk->sk_security;
3919 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003920 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003921 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003922 }
3923
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
3927/* Range of port numbers used to automatically bind.
3928 Need to determine whether we should perform a name_bind
3929 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3932{
Paul Moore253bfae2010-04-22 14:46:19 -04003933 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 u16 family;
3935 int err;
3936
Paul Moore253bfae2010-04-22 14:46:19 -04003937 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (err)
3939 goto out;
3940
3941 /*
3942 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003943 * Multiple address binding for SCTP is not supported yet: we just
3944 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 */
Paul Moore253bfae2010-04-22 14:46:19 -04003946 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 if (family == PF_INET || family == PF_INET6) {
3948 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003949 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003950 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003951 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003952 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 struct sockaddr_in *addr4 = NULL;
3954 struct sockaddr_in6 *addr6 = NULL;
3955 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003956 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (family == PF_INET) {
3959 addr4 = (struct sockaddr_in *)address;
3960 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 addrp = (char *)&addr4->sin_addr.s_addr;
3962 } else {
3963 addr6 = (struct sockaddr_in6 *)address;
3964 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 addrp = (char *)&addr6->sin6_addr.s6_addr;
3966 }
3967
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003968 if (snum) {
3969 int low, high;
3970
3971 inet_get_local_port_range(&low, &high);
3972
3973 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003974 err = sel_netport_sid(sk->sk_protocol,
3975 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003976 if (err)
3977 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04003978 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003979 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003980 ad.u.net = &net;
3981 ad.u.net->sport = htons(snum);
3982 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04003983 err = avc_has_perm(sksec->sid, sid,
3984 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003985 SOCKET__NAME_BIND, &ad);
3986 if (err)
3987 goto out;
3988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 }
Eric Paris828dfe12008-04-17 13:17:49 -04003990
Paul Moore253bfae2010-04-22 14:46:19 -04003991 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04003992 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 node_perm = TCP_SOCKET__NODE_BIND;
3994 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003995
James Morris13402582005-09-30 14:24:34 -04003996 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 node_perm = UDP_SOCKET__NODE_BIND;
3998 break;
James Morris2ee92d42006-11-13 16:09:01 -08003999
4000 case SECCLASS_DCCP_SOCKET:
4001 node_perm = DCCP_SOCKET__NODE_BIND;
4002 break;
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 default:
4005 node_perm = RAWIP_SOCKET__NODE_BIND;
4006 break;
4007 }
Eric Paris828dfe12008-04-17 13:17:49 -04004008
Paul Moore224dfbd2008-01-29 08:38:13 -05004009 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (err)
4011 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004012
Thomas Liu2bf49692009-07-14 12:14:09 -04004013 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004014 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004015 ad.u.net = &net;
4016 ad.u.net->sport = htons(snum);
4017 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
4019 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004020 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 else
Eric Paris48c62af2012-04-02 13:15:44 -04004022 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Paul Moore253bfae2010-04-22 14:46:19 -04004024 err = avc_has_perm(sksec->sid, sid,
4025 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 if (err)
4027 goto out;
4028 }
4029out:
4030 return err;
4031}
4032
4033static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4034{
Paul Moore014ab192008-10-10 10:16:33 -04004035 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004036 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 int err;
4038
Paul Moore253bfae2010-04-22 14:46:19 -04004039 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 if (err)
4041 return err;
4042
4043 /*
James Morris2ee92d42006-11-13 16:09:01 -08004044 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 */
Paul Moore253bfae2010-04-22 14:46:19 -04004046 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4047 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004048 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004049 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004050 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 struct sockaddr_in *addr4 = NULL;
4052 struct sockaddr_in6 *addr6 = NULL;
4053 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004054 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 if (sk->sk_family == PF_INET) {
4057 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004058 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return -EINVAL;
4060 snum = ntohs(addr4->sin_port);
4061 } else {
4062 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004063 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return -EINVAL;
4065 snum = ntohs(addr6->sin6_port);
4066 }
4067
Paul Moore3e112172008-04-10 10:48:14 -04004068 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 if (err)
4070 goto out;
4071
Paul Moore253bfae2010-04-22 14:46:19 -04004072 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004073 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4074
Thomas Liu2bf49692009-07-14 12:14:09 -04004075 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004076 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004077 ad.u.net = &net;
4078 ad.u.net->dport = htons(snum);
4079 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004080 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (err)
4082 goto out;
4083 }
4084
Paul Moore014ab192008-10-10 10:16:33 -04004085 err = selinux_netlbl_socket_connect(sk, address);
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087out:
4088 return err;
4089}
4090
4091static int selinux_socket_listen(struct socket *sock, int backlog)
4092{
Paul Moore253bfae2010-04-22 14:46:19 -04004093 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094}
4095
4096static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4097{
4098 int err;
4099 struct inode_security_struct *isec;
4100 struct inode_security_struct *newisec;
4101
Paul Moore253bfae2010-04-22 14:46:19 -04004102 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 if (err)
4104 return err;
4105
4106 newisec = SOCK_INODE(newsock)->i_security;
4107
4108 isec = SOCK_INODE(sock)->i_security;
4109 newisec->sclass = isec->sclass;
4110 newisec->sid = isec->sid;
4111 newisec->initialized = 1;
4112
4113 return 0;
4114}
4115
4116static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004117 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Paul Moore253bfae2010-04-22 14:46:19 -04004119 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
4122static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4123 int size, int flags)
4124{
Paul Moore253bfae2010-04-22 14:46:19 -04004125 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126}
4127
4128static int selinux_socket_getsockname(struct socket *sock)
4129{
Paul Moore253bfae2010-04-22 14:46:19 -04004130 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
4133static int selinux_socket_getpeername(struct socket *sock)
4134{
Paul Moore253bfae2010-04-22 14:46:19 -04004135 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Eric Paris828dfe12008-04-17 13:17:49 -04004138static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Paul Mooref8687af2006-10-30 15:22:15 -08004140 int err;
4141
Paul Moore253bfae2010-04-22 14:46:19 -04004142 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004143 if (err)
4144 return err;
4145
4146 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
4149static int selinux_socket_getsockopt(struct socket *sock, int level,
4150 int optname)
4151{
Paul Moore253bfae2010-04-22 14:46:19 -04004152 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
4155static int selinux_socket_shutdown(struct socket *sock, int how)
4156{
Paul Moore253bfae2010-04-22 14:46:19 -04004157 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
David S. Miller3610cda2011-01-05 15:38:53 -08004160static int selinux_socket_unix_stream_connect(struct sock *sock,
4161 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct sock *newsk)
4163{
David S. Miller3610cda2011-01-05 15:38:53 -08004164 struct sk_security_struct *sksec_sock = sock->sk_security;
4165 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004166 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004167 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004168 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004169 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 int err;
4171
Thomas Liu2bf49692009-07-14 12:14:09 -04004172 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004173 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004174 ad.u.net = &net;
4175 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Paul Moore4d1e2452010-04-22 14:46:18 -04004177 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4178 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4180 if (err)
4181 return err;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004184 sksec_new->peer_sid = sksec_sock->sid;
4185 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4186 &sksec_new->sid);
4187 if (err)
4188 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004189
Paul Moore4d1e2452010-04-22 14:46:18 -04004190 /* connecting socket */
4191 sksec_sock->peer_sid = sksec_new->sid;
4192
4193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
4196static int selinux_socket_unix_may_send(struct socket *sock,
4197 struct socket *other)
4198{
Paul Moore253bfae2010-04-22 14:46:19 -04004199 struct sk_security_struct *ssec = sock->sk->sk_security;
4200 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004201 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004202 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004203 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Thomas Liu2bf49692009-07-14 12:14:09 -04004205 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004206 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004207 ad.u.net = &net;
4208 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Paul Moore253bfae2010-04-22 14:46:19 -04004210 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4211 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Paul Mooreeffad8d2008-01-29 08:49:27 -05004214static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4215 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004216 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004217{
4218 int err;
4219 u32 if_sid;
4220 u32 node_sid;
4221
4222 err = sel_netif_sid(ifindex, &if_sid);
4223 if (err)
4224 return err;
4225 err = avc_has_perm(peer_sid, if_sid,
4226 SECCLASS_NETIF, NETIF__INGRESS, ad);
4227 if (err)
4228 return err;
4229
4230 err = sel_netnode_sid(addrp, family, &node_sid);
4231 if (err)
4232 return err;
4233 return avc_has_perm(peer_sid, node_sid,
4234 SECCLASS_NODE, NODE__RECVFROM, ad);
4235}
4236
Paul Moore220deb92008-01-29 08:38:23 -05004237static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004238 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004239{
Paul Moore277d3422008-12-31 12:54:11 -05004240 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004241 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004242 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004243 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004244 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004245 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004246 char *addrp;
4247
Thomas Liu2bf49692009-07-14 12:14:09 -04004248 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004249 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004250 ad.u.net = &net;
4251 ad.u.net->netif = skb->skb_iif;
4252 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004253 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4254 if (err)
4255 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004256
Paul Moore58bfbb52009-03-27 17:10:41 -04004257 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004258 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004259 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004260 if (err)
4261 return err;
4262 }
Paul Moore220deb92008-01-29 08:38:23 -05004263
Steffen Klassertb9679a72011-02-23 12:55:21 +01004264 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4265 if (err)
4266 return err;
4267 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004268
James Morris4e5ab4c2006-06-09 00:33:33 -07004269 return err;
4270}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004271
James Morris4e5ab4c2006-06-09 00:33:33 -07004272static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4273{
Paul Moore220deb92008-01-29 08:38:23 -05004274 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004275 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004276 u16 family = sk->sk_family;
4277 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004278 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004279 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004280 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004281 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004282 u8 secmark_active;
4283 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004284
James Morris4e5ab4c2006-06-09 00:33:33 -07004285 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004286 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004287
4288 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004289 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004290 family = PF_INET;
4291
Paul Moored8395c82008-10-10 10:16:30 -04004292 /* If any sort of compatibility mode is enabled then handoff processing
4293 * to the selinux_sock_rcv_skb_compat() function to deal with the
4294 * special handling. We do this in an attempt to keep this function
4295 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004296 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004297 return selinux_sock_rcv_skb_compat(sk, skb, family);
4298
4299 secmark_active = selinux_secmark_enabled();
4300 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4301 if (!secmark_active && !peerlbl_active)
4302 return 0;
4303
Thomas Liu2bf49692009-07-14 12:14:09 -04004304 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004305 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004306 ad.u.net = &net;
4307 ad.u.net->netif = skb->skb_iif;
4308 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004309 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004310 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004311 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004312
Paul Moored8395c82008-10-10 10:16:30 -04004313 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004314 u32 peer_sid;
4315
4316 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4317 if (err)
4318 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004319 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004320 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004321 if (err) {
4322 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004323 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004324 }
Paul Moored621d352008-01-29 08:43:36 -05004325 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4326 PEER__RECV, &ad);
Chad Hanson351381d2013-12-23 17:45:01 -05004327 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004328 selinux_netlbl_err(skb, err, 0);
Chad Hanson351381d2013-12-23 17:45:01 -05004329 return err;
4330 }
Paul Moored621d352008-01-29 08:43:36 -05004331 }
4332
Paul Moored8395c82008-10-10 10:16:30 -04004333 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004334 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4335 PACKET__RECV, &ad);
4336 if (err)
4337 return err;
4338 }
4339
Paul Moored621d352008-01-29 08:43:36 -05004340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004343static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4344 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 int err = 0;
4347 char *scontext;
4348 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004349 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004350 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Paul Moore253bfae2010-04-22 14:46:19 -04004352 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4353 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004354 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004355 if (peer_sid == SECSID_NULL)
4356 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004358 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 if (scontext_len > len) {
4363 err = -ERANGE;
4364 goto out_len;
4365 }
4366
4367 if (copy_to_user(optval, scontext, scontext_len))
4368 err = -EFAULT;
4369
4370out_len:
4371 if (put_user(scontext_len, optlen))
4372 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return err;
4375}
4376
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004377static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004378{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004379 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004380 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004381
Paul Mooreaa862902008-10-10 10:16:29 -04004382 if (skb && skb->protocol == htons(ETH_P_IP))
4383 family = PF_INET;
4384 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4385 family = PF_INET6;
4386 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004387 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004388 else
4389 goto out;
4390
4391 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004392 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004393 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004394 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004395
Paul Moore75e22912008-01-29 08:38:04 -05004396out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004397 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004398 if (peer_secid == SECSID_NULL)
4399 return -EINVAL;
4400 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004401}
4402
Al Viro7d877f32005-10-21 03:20:43 -04004403static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404{
Paul Moore84914b72010-04-22 14:46:18 -04004405 struct sk_security_struct *sksec;
4406
4407 sksec = kzalloc(sizeof(*sksec), priority);
4408 if (!sksec)
4409 return -ENOMEM;
4410
4411 sksec->peer_sid = SECINITSID_UNLABELED;
4412 sksec->sid = SECINITSID_UNLABELED;
4413 selinux_netlbl_sk_security_reset(sksec);
4414 sk->sk_security = sksec;
4415
4416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
4419static void selinux_sk_free_security(struct sock *sk)
4420{
Paul Moore84914b72010-04-22 14:46:18 -04004421 struct sk_security_struct *sksec = sk->sk_security;
4422
4423 sk->sk_security = NULL;
4424 selinux_netlbl_sk_security_free(sksec);
4425 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004428static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4429{
Eric Parisdd3e7832010-04-07 15:08:46 -04004430 struct sk_security_struct *sksec = sk->sk_security;
4431 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004432
Eric Parisdd3e7832010-04-07 15:08:46 -04004433 newsksec->sid = sksec->sid;
4434 newsksec->peer_sid = sksec->peer_sid;
4435 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004436
Eric Parisdd3e7832010-04-07 15:08:46 -04004437 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004438}
4439
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004440static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004441{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004442 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004443 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004444 else {
4445 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004446
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004447 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004448 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004449}
4450
Eric Paris828dfe12008-04-17 13:17:49 -04004451static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004452{
4453 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4454 struct sk_security_struct *sksec = sk->sk_security;
4455
David Woodhouse2148ccc2006-09-29 15:50:25 -07004456 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4457 sk->sk_family == PF_UNIX)
4458 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004459 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004460}
4461
Adrian Bunk9a673e52006-08-15 00:03:53 -07004462static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4463 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004464{
4465 struct sk_security_struct *sksec = sk->sk_security;
4466 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004467 u16 family = sk->sk_family;
Paul Moore2ea04e52013-12-04 16:10:51 -05004468 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004469 u32 peersid;
4470
Paul Mooreaa862902008-10-10 10:16:29 -04004471 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4472 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4473 family = PF_INET;
4474
4475 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004476 if (err)
4477 return err;
Paul Moore2ea04e52013-12-04 16:10:51 -05004478 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4479 if (err)
4480 return err;
4481 req->secid = connsid;
4482 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004483
Paul Moore389fb802009-03-27 17:10:34 -04004484 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004485}
4486
Adrian Bunk9a673e52006-08-15 00:03:53 -07004487static void selinux_inet_csk_clone(struct sock *newsk,
4488 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004489{
4490 struct sk_security_struct *newsksec = newsk->sk_security;
4491
4492 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004493 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004494 /* NOTE: Ideally, we should also get the isec->sid for the
4495 new socket in sync, but we don't have the isec available yet.
4496 So we will wait until sock_graft to do it, by which
4497 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004498
Paul Moore9f2ad662006-11-17 17:38:53 -05004499 /* We don't need to take any sort of lock here as we are the only
4500 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004501 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004502}
4503
Paul Moore014ab192008-10-10 10:16:33 -04004504static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004505{
Paul Mooreaa862902008-10-10 10:16:29 -04004506 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004507 struct sk_security_struct *sksec = sk->sk_security;
4508
Paul Mooreaa862902008-10-10 10:16:29 -04004509 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4510 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4511 family = PF_INET;
4512
4513 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004514}
4515
Eric Paris2606fd12010-10-13 16:24:41 -04004516static int selinux_secmark_relabel_packet(u32 sid)
4517{
4518 const struct task_security_struct *__tsec;
4519 u32 tsid;
4520
4521 __tsec = current_security();
4522 tsid = __tsec->sid;
4523
4524 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4525}
4526
4527static void selinux_secmark_refcount_inc(void)
4528{
4529 atomic_inc(&selinux_secmark_refcount);
4530}
4531
4532static void selinux_secmark_refcount_dec(void)
4533{
4534 atomic_dec(&selinux_secmark_refcount);
4535}
4536
Adrian Bunk9a673e52006-08-15 00:03:53 -07004537static void selinux_req_classify_flow(const struct request_sock *req,
4538 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004539{
David S. Miller1d28f422011-03-12 00:29:39 -05004540 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004541}
4542
Paul Mooreed6d76e2009-08-28 18:12:49 -04004543static int selinux_tun_dev_create(void)
4544{
4545 u32 sid = current_sid();
4546
4547 /* we aren't taking into account the "sockcreate" SID since the socket
4548 * that is being created here is not a socket in the traditional sense,
4549 * instead it is a private sock, accessible only to the kernel, and
4550 * representing a wide range of network traffic spanning multiple
4551 * connections unlike traditional sockets - check the TUN driver to
4552 * get a better understanding of why this socket is special */
4553
4554 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4555 NULL);
4556}
4557
4558static void selinux_tun_dev_post_create(struct sock *sk)
4559{
4560 struct sk_security_struct *sksec = sk->sk_security;
4561
4562 /* we don't currently perform any NetLabel based labeling here and it
4563 * isn't clear that we would want to do so anyway; while we could apply
4564 * labeling without the support of the TUN user the resulting labeled
4565 * traffic from the other end of the connection would almost certainly
4566 * cause confusion to the TUN user that had no idea network labeling
4567 * protocols were being used */
4568
4569 /* see the comments in selinux_tun_dev_create() about why we don't use
4570 * the sockcreate SID here */
4571
4572 sksec->sid = current_sid();
4573 sksec->sclass = SECCLASS_TUN_SOCKET;
4574}
4575
4576static int selinux_tun_dev_attach(struct sock *sk)
4577{
4578 struct sk_security_struct *sksec = sk->sk_security;
4579 u32 sid = current_sid();
4580 int err;
4581
4582 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4583 TUN_SOCKET__RELABELFROM, NULL);
4584 if (err)
4585 return err;
4586 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4587 TUN_SOCKET__RELABELTO, NULL);
4588 if (err)
4589 return err;
4590
4591 sksec->sid = sid;
4592
4593 return 0;
4594}
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4597{
4598 int err = 0;
4599 u32 perm;
4600 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004601 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 if (skb->len < NLMSG_SPACE(0)) {
4604 err = -EINVAL;
4605 goto out;
4606 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004607 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004608
Paul Moore253bfae2010-04-22 14:46:19 -04004609 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if (err) {
4611 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004612 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 "SELinux: unrecognized netlink message"
4614 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004615 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004616 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 err = 0;
4618 }
4619
4620 /* Ignore */
4621 if (err == -ENOENT)
4622 err = 0;
4623 goto out;
4624 }
4625
Paul Moore253bfae2010-04-22 14:46:19 -04004626 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627out:
4628 return err;
4629}
4630
4631#ifdef CONFIG_NETFILTER
4632
Paul Mooreeffad8d2008-01-29 08:49:27 -05004633static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4634 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Paul Mooredfaebe92008-10-10 10:16:31 -04004636 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004637 char *addrp;
4638 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004639 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004640 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004641 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004642 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004643 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004644 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004645
Paul Mooreeffad8d2008-01-29 08:49:27 -05004646 if (!selinux_policycap_netpeer)
4647 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004648
Paul Mooreeffad8d2008-01-29 08:49:27 -05004649 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004650 netlbl_active = netlbl_enabled();
4651 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004652 if (!secmark_active && !peerlbl_active)
4653 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004654
Paul Moored8395c82008-10-10 10:16:30 -04004655 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4656 return NF_DROP;
4657
Thomas Liu2bf49692009-07-14 12:14:09 -04004658 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004659 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004660 ad.u.net = &net;
4661 ad.u.net->netif = ifindex;
4662 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004663 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4664 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Paul Mooredfaebe92008-10-10 10:16:31 -04004666 if (peerlbl_active) {
4667 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4668 peer_sid, &ad);
4669 if (err) {
4670 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004671 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004672 }
4673 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004674
4675 if (secmark_active)
4676 if (avc_has_perm(peer_sid, skb->secmark,
4677 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4678 return NF_DROP;
4679
Paul Moore948bf852008-10-10 10:16:32 -04004680 if (netlbl_active)
4681 /* we do this in the FORWARD path and not the POST_ROUTING
4682 * path because we want to make sure we apply the necessary
4683 * labeling before IPsec is applied so we can leverage AH
4684 * protection */
4685 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4686 return NF_DROP;
4687
Paul Mooreeffad8d2008-01-29 08:49:27 -05004688 return NF_ACCEPT;
4689}
4690
4691static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4692 struct sk_buff *skb,
4693 const struct net_device *in,
4694 const struct net_device *out,
4695 int (*okfn)(struct sk_buff *))
4696{
4697 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4698}
4699
4700#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4701static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4702 struct sk_buff *skb,
4703 const struct net_device *in,
4704 const struct net_device *out,
4705 int (*okfn)(struct sk_buff *))
4706{
4707 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4708}
4709#endif /* IPV6 */
4710
Paul Moore948bf852008-10-10 10:16:32 -04004711static unsigned int selinux_ip_output(struct sk_buff *skb,
4712 u16 family)
4713{
Paul Moore1c5d9d12013-12-04 16:10:45 -05004714 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004715 u32 sid;
4716
4717 if (!netlbl_enabled())
4718 return NF_ACCEPT;
4719
4720 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4721 * because we want to make sure we apply the necessary labeling
4722 * before IPsec is applied so we can leverage AH protection */
Paul Moore1c5d9d12013-12-04 16:10:45 -05004723 sk = skb->sk;
4724 if (sk) {
4725 struct sk_security_struct *sksec;
4726
4727 if (sk->sk_state == TCP_LISTEN)
4728 /* if the socket is the listening state then this
4729 * packet is a SYN-ACK packet which means it needs to
4730 * be labeled based on the connection/request_sock and
4731 * not the parent socket. unfortunately, we can't
4732 * lookup the request_sock yet as it isn't queued on
4733 * the parent socket until after the SYN-ACK is sent.
4734 * the "solution" is to simply pass the packet as-is
4735 * as any IP option based labeling should be copied
4736 * from the initial connection request (in the IP
4737 * layer). it is far from ideal, but until we get a
4738 * security label in the packet itself this is the
4739 * best we can do. */
4740 return NF_ACCEPT;
4741
4742 /* standard practice, label using the parent socket */
4743 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004744 sid = sksec->sid;
4745 } else
4746 sid = SECINITSID_KERNEL;
4747 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4748 return NF_DROP;
4749
4750 return NF_ACCEPT;
4751}
4752
4753static unsigned int selinux_ipv4_output(unsigned int hooknum,
4754 struct sk_buff *skb,
4755 const struct net_device *in,
4756 const struct net_device *out,
4757 int (*okfn)(struct sk_buff *))
4758{
4759 return selinux_ip_output(skb, PF_INET);
4760}
4761
Paul Mooreeffad8d2008-01-29 08:49:27 -05004762static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4763 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004764 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004765{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004766 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004767 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004768 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004769 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004770 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004771 char *addrp;
4772 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004773
Paul Mooreeffad8d2008-01-29 08:49:27 -05004774 if (sk == NULL)
4775 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004776 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004777
Thomas Liu2bf49692009-07-14 12:14:09 -04004778 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004779 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004780 ad.u.net = &net;
4781 ad.u.net->netif = ifindex;
4782 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004783 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4784 return NF_DROP;
4785
Paul Moore58bfbb52009-03-27 17:10:41 -04004786 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004787 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004788 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004789 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004790
Steffen Klassertb9679a72011-02-23 12:55:21 +01004791 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4792 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004793
Paul Mooreeffad8d2008-01-29 08:49:27 -05004794 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
Paul Mooreeffad8d2008-01-29 08:49:27 -05004797static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4798 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004800 u32 secmark_perm;
4801 u32 peer_sid;
4802 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004803 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004804 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004805 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004806 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004807 u8 secmark_active;
4808 u8 peerlbl_active;
4809
Paul Mooreeffad8d2008-01-29 08:49:27 -05004810 /* If any sort of compatibility mode is enabled then handoff processing
4811 * to the selinux_ip_postroute_compat() function to deal with the
4812 * special handling. We do this in an attempt to keep this function
4813 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004814 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004815 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moore420cc6d2013-12-10 14:58:01 -05004816
Paul Mooreeffad8d2008-01-29 08:49:27 -05004817 secmark_active = selinux_secmark_enabled();
4818 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4819 if (!secmark_active && !peerlbl_active)
4820 return NF_ACCEPT;
4821
Paul Mooreeffad8d2008-01-29 08:49:27 -05004822 sk = skb->sk;
Paul Moore420cc6d2013-12-10 14:58:01 -05004823
4824#ifdef CONFIG_XFRM
4825 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4826 * packet transformation so allow the packet to pass without any checks
4827 * since we'll have another chance to perform access control checks
4828 * when the packet is on it's final way out.
4829 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4830 * is NULL, in this case go ahead and apply access control.
4831 * is NULL, in this case go ahead and apply access control.
4832 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4833 * TCP listening state we cannot wait until the XFRM processing
4834 * is done as we will miss out on the SA label if we do;
4835 * unfortunately, this means more work, but it is only once per
4836 * connection. */
4837 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4838 !(sk != NULL && sk->sk_state == TCP_LISTEN))
4839 return NF_ACCEPT;
4840#endif
4841
Paul Moored8395c82008-10-10 10:16:30 -04004842 if (sk == NULL) {
Paul Moore2ea04e52013-12-04 16:10:51 -05004843 /* Without an associated socket the packet is either coming
4844 * from the kernel or it is being forwarded; check the packet
4845 * to determine which and if the packet is being forwarded
4846 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004847 if (skb->skb_iif) {
4848 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004849 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004850 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004851 } else {
4852 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004853 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004854 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004855 } else if (sk->sk_state == TCP_LISTEN) {
4856 /* Locally generated packet but the associated socket is in the
4857 * listening state which means this is a SYN-ACK packet. In
4858 * this particular case the correct security label is assigned
4859 * to the connection/request_sock but unfortunately we can't
4860 * query the request_sock as it isn't queued on the parent
4861 * socket until after the SYN-ACK packet is sent; the only
4862 * viable choice is to regenerate the label like we do in
4863 * selinux_inet_conn_request(). See also selinux_ip_output()
4864 * for similar problems. */
4865 u32 skb_sid;
4866 struct sk_security_struct *sksec = sk->sk_security;
4867 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4868 return NF_DROP;
Paul Moore420cc6d2013-12-10 14:58:01 -05004869 /* At this point, if the returned skb peerlbl is SECSID_NULL
4870 * and the packet has been through at least one XFRM
4871 * transformation then we must be dealing with the "final"
4872 * form of labeled IPsec packet; since we've already applied
4873 * all of our access controls on this packet we can safely
4874 * pass the packet. */
4875 if (skb_sid == SECSID_NULL) {
4876 switch (family) {
4877 case PF_INET:
4878 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4879 return NF_ACCEPT;
4880 break;
4881 case PF_INET6:
4882 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4883 return NF_ACCEPT;
4884 default:
4885 return NF_DROP_ERR(-ECONNREFUSED);
4886 }
4887 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004888 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4889 return NF_DROP;
4890 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004891 } else {
Paul Moore2ea04e52013-12-04 16:10:51 -05004892 /* Locally generated packet, fetch the security label from the
4893 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004894 struct sk_security_struct *sksec = sk->sk_security;
4895 peer_sid = sksec->sid;
4896 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004897 }
4898
Thomas Liu2bf49692009-07-14 12:14:09 -04004899 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004900 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004901 ad.u.net = &net;
4902 ad.u.net->netif = ifindex;
4903 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004904 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004905 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004906
Paul Mooreeffad8d2008-01-29 08:49:27 -05004907 if (secmark_active)
4908 if (avc_has_perm(peer_sid, skb->secmark,
4909 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004910 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004911
4912 if (peerlbl_active) {
4913 u32 if_sid;
4914 u32 node_sid;
4915
4916 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004917 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004918 if (avc_has_perm(peer_sid, if_sid,
4919 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004920 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004921
4922 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004923 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004924 if (avc_has_perm(peer_sid, node_sid,
4925 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004926 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004927 }
4928
4929 return NF_ACCEPT;
4930}
4931
4932static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4933 struct sk_buff *skb,
4934 const struct net_device *in,
4935 const struct net_device *out,
4936 int (*okfn)(struct sk_buff *))
4937{
4938 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939}
4940
4941#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004942static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4943 struct sk_buff *skb,
4944 const struct net_device *in,
4945 const struct net_device *out,
4946 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004948 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950#endif /* IPV6 */
4951
4952#endif /* CONFIG_NETFILTER */
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 int err;
4957
Eric Paris200ac532009-02-12 15:01:04 -05004958 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 if (err)
4960 return err;
4961
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004962 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963}
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965static int ipc_alloc_security(struct task_struct *task,
4966 struct kern_ipc_perm *perm,
4967 u16 sclass)
4968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004970 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
James Morris89d155e2005-10-30 14:59:21 -08004972 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 if (!isec)
4974 return -ENOMEM;
4975
David Howells275bb412008-11-14 10:39:19 +11004976 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004978 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 perm->security = isec;
4980
4981 return 0;
4982}
4983
4984static void ipc_free_security(struct kern_ipc_perm *perm)
4985{
4986 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 perm->security = NULL;
4988 kfree(isec);
4989}
4990
4991static int msg_msg_alloc_security(struct msg_msg *msg)
4992{
4993 struct msg_security_struct *msec;
4994
James Morris89d155e2005-10-30 14:59:21 -08004995 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 if (!msec)
4997 return -ENOMEM;
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 msec->sid = SECINITSID_UNLABELED;
5000 msg->security = msec;
5001
5002 return 0;
5003}
5004
5005static void msg_msg_free_security(struct msg_msg *msg)
5006{
5007 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 msg->security = NULL;
5010 kfree(msec);
5011}
5012
5013static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005014 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005017 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005018 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005019 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 isec = ipc_perms->security;
5022
Thomas Liu2bf49692009-07-14 12:14:09 -04005023 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005024 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 ad.u.ipc_id = ipc_perms->key;
5026
David Howells275bb412008-11-14 10:39:19 +11005027 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
5030static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5031{
5032 return msg_msg_alloc_security(msg);
5033}
5034
5035static void selinux_msg_msg_free_security(struct msg_msg *msg)
5036{
5037 msg_msg_free_security(msg);
5038}
5039
5040/* message queue security operations */
5041static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005044 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005045 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005046 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 int rc;
5048
5049 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5050 if (rc)
5051 return rc;
5052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 isec = msq->q_perm.security;
5054
Thomas Liu2bf49692009-07-14 12:14:09 -04005055 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005056 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005057 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
David Howells275bb412008-11-14 10:39:19 +11005059 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 MSGQ__CREATE, &ad);
5061 if (rc) {
5062 ipc_free_security(&msq->q_perm);
5063 return rc;
5064 }
5065 return 0;
5066}
5067
5068static void selinux_msg_queue_free_security(struct msg_queue *msq)
5069{
5070 ipc_free_security(&msq->q_perm);
5071}
5072
5073static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005076 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005077 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005078 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 isec = msq->q_perm.security;
5081
Thomas Liu2bf49692009-07-14 12:14:09 -04005082 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005083 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 ad.u.ipc_id = msq->q_perm.key;
5085
David Howells275bb412008-11-14 10:39:19 +11005086 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 MSGQ__ASSOCIATE, &ad);
5088}
5089
5090static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5091{
5092 int err;
5093 int perms;
5094
Eric Paris828dfe12008-04-17 13:17:49 -04005095 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 case IPC_INFO:
5097 case MSG_INFO:
5098 /* No specific object, just general system-wide information. */
5099 return task_has_system(current, SYSTEM__IPC_INFO);
5100 case IPC_STAT:
5101 case MSG_STAT:
5102 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5103 break;
5104 case IPC_SET:
5105 perms = MSGQ__SETATTR;
5106 break;
5107 case IPC_RMID:
5108 perms = MSGQ__DESTROY;
5109 break;
5110 default:
5111 return 0;
5112 }
5113
Stephen Smalley6af963f2005-05-01 08:58:39 -07005114 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 return err;
5116}
5117
5118static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 struct ipc_security_struct *isec;
5121 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005122 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005123 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005124 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 int rc;
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 isec = msq->q_perm.security;
5128 msec = msg->security;
5129
5130 /*
5131 * First time through, need to assign label to the message
5132 */
5133 if (msec->sid == SECINITSID_UNLABELED) {
5134 /*
5135 * Compute new sid based on current process and
5136 * message queue this message will be stored in
5137 */
David Howells275bb412008-11-14 10:39:19 +11005138 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005139 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 if (rc)
5141 return rc;
5142 }
5143
Thomas Liu2bf49692009-07-14 12:14:09 -04005144 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005145 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 ad.u.ipc_id = msq->q_perm.key;
5147
5148 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005149 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 MSGQ__WRITE, &ad);
5151 if (!rc)
5152 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005153 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5154 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 if (!rc)
5156 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005157 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5158 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
5160 return rc;
5161}
5162
5163static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5164 struct task_struct *target,
5165 long type, int mode)
5166{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 struct ipc_security_struct *isec;
5168 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005169 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005170 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005171 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 int rc;
5173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 isec = msq->q_perm.security;
5175 msec = msg->security;
5176
Thomas Liu2bf49692009-07-14 12:14:09 -04005177 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005178 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005179 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
David Howells275bb412008-11-14 10:39:19 +11005181 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 SECCLASS_MSGQ, MSGQ__READ, &ad);
5183 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005184 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 SECCLASS_MSG, MSG__RECEIVE, &ad);
5186 return rc;
5187}
5188
5189/* Shared Memory security operations */
5190static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005193 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005194 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005195 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 int rc;
5197
5198 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5199 if (rc)
5200 return rc;
5201
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 isec = shp->shm_perm.security;
5203
Thomas Liu2bf49692009-07-14 12:14:09 -04005204 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005205 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005206 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
David Howells275bb412008-11-14 10:39:19 +11005208 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 SHM__CREATE, &ad);
5210 if (rc) {
5211 ipc_free_security(&shp->shm_perm);
5212 return rc;
5213 }
5214 return 0;
5215}
5216
5217static void selinux_shm_free_security(struct shmid_kernel *shp)
5218{
5219 ipc_free_security(&shp->shm_perm);
5220}
5221
5222static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5223{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005225 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005226 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005227 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 isec = shp->shm_perm.security;
5230
Thomas Liu2bf49692009-07-14 12:14:09 -04005231 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005232 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 ad.u.ipc_id = shp->shm_perm.key;
5234
David Howells275bb412008-11-14 10:39:19 +11005235 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 SHM__ASSOCIATE, &ad);
5237}
5238
5239/* Note, at this point, shp is locked down */
5240static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5241{
5242 int perms;
5243 int err;
5244
Eric Paris828dfe12008-04-17 13:17:49 -04005245 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 case IPC_INFO:
5247 case SHM_INFO:
5248 /* No specific object, just general system-wide information. */
5249 return task_has_system(current, SYSTEM__IPC_INFO);
5250 case IPC_STAT:
5251 case SHM_STAT:
5252 perms = SHM__GETATTR | SHM__ASSOCIATE;
5253 break;
5254 case IPC_SET:
5255 perms = SHM__SETATTR;
5256 break;
5257 case SHM_LOCK:
5258 case SHM_UNLOCK:
5259 perms = SHM__LOCK;
5260 break;
5261 case IPC_RMID:
5262 perms = SHM__DESTROY;
5263 break;
5264 default:
5265 return 0;
5266 }
5267
Stephen Smalley6af963f2005-05-01 08:58:39 -07005268 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 return err;
5270}
5271
5272static int selinux_shm_shmat(struct shmid_kernel *shp,
5273 char __user *shmaddr, int shmflg)
5274{
5275 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
5277 if (shmflg & SHM_RDONLY)
5278 perms = SHM__READ;
5279 else
5280 perms = SHM__READ | SHM__WRITE;
5281
Stephen Smalley6af963f2005-05-01 08:58:39 -07005282 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
5285/* Semaphore security operations */
5286static int selinux_sem_alloc_security(struct sem_array *sma)
5287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005289 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005290 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005291 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 int rc;
5293
5294 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5295 if (rc)
5296 return rc;
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 isec = sma->sem_perm.security;
5299
Thomas Liu2bf49692009-07-14 12:14:09 -04005300 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005301 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005302 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
David Howells275bb412008-11-14 10:39:19 +11005304 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 SEM__CREATE, &ad);
5306 if (rc) {
5307 ipc_free_security(&sma->sem_perm);
5308 return rc;
5309 }
5310 return 0;
5311}
5312
5313static void selinux_sem_free_security(struct sem_array *sma)
5314{
5315 ipc_free_security(&sma->sem_perm);
5316}
5317
5318static int selinux_sem_associate(struct sem_array *sma, int semflg)
5319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005321 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005322 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005323 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 isec = sma->sem_perm.security;
5326
Thomas Liu2bf49692009-07-14 12:14:09 -04005327 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005328 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 ad.u.ipc_id = sma->sem_perm.key;
5330
David Howells275bb412008-11-14 10:39:19 +11005331 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 SEM__ASSOCIATE, &ad);
5333}
5334
5335/* Note, at this point, sma is locked down */
5336static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5337{
5338 int err;
5339 u32 perms;
5340
Eric Paris828dfe12008-04-17 13:17:49 -04005341 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 case IPC_INFO:
5343 case SEM_INFO:
5344 /* No specific object, just general system-wide information. */
5345 return task_has_system(current, SYSTEM__IPC_INFO);
5346 case GETPID:
5347 case GETNCNT:
5348 case GETZCNT:
5349 perms = SEM__GETATTR;
5350 break;
5351 case GETVAL:
5352 case GETALL:
5353 perms = SEM__READ;
5354 break;
5355 case SETVAL:
5356 case SETALL:
5357 perms = SEM__WRITE;
5358 break;
5359 case IPC_RMID:
5360 perms = SEM__DESTROY;
5361 break;
5362 case IPC_SET:
5363 perms = SEM__SETATTR;
5364 break;
5365 case IPC_STAT:
5366 case SEM_STAT:
5367 perms = SEM__GETATTR | SEM__ASSOCIATE;
5368 break;
5369 default:
5370 return 0;
5371 }
5372
Stephen Smalley6af963f2005-05-01 08:58:39 -07005373 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 return err;
5375}
5376
5377static int selinux_sem_semop(struct sem_array *sma,
5378 struct sembuf *sops, unsigned nsops, int alter)
5379{
5380 u32 perms;
5381
5382 if (alter)
5383 perms = SEM__READ | SEM__WRITE;
5384 else
5385 perms = SEM__READ;
5386
Stephen Smalley6af963f2005-05-01 08:58:39 -07005387 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388}
5389
5390static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 u32 av = 0;
5393
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 av = 0;
5395 if (flag & S_IRUGO)
5396 av |= IPC__UNIX_READ;
5397 if (flag & S_IWUGO)
5398 av |= IPC__UNIX_WRITE;
5399
5400 if (av == 0)
5401 return 0;
5402
Stephen Smalley6af963f2005-05-01 08:58:39 -07005403 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404}
5405
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005406static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5407{
5408 struct ipc_security_struct *isec = ipcp->security;
5409 *secid = isec->sid;
5410}
5411
Eric Paris828dfe12008-04-17 13:17:49 -04005412static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
5414 if (inode)
5415 inode_doinit_with_dentry(inode, dentry);
5416}
5417
5418static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005419 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
David Howells275bb412008-11-14 10:39:19 +11005421 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005422 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005424 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005427 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 if (error)
5429 return error;
5430 }
5431
David Howells275bb412008-11-14 10:39:19 +11005432 rcu_read_lock();
5433 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
5435 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005436 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005438 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005440 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005442 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005443 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005444 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005445 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005446 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 else
David Howells275bb412008-11-14 10:39:19 +11005448 goto invalid;
5449 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 if (!sid)
5452 return 0;
5453
Al Viro04ff9702007-03-12 16:17:58 +00005454 error = security_sid_to_context(sid, value, &len);
5455 if (error)
5456 return error;
5457 return len;
David Howells275bb412008-11-14 10:39:19 +11005458
5459invalid:
5460 rcu_read_unlock();
5461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462}
5463
5464static int selinux_setprocattr(struct task_struct *p,
5465 char *name, void *value, size_t size)
5466{
5467 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005468 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005469 struct cred *new;
5470 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 int error;
5472 char *str = value;
5473
5474 if (current != p) {
5475 /* SELinux only allows a process to change its own
5476 security attributes. */
5477 return -EACCES;
5478 }
5479
5480 /*
5481 * Basic control over ability to set these attributes at all.
5482 * current == p, but we'll pass them separately in case the
5483 * above restriction is ever removed.
5484 */
5485 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005486 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005488 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005489 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005490 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005491 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005492 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005494 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 else
5496 error = -EINVAL;
5497 if (error)
5498 return error;
5499
5500 /* Obtain a SID for the context, if one was specified. */
5501 if (size && str[1] && str[1] != '\n') {
5502 if (str[size-1] == '\n') {
5503 str[size-1] = 0;
5504 size--;
5505 }
5506 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005507 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5508 if (!capable(CAP_MAC_ADMIN))
5509 return error;
5510 error = security_context_to_sid_force(value, size,
5511 &sid);
5512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 if (error)
5514 return error;
5515 }
5516
David Howellsd84f4f92008-11-14 10:39:23 +11005517 new = prepare_creds();
5518 if (!new)
5519 return -ENOMEM;
5520
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 /* Permission checking based on the specified context is
5522 performed during the actual operation (execve,
5523 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005524 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 checks and may_create for the file creation checks. The
5526 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005527 tsec = new->security;
5528 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005530 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005532 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005533 error = may_create_key(sid, p);
5534 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005535 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005536 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005537 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005538 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005539 } else if (!strcmp(name, "current")) {
5540 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005542 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005543
David Howellsd84f4f92008-11-14 10:39:23 +11005544 /* Only allow single threaded processes to change context */
5545 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005546 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005547 error = security_bounded_transition(tsec->sid, sid);
5548 if (error)
5549 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
5552 /* Check permissions for the transition. */
5553 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005554 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005556 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
5558 /* Check for ptracing, and update the task SID if ok.
5559 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005560 ptsid = 0;
Oleg Nesterov58c23142013-12-23 17:45:01 -05005561 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02005562 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005563 if (tracer)
5564 ptsid = task_sid(tracer);
Oleg Nesterov58c23142013-12-23 17:45:01 -05005565 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
David Howellsd84f4f92008-11-14 10:39:23 +11005567 if (tracer) {
5568 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5569 PROCESS__PTRACE, NULL);
5570 if (error)
5571 goto abort_change;
5572 }
5573
5574 tsec->sid = sid;
5575 } else {
5576 error = -EINVAL;
5577 goto abort_change;
5578 }
5579
5580 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005582
5583abort_change:
5584 abort_creds(new);
5585 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586}
5587
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005588static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5589{
5590 return security_sid_to_context(secid, secdata, seclen);
5591}
5592
David Howells7bf570d2008-04-29 20:52:51 +01005593static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005594{
5595 return security_context_to_sid(secdata, seclen, secid);
5596}
5597
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005598static void selinux_release_secctx(char *secdata, u32 seclen)
5599{
Paul Moore088999e2007-08-01 11:12:58 -04005600 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005601}
5602
David P. Quigley1ee65e32009-09-03 14:25:57 -04005603/*
5604 * called with inode->i_mutex locked
5605 */
5606static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5607{
5608 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5609}
5610
5611/*
5612 * called with inode->i_mutex locked
5613 */
5614static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5615{
5616 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5617}
5618
5619static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5620{
5621 int len = 0;
5622 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5623 ctx, true);
5624 if (len < 0)
5625 return len;
5626 *ctxlen = len;
5627 return 0;
5628}
Michael LeMayd7200242006-06-22 14:47:17 -07005629#ifdef CONFIG_KEYS
5630
David Howellsd84f4f92008-11-14 10:39:23 +11005631static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005632 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005633{
David Howellsd84f4f92008-11-14 10:39:23 +11005634 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005635 struct key_security_struct *ksec;
5636
5637 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5638 if (!ksec)
5639 return -ENOMEM;
5640
David Howellsd84f4f92008-11-14 10:39:23 +11005641 tsec = cred->security;
5642 if (tsec->keycreate_sid)
5643 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005644 else
David Howellsd84f4f92008-11-14 10:39:23 +11005645 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005646
David Howells275bb412008-11-14 10:39:19 +11005647 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005648 return 0;
5649}
5650
5651static void selinux_key_free(struct key *k)
5652{
5653 struct key_security_struct *ksec = k->security;
5654
5655 k->security = NULL;
5656 kfree(ksec);
5657}
5658
5659static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005660 const struct cred *cred,
5661 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005662{
5663 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005664 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005665 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005666
5667 /* if no specific permissions are requested, we skip the
5668 permission check. No serious, additional covert channels
5669 appear to be created. */
5670 if (perm == 0)
5671 return 0;
5672
David Howellsd84f4f92008-11-14 10:39:23 +11005673 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005674
5675 key = key_ref_to_ptr(key_ref);
5676 ksec = key->security;
5677
5678 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005679}
5680
David Howells70a5bb72008-04-29 01:01:26 -07005681static int selinux_key_getsecurity(struct key *key, char **_buffer)
5682{
5683 struct key_security_struct *ksec = key->security;
5684 char *context = NULL;
5685 unsigned len;
5686 int rc;
5687
5688 rc = security_sid_to_context(ksec->sid, &context, &len);
5689 if (!rc)
5690 rc = len;
5691 *_buffer = context;
5692 return rc;
5693}
5694
Michael LeMayd7200242006-06-22 14:47:17 -07005695#endif
5696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005698 .name = "selinux",
5699
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05005700 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5701 .binder_transaction = selinux_binder_transaction,
5702 .binder_transfer_binder = selinux_binder_transfer_binder,
5703 .binder_transfer_file = selinux_binder_transfer_file,
5704
Ingo Molnar9e488582009-05-07 19:26:19 +10005705 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005706 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005708 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 .capable = selinux_capable,
5710 .quotactl = selinux_quotactl,
5711 .quota_on = selinux_quota_on,
5712 .syslog = selinux_syslog,
5713 .vm_enough_memory = selinux_vm_enough_memory,
5714
5715 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
David Howellsa6f76f22008-11-14 10:39:24 +11005717 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005718 .bprm_committing_creds = selinux_bprm_committing_creds,
5719 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 .bprm_secureexec = selinux_bprm_secureexec,
5721
5722 .sb_alloc_security = selinux_sb_alloc_security,
5723 .sb_free_security = selinux_sb_free_security,
5724 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005725 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005726 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005727 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 .sb_statfs = selinux_sb_statfs,
5729 .sb_mount = selinux_mount,
5730 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005731 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005732 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005733 .sb_parse_opts_str = selinux_parse_opts_str,
5734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 .inode_alloc_security = selinux_inode_alloc_security,
5737 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005738 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 .inode_unlink = selinux_inode_unlink,
5742 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 .inode_rmdir = selinux_inode_rmdir,
5745 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 .inode_readlink = selinux_inode_readlink,
5748 .inode_follow_link = selinux_inode_follow_link,
5749 .inode_permission = selinux_inode_permission,
5750 .inode_setattr = selinux_inode_setattr,
5751 .inode_getattr = selinux_inode_getattr,
5752 .inode_setxattr = selinux_inode_setxattr,
5753 .inode_post_setxattr = selinux_inode_post_setxattr,
5754 .inode_getxattr = selinux_inode_getxattr,
5755 .inode_listxattr = selinux_inode_listxattr,
5756 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005757 .inode_getsecurity = selinux_inode_getsecurity,
5758 .inode_setsecurity = selinux_inode_setsecurity,
5759 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005760 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 .file_permission = selinux_file_permission,
5763 .file_alloc_security = selinux_file_alloc_security,
5764 .file_free_security = selinux_file_free_security,
5765 .file_ioctl = selinux_file_ioctl,
5766 .file_mmap = selinux_file_mmap,
5767 .file_mprotect = selinux_file_mprotect,
5768 .file_lock = selinux_file_lock,
5769 .file_fcntl = selinux_file_fcntl,
5770 .file_set_fowner = selinux_file_set_fowner,
5771 .file_send_sigiotask = selinux_file_send_sigiotask,
5772 .file_receive = selinux_file_receive,
5773
Eric Paris828dfe12008-04-17 13:17:49 -04005774 .dentry_open = selinux_dentry_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005775
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005777 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005778 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005779 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005780 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005781 .kernel_act_as = selinux_kernel_act_as,
5782 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005783 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 .task_setpgid = selinux_task_setpgid,
5785 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005786 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005787 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005789 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005790 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 .task_setrlimit = selinux_task_setrlimit,
5792 .task_setscheduler = selinux_task_setscheduler,
5793 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005794 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 .task_kill = selinux_task_kill,
5796 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005797 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005800 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5803 .msg_msg_free_security = selinux_msg_msg_free_security,
5804
5805 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5806 .msg_queue_free_security = selinux_msg_queue_free_security,
5807 .msg_queue_associate = selinux_msg_queue_associate,
5808 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5809 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5810 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5811
5812 .shm_alloc_security = selinux_shm_alloc_security,
5813 .shm_free_security = selinux_shm_free_security,
5814 .shm_associate = selinux_shm_associate,
5815 .shm_shmctl = selinux_shm_shmctl,
5816 .shm_shmat = selinux_shm_shmat,
5817
Eric Paris828dfe12008-04-17 13:17:49 -04005818 .sem_alloc_security = selinux_sem_alloc_security,
5819 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 .sem_associate = selinux_sem_associate,
5821 .sem_semctl = selinux_sem_semctl,
5822 .sem_semop = selinux_sem_semop,
5823
Eric Paris828dfe12008-04-17 13:17:49 -04005824 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Eric Paris828dfe12008-04-17 13:17:49 -04005826 .getprocattr = selinux_getprocattr,
5827 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005829 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005830 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005831 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005832 .inode_notifysecctx = selinux_inode_notifysecctx,
5833 .inode_setsecctx = selinux_inode_setsecctx,
5834 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005835
Eric Paris828dfe12008-04-17 13:17:49 -04005836 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 .unix_may_send = selinux_socket_unix_may_send,
5838
5839 .socket_create = selinux_socket_create,
5840 .socket_post_create = selinux_socket_post_create,
5841 .socket_bind = selinux_socket_bind,
5842 .socket_connect = selinux_socket_connect,
5843 .socket_listen = selinux_socket_listen,
5844 .socket_accept = selinux_socket_accept,
5845 .socket_sendmsg = selinux_socket_sendmsg,
5846 .socket_recvmsg = selinux_socket_recvmsg,
5847 .socket_getsockname = selinux_socket_getsockname,
5848 .socket_getpeername = selinux_socket_getpeername,
5849 .socket_getsockopt = selinux_socket_getsockopt,
5850 .socket_setsockopt = selinux_socket_setsockopt,
5851 .socket_shutdown = selinux_socket_shutdown,
5852 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005853 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5854 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 .sk_alloc_security = selinux_sk_alloc_security,
5856 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005857 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005858 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005859 .sock_graft = selinux_sock_graft,
5860 .inet_conn_request = selinux_inet_conn_request,
5861 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005862 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005863 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5864 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5865 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005866 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005867 .tun_dev_create = selinux_tun_dev_create,
5868 .tun_dev_post_create = selinux_tun_dev_post_create,
5869 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005870
5871#ifdef CONFIG_SECURITY_NETWORK_XFRM
5872 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5873 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5874 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005875 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005876 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5877 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005878 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005879 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005880 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005881 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005883
5884#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005885 .key_alloc = selinux_key_alloc,
5886 .key_free = selinux_key_free,
5887 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005888 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005889#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005890
5891#ifdef CONFIG_AUDIT
5892 .audit_rule_init = selinux_audit_rule_init,
5893 .audit_rule_known = selinux_audit_rule_known,
5894 .audit_rule_match = selinux_audit_rule_match,
5895 .audit_rule_free = selinux_audit_rule_free,
5896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897};
5898
5899static __init int selinux_init(void)
5900{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005901 if (!security_module_enable(&selinux_ops)) {
5902 selinux_enabled = 0;
5903 return 0;
5904 }
5905
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 if (!selinux_enabled) {
5907 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5908 return 0;
5909 }
5910
5911 printk(KERN_INFO "SELinux: Initializing.\n");
5912
5913 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005914 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005916 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5917
James Morris7cae7e22006-03-22 00:09:22 -08005918 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5919 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005920 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 avc_init();
5922
Eric Paris828dfe12008-04-17 13:17:49 -04005923 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 panic("SELinux: Unable to register with kernel.\n");
5925
Eric Paris828dfe12008-04-17 13:17:49 -04005926 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005927 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005928 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005929 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005930
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 return 0;
5932}
5933
Al Viroe8c26252010-03-23 06:36:54 -04005934static void delayed_superblock_init(struct super_block *sb, void *unused)
5935{
5936 superblock_doinit(sb, NULL);
5937}
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939void selinux_complete_init(void)
5940{
Eric Parisfadcdb42007-02-22 18:11:31 -05005941 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
5943 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005944 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005945 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946}
5947
5948/* SELinux requires early initialization in order to label
5949 all processes and objects when they are created. */
5950security_initcall(selinux_init);
5951
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005952#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Paul Mooreeffad8d2008-01-29 08:49:27 -05005954static struct nf_hook_ops selinux_ipv4_ops[] = {
5955 {
5956 .hook = selinux_ipv4_postroute,
5957 .owner = THIS_MODULE,
5958 .pf = PF_INET,
5959 .hooknum = NF_INET_POST_ROUTING,
5960 .priority = NF_IP_PRI_SELINUX_LAST,
5961 },
5962 {
5963 .hook = selinux_ipv4_forward,
5964 .owner = THIS_MODULE,
5965 .pf = PF_INET,
5966 .hooknum = NF_INET_FORWARD,
5967 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005968 },
5969 {
5970 .hook = selinux_ipv4_output,
5971 .owner = THIS_MODULE,
5972 .pf = PF_INET,
5973 .hooknum = NF_INET_LOCAL_OUT,
5974 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976};
5977
5978#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5979
Paul Mooreeffad8d2008-01-29 08:49:27 -05005980static struct nf_hook_ops selinux_ipv6_ops[] = {
5981 {
5982 .hook = selinux_ipv6_postroute,
5983 .owner = THIS_MODULE,
5984 .pf = PF_INET6,
5985 .hooknum = NF_INET_POST_ROUTING,
5986 .priority = NF_IP6_PRI_SELINUX_LAST,
5987 },
5988 {
5989 .hook = selinux_ipv6_forward,
5990 .owner = THIS_MODULE,
5991 .pf = PF_INET6,
5992 .hooknum = NF_INET_FORWARD,
5993 .priority = NF_IP6_PRI_SELINUX_FIRST,
5994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995};
5996
5997#endif /* IPV6 */
5998
5999static int __init selinux_nf_ip_init(void)
6000{
6001 int err = 0;
6002
6003 if (!selinux_enabled)
6004 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006005
6006 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6007
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006008 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6009 if (err)
6010 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
6012#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006013 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6014 if (err)
6015 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018out:
6019 return err;
6020}
6021
6022__initcall(selinux_nf_ip_init);
6023
6024#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6025static void selinux_nf_ip_exit(void)
6026{
Eric Parisfadcdb42007-02-22 18:11:31 -05006027 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006029 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006031 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032#endif /* IPV6 */
6033}
6034#endif
6035
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006036#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
6038#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6039#define selinux_nf_ip_exit()
6040#endif
6041
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006042#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
6044#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006045static int selinux_disabled;
6046
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047int selinux_disable(void)
6048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 if (ss_initialized) {
6050 /* Not permitted after initial policy load. */
6051 return -EINVAL;
6052 }
6053
6054 if (selinux_disabled) {
6055 /* Only do this once. */
6056 return -EINVAL;
6057 }
6058
6059 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6060
6061 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006062 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006064 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
Eric Parisaf8ff042009-09-20 21:23:01 -04006066 /* Try to destroy the avc node cache */
6067 avc_disable();
6068
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 /* Unregister netfilter hooks. */
6070 selinux_nf_ip_exit();
6071
6072 /* Unregister selinuxfs. */
6073 exit_sel_fs();
6074
6075 return 0;
6076}
6077#endif