blob: 9d204dbca216a68487b0b20188e32ba1e6662c7d [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;
Stephen Smalley61dedfa2014-10-06 16:32:52 -0400446 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_unlock(&sbsec->isec_lock);
448 inode = igrab(inode);
449 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500450 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 inode_doinit(inode);
452 iput(inode);
453 }
454 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 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)) {
Paul Moore23a5a7a2014-03-19 16:46:18 -04001339 /* We must have a dentry to determine the label on
1340 * procfs inodes */
1341 if (opt_dentry)
1342 /* Called from d_instantiate or
1343 * d_splice_alias. */
1344 dentry = dget(opt_dentry);
1345 else
1346 /* Called from selinux_complete_init, try to
1347 * find a dentry. */
1348 dentry = d_find_alias(inode);
1349 /*
1350 * This can be hit on boot when a file is accessed
1351 * before the policy is loaded. When we load policy we
1352 * may find inodes that have no dentry on the
1353 * sbsec->isec_head list. No reason to complain as
1354 * these will get fixed up the next time we go through
1355 * inode_doinit() with a dentry, before these inodes
1356 * could be used again by userspace.
1357 */
1358 if (!dentry)
1359 goto out_unlock;
1360 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1361 rc = selinux_proc_get_sid(dentry, isec->sclass, &sid);
1362 dput(dentry);
1363 if (rc)
1364 goto out_unlock;
1365 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 break;
1368 }
1369
1370 isec->initialized = 1;
1371
Eric Paris23970742006-09-25 23:32:01 -07001372out_unlock:
1373 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374out:
1375 if (isec->sclass == SECCLASS_FILE)
1376 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return rc;
1378}
1379
1380/* Convert a Linux signal to an access vector. */
1381static inline u32 signal_to_av(int sig)
1382{
1383 u32 perm = 0;
1384
1385 switch (sig) {
1386 case SIGCHLD:
1387 /* Commonly granted from child to parent. */
1388 perm = PROCESS__SIGCHLD;
1389 break;
1390 case SIGKILL:
1391 /* Cannot be caught or ignored */
1392 perm = PROCESS__SIGKILL;
1393 break;
1394 case SIGSTOP:
1395 /* Cannot be caught or ignored */
1396 perm = PROCESS__SIGSTOP;
1397 break;
1398 default:
1399 /* All other signals. */
1400 perm = PROCESS__SIGNAL;
1401 break;
1402 }
1403
1404 return perm;
1405}
1406
David Howells275bb412008-11-14 10:39:19 +11001407/*
David Howellsd84f4f92008-11-14 10:39:23 +11001408 * Check permission between a pair of credentials
1409 * fork check, ptrace check, etc.
1410 */
1411static int cred_has_perm(const struct cred *actor,
1412 const struct cred *target,
1413 u32 perms)
1414{
1415 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1416
1417 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1418}
1419
1420/*
David Howells88e67f32008-11-14 10:39:21 +11001421 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001422 * fork check, ptrace check, etc.
1423 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001424 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001425 */
1426static int task_has_perm(const struct task_struct *tsk1,
1427 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 u32 perms)
1429{
David Howells275bb412008-11-14 10:39:19 +11001430 const struct task_security_struct *__tsec1, *__tsec2;
1431 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
David Howells275bb412008-11-14 10:39:19 +11001433 rcu_read_lock();
1434 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1435 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1436 rcu_read_unlock();
1437 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
David Howells3b11a1d2008-11-14 10:39:26 +11001440/*
1441 * Check permission between current and another task, e.g. signal checks,
1442 * fork check, ptrace check, etc.
1443 * current is the actor and tsk2 is the target
1444 * - this uses current's subjective creds
1445 */
1446static int current_has_perm(const struct task_struct *tsk,
1447 u32 perms)
1448{
1449 u32 sid, tsid;
1450
1451 sid = current_sid();
1452 tsid = task_sid(tsk);
1453 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1454}
1455
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001456#if CAP_LAST_CAP > 63
1457#error Fix SELinux to handle capabilities > 63.
1458#endif
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001461static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001462 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Thomas Liu2bf49692009-07-14 12:14:09 -04001464 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001465 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001466 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001467 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001468 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001469 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001470 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Thomas Liu2bf49692009-07-14 12:14:09 -04001472 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001473 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001474 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 ad.u.cap = cap;
1476
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001477 switch (CAP_TO_INDEX(cap)) {
1478 case 0:
1479 sclass = SECCLASS_CAPABILITY;
1480 break;
1481 case 1:
1482 sclass = SECCLASS_CAPABILITY2;
1483 break;
1484 default:
1485 printk(KERN_ERR
1486 "SELinux: out of range capability %d\n", cap);
1487 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001488 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001489 }
Eric Paris06112162008-11-11 22:02:50 +11001490
David Howells275bb412008-11-14 10:39:19 +11001491 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001492 if (audit == SECURITY_CAP_AUDIT) {
1493 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1494 if (rc2)
1495 return rc2;
1496 }
Eric Paris06112162008-11-11 22:02:50 +11001497 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/* Check whether a task is allowed to use a system operation. */
1501static int task_has_system(struct task_struct *tsk,
1502 u32 perms)
1503{
David Howells275bb412008-11-14 10:39:19 +11001504 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
David Howells275bb412008-11-14 10:39:19 +11001506 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 SECCLASS_SYSTEM, perms, NULL);
1508}
1509
1510/* Check whether a task has a particular permission to an inode.
1511 The 'adp' parameter is optional and allows other audit
1512 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001513static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 struct inode *inode,
1515 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001516 struct common_audit_data *adp,
1517 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001520 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
David Howellse0e81732009-09-02 09:13:40 +01001522 validate_creds(cred);
1523
Eric Paris828dfe12008-04-17 13:17:49 -04001524 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001525 return 0;
1526
David Howells88e67f32008-11-14 10:39:21 +11001527 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 isec = inode->i_security;
1529
Eric Paris9ade0cf2011-04-25 16:26:29 -04001530 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001533static int inode_has_perm_noadp(const struct cred *cred,
1534 struct inode *inode,
1535 u32 perms,
1536 unsigned flags)
1537{
1538 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001539 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001540
1541 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1542 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001543 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001544 return inode_has_perm(cred, inode, perms, &ad, flags);
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/* Same as inode_has_perm, but pass explicit audit data containing
1548 the dentry to help the auditing code to more easily generate the
1549 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001550static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct dentry *dentry,
1552 u32 av)
1553{
1554 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001555 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001556 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001557
Eric Paris2875fa02011-04-28 16:04:24 -04001558 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1559 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001560 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001561 return inode_has_perm(cred, inode, av, &ad, 0);
1562}
1563
1564/* Same as inode_has_perm, but pass explicit audit data containing
1565 the path to help the auditing code to more easily generate the
1566 pathname if needed. */
1567static inline int path_has_perm(const struct cred *cred,
1568 struct path *path,
1569 u32 av)
1570{
1571 struct inode *inode = path->dentry->d_inode;
1572 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001573 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001574
Eric Parisf48b7392011-04-25 12:54:27 -04001575 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001576 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001577 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001578 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581/* Check whether a task can use an open file descriptor to
1582 access an inode in a given way. Check access to the
1583 descriptor itself, and then use dentry_has_perm to
1584 check a particular permission to the file.
1585 Access to the descriptor is implicitly granted if it
1586 has the same SID as the process. If av is zero, then
1587 access to the file is not checked, e.g. for cases
1588 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001589static int file_has_perm(const struct cred *cred,
1590 struct file *file,
1591 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001594 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001595 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001596 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001597 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int rc;
1599
Eric Parisf48b7392011-04-25 12:54:27 -04001600 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1601 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001602 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
David Howells275bb412008-11-14 10:39:19 +11001604 if (sid != fsec->sid) {
1605 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 SECCLASS_FD,
1607 FD__USE,
1608 &ad);
1609 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
1613 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001614 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001616 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
David Howells88e67f32008-11-14 10:39:21 +11001618out:
1619 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622/* Check whether a task can create a file. */
1623static int may_create(struct inode *dir,
1624 struct dentry *dentry,
1625 u16 tclass)
1626{
Paul Moore5fb49872010-04-22 14:46:19 -04001627 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct inode_security_struct *dsec;
1629 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001630 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001631 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001632 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int rc;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 dsec = dir->i_security;
1636 sbsec = dir->i_sb->s_security;
1637
David Howells275bb412008-11-14 10:39:19 +11001638 sid = tsec->sid;
1639 newsid = tsec->create_sid;
1640
Eric Parisa2694342011-04-25 13:10:27 -04001641 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1642 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001643 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
David Howells275bb412008-11-14 10:39:19 +11001645 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 DIR__ADD_NAME | DIR__SEARCH,
1647 &ad);
1648 if (rc)
1649 return rc;
1650
David P. Quigleycd895962009-01-16 09:22:04 -05001651 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001652 rc = security_transition_sid(sid, dsec->sid, tclass,
1653 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (rc)
1655 return rc;
1656 }
1657
David Howells275bb412008-11-14 10:39:19 +11001658 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (rc)
1660 return rc;
1661
1662 return avc_has_perm(newsid, sbsec->sid,
1663 SECCLASS_FILESYSTEM,
1664 FILESYSTEM__ASSOCIATE, &ad);
1665}
1666
Michael LeMay4eb582c2006-06-26 00:24:57 -07001667/* Check whether a task can create a key. */
1668static int may_create_key(u32 ksid,
1669 struct task_struct *ctx)
1670{
David Howells275bb412008-11-14 10:39:19 +11001671 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001672
David Howells275bb412008-11-14 10:39:19 +11001673 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001674}
1675
Eric Paris828dfe12008-04-17 13:17:49 -04001676#define MAY_LINK 0
1677#define MAY_UNLINK 1
1678#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680/* Check whether a task can link, unlink, or rmdir a file/directory. */
1681static int may_link(struct inode *dir,
1682 struct dentry *dentry,
1683 int kind)
1684
1685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001687 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001688 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001689 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 u32 av;
1691 int rc;
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 dsec = dir->i_security;
1694 isec = dentry->d_inode->i_security;
1695
Eric Parisa2694342011-04-25 13:10:27 -04001696 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1697 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001698 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 av = DIR__SEARCH;
1701 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001702 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (rc)
1704 return rc;
1705
1706 switch (kind) {
1707 case MAY_LINK:
1708 av = FILE__LINK;
1709 break;
1710 case MAY_UNLINK:
1711 av = FILE__UNLINK;
1712 break;
1713 case MAY_RMDIR:
1714 av = DIR__RMDIR;
1715 break;
1716 default:
Eric Paris744ba352008-04-17 11:52:44 -04001717 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1718 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return 0;
1720 }
1721
David Howells275bb412008-11-14 10:39:19 +11001722 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return rc;
1724}
1725
1726static inline int may_rename(struct inode *old_dir,
1727 struct dentry *old_dentry,
1728 struct inode *new_dir,
1729 struct dentry *new_dentry)
1730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001732 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001733 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001734 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 u32 av;
1736 int old_is_dir, new_is_dir;
1737 int rc;
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 old_dsec = old_dir->i_security;
1740 old_isec = old_dentry->d_inode->i_security;
1741 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1742 new_dsec = new_dir->i_security;
1743
Eric Parisa2694342011-04-25 13:10:27 -04001744 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001745 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Eric Parisa2694342011-04-25 13:10:27 -04001747 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001748 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1750 if (rc)
1751 return rc;
David Howells275bb412008-11-14 10:39:19 +11001752 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 old_isec->sclass, FILE__RENAME, &ad);
1754 if (rc)
1755 return rc;
1756 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001757 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 old_isec->sclass, DIR__REPARENT, &ad);
1759 if (rc)
1760 return rc;
1761 }
1762
Eric Parisa2694342011-04-25 13:10:27 -04001763 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 av = DIR__ADD_NAME | DIR__SEARCH;
1765 if (new_dentry->d_inode)
1766 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001767 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (rc)
1769 return rc;
1770 if (new_dentry->d_inode) {
1771 new_isec = new_dentry->d_inode->i_security;
1772 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001773 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 new_isec->sclass,
1775 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1776 if (rc)
1777 return rc;
1778 }
1779
1780 return 0;
1781}
1782
1783/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001784static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct super_block *sb,
1786 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001787 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001790 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001793 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
1796/* Convert a Linux mode and permission mask to an access vector. */
1797static inline u32 file_mask_to_av(int mode, int mask)
1798{
1799 u32 av = 0;
1800
Al Virodba19c62011-07-25 20:49:29 -04001801 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (mask & MAY_EXEC)
1803 av |= FILE__EXECUTE;
1804 if (mask & MAY_READ)
1805 av |= FILE__READ;
1806
1807 if (mask & MAY_APPEND)
1808 av |= FILE__APPEND;
1809 else if (mask & MAY_WRITE)
1810 av |= FILE__WRITE;
1811
1812 } else {
1813 if (mask & MAY_EXEC)
1814 av |= DIR__SEARCH;
1815 if (mask & MAY_WRITE)
1816 av |= DIR__WRITE;
1817 if (mask & MAY_READ)
1818 av |= DIR__READ;
1819 }
1820
1821 return av;
1822}
1823
1824/* Convert a Linux file to an access vector. */
1825static inline u32 file_to_av(struct file *file)
1826{
1827 u32 av = 0;
1828
1829 if (file->f_mode & FMODE_READ)
1830 av |= FILE__READ;
1831 if (file->f_mode & FMODE_WRITE) {
1832 if (file->f_flags & O_APPEND)
1833 av |= FILE__APPEND;
1834 else
1835 av |= FILE__WRITE;
1836 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001837 if (!av) {
1838 /*
1839 * Special file opened with flags 3 for ioctl-only use.
1840 */
1841 av = FILE__IOCTL;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 return av;
1845}
1846
Eric Paris8b6a5a32008-10-29 17:06:46 -04001847/*
1848 * Convert a file to an access vector and include the correct open
1849 * open permission.
1850 */
1851static inline u32 open_file_to_av(struct file *file)
1852{
1853 u32 av = file_to_av(file);
1854
Eric Paris49b7b8d2010-07-23 11:44:09 -04001855 if (selinux_policycap_openperm)
1856 av |= FILE__OPEN;
1857
Eric Paris8b6a5a32008-10-29 17:06:46 -04001858 return av;
1859}
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/* Hook functions begin here. */
1862
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05001863static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1864{
1865 u32 mysid = current_sid();
1866 u32 mgrsid = task_sid(mgr);
1867
1868 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1869}
1870
1871static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1872{
1873 u32 mysid = current_sid();
1874 u32 fromsid = task_sid(from);
1875 u32 tosid = task_sid(to);
1876 int rc;
1877
1878 if (mysid != fromsid) {
1879 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1880 if (rc)
1881 return rc;
1882 }
1883
1884 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1885}
1886
1887static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1888{
1889 u32 fromsid = task_sid(from);
1890 u32 tosid = task_sid(to);
1891 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1892}
1893
1894static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1895{
1896 u32 sid = task_sid(to);
1897 struct file_security_struct *fsec = file->f_security;
1898 struct inode *inode = file->f_path.dentry->d_inode;
1899 struct inode_security_struct *isec = inode->i_security;
1900 struct common_audit_data ad;
1901 struct selinux_audit_data sad = {0,};
1902 int rc;
1903
1904 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1905 ad.u.path = file->f_path;
1906 ad.selinux_audit_data = &sad;
1907
1908 if (sid != fsec->sid) {
1909 rc = avc_has_perm(sid, fsec->sid,
1910 SECCLASS_FD,
1911 FD__USE,
1912 &ad);
1913 if (rc)
1914 return rc;
1915 }
1916
1917 if (unlikely(IS_PRIVATE(inode)))
1918 return 0;
1919
1920 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1921 &ad);
1922}
1923
Ingo Molnar9e488582009-05-07 19:26:19 +10001924static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001925 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int rc;
1928
Ingo Molnar9e488582009-05-07 19:26:19 +10001929 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (rc)
1931 return rc;
1932
Eric Paris69f594a2012-01-03 12:25:15 -05001933 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001934 u32 sid = current_sid();
1935 u32 csid = task_sid(child);
1936 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001937 }
1938
David Howells3b11a1d2008-11-14 10:39:26 +11001939 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001940}
1941
1942static int selinux_ptrace_traceme(struct task_struct *parent)
1943{
1944 int rc;
1945
Eric Paris200ac532009-02-12 15:01:04 -05001946 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001947 if (rc)
1948 return rc;
1949
1950 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001954 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 int error;
1957
David Howells3b11a1d2008-11-14 10:39:26 +11001958 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (error)
1960 return error;
1961
Eric Paris200ac532009-02-12 15:01:04 -05001962 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
David Howellsd84f4f92008-11-14 10:39:23 +11001965static int selinux_capset(struct cred *new, const struct cred *old,
1966 const kernel_cap_t *effective,
1967 const kernel_cap_t *inheritable,
1968 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 int error;
1971
Eric Paris200ac532009-02-12 15:01:04 -05001972 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001973 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (error)
1975 return error;
1976
David Howellsd84f4f92008-11-14 10:39:23 +11001977 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
James Morris5626d3e2009-01-30 10:05:06 +11001980/*
1981 * (This comment used to live with the selinux_task_setuid hook,
1982 * which was removed).
1983 *
1984 * Since setuid only affects the current process, and since the SELinux
1985 * controls are not based on the Linux identity attributes, SELinux does not
1986 * need to control this operation. However, SELinux does control the use of
1987 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1988 */
1989
Eric Paris6a9de492012-01-03 12:25:14 -05001990static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1991 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 int rc;
1994
Eric Paris6a9de492012-01-03 12:25:14 -05001995 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (rc)
1997 return rc;
1998
Eric Paris6a9de492012-01-03 12:25:14 -05001999 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2003{
David Howells88e67f32008-11-14 10:39:21 +11002004 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int rc = 0;
2006
2007 if (!sb)
2008 return 0;
2009
2010 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002011 case Q_SYNC:
2012 case Q_QUOTAON:
2013 case Q_QUOTAOFF:
2014 case Q_SETINFO:
2015 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002016 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002017 break;
2018 case Q_GETFMT:
2019 case Q_GETINFO:
2020 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002021 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002022 break;
2023 default:
2024 rc = 0; /* let the kernel handle invalid cmds */
2025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 return rc;
2028}
2029
2030static int selinux_quota_on(struct dentry *dentry)
2031{
David Howells88e67f32008-11-14 10:39:21 +11002032 const struct cred *cred = current_cred();
2033
Eric Paris2875fa02011-04-28 16:04:24 -04002034 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Eric Paris12b30522010-11-15 18:36:29 -05002037static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 int rc;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002042 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2043 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002044 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2045 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002046 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2047 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2048 /* Set level of messages printed to console */
2049 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002050 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2051 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002052 case SYSLOG_ACTION_CLOSE: /* Close log */
2053 case SYSLOG_ACTION_OPEN: /* Open log */
2054 case SYSLOG_ACTION_READ: /* Read from log */
2055 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2056 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002057 default:
2058 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 return rc;
2062}
2063
2064/*
2065 * Check that a process has enough memory to allocate a new virtual
2066 * mapping. 0 means there is enough memory for the allocation to
2067 * succeed and -ENOMEM implies there is not.
2068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Do not audit the selinux permission check, as this is applied to all
2070 * processes that allocate mappings.
2071 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002072static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Eric Paris6a9de492012-01-03 12:25:14 -05002076 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002077 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (rc == 0)
2079 cap_sys_admin = 1;
2080
Alan Cox34b4e4a2007-08-22 14:01:28 -07002081 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084/* binprm security operations */
2085
David Howellsa6f76f22008-11-14 10:39:24 +11002086static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
David Howellsa6f76f22008-11-14 10:39:24 +11002088 const struct task_security_struct *old_tsec;
2089 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002091 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002092 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002093 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int rc;
2095
Eric Paris200ac532009-02-12 15:01:04 -05002096 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (rc)
2098 return rc;
2099
David Howellsa6f76f22008-11-14 10:39:24 +11002100 /* SELinux context only depends on initial program or script and not
2101 * the script interpreter */
2102 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
2104
David Howellsa6f76f22008-11-14 10:39:24 +11002105 old_tsec = current_security();
2106 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 isec = inode->i_security;
2108
2109 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002110 new_tsec->sid = old_tsec->sid;
2111 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Michael LeMay28eba5b2006-06-27 02:53:42 -07002113 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002114 new_tsec->create_sid = 0;
2115 new_tsec->keycreate_sid = 0;
2116 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
David Howellsa6f76f22008-11-14 10:39:24 +11002118 if (old_tsec->exec_sid) {
2119 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002121 new_tsec->exec_sid = 0;
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002122
2123 /*
2124 * Minimize confusion: if no_new_privs and a transition is
2125 * explicitly requested, then fail the exec.
2126 */
2127 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2128 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 } else {
2130 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002131 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002132 SECCLASS_PROCESS, NULL,
2133 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (rc)
2135 return rc;
2136 }
2137
Eric Parisf48b7392011-04-25 12:54:27 -04002138 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002139 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002140 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002142 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2143 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002144 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
David Howellsa6f76f22008-11-14 10:39:24 +11002146 if (new_tsec->sid == old_tsec->sid) {
2147 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2149 if (rc)
2150 return rc;
2151 } else {
2152 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002153 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2155 if (rc)
2156 return rc;
2157
David Howellsa6f76f22008-11-14 10:39:24 +11002158 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2160 if (rc)
2161 return rc;
2162
David Howellsa6f76f22008-11-14 10:39:24 +11002163 /* Check for shared state */
2164 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2165 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2166 SECCLASS_PROCESS, PROCESS__SHARE,
2167 NULL);
2168 if (rc)
2169 return -EPERM;
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
David Howellsa6f76f22008-11-14 10:39:24 +11002172 /* Make sure that anyone attempting to ptrace over a task that
2173 * changes its SID has the appropriate permit */
2174 if (bprm->unsafe &
2175 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2176 struct task_struct *tracer;
2177 struct task_security_struct *sec;
2178 u32 ptsid = 0;
2179
2180 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002181 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002182 if (likely(tracer != NULL)) {
2183 sec = __task_cred(tracer)->security;
2184 ptsid = sec->sid;
2185 }
2186 rcu_read_unlock();
2187
2188 if (ptsid != 0) {
2189 rc = avc_has_perm(ptsid, new_tsec->sid,
2190 SECCLASS_PROCESS,
2191 PROCESS__PTRACE, NULL);
2192 if (rc)
2193 return -EPERM;
2194 }
2195 }
2196
2197 /* Clear any possibly unsafe personality bits on exec: */
2198 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
2202}
2203
Eric Paris828dfe12008-04-17 13:17:49 -04002204static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Paul Moore5fb49872010-04-22 14:46:19 -04002206 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002207 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 int atsecure = 0;
2209
David Howells275bb412008-11-14 10:39:19 +11002210 sid = tsec->sid;
2211 osid = tsec->osid;
2212
2213 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* Enable secure mode for SIDs transitions unless
2215 the noatsecure permission is granted between
2216 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002217 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002218 SECCLASS_PROCESS,
2219 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221
Eric Paris200ac532009-02-12 15:01:04 -05002222 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002226static inline void flush_unauthorized_files(const struct cred *cred,
2227 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Thomas Liu2bf49692009-07-14 12:14:09 -04002229 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002230 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002232 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002233 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002235 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002237 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002239 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002240 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002241 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002242 struct inode *inode;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* Revalidate access to controlling tty.
2245 Use inode_has_perm on the tty inode directly rather
2246 than using file_has_perm, as this particular open
2247 file may belong to another process and we are only
2248 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002249 file_priv = list_first_entry(&tty->tty_files,
2250 struct tty_file_private, list);
2251 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002252 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002253 if (inode_has_perm_noadp(cred, inode,
2254 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002255 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002258 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002259 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002261 /* Reset controlling tty. */
2262 if (drop_tty)
2263 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 /* Revalidate access to inherited open files. */
2266
Eric Parisf48b7392011-04-25 12:54:27 -04002267 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002268 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 spin_lock(&files->file_lock);
2271 for (;;) {
2272 unsigned long set, i;
2273 int fd;
2274
2275 j++;
Josh Boyer27cd8f52012-07-25 10:40:34 -04002276 i = j * BITS_PER_LONG;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002277 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002278 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002280 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!set)
2282 continue;
2283 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002284 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (set & 1) {
2286 file = fget(i);
2287 if (!file)
2288 continue;
David Howells88e67f32008-11-14 10:39:21 +11002289 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 file,
2291 file_to_av(file))) {
2292 sys_close(i);
2293 fd = get_unused_fd();
2294 if (fd != i) {
2295 if (fd >= 0)
2296 put_unused_fd(fd);
2297 fput(file);
2298 continue;
2299 }
2300 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002301 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 } else {
David Howells745ca242008-11-14 10:39:22 +11002303 devnull = dentry_open(
2304 dget(selinux_null),
2305 mntget(selinuxfs_mount),
2306 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002307 if (IS_ERR(devnull)) {
2308 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 put_unused_fd(fd);
2310 fput(file);
2311 continue;
2312 }
2313 }
2314 fd_install(fd, devnull);
2315 }
2316 fput(file);
2317 }
2318 }
2319 spin_lock(&files->file_lock);
2320
2321 }
2322 spin_unlock(&files->file_lock);
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/*
David Howellsa6f76f22008-11-14 10:39:24 +11002326 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 */
David Howellsa6f76f22008-11-14 10:39:24 +11002328static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
David Howellsa6f76f22008-11-14 10:39:24 +11002330 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 int rc, i;
2333
David Howellsa6f76f22008-11-14 10:39:24 +11002334 new_tsec = bprm->cred->security;
2335 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return;
2337
2338 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002339 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
David Howellsa6f76f22008-11-14 10:39:24 +11002341 /* Always clear parent death signal on SID transitions. */
2342 current->pdeath_signal = 0;
2343
2344 /* Check whether the new SID can inherit resource limits from the old
2345 * SID. If not, reset all soft limits to the lower of the current
2346 * task's hard limit and the init task's soft limit.
2347 *
2348 * Note that the setting of hard limits (even to lower them) can be
2349 * controlled by the setrlimit check. The inclusion of the init task's
2350 * soft limit into the computation is to avoid resetting soft limits
2351 * higher than the default soft limit for cases where the default is
2352 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2353 */
2354 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2355 PROCESS__RLIMITINH, NULL);
2356 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002357 /* protect against do_prlimit() */
2358 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002359 for (i = 0; i < RLIM_NLIMITS; i++) {
2360 rlim = current->signal->rlim + i;
2361 initrlim = init_task.signal->rlim + i;
2362 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2363 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002364 task_unlock(current);
2365 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002366 }
2367}
2368
2369/*
2370 * Clean up the process immediately after the installation of new credentials
2371 * due to exec
2372 */
2373static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2374{
2375 const struct task_security_struct *tsec = current_security();
2376 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002377 u32 osid, sid;
2378 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002379
David Howellsa6f76f22008-11-14 10:39:24 +11002380 osid = tsec->osid;
2381 sid = tsec->sid;
2382
2383 if (sid == osid)
2384 return;
2385
2386 /* Check whether the new SID can inherit signal state from the old SID.
2387 * If not, clear itimers to avoid subsequent signal generation and
2388 * flush and unblock signals.
2389 *
2390 * This must occur _after_ the task SID has been updated so that any
2391 * kill done after the flush will be checked against the new SID.
2392 */
2393 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (rc) {
2395 memset(&itimer, 0, sizeof itimer);
2396 for (i = 0; i < 3; i++)
2397 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002399 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2400 __flush_signals(current);
2401 flush_signal_handlers(current, 1);
2402 sigemptyset(&current->blocked);
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 spin_unlock_irq(&current->sighand->siglock);
2405 }
2406
David Howellsa6f76f22008-11-14 10:39:24 +11002407 /* Wake up the parent if it is waiting so that it can recheck
2408 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002409 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002410 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002411 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
2414/* superblock security operations */
2415
2416static int selinux_sb_alloc_security(struct super_block *sb)
2417{
2418 return superblock_alloc_security(sb);
2419}
2420
2421static void selinux_sb_free_security(struct super_block *sb)
2422{
2423 superblock_free_security(sb);
2424}
2425
2426static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2427{
2428 if (plen > olen)
2429 return 0;
2430
2431 return !memcmp(prefix, option, plen);
2432}
2433
2434static inline int selinux_option(char *option, int len)
2435{
Eric Paris832cbd92008-04-01 13:24:09 -04002436 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2437 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2438 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002439 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2440 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
2443static inline void take_option(char **to, char *from, int *first, int len)
2444{
2445 if (!*first) {
2446 **to = ',';
2447 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002448 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 *first = 0;
2450 memcpy(*to, from, len);
2451 *to += len;
2452}
2453
Eric Paris828dfe12008-04-17 13:17:49 -04002454static inline void take_selinux_option(char **to, char *from, int *first,
2455 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002456{
2457 int current_size = 0;
2458
2459 if (!*first) {
2460 **to = '|';
2461 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002462 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002463 *first = 0;
2464
2465 while (current_size < len) {
2466 if (*from != '"') {
2467 **to = *from;
2468 *to += 1;
2469 }
2470 from += 1;
2471 current_size += 1;
2472 }
2473}
2474
Eric Parise0007522008-03-05 10:31:54 -05002475static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 int fnosec, fsec, rc = 0;
2478 char *in_save, *in_curr, *in_end;
2479 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002480 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 in_curr = orig;
2483 sec_curr = copy;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2486 if (!nosec) {
2487 rc = -ENOMEM;
2488 goto out;
2489 }
2490
2491 nosec_save = nosec;
2492 fnosec = fsec = 1;
2493 in_save = in_end = orig;
2494
2495 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002496 if (*in_end == '"')
2497 open_quote = !open_quote;
2498 if ((*in_end == ',' && open_quote == 0) ||
2499 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 int len = in_end - in_curr;
2501
2502 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002503 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 else
2505 take_option(&nosec, in_curr, &fnosec, len);
2506
2507 in_curr = in_end + 1;
2508 }
2509 } while (*in_end++);
2510
Eric Paris6931dfc2005-06-30 02:58:51 -07002511 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002512 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513out:
2514 return rc;
2515}
2516
Eric Paris026eb162011-03-03 16:09:14 -05002517static int selinux_sb_remount(struct super_block *sb, void *data)
2518{
2519 int rc, i, *flags;
2520 struct security_mnt_opts opts;
2521 char *secdata, **mount_options;
2522 struct superblock_security_struct *sbsec = sb->s_security;
2523
2524 if (!(sbsec->flags & SE_SBINITIALIZED))
2525 return 0;
2526
2527 if (!data)
2528 return 0;
2529
2530 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2531 return 0;
2532
2533 security_init_mnt_opts(&opts);
2534 secdata = alloc_secdata();
2535 if (!secdata)
2536 return -ENOMEM;
2537 rc = selinux_sb_copy_data(data, secdata);
2538 if (rc)
2539 goto out_free_secdata;
2540
2541 rc = selinux_parse_opts_str(secdata, &opts);
2542 if (rc)
2543 goto out_free_secdata;
2544
2545 mount_options = opts.mnt_opts;
2546 flags = opts.mnt_opts_flags;
2547
2548 for (i = 0; i < opts.num_mnt_opts; i++) {
2549 u32 sid;
2550 size_t len;
2551
2552 if (flags[i] == SE_SBLABELSUPP)
2553 continue;
2554 len = strlen(mount_options[i]);
2555 rc = security_context_to_sid(mount_options[i], len, &sid);
2556 if (rc) {
2557 printk(KERN_WARNING "SELinux: security_context_to_sid"
2558 "(%s) failed for (dev %s, type %s) errno=%d\n",
2559 mount_options[i], sb->s_id, sb->s_type->name, rc);
2560 goto out_free_opts;
2561 }
2562 rc = -EINVAL;
2563 switch (flags[i]) {
2564 case FSCONTEXT_MNT:
2565 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2566 goto out_bad_option;
2567 break;
2568 case CONTEXT_MNT:
2569 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2570 goto out_bad_option;
2571 break;
2572 case ROOTCONTEXT_MNT: {
2573 struct inode_security_struct *root_isec;
2574 root_isec = sb->s_root->d_inode->i_security;
2575
2576 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2577 goto out_bad_option;
2578 break;
2579 }
2580 case DEFCONTEXT_MNT:
2581 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2582 goto out_bad_option;
2583 break;
2584 default:
2585 goto out_free_opts;
2586 }
2587 }
2588
2589 rc = 0;
2590out_free_opts:
2591 security_free_mnt_opts(&opts);
2592out_free_secdata:
2593 free_secdata(secdata);
2594 return rc;
2595out_bad_option:
2596 printk(KERN_WARNING "SELinux: unable to change security options "
2597 "during remount (dev %s, type=%s)\n", sb->s_id,
2598 sb->s_type->name);
2599 goto out_free_opts;
2600}
2601
James Morris12204e22008-12-19 10:44:42 +11002602static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
David Howells88e67f32008-11-14 10:39:21 +11002604 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002605 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002606 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 int rc;
2608
2609 rc = superblock_doinit(sb, data);
2610 if (rc)
2611 return rc;
2612
James Morris74192242008-12-19 11:41:10 +11002613 /* Allow all mounts performed by the kernel */
2614 if (flags & MS_KERNMOUNT)
2615 return 0;
2616
Eric Parisa2694342011-04-25 13:10:27 -04002617 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002618 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002619 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002620 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
David Howells726c3342006-06-23 02:02:58 -07002623static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
David Howells88e67f32008-11-14 10:39:21 +11002625 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002626 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002627 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Eric Parisa2694342011-04-25 13:10:27 -04002629 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002630 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002631 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002632 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Eric Paris828dfe12008-04-17 13:17:49 -04002635static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002636 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002637 char *type,
2638 unsigned long flags,
2639 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
David Howells88e67f32008-11-14 10:39:21 +11002641 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002644 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002645 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 else
Eric Paris2875fa02011-04-28 16:04:24 -04002647 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650static int selinux_umount(struct vfsmount *mnt, int flags)
2651{
David Howells88e67f32008-11-14 10:39:21 +11002652 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
David Howells88e67f32008-11-14 10:39:21 +11002654 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002655 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/* inode security operations */
2659
2660static int selinux_inode_alloc_security(struct inode *inode)
2661{
2662 return inode_alloc_security(inode);
2663}
2664
2665static void selinux_inode_free_security(struct inode *inode)
2666{
2667 inode_free_security(inode);
2668}
2669
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002670static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002671 const struct qstr *qstr, char **name,
2672 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002673{
Paul Moore5fb49872010-04-22 14:46:19 -04002674 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002675 struct inode_security_struct *dsec;
2676 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002677 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002678 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002679 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002680
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002681 dsec = dir->i_security;
2682 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002683
David Howells275bb412008-11-14 10:39:19 +11002684 sid = tsec->sid;
2685 newsid = tsec->create_sid;
2686
Eric Paris415103f2010-12-02 16:13:40 -05002687 if ((sbsec->flags & SE_SBINITIALIZED) &&
2688 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2689 newsid = sbsec->mntpoint_sid;
2690 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002691 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002692 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002693 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002694 if (rc) {
2695 printk(KERN_WARNING "%s: "
2696 "security_transition_sid failed, rc=%d (dev=%s "
2697 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002698 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002699 -rc, inode->i_sb->s_id, inode->i_ino);
2700 return rc;
2701 }
2702 }
2703
Eric Paris296fddf2006-09-25 23:32:00 -07002704 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002705 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002706 struct inode_security_struct *isec = inode->i_security;
2707 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2708 isec->sid = newsid;
2709 isec->initialized = 1;
2710 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002711
David P. Quigleycd895962009-01-16 09:22:04 -05002712 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002713 return -EOPNOTSUPP;
2714
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002715 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002716 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002717 if (!namep)
2718 return -ENOMEM;
2719 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002720 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002721
2722 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002723 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002724 if (rc) {
2725 kfree(namep);
2726 return rc;
2727 }
2728 *value = context;
2729 *len = clen;
2730 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002731
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002732 return 0;
2733}
2734
Al Viro4acdaf22011-07-26 01:42:34 -04002735static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 return may_create(dir, dentry, SECCLASS_FILE);
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return may_link(dir, old_dentry, MAY_LINK);
2743}
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return may_link(dir, dentry, MAY_UNLINK);
2748}
2749
2750static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2751{
2752 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2753}
2754
Al Viro18bb1db2011-07-26 01:41:39 -04002755static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 return may_create(dir, dentry, SECCLASS_DIR);
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2761{
2762 return may_link(dir, dentry, MAY_RMDIR);
2763}
2764
Al Viro1a67aaf2011-07-26 01:52:52 -04002765static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002771 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776static int selinux_inode_readlink(struct dentry *dentry)
2777{
David Howells88e67f32008-11-14 10:39:21 +11002778 const struct cred *cred = current_cred();
2779
Eric Paris2875fa02011-04-28 16:04:24 -04002780 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2784{
David Howells88e67f32008-11-14 10:39:21 +11002785 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Eric Paris2875fa02011-04-28 16:04:24 -04002787 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
Al Viroe74f71e2011-06-20 19:38:15 -04002790static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
David Howells88e67f32008-11-14 10:39:21 +11002792 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002793 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002794 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002795 u32 perms;
2796 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002797 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Eric Parisb782e0a2010-07-23 11:44:03 -04002799 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002800 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2801
Eric Parisb782e0a2010-07-23 11:44:03 -04002802 /* No permission to check. Existence test. */
2803 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Eric Parisf48b7392011-04-25 12:54:27 -04002806 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002807 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002808 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002809
2810 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002811 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002812
2813 perms = file_mask_to_av(inode->i_mode, mask);
2814
Eric Paris9ade0cf2011-04-25 16:26:29 -04002815 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2819{
David Howells88e67f32008-11-14 10:39:21 +11002820 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002821 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002823 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2824 if (ia_valid & ATTR_FORCE) {
2825 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2826 ATTR_FORCE);
2827 if (!ia_valid)
2828 return 0;
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002831 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2832 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002833 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Eric Paris2875fa02011-04-28 16:04:24 -04002835 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2839{
David Howells88e67f32008-11-14 10:39:21 +11002840 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002841 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002842
Eric Paris2875fa02011-04-28 16:04:24 -04002843 path.dentry = dentry;
2844 path.mnt = mnt;
2845
2846 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
David Howells8f0cfa52008-04-29 00:59:41 -07002849static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002850{
David Howells88e67f32008-11-14 10:39:21 +11002851 const struct cred *cred = current_cred();
2852
Serge E. Hallynb5376772007-10-16 23:31:36 -07002853 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2854 sizeof XATTR_SECURITY_PREFIX - 1)) {
2855 if (!strcmp(name, XATTR_NAME_CAPS)) {
2856 if (!capable(CAP_SETFCAP))
2857 return -EPERM;
2858 } else if (!capable(CAP_SYS_ADMIN)) {
2859 /* A different attribute in the security namespace.
2860 Restrict to administrator. */
2861 return -EPERM;
2862 }
2863 }
2864
2865 /* Not an attribute we recognize, so just check the
2866 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002867 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002868}
2869
David Howells8f0cfa52008-04-29 00:59:41 -07002870static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2871 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct inode *inode = dentry->d_inode;
2874 struct inode_security_struct *isec = inode->i_security;
2875 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002876 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002877 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002878 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 int rc = 0;
2880
Serge E. Hallynb5376772007-10-16 23:31:36 -07002881 if (strcmp(name, XATTR_NAME_SELINUX))
2882 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002885 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -EOPNOTSUPP;
2887
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002888 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return -EPERM;
2890
Eric Parisa2694342011-04-25 13:10:27 -04002891 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002892 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002893 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
David Howells275bb412008-11-14 10:39:19 +11002895 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 FILE__RELABELFROM, &ad);
2897 if (rc)
2898 return rc;
2899
2900 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002901 if (rc == -EINVAL) {
2902 if (!capable(CAP_MAC_ADMIN))
2903 return rc;
2904 rc = security_context_to_sid_force(value, size, &newsid);
2905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (rc)
2907 return rc;
2908
David Howells275bb412008-11-14 10:39:19 +11002909 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 FILE__RELABELTO, &ad);
2911 if (rc)
2912 return rc;
2913
David Howells275bb412008-11-14 10:39:19 +11002914 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002915 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (rc)
2917 return rc;
2918
2919 return avc_has_perm(newsid,
2920 sbsec->sid,
2921 SECCLASS_FILESYSTEM,
2922 FILESYSTEM__ASSOCIATE,
2923 &ad);
2924}
2925
David Howells8f0cfa52008-04-29 00:59:41 -07002926static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002927 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002928 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 struct inode *inode = dentry->d_inode;
2931 struct inode_security_struct *isec = inode->i_security;
2932 u32 newsid;
2933 int rc;
2934
2935 if (strcmp(name, XATTR_NAME_SELINUX)) {
2936 /* Not an attribute we recognize, so nothing to do. */
2937 return;
2938 }
2939
Stephen Smalley12b29f32008-05-07 13:03:20 -04002940 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002942 printk(KERN_ERR "SELinux: unable to map context to SID"
2943 "for (%s, %lu), rc=%d\n",
2944 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return;
2946 }
2947
2948 isec->sid = newsid;
2949 return;
2950}
2951
David Howells8f0cfa52008-04-29 00:59:41 -07002952static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
David Howells88e67f32008-11-14 10:39:21 +11002954 const struct cred *cred = current_cred();
2955
Eric Paris2875fa02011-04-28 16:04:24 -04002956 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
Eric Paris828dfe12008-04-17 13:17:49 -04002959static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
David Howells88e67f32008-11-14 10:39:21 +11002961 const struct cred *cred = current_cred();
2962
Eric Paris2875fa02011-04-28 16:04:24 -04002963 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
David Howells8f0cfa52008-04-29 00:59:41 -07002966static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002968 if (strcmp(name, XATTR_NAME_SELINUX))
2969 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 /* No one is allowed to remove a SELinux security label.
2972 You can change the label, but all data must be labeled. */
2973 return -EACCES;
2974}
2975
James Morrisd381d8a2005-10-30 14:59:22 -08002976/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002977 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002978 *
2979 * Permission check is handled by selinux_inode_getxattr hook.
2980 */
David P. Quigley42492592008-02-04 22:29:39 -08002981static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
David P. Quigley42492592008-02-04 22:29:39 -08002983 u32 size;
2984 int error;
2985 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002988 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2989 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002991 /*
2992 * If the caller has CAP_MAC_ADMIN, then get the raw context
2993 * value even if it is not defined by current policy; otherwise,
2994 * use the in-core value under current policy.
2995 * Use the non-auditing forms of the permission checks since
2996 * getxattr may be called by unprivileged processes commonly
2997 * and lack of permission just means that we fall back to the
2998 * in-core context value, not a denial.
2999 */
Eric Paris6a9de492012-01-03 12:25:14 -05003000 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00003001 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003002 if (!error)
3003 error = security_sid_to_context_force(isec->sid, &context,
3004 &size);
3005 else
3006 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003007 if (error)
3008 return error;
3009 error = size;
3010 if (alloc) {
3011 *buffer = context;
3012 goto out_nofree;
3013 }
3014 kfree(context);
3015out_nofree:
3016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
3019static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003020 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 struct inode_security_struct *isec = inode->i_security;
3023 u32 newsid;
3024 int rc;
3025
3026 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3027 return -EOPNOTSUPP;
3028
3029 if (!value || !size)
3030 return -EACCES;
3031
Eric Paris828dfe12008-04-17 13:17:49 -04003032 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (rc)
3034 return rc;
3035
3036 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003037 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
3039}
3040
3041static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3042{
3043 const int len = sizeof(XATTR_NAME_SELINUX);
3044 if (buffer && len <= buffer_size)
3045 memcpy(buffer, XATTR_NAME_SELINUX, len);
3046 return len;
3047}
3048
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003049static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3050{
3051 struct inode_security_struct *isec = inode->i_security;
3052 *secid = isec->sid;
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055/* file security operations */
3056
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003057static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
David Howells88e67f32008-11-14 10:39:21 +11003059 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003060 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3063 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3064 mask |= MAY_APPEND;
3065
Paul Moore389fb802009-03-27 17:10:34 -04003066 return file_has_perm(cred, file,
3067 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003070static int selinux_file_permission(struct file *file, int mask)
3071{
Stephen Smalley20dda182009-06-22 14:54:53 -04003072 struct inode *inode = file->f_path.dentry->d_inode;
3073 struct file_security_struct *fsec = file->f_security;
3074 struct inode_security_struct *isec = inode->i_security;
3075 u32 sid = current_sid();
3076
Paul Moore389fb802009-03-27 17:10:34 -04003077 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003078 /* No permission to check. Existence test. */
3079 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003080
Stephen Smalley20dda182009-06-22 14:54:53 -04003081 if (sid == fsec->sid && fsec->isid == isec->sid &&
3082 fsec->pseqno == avc_policy_seqno())
3083 /* No change since dentry_open check. */
3084 return 0;
3085
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003086 return selinux_revalidate_file_permission(file, mask);
3087}
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089static int selinux_file_alloc_security(struct file *file)
3090{
3091 return file_alloc_security(file);
3092}
3093
3094static void selinux_file_free_security(struct file *file)
3095{
3096 file_free_security(file);
3097}
3098
Jeff Vander Stoepf3e85342015-04-08 11:27:46 -07003099/*
3100 * Check whether a task has the ioctl permission and cmd
3101 * operation to an inode.
3102 */
3103int ioctl_has_perm(const struct cred *cred, struct file *file,
3104 u32 requested, u16 cmd)
3105{
3106 struct common_audit_data ad;
3107 struct file_security_struct *fsec = file->f_security;
3108 struct inode *inode = file->f_path.dentry->d_inode;
3109 struct inode_security_struct *isec = inode->i_security;
3110 struct lsm_ioctlop_audit ioctl;
3111 u32 ssid = cred_sid(cred);
3112 struct selinux_audit_data sad = {0,};
3113 int rc;
3114
3115 COMMON_AUDIT_DATA_INIT(&ad, IOCTL_OP);
3116 ad.u.op = &ioctl;
3117 ad.u.op->cmd = cmd;
3118 ad.u.op->path = file->f_path;
3119 ad.selinux_audit_data = &sad;
3120
3121 if (ssid != fsec->sid) {
3122 rc = avc_has_perm(ssid, fsec->sid,
3123 SECCLASS_FD,
3124 FD__USE,
3125 &ad);
3126 if (rc)
3127 goto out;
3128 }
3129
3130 if (unlikely(IS_PRIVATE(inode)))
3131 return 0;
3132
3133 rc = avc_has_operation(ssid, isec->sid, isec->sclass,
3134 requested, cmd, &ad);
3135out:
3136 return rc;
3137}
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3140 unsigned long arg)
3141{
David Howells88e67f32008-11-14 10:39:21 +11003142 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003143 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Eric Paris0b24dcb2011-02-25 15:39:20 -05003145 switch (cmd) {
3146 case FIONREAD:
3147 /* fall through */
3148 case FIBMAP:
3149 /* fall through */
3150 case FIGETBSZ:
3151 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003152 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003153 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003154 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003155 error = file_has_perm(cred, file, FILE__GETATTR);
3156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Al Viro2f99c362012-03-23 16:04:05 -04003158 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003159 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003160 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003161 error = file_has_perm(cred, file, FILE__SETATTR);
3162 break;
3163
3164 /* sys_ioctl() checks */
3165 case FIONBIO:
3166 /* fall through */
3167 case FIOASYNC:
3168 error = file_has_perm(cred, file, 0);
3169 break;
3170
3171 case KDSKBENT:
3172 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003173 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3174 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003175 break;
3176
3177 /* default case assumes that the command will go
3178 * to the file's ioctl() function.
3179 */
3180 default:
Jeff Vander Stoepf3e85342015-04-08 11:27:46 -07003181 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003182 }
3183 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003186static int default_noexec;
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3189{
David Howells88e67f32008-11-14 10:39:21 +11003190 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003191 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003192
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003193 if (default_noexec &&
3194 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 /*
3196 * We are making executable an anonymous mapping or a
3197 * private file mapping that will also be writable.
3198 * This has an additional check.
3199 */
David Howellsd84f4f92008-11-14 10:39:23 +11003200 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003202 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 if (file) {
3206 /* read access is always possible with a mapping */
3207 u32 av = FILE__READ;
3208
3209 /* write access only matters if the mapping is shared */
3210 if (shared && (prot & PROT_WRITE))
3211 av |= FILE__WRITE;
3212
3213 if (prot & PROT_EXEC)
3214 av |= FILE__EXECUTE;
3215
David Howells88e67f32008-11-14 10:39:21 +11003216 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
David Howellsd84f4f92008-11-14 10:39:23 +11003218
3219error:
3220 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
3223static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003224 unsigned long prot, unsigned long flags,
3225 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Eric Parised032182007-06-28 15:55:21 -04003227 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003228 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Eric Paris84336d1a2009-07-31 12:54:05 -04003230 /*
3231 * notice that we are intentionally putting the SELinux check before
3232 * the secondary cap_file_mmap check. This is such a likely attempt
3233 * at bad behaviour/exploit that we always want to get the AVC, even
3234 * if DAC would have also denied the operation.
3235 */
Eric Parisa2551df2009-07-31 12:54:11 -04003236 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003237 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3238 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003239 if (rc)
3240 return rc;
3241 }
3242
3243 /* do DAC check on address space usage */
3244 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003245 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return rc;
3247
3248 if (selinux_checkreqprot)
3249 prot = reqprot;
3250
3251 return file_map_prot_check(file, prot,
3252 (flags & MAP_TYPE) == MAP_SHARED);
3253}
3254
3255static int selinux_file_mprotect(struct vm_area_struct *vma,
3256 unsigned long reqprot,
3257 unsigned long prot)
3258{
David Howells88e67f32008-11-14 10:39:21 +11003259 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 if (selinux_checkreqprot)
3262 prot = reqprot;
3263
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003264 if (default_noexec &&
3265 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003266 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003267 if (vma->vm_start >= vma->vm_mm->start_brk &&
3268 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003269 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003270 } else if (!vma->vm_file &&
3271 vma->vm_start <= vma->vm_mm->start_stack &&
3272 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003273 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003274 } else if (vma->vm_file && vma->anon_vma) {
3275 /*
3276 * We are making executable a file mapping that has
3277 * had some COW done. Since pages might have been
3278 * written, check ability to execute the possibly
3279 * modified content. This typically should only
3280 * occur for text relocations.
3281 */
David Howellsd84f4f92008-11-14 10:39:23 +11003282 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003283 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003284 if (rc)
3285 return rc;
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3289}
3290
3291static int selinux_file_lock(struct file *file, unsigned int cmd)
3292{
David Howells88e67f32008-11-14 10:39:21 +11003293 const struct cred *cred = current_cred();
3294
3295 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
3298static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3299 unsigned long arg)
3300{
David Howells88e67f32008-11-14 10:39:21 +11003301 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int err = 0;
3303
3304 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003305 case F_SETFL:
3306 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3307 err = -EINVAL;
3308 break;
3309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Eric Paris828dfe12008-04-17 13:17:49 -04003311 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003312 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003314 }
3315 /* fall through */
3316 case F_SETOWN:
3317 case F_SETSIG:
3318 case F_GETFL:
3319 case F_GETOWN:
3320 case F_GETSIG:
3321 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003322 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003323 break;
3324 case F_GETLK:
3325 case F_SETLK:
3326 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003328 case F_GETLK64:
3329 case F_SETLK64:
3330 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003332 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3333 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003335 }
David Howells88e67f32008-11-14 10:39:21 +11003336 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
3339
3340 return err;
3341}
3342
3343static int selinux_file_set_fowner(struct file *file)
3344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct file_security_struct *fsec;
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003348 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 return 0;
3351}
3352
3353static int selinux_file_send_sigiotask(struct task_struct *tsk,
3354 struct fown_struct *fown, int signum)
3355{
Eric Paris828dfe12008-04-17 13:17:49 -04003356 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003357 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 struct file_security_struct *fsec;
3360
3361 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003362 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 fsec = file->f_security;
3365
3366 if (!signum)
3367 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3368 else
3369 perm = signal_to_av(signum);
3370
David Howells275bb412008-11-14 10:39:19 +11003371 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 SECCLASS_PROCESS, perm, NULL);
3373}
3374
3375static int selinux_file_receive(struct file *file)
3376{
David Howells88e67f32008-11-14 10:39:21 +11003377 const struct cred *cred = current_cred();
3378
3379 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
David Howells745ca242008-11-14 10:39:22 +11003382static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003383{
3384 struct file_security_struct *fsec;
3385 struct inode *inode;
3386 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003387
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003388 inode = file->f_path.dentry->d_inode;
3389 fsec = file->f_security;
3390 isec = inode->i_security;
3391 /*
3392 * Save inode label and policy sequence number
3393 * at open-time so that selinux_file_permission
3394 * can determine whether revalidation is necessary.
3395 * Task label is already saved in the file security
3396 * struct as its SID.
3397 */
3398 fsec->isid = isec->sid;
3399 fsec->pseqno = avc_policy_seqno();
3400 /*
3401 * Since the inode label or policy seqno may have changed
3402 * between the selinux_inode_permission check and the saving
3403 * of state above, recheck that access is still permitted.
3404 * Otherwise, access might never be revalidated against the
3405 * new inode label or new policy.
3406 * This check is not redundant - do not remove.
3407 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003408 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003409}
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411/* task security operations */
3412
3413static int selinux_task_create(unsigned long clone_flags)
3414{
David Howells3b11a1d2008-11-14 10:39:26 +11003415 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
David Howellsf1752ee2008-11-14 10:39:17 +11003418/*
David Howellsee18d642009-09-02 09:14:21 +01003419 * allocate the SELinux part of blank credentials
3420 */
3421static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3422{
3423 struct task_security_struct *tsec;
3424
3425 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3426 if (!tsec)
3427 return -ENOMEM;
3428
3429 cred->security = tsec;
3430 return 0;
3431}
3432
3433/*
David Howellsf1752ee2008-11-14 10:39:17 +11003434 * detach and free the LSM part of a set of credentials
3435 */
3436static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
David Howellsf1752ee2008-11-14 10:39:17 +11003438 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003439
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003440 /*
3441 * cred->security == NULL if security_cred_alloc_blank() or
3442 * security_prepare_creds() returned an error.
3443 */
3444 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003445 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003446 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
David Howellsd84f4f92008-11-14 10:39:23 +11003449/*
3450 * prepare a new set of credentials for modification
3451 */
3452static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3453 gfp_t gfp)
3454{
3455 const struct task_security_struct *old_tsec;
3456 struct task_security_struct *tsec;
3457
3458 old_tsec = old->security;
3459
3460 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3461 if (!tsec)
3462 return -ENOMEM;
3463
3464 new->security = tsec;
3465 return 0;
3466}
3467
3468/*
David Howellsee18d642009-09-02 09:14:21 +01003469 * transfer the SELinux data to a blank set of creds
3470 */
3471static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3472{
3473 const struct task_security_struct *old_tsec = old->security;
3474 struct task_security_struct *tsec = new->security;
3475
3476 *tsec = *old_tsec;
3477}
3478
3479/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003480 * set the security data for a kernel service
3481 * - all the creation contexts are set to unlabelled
3482 */
3483static int selinux_kernel_act_as(struct cred *new, u32 secid)
3484{
3485 struct task_security_struct *tsec = new->security;
3486 u32 sid = current_sid();
3487 int ret;
3488
3489 ret = avc_has_perm(sid, secid,
3490 SECCLASS_KERNEL_SERVICE,
3491 KERNEL_SERVICE__USE_AS_OVERRIDE,
3492 NULL);
3493 if (ret == 0) {
3494 tsec->sid = secid;
3495 tsec->create_sid = 0;
3496 tsec->keycreate_sid = 0;
3497 tsec->sockcreate_sid = 0;
3498 }
3499 return ret;
3500}
3501
3502/*
3503 * set the file creation context in a security record to the same as the
3504 * objective context of the specified inode
3505 */
3506static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3507{
3508 struct inode_security_struct *isec = inode->i_security;
3509 struct task_security_struct *tsec = new->security;
3510 u32 sid = current_sid();
3511 int ret;
3512
3513 ret = avc_has_perm(sid, isec->sid,
3514 SECCLASS_KERNEL_SERVICE,
3515 KERNEL_SERVICE__CREATE_FILES_AS,
3516 NULL);
3517
3518 if (ret == 0)
3519 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003520 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003521}
3522
Eric Parisdd8dbf22009-11-03 16:35:32 +11003523static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003524{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003525 u32 sid;
3526 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003527 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003528
3529 sid = task_sid(current);
3530
3531 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003532 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003533 ad.u.kmod_name = kmod_name;
3534
3535 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3536 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003537}
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3540{
David Howells3b11a1d2008-11-14 10:39:26 +11003541 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
3544static int selinux_task_getpgid(struct task_struct *p)
3545{
David Howells3b11a1d2008-11-14 10:39:26 +11003546 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
3549static int selinux_task_getsid(struct task_struct *p)
3550{
David Howells3b11a1d2008-11-14 10:39:26 +11003551 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
David Quigleyf9008e42006-06-30 01:55:46 -07003554static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3555{
David Howells275bb412008-11-14 10:39:19 +11003556 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003557}
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559static int selinux_task_setnice(struct task_struct *p, int nice)
3560{
3561 int rc;
3562
Eric Paris200ac532009-02-12 15:01:04 -05003563 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 if (rc)
3565 return rc;
3566
David Howells3b11a1d2008-11-14 10:39:26 +11003567 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
James Morris03e68062006-06-23 02:03:58 -07003570static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3571{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003572 int rc;
3573
Eric Paris200ac532009-02-12 15:01:04 -05003574 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003575 if (rc)
3576 return rc;
3577
David Howells3b11a1d2008-11-14 10:39:26 +11003578 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003579}
3580
David Quigleya1836a42006-06-30 01:55:49 -07003581static int selinux_task_getioprio(struct task_struct *p)
3582{
David Howells3b11a1d2008-11-14 10:39:26 +11003583 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003584}
3585
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003586static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3587 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003589 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 /* Control the ability to change the hard limit (whether
3592 lowering or raising it), so that the hard limit can
3593 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003594 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003596 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 return 0;
3599}
3600
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003601static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003603 int rc;
3604
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003605 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003606 if (rc)
3607 return rc;
3608
David Howells3b11a1d2008-11-14 10:39:26 +11003609 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
3612static int selinux_task_getscheduler(struct task_struct *p)
3613{
David Howells3b11a1d2008-11-14 10:39:26 +11003614 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
David Quigley35601542006-06-23 02:04:01 -07003617static int selinux_task_movememory(struct task_struct *p)
3618{
David Howells3b11a1d2008-11-14 10:39:26 +11003619 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003620}
3621
David Quigleyf9008e42006-06-30 01:55:46 -07003622static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3623 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 u32 perm;
3626 int rc;
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 if (!sig)
3629 perm = PROCESS__SIGNULL; /* null signal; existence test */
3630 else
3631 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003632 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003633 rc = avc_has_perm(secid, task_sid(p),
3634 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003635 else
David Howells3b11a1d2008-11-14 10:39:26 +11003636 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003637 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640static int selinux_task_wait(struct task_struct *p)
3641{
Eric Paris8a535142007-10-22 16:10:31 -04003642 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645static void selinux_task_to_inode(struct task_struct *p,
3646 struct inode *inode)
3647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003649 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
David Howells275bb412008-11-14 10:39:19 +11003651 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653}
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003656static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003657 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
3659 int offset, ihlen, ret = -EINVAL;
3660 struct iphdr _iph, *ih;
3661
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003662 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3664 if (ih == NULL)
3665 goto out;
3666
3667 ihlen = ih->ihl * 4;
3668 if (ihlen < sizeof(_iph))
3669 goto out;
3670
Eric Paris48c62af2012-04-02 13:15:44 -04003671 ad->u.net->v4info.saddr = ih->saddr;
3672 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 ret = 0;
3674
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003675 if (proto)
3676 *proto = ih->protocol;
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003679 case IPPROTO_TCP: {
3680 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Eric Paris828dfe12008-04-17 13:17:49 -04003682 if (ntohs(ih->frag_off) & IP_OFFSET)
3683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685 offset += ihlen;
3686 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3687 if (th == NULL)
3688 break;
3689
Eric Paris48c62af2012-04-02 13:15:44 -04003690 ad->u.net->sport = th->source;
3691 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
Eric Paris828dfe12008-04-17 13:17:49 -04003695 case IPPROTO_UDP: {
3696 struct udphdr _udph, *uh;
3697
3698 if (ntohs(ih->frag_off) & IP_OFFSET)
3699 break;
3700
3701 offset += ihlen;
3702 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3703 if (uh == NULL)
3704 break;
3705
Eric Paris48c62af2012-04-02 13:15:44 -04003706 ad->u.net->sport = uh->source;
3707 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003708 break;
3709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
James Morris2ee92d42006-11-13 16:09:01 -08003711 case IPPROTO_DCCP: {
3712 struct dccp_hdr _dccph, *dh;
3713
3714 if (ntohs(ih->frag_off) & IP_OFFSET)
3715 break;
3716
3717 offset += ihlen;
3718 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3719 if (dh == NULL)
3720 break;
3721
Eric Paris48c62af2012-04-02 13:15:44 -04003722 ad->u.net->sport = dh->dccph_sport;
3723 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003724 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003725 }
James Morris2ee92d42006-11-13 16:09:01 -08003726
Eric Paris828dfe12008-04-17 13:17:49 -04003727 default:
3728 break;
3729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730out:
3731 return ret;
3732}
3733
3734#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3735
3736/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003737static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003738 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
3740 u8 nexthdr;
3741 int ret = -EINVAL, offset;
3742 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003743 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003745 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3747 if (ip6 == NULL)
3748 goto out;
3749
Eric Paris48c62af2012-04-02 13:15:44 -04003750 ad->u.net->v6info.saddr = ip6->saddr;
3751 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 ret = 0;
3753
3754 nexthdr = ip6->nexthdr;
3755 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003756 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (offset < 0)
3758 goto out;
3759
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003760 if (proto)
3761 *proto = nexthdr;
3762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 switch (nexthdr) {
3764 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003765 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3768 if (th == NULL)
3769 break;
3770
Eric Paris48c62af2012-04-02 13:15:44 -04003771 ad->u.net->sport = th->source;
3772 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 break;
3774 }
3775
3776 case IPPROTO_UDP: {
3777 struct udphdr _udph, *uh;
3778
3779 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3780 if (uh == NULL)
3781 break;
3782
Eric Paris48c62af2012-04-02 13:15:44 -04003783 ad->u.net->sport = uh->source;
3784 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 break;
3786 }
3787
James Morris2ee92d42006-11-13 16:09:01 -08003788 case IPPROTO_DCCP: {
3789 struct dccp_hdr _dccph, *dh;
3790
3791 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3792 if (dh == NULL)
3793 break;
3794
Eric Paris48c62af2012-04-02 13:15:44 -04003795 ad->u.net->sport = dh->dccph_sport;
3796 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003797 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003798 }
James Morris2ee92d42006-11-13 16:09:01 -08003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 /* includes fragments */
3801 default:
3802 break;
3803 }
3804out:
3805 return ret;
3806}
3807
3808#endif /* IPV6 */
3809
Thomas Liu2bf49692009-07-14 12:14:09 -04003810static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003811 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
David Howellscf9481e2008-07-27 21:31:07 +10003813 char *addrp;
3814 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Eric Paris48c62af2012-04-02 13:15:44 -04003816 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003818 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003819 if (ret)
3820 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003821 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3822 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003823 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3826 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003827 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003828 if (ret)
3829 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003830 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3831 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003832 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833#endif /* IPV6 */
3834 default:
David Howellscf9481e2008-07-27 21:31:07 +10003835 addrp = NULL;
3836 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 }
3838
David Howellscf9481e2008-07-27 21:31:07 +10003839parse_error:
3840 printk(KERN_WARNING
3841 "SELinux: failure in selinux_parse_skb(),"
3842 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003844
3845okay:
3846 if (_addrp)
3847 *_addrp = addrp;
3848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850
Paul Moore4f6a9932007-03-01 14:35:22 -05003851/**
Paul Moore220deb92008-01-29 08:38:23 -05003852 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003853 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003854 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003855 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003856 *
3857 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003858 * Check the various different forms of network peer labeling and determine
3859 * the peer label/SID for the packet; most of the magic actually occurs in
3860 * the security server function security_net_peersid_cmp(). The function
3861 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3862 * or -EACCES if @sid is invalid due to inconsistencies with the different
3863 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003864 *
3865 */
Paul Moore220deb92008-01-29 08:38:23 -05003866static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003867{
Paul Moore71f1cb02008-01-29 08:51:16 -05003868 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003869 u32 xfrm_sid;
3870 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003871 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003872
Paul Moore73ec9552013-12-10 14:57:54 -05003873 selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003874 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003875
Paul Moore71f1cb02008-01-29 08:51:16 -05003876 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3877 if (unlikely(err)) {
3878 printk(KERN_WARNING
3879 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3880 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003881 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003882 }
Paul Moore220deb92008-01-29 08:38:23 -05003883
3884 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003885}
3886
Paul Moore2ea04e52013-12-04 16:10:51 -05003887/**
3888 * selinux_conn_sid - Determine the child socket label for a connection
3889 * @sk_sid: the parent socket's SID
3890 * @skb_sid: the packet's SID
3891 * @conn_sid: the resulting connection SID
3892 *
3893 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3894 * combined with the MLS information from @skb_sid in order to create
3895 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3896 * of @sk_sid. Returns zero on success, negative values on failure.
3897 *
3898 */
3899static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3900{
3901 int err = 0;
3902
3903 if (skb_sid != SECSID_NULL)
3904 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3905 else
3906 *conn_sid = sk_sid;
3907
3908 return err;
3909}
3910
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003912
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003913static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3914 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003915{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003916 if (tsec->sockcreate_sid > SECSID_NULL) {
3917 *socksid = tsec->sockcreate_sid;
3918 return 0;
3919 }
3920
3921 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3922 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003923}
3924
Paul Moore253bfae2010-04-22 14:46:19 -04003925static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Paul Moore253bfae2010-04-22 14:46:19 -04003927 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003928 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003929 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003930 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003931 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Paul Moore253bfae2010-04-22 14:46:19 -04003933 if (sksec->sid == SECINITSID_KERNEL)
3934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Thomas Liu2bf49692009-07-14 12:14:09 -04003936 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003937 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003938 ad.u.net = &net;
3939 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Paul Moore253bfae2010-04-22 14:46:19 -04003941 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
3944static int selinux_socket_create(int family, int type,
3945 int protocol, int kern)
3946{
Paul Moore5fb49872010-04-22 14:46:19 -04003947 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003948 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003949 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003950 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
David Howells275bb412008-11-14 10:39:19 +11003955 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003956 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3957 if (rc)
3958 return rc;
3959
Paul Moored4f2d972010-04-22 14:46:18 -04003960 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003963static int selinux_socket_post_create(struct socket *sock, int family,
3964 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Paul Moore5fb49872010-04-22 14:46:19 -04003966 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003967 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003968 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003969 int err = 0;
3970
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003971 isec->sclass = socket_type_to_security_class(family, type, protocol);
3972
David Howells275bb412008-11-14 10:39:19 +11003973 if (kern)
3974 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003975 else {
3976 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3977 if (err)
3978 return err;
3979 }
David Howells275bb412008-11-14 10:39:19 +11003980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 isec->initialized = 1;
3982
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003983 if (sock->sk) {
3984 sksec = sock->sk->sk_security;
3985 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003986 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003987 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003988 }
3989
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
3993/* Range of port numbers used to automatically bind.
3994 Need to determine whether we should perform a name_bind
3995 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3998{
Paul Moore253bfae2010-04-22 14:46:19 -04003999 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 u16 family;
4001 int err;
4002
Paul Moore253bfae2010-04-22 14:46:19 -04004003 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 if (err)
4005 goto out;
4006
4007 /*
4008 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04004009 * Multiple address binding for SCTP is not supported yet: we just
4010 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 */
Paul Moore253bfae2010-04-22 14:46:19 -04004012 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (family == PF_INET || family == PF_INET6) {
4014 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04004015 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004016 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004017 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004018 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 struct sockaddr_in *addr4 = NULL;
4020 struct sockaddr_in6 *addr6 = NULL;
4021 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004022 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (family == PF_INET) {
4025 addr4 = (struct sockaddr_in *)address;
4026 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 addrp = (char *)&addr4->sin_addr.s_addr;
4028 } else {
4029 addr6 = (struct sockaddr_in6 *)address;
4030 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 addrp = (char *)&addr6->sin6_addr.s6_addr;
4032 }
4033
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004034 if (snum) {
4035 int low, high;
4036
4037 inet_get_local_port_range(&low, &high);
4038
4039 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004040 err = sel_netport_sid(sk->sk_protocol,
4041 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004042 if (err)
4043 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04004044 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004045 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004046 ad.u.net = &net;
4047 ad.u.net->sport = htons(snum);
4048 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004049 err = avc_has_perm(sksec->sid, sid,
4050 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004051 SOCKET__NAME_BIND, &ad);
4052 if (err)
4053 goto out;
4054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 }
Eric Paris828dfe12008-04-17 13:17:49 -04004056
Paul Moore253bfae2010-04-22 14:46:19 -04004057 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004058 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 node_perm = TCP_SOCKET__NODE_BIND;
4060 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004061
James Morris13402582005-09-30 14:24:34 -04004062 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 node_perm = UDP_SOCKET__NODE_BIND;
4064 break;
James Morris2ee92d42006-11-13 16:09:01 -08004065
4066 case SECCLASS_DCCP_SOCKET:
4067 node_perm = DCCP_SOCKET__NODE_BIND;
4068 break;
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 default:
4071 node_perm = RAWIP_SOCKET__NODE_BIND;
4072 break;
4073 }
Eric Paris828dfe12008-04-17 13:17:49 -04004074
Paul Moore224dfbd2008-01-29 08:38:13 -05004075 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 if (err)
4077 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004078
Thomas Liu2bf49692009-07-14 12:14:09 -04004079 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004080 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004081 ad.u.net = &net;
4082 ad.u.net->sport = htons(snum);
4083 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004086 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 else
Eric Paris48c62af2012-04-02 13:15:44 -04004088 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Paul Moore253bfae2010-04-22 14:46:19 -04004090 err = avc_has_perm(sksec->sid, sid,
4091 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (err)
4093 goto out;
4094 }
4095out:
4096 return err;
4097}
4098
4099static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4100{
Paul Moore014ab192008-10-10 10:16:33 -04004101 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004102 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 int err;
4104
Paul Moore253bfae2010-04-22 14:46:19 -04004105 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (err)
4107 return err;
4108
4109 /*
James Morris2ee92d42006-11-13 16:09:01 -08004110 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 */
Paul Moore253bfae2010-04-22 14:46:19 -04004112 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4113 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004114 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004115 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004116 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 struct sockaddr_in *addr4 = NULL;
4118 struct sockaddr_in6 *addr6 = NULL;
4119 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004120 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 if (sk->sk_family == PF_INET) {
4123 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004124 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return -EINVAL;
4126 snum = ntohs(addr4->sin_port);
4127 } else {
4128 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004129 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return -EINVAL;
4131 snum = ntohs(addr6->sin6_port);
4132 }
4133
Paul Moore3e112172008-04-10 10:48:14 -04004134 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (err)
4136 goto out;
4137
Paul Moore253bfae2010-04-22 14:46:19 -04004138 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004139 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4140
Thomas Liu2bf49692009-07-14 12:14:09 -04004141 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004142 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004143 ad.u.net = &net;
4144 ad.u.net->dport = htons(snum);
4145 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004146 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 if (err)
4148 goto out;
4149 }
4150
Paul Moore014ab192008-10-10 10:16:33 -04004151 err = selinux_netlbl_socket_connect(sk, address);
4152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153out:
4154 return err;
4155}
4156
4157static int selinux_socket_listen(struct socket *sock, int backlog)
4158{
Paul Moore253bfae2010-04-22 14:46:19 -04004159 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160}
4161
4162static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4163{
4164 int err;
4165 struct inode_security_struct *isec;
4166 struct inode_security_struct *newisec;
4167
Paul Moore253bfae2010-04-22 14:46:19 -04004168 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (err)
4170 return err;
4171
4172 newisec = SOCK_INODE(newsock)->i_security;
4173
4174 isec = SOCK_INODE(sock)->i_security;
4175 newisec->sclass = isec->sclass;
4176 newisec->sid = isec->sid;
4177 newisec->initialized = 1;
4178
4179 return 0;
4180}
4181
4182static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004183 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
Paul Moore253bfae2010-04-22 14:46:19 -04004185 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
4188static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4189 int size, int flags)
4190{
Paul Moore253bfae2010-04-22 14:46:19 -04004191 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
4194static int selinux_socket_getsockname(struct socket *sock)
4195{
Paul Moore253bfae2010-04-22 14:46:19 -04004196 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
4199static int selinux_socket_getpeername(struct socket *sock)
4200{
Paul Moore253bfae2010-04-22 14:46:19 -04004201 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Eric Paris828dfe12008-04-17 13:17:49 -04004204static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Paul Mooref8687af2006-10-30 15:22:15 -08004206 int err;
4207
Paul Moore253bfae2010-04-22 14:46:19 -04004208 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004209 if (err)
4210 return err;
4211
4212 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
4215static int selinux_socket_getsockopt(struct socket *sock, int level,
4216 int optname)
4217{
Paul Moore253bfae2010-04-22 14:46:19 -04004218 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219}
4220
4221static int selinux_socket_shutdown(struct socket *sock, int how)
4222{
Paul Moore253bfae2010-04-22 14:46:19 -04004223 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
David S. Miller3610cda2011-01-05 15:38:53 -08004226static int selinux_socket_unix_stream_connect(struct sock *sock,
4227 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 struct sock *newsk)
4229{
David S. Miller3610cda2011-01-05 15:38:53 -08004230 struct sk_security_struct *sksec_sock = sock->sk_security;
4231 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004232 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004233 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004234 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004235 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 int err;
4237
Thomas Liu2bf49692009-07-14 12:14:09 -04004238 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004239 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004240 ad.u.net = &net;
4241 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Paul Moore4d1e2452010-04-22 14:46:18 -04004243 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4244 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4246 if (err)
4247 return err;
4248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004250 sksec_new->peer_sid = sksec_sock->sid;
4251 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4252 &sksec_new->sid);
4253 if (err)
4254 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004255
Paul Moore4d1e2452010-04-22 14:46:18 -04004256 /* connecting socket */
4257 sksec_sock->peer_sid = sksec_new->sid;
4258
4259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
4262static int selinux_socket_unix_may_send(struct socket *sock,
4263 struct socket *other)
4264{
Paul Moore253bfae2010-04-22 14:46:19 -04004265 struct sk_security_struct *ssec = sock->sk->sk_security;
4266 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004267 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004268 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004269 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Thomas Liu2bf49692009-07-14 12:14:09 -04004271 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004272 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004273 ad.u.net = &net;
4274 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Paul Moore253bfae2010-04-22 14:46:19 -04004276 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4277 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278}
4279
Paul Mooreeffad8d2008-01-29 08:49:27 -05004280static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4281 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004282 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004283{
4284 int err;
4285 u32 if_sid;
4286 u32 node_sid;
4287
4288 err = sel_netif_sid(ifindex, &if_sid);
4289 if (err)
4290 return err;
4291 err = avc_has_perm(peer_sid, if_sid,
4292 SECCLASS_NETIF, NETIF__INGRESS, ad);
4293 if (err)
4294 return err;
4295
4296 err = sel_netnode_sid(addrp, family, &node_sid);
4297 if (err)
4298 return err;
4299 return avc_has_perm(peer_sid, node_sid,
4300 SECCLASS_NODE, NODE__RECVFROM, ad);
4301}
4302
Paul Moore220deb92008-01-29 08:38:23 -05004303static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004304 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004305{
Paul Moore277d3422008-12-31 12:54:11 -05004306 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004307 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004308 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004309 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004310 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004311 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004312 char *addrp;
4313
Thomas Liu2bf49692009-07-14 12:14:09 -04004314 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004315 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004316 ad.u.net = &net;
4317 ad.u.net->netif = skb->skb_iif;
4318 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004319 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4320 if (err)
4321 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004322
Paul Moore58bfbb52009-03-27 17:10:41 -04004323 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004324 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004325 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004326 if (err)
4327 return err;
4328 }
Paul Moore220deb92008-01-29 08:38:23 -05004329
Steffen Klassertb9679a72011-02-23 12:55:21 +01004330 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4331 if (err)
4332 return err;
4333 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004334
James Morris4e5ab4c2006-06-09 00:33:33 -07004335 return err;
4336}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004337
James Morris4e5ab4c2006-06-09 00:33:33 -07004338static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4339{
Paul Moore220deb92008-01-29 08:38:23 -05004340 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004341 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004342 u16 family = sk->sk_family;
4343 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004344 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004345 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004346 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004347 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004348 u8 secmark_active;
4349 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004350
James Morris4e5ab4c2006-06-09 00:33:33 -07004351 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004352 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004353
4354 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004355 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004356 family = PF_INET;
4357
Paul Moored8395c82008-10-10 10:16:30 -04004358 /* If any sort of compatibility mode is enabled then handoff processing
4359 * to the selinux_sock_rcv_skb_compat() function to deal with the
4360 * special handling. We do this in an attempt to keep this function
4361 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004362 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004363 return selinux_sock_rcv_skb_compat(sk, skb, family);
4364
4365 secmark_active = selinux_secmark_enabled();
4366 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4367 if (!secmark_active && !peerlbl_active)
4368 return 0;
4369
Thomas Liu2bf49692009-07-14 12:14:09 -04004370 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004371 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004372 ad.u.net = &net;
4373 ad.u.net->netif = skb->skb_iif;
4374 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004375 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004376 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004377 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004378
Paul Moored8395c82008-10-10 10:16:30 -04004379 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004380 u32 peer_sid;
4381
4382 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4383 if (err)
4384 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004385 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004386 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004387 if (err) {
4388 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004389 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004390 }
Paul Moored621d352008-01-29 08:43:36 -05004391 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4392 PEER__RECV, &ad);
Chad Hanson351381d2013-12-23 17:45:01 -05004393 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004394 selinux_netlbl_err(skb, err, 0);
Chad Hanson351381d2013-12-23 17:45:01 -05004395 return err;
4396 }
Paul Moored621d352008-01-29 08:43:36 -05004397 }
4398
Paul Moored8395c82008-10-10 10:16:30 -04004399 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004400 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4401 PACKET__RECV, &ad);
4402 if (err)
4403 return err;
4404 }
4405
Paul Moored621d352008-01-29 08:43:36 -05004406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004409static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4410 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
4412 int err = 0;
4413 char *scontext;
4414 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004415 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004416 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Paul Moore253bfae2010-04-22 14:46:19 -04004418 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4419 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004420 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004421 if (peer_sid == SECSID_NULL)
4422 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004424 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 if (scontext_len > len) {
4429 err = -ERANGE;
4430 goto out_len;
4431 }
4432
4433 if (copy_to_user(optval, scontext, scontext_len))
4434 err = -EFAULT;
4435
4436out_len:
4437 if (put_user(scontext_len, optlen))
4438 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return err;
4441}
4442
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004443static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004444{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004445 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004446 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004447
Paul Mooreaa862902008-10-10 10:16:29 -04004448 if (skb && skb->protocol == htons(ETH_P_IP))
4449 family = PF_INET;
4450 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4451 family = PF_INET6;
4452 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004453 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004454 else
4455 goto out;
4456
4457 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004458 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004459 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004460 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004461
Paul Moore75e22912008-01-29 08:38:04 -05004462out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004463 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004464 if (peer_secid == SECSID_NULL)
4465 return -EINVAL;
4466 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004467}
4468
Al Viro7d877f32005-10-21 03:20:43 -04004469static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
Paul Moore84914b72010-04-22 14:46:18 -04004471 struct sk_security_struct *sksec;
4472
4473 sksec = kzalloc(sizeof(*sksec), priority);
4474 if (!sksec)
4475 return -ENOMEM;
4476
4477 sksec->peer_sid = SECINITSID_UNLABELED;
4478 sksec->sid = SECINITSID_UNLABELED;
4479 selinux_netlbl_sk_security_reset(sksec);
4480 sk->sk_security = sksec;
4481
4482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483}
4484
4485static void selinux_sk_free_security(struct sock *sk)
4486{
Paul Moore84914b72010-04-22 14:46:18 -04004487 struct sk_security_struct *sksec = sk->sk_security;
4488
4489 sk->sk_security = NULL;
4490 selinux_netlbl_sk_security_free(sksec);
4491 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
4493
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004494static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4495{
Eric Parisdd3e7832010-04-07 15:08:46 -04004496 struct sk_security_struct *sksec = sk->sk_security;
4497 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004498
Eric Parisdd3e7832010-04-07 15:08:46 -04004499 newsksec->sid = sksec->sid;
4500 newsksec->peer_sid = sksec->peer_sid;
4501 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004502
Eric Parisdd3e7832010-04-07 15:08:46 -04004503 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004504}
4505
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004506static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004507{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004508 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004509 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004510 else {
4511 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004512
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004513 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004514 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004515}
4516
Eric Paris828dfe12008-04-17 13:17:49 -04004517static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004518{
4519 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4520 struct sk_security_struct *sksec = sk->sk_security;
4521
David Woodhouse2148ccc2006-09-29 15:50:25 -07004522 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4523 sk->sk_family == PF_UNIX)
4524 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004525 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004526}
4527
Adrian Bunk9a673e52006-08-15 00:03:53 -07004528static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4529 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004530{
4531 struct sk_security_struct *sksec = sk->sk_security;
4532 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004533 u16 family = sk->sk_family;
Paul Moore2ea04e52013-12-04 16:10:51 -05004534 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004535 u32 peersid;
4536
Paul Mooreaa862902008-10-10 10:16:29 -04004537 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4538 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4539 family = PF_INET;
4540
4541 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004542 if (err)
4543 return err;
Paul Moore2ea04e52013-12-04 16:10:51 -05004544 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4545 if (err)
4546 return err;
4547 req->secid = connsid;
4548 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004549
Paul Moore389fb802009-03-27 17:10:34 -04004550 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004551}
4552
Adrian Bunk9a673e52006-08-15 00:03:53 -07004553static void selinux_inet_csk_clone(struct sock *newsk,
4554 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004555{
4556 struct sk_security_struct *newsksec = newsk->sk_security;
4557
4558 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004559 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004560 /* NOTE: Ideally, we should also get the isec->sid for the
4561 new socket in sync, but we don't have the isec available yet.
4562 So we will wait until sock_graft to do it, by which
4563 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004564
Paul Moore9f2ad662006-11-17 17:38:53 -05004565 /* We don't need to take any sort of lock here as we are the only
4566 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004567 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004568}
4569
Paul Moore014ab192008-10-10 10:16:33 -04004570static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004571{
Paul Mooreaa862902008-10-10 10:16:29 -04004572 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004573 struct sk_security_struct *sksec = sk->sk_security;
4574
Paul Mooreaa862902008-10-10 10:16:29 -04004575 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4576 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4577 family = PF_INET;
4578
4579 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004580}
4581
Eric Paris2606fd12010-10-13 16:24:41 -04004582static int selinux_secmark_relabel_packet(u32 sid)
4583{
4584 const struct task_security_struct *__tsec;
4585 u32 tsid;
4586
4587 __tsec = current_security();
4588 tsid = __tsec->sid;
4589
4590 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4591}
4592
4593static void selinux_secmark_refcount_inc(void)
4594{
4595 atomic_inc(&selinux_secmark_refcount);
4596}
4597
4598static void selinux_secmark_refcount_dec(void)
4599{
4600 atomic_dec(&selinux_secmark_refcount);
4601}
4602
Adrian Bunk9a673e52006-08-15 00:03:53 -07004603static void selinux_req_classify_flow(const struct request_sock *req,
4604 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004605{
David S. Miller1d28f422011-03-12 00:29:39 -05004606 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004607}
4608
Paul Mooreed6d76e2009-08-28 18:12:49 -04004609static int selinux_tun_dev_create(void)
4610{
4611 u32 sid = current_sid();
4612
4613 /* we aren't taking into account the "sockcreate" SID since the socket
4614 * that is being created here is not a socket in the traditional sense,
4615 * instead it is a private sock, accessible only to the kernel, and
4616 * representing a wide range of network traffic spanning multiple
4617 * connections unlike traditional sockets - check the TUN driver to
4618 * get a better understanding of why this socket is special */
4619
4620 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4621 NULL);
4622}
4623
4624static void selinux_tun_dev_post_create(struct sock *sk)
4625{
4626 struct sk_security_struct *sksec = sk->sk_security;
4627
4628 /* we don't currently perform any NetLabel based labeling here and it
4629 * isn't clear that we would want to do so anyway; while we could apply
4630 * labeling without the support of the TUN user the resulting labeled
4631 * traffic from the other end of the connection would almost certainly
4632 * cause confusion to the TUN user that had no idea network labeling
4633 * protocols were being used */
4634
4635 /* see the comments in selinux_tun_dev_create() about why we don't use
4636 * the sockcreate SID here */
4637
4638 sksec->sid = current_sid();
4639 sksec->sclass = SECCLASS_TUN_SOCKET;
4640}
4641
4642static int selinux_tun_dev_attach(struct sock *sk)
4643{
4644 struct sk_security_struct *sksec = sk->sk_security;
4645 u32 sid = current_sid();
4646 int err;
4647
4648 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4649 TUN_SOCKET__RELABELFROM, NULL);
4650 if (err)
4651 return err;
4652 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4653 TUN_SOCKET__RELABELTO, NULL);
4654 if (err)
4655 return err;
4656
4657 sksec->sid = sid;
4658
4659 return 0;
4660}
4661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4663{
4664 int err = 0;
4665 u32 perm;
4666 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004667 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (skb->len < NLMSG_SPACE(0)) {
4670 err = -EINVAL;
4671 goto out;
4672 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004673 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004674
Paul Moore253bfae2010-04-22 14:46:19 -04004675 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (err) {
4677 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004678 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 "SELinux: unrecognized netlink message"
4680 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004681 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004682 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 err = 0;
4684 }
4685
4686 /* Ignore */
4687 if (err == -ENOENT)
4688 err = 0;
4689 goto out;
4690 }
4691
Paul Moore253bfae2010-04-22 14:46:19 -04004692 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693out:
4694 return err;
4695}
4696
4697#ifdef CONFIG_NETFILTER
4698
Paul Mooreeffad8d2008-01-29 08:49:27 -05004699static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4700 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
Paul Mooredfaebe92008-10-10 10:16:31 -04004702 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004703 char *addrp;
4704 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004705 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004706 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004707 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004708 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004709 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004710 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004711
Paul Mooreeffad8d2008-01-29 08:49:27 -05004712 if (!selinux_policycap_netpeer)
4713 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004714
Paul Mooreeffad8d2008-01-29 08:49:27 -05004715 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004716 netlbl_active = netlbl_enabled();
4717 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004718 if (!secmark_active && !peerlbl_active)
4719 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004720
Paul Moored8395c82008-10-10 10:16:30 -04004721 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4722 return NF_DROP;
4723
Thomas Liu2bf49692009-07-14 12:14:09 -04004724 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004725 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004726 ad.u.net = &net;
4727 ad.u.net->netif = ifindex;
4728 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004729 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4730 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Paul Mooredfaebe92008-10-10 10:16:31 -04004732 if (peerlbl_active) {
4733 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4734 peer_sid, &ad);
4735 if (err) {
4736 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004737 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004738 }
4739 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004740
4741 if (secmark_active)
4742 if (avc_has_perm(peer_sid, skb->secmark,
4743 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4744 return NF_DROP;
4745
Paul Moore948bf852008-10-10 10:16:32 -04004746 if (netlbl_active)
4747 /* we do this in the FORWARD path and not the POST_ROUTING
4748 * path because we want to make sure we apply the necessary
4749 * labeling before IPsec is applied so we can leverage AH
4750 * protection */
4751 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4752 return NF_DROP;
4753
Paul Mooreeffad8d2008-01-29 08:49:27 -05004754 return NF_ACCEPT;
4755}
4756
4757static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4758 struct sk_buff *skb,
4759 const struct net_device *in,
4760 const struct net_device *out,
4761 int (*okfn)(struct sk_buff *))
4762{
4763 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4764}
4765
4766#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4767static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4768 struct sk_buff *skb,
4769 const struct net_device *in,
4770 const struct net_device *out,
4771 int (*okfn)(struct sk_buff *))
4772{
4773 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4774}
4775#endif /* IPV6 */
4776
Paul Moore948bf852008-10-10 10:16:32 -04004777static unsigned int selinux_ip_output(struct sk_buff *skb,
4778 u16 family)
4779{
Paul Moore1c5d9d12013-12-04 16:10:45 -05004780 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004781 u32 sid;
4782
4783 if (!netlbl_enabled())
4784 return NF_ACCEPT;
4785
4786 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4787 * because we want to make sure we apply the necessary labeling
4788 * before IPsec is applied so we can leverage AH protection */
Paul Moore1c5d9d12013-12-04 16:10:45 -05004789 sk = skb->sk;
4790 if (sk) {
4791 struct sk_security_struct *sksec;
4792
4793 if (sk->sk_state == TCP_LISTEN)
4794 /* if the socket is the listening state then this
4795 * packet is a SYN-ACK packet which means it needs to
4796 * be labeled based on the connection/request_sock and
4797 * not the parent socket. unfortunately, we can't
4798 * lookup the request_sock yet as it isn't queued on
4799 * the parent socket until after the SYN-ACK is sent.
4800 * the "solution" is to simply pass the packet as-is
4801 * as any IP option based labeling should be copied
4802 * from the initial connection request (in the IP
4803 * layer). it is far from ideal, but until we get a
4804 * security label in the packet itself this is the
4805 * best we can do. */
4806 return NF_ACCEPT;
4807
4808 /* standard practice, label using the parent socket */
4809 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004810 sid = sksec->sid;
4811 } else
4812 sid = SECINITSID_KERNEL;
4813 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4814 return NF_DROP;
4815
4816 return NF_ACCEPT;
4817}
4818
4819static unsigned int selinux_ipv4_output(unsigned int hooknum,
4820 struct sk_buff *skb,
4821 const struct net_device *in,
4822 const struct net_device *out,
4823 int (*okfn)(struct sk_buff *))
4824{
4825 return selinux_ip_output(skb, PF_INET);
4826}
4827
Paul Mooreeffad8d2008-01-29 08:49:27 -05004828static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4829 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004830 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004831{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004832 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004833 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004834 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004835 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004836 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004837 char *addrp;
4838 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004839
Paul Mooreeffad8d2008-01-29 08:49:27 -05004840 if (sk == NULL)
4841 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004842 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004843
Thomas Liu2bf49692009-07-14 12:14:09 -04004844 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004845 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004846 ad.u.net = &net;
4847 ad.u.net->netif = ifindex;
4848 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004849 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4850 return NF_DROP;
4851
Paul Moore58bfbb52009-03-27 17:10:41 -04004852 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004853 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004854 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004855 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004856
Steffen Klassertb9679a72011-02-23 12:55:21 +01004857 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4858 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004859
Paul Mooreeffad8d2008-01-29 08:49:27 -05004860 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861}
4862
Paul Mooreeffad8d2008-01-29 08:49:27 -05004863static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4864 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004866 u32 secmark_perm;
4867 u32 peer_sid;
4868 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004869 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004870 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004871 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004872 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004873 u8 secmark_active;
4874 u8 peerlbl_active;
4875
Paul Mooreeffad8d2008-01-29 08:49:27 -05004876 /* If any sort of compatibility mode is enabled then handoff processing
4877 * to the selinux_ip_postroute_compat() function to deal with the
4878 * special handling. We do this in an attempt to keep this function
4879 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004880 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004881 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moore420cc6d2013-12-10 14:58:01 -05004882
Paul Mooreeffad8d2008-01-29 08:49:27 -05004883 secmark_active = selinux_secmark_enabled();
4884 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4885 if (!secmark_active && !peerlbl_active)
4886 return NF_ACCEPT;
4887
Paul Mooreeffad8d2008-01-29 08:49:27 -05004888 sk = skb->sk;
Paul Moore420cc6d2013-12-10 14:58:01 -05004889
4890#ifdef CONFIG_XFRM
4891 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4892 * packet transformation so allow the packet to pass without any checks
4893 * since we'll have another chance to perform access control checks
4894 * when the packet is on it's final way out.
4895 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4896 * is NULL, in this case go ahead and apply access control.
4897 * is NULL, in this case go ahead and apply access control.
4898 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4899 * TCP listening state we cannot wait until the XFRM processing
4900 * is done as we will miss out on the SA label if we do;
4901 * unfortunately, this means more work, but it is only once per
4902 * connection. */
4903 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4904 !(sk != NULL && sk->sk_state == TCP_LISTEN))
4905 return NF_ACCEPT;
4906#endif
4907
Paul Moored8395c82008-10-10 10:16:30 -04004908 if (sk == NULL) {
Paul Moore2ea04e52013-12-04 16:10:51 -05004909 /* Without an associated socket the packet is either coming
4910 * from the kernel or it is being forwarded; check the packet
4911 * to determine which and if the packet is being forwarded
4912 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004913 if (skb->skb_iif) {
4914 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004915 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004916 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004917 } else {
4918 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004919 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004920 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004921 } else if (sk->sk_state == TCP_LISTEN) {
4922 /* Locally generated packet but the associated socket is in the
4923 * listening state which means this is a SYN-ACK packet. In
4924 * this particular case the correct security label is assigned
4925 * to the connection/request_sock but unfortunately we can't
4926 * query the request_sock as it isn't queued on the parent
4927 * socket until after the SYN-ACK packet is sent; the only
4928 * viable choice is to regenerate the label like we do in
4929 * selinux_inet_conn_request(). See also selinux_ip_output()
4930 * for similar problems. */
4931 u32 skb_sid;
4932 struct sk_security_struct *sksec = sk->sk_security;
4933 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4934 return NF_DROP;
Paul Moore420cc6d2013-12-10 14:58:01 -05004935 /* At this point, if the returned skb peerlbl is SECSID_NULL
4936 * and the packet has been through at least one XFRM
4937 * transformation then we must be dealing with the "final"
4938 * form of labeled IPsec packet; since we've already applied
4939 * all of our access controls on this packet we can safely
4940 * pass the packet. */
4941 if (skb_sid == SECSID_NULL) {
4942 switch (family) {
4943 case PF_INET:
4944 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4945 return NF_ACCEPT;
4946 break;
4947 case PF_INET6:
4948 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4949 return NF_ACCEPT;
4950 default:
4951 return NF_DROP_ERR(-ECONNREFUSED);
4952 }
4953 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004954 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4955 return NF_DROP;
4956 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004957 } else {
Paul Moore2ea04e52013-12-04 16:10:51 -05004958 /* Locally generated packet, fetch the security label from the
4959 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004960 struct sk_security_struct *sksec = sk->sk_security;
4961 peer_sid = sksec->sid;
4962 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004963 }
4964
Thomas Liu2bf49692009-07-14 12:14:09 -04004965 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004966 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004967 ad.u.net = &net;
4968 ad.u.net->netif = ifindex;
4969 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004970 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004971 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004972
Paul Mooreeffad8d2008-01-29 08:49:27 -05004973 if (secmark_active)
4974 if (avc_has_perm(peer_sid, skb->secmark,
4975 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004976 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004977
4978 if (peerlbl_active) {
4979 u32 if_sid;
4980 u32 node_sid;
4981
4982 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004983 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004984 if (avc_has_perm(peer_sid, if_sid,
4985 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004986 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004987
4988 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004989 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004990 if (avc_has_perm(peer_sid, node_sid,
4991 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004992 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004993 }
4994
4995 return NF_ACCEPT;
4996}
4997
4998static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4999 struct sk_buff *skb,
5000 const struct net_device *in,
5001 const struct net_device *out,
5002 int (*okfn)(struct sk_buff *))
5003{
5004 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005}
5006
5007#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005008static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
5009 struct sk_buff *skb,
5010 const struct net_device *in,
5011 const struct net_device *out,
5012 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005014 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016#endif /* IPV6 */
5017
5018#endif /* CONFIG_NETFILTER */
5019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 int err;
5023
Eric Paris200ac532009-02-12 15:01:04 -05005024 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 if (err)
5026 return err;
5027
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005028 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029}
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031static int ipc_alloc_security(struct task_struct *task,
5032 struct kern_ipc_perm *perm,
5033 u16 sclass)
5034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11005036 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
James Morris89d155e2005-10-30 14:59:21 -08005038 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 if (!isec)
5040 return -ENOMEM;
5041
David Howells275bb412008-11-14 10:39:19 +11005042 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11005044 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 perm->security = isec;
5046
5047 return 0;
5048}
5049
5050static void ipc_free_security(struct kern_ipc_perm *perm)
5051{
5052 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 perm->security = NULL;
5054 kfree(isec);
5055}
5056
5057static int msg_msg_alloc_security(struct msg_msg *msg)
5058{
5059 struct msg_security_struct *msec;
5060
James Morris89d155e2005-10-30 14:59:21 -08005061 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 if (!msec)
5063 return -ENOMEM;
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 msec->sid = SECINITSID_UNLABELED;
5066 msg->security = msec;
5067
5068 return 0;
5069}
5070
5071static void msg_msg_free_security(struct msg_msg *msg)
5072{
5073 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
5075 msg->security = NULL;
5076 kfree(msec);
5077}
5078
5079static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005080 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005083 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005084 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005085 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 isec = ipc_perms->security;
5088
Thomas Liu2bf49692009-07-14 12:14:09 -04005089 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005090 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 ad.u.ipc_id = ipc_perms->key;
5092
David Howells275bb412008-11-14 10:39:19 +11005093 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094}
5095
5096static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5097{
5098 return msg_msg_alloc_security(msg);
5099}
5100
5101static void selinux_msg_msg_free_security(struct msg_msg *msg)
5102{
5103 msg_msg_free_security(msg);
5104}
5105
5106/* message queue security operations */
5107static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005110 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005111 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005112 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 int rc;
5114
5115 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5116 if (rc)
5117 return rc;
5118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 isec = msq->q_perm.security;
5120
Thomas Liu2bf49692009-07-14 12:14:09 -04005121 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005122 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005123 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
David Howells275bb412008-11-14 10:39:19 +11005125 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 MSGQ__CREATE, &ad);
5127 if (rc) {
5128 ipc_free_security(&msq->q_perm);
5129 return rc;
5130 }
5131 return 0;
5132}
5133
5134static void selinux_msg_queue_free_security(struct msg_queue *msq)
5135{
5136 ipc_free_security(&msq->q_perm);
5137}
5138
5139static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005142 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005143 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005144 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 isec = msq->q_perm.security;
5147
Thomas Liu2bf49692009-07-14 12:14:09 -04005148 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005149 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 ad.u.ipc_id = msq->q_perm.key;
5151
David Howells275bb412008-11-14 10:39:19 +11005152 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 MSGQ__ASSOCIATE, &ad);
5154}
5155
5156static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5157{
5158 int err;
5159 int perms;
5160
Eric Paris828dfe12008-04-17 13:17:49 -04005161 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 case IPC_INFO:
5163 case MSG_INFO:
5164 /* No specific object, just general system-wide information. */
5165 return task_has_system(current, SYSTEM__IPC_INFO);
5166 case IPC_STAT:
5167 case MSG_STAT:
5168 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5169 break;
5170 case IPC_SET:
5171 perms = MSGQ__SETATTR;
5172 break;
5173 case IPC_RMID:
5174 perms = MSGQ__DESTROY;
5175 break;
5176 default:
5177 return 0;
5178 }
5179
Stephen Smalley6af963f2005-05-01 08:58:39 -07005180 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 return err;
5182}
5183
5184static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 struct ipc_security_struct *isec;
5187 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005188 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005189 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005190 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 int rc;
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 isec = msq->q_perm.security;
5194 msec = msg->security;
5195
5196 /*
5197 * First time through, need to assign label to the message
5198 */
5199 if (msec->sid == SECINITSID_UNLABELED) {
5200 /*
5201 * Compute new sid based on current process and
5202 * message queue this message will be stored in
5203 */
David Howells275bb412008-11-14 10:39:19 +11005204 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005205 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 if (rc)
5207 return rc;
5208 }
5209
Thomas Liu2bf49692009-07-14 12:14:09 -04005210 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005211 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 ad.u.ipc_id = msq->q_perm.key;
5213
5214 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005215 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 MSGQ__WRITE, &ad);
5217 if (!rc)
5218 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005219 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5220 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 if (!rc)
5222 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005223 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5224 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
5226 return rc;
5227}
5228
5229static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5230 struct task_struct *target,
5231 long type, int mode)
5232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 struct ipc_security_struct *isec;
5234 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005235 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005236 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005237 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 int rc;
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 isec = msq->q_perm.security;
5241 msec = msg->security;
5242
Thomas Liu2bf49692009-07-14 12:14:09 -04005243 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005244 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005245 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
David Howells275bb412008-11-14 10:39:19 +11005247 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 SECCLASS_MSGQ, MSGQ__READ, &ad);
5249 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005250 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 SECCLASS_MSG, MSG__RECEIVE, &ad);
5252 return rc;
5253}
5254
5255/* Shared Memory security operations */
5256static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005259 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005260 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005261 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 int rc;
5263
5264 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5265 if (rc)
5266 return rc;
5267
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 isec = shp->shm_perm.security;
5269
Thomas Liu2bf49692009-07-14 12:14:09 -04005270 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005271 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005272 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
David Howells275bb412008-11-14 10:39:19 +11005274 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 SHM__CREATE, &ad);
5276 if (rc) {
5277 ipc_free_security(&shp->shm_perm);
5278 return rc;
5279 }
5280 return 0;
5281}
5282
5283static void selinux_shm_free_security(struct shmid_kernel *shp)
5284{
5285 ipc_free_security(&shp->shm_perm);
5286}
5287
5288static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005291 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005292 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005293 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 isec = shp->shm_perm.security;
5296
Thomas Liu2bf49692009-07-14 12:14:09 -04005297 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005298 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 ad.u.ipc_id = shp->shm_perm.key;
5300
David Howells275bb412008-11-14 10:39:19 +11005301 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 SHM__ASSOCIATE, &ad);
5303}
5304
5305/* Note, at this point, shp is locked down */
5306static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5307{
5308 int perms;
5309 int err;
5310
Eric Paris828dfe12008-04-17 13:17:49 -04005311 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 case IPC_INFO:
5313 case SHM_INFO:
5314 /* No specific object, just general system-wide information. */
5315 return task_has_system(current, SYSTEM__IPC_INFO);
5316 case IPC_STAT:
5317 case SHM_STAT:
5318 perms = SHM__GETATTR | SHM__ASSOCIATE;
5319 break;
5320 case IPC_SET:
5321 perms = SHM__SETATTR;
5322 break;
5323 case SHM_LOCK:
5324 case SHM_UNLOCK:
5325 perms = SHM__LOCK;
5326 break;
5327 case IPC_RMID:
5328 perms = SHM__DESTROY;
5329 break;
5330 default:
5331 return 0;
5332 }
5333
Stephen Smalley6af963f2005-05-01 08:58:39 -07005334 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 return err;
5336}
5337
5338static int selinux_shm_shmat(struct shmid_kernel *shp,
5339 char __user *shmaddr, int shmflg)
5340{
5341 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
5343 if (shmflg & SHM_RDONLY)
5344 perms = SHM__READ;
5345 else
5346 perms = SHM__READ | SHM__WRITE;
5347
Stephen Smalley6af963f2005-05-01 08:58:39 -07005348 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
5351/* Semaphore security operations */
5352static int selinux_sem_alloc_security(struct sem_array *sma)
5353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005355 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005356 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005357 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 int rc;
5359
5360 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5361 if (rc)
5362 return rc;
5363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 isec = sma->sem_perm.security;
5365
Thomas Liu2bf49692009-07-14 12:14:09 -04005366 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005367 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005368 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
David Howells275bb412008-11-14 10:39:19 +11005370 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 SEM__CREATE, &ad);
5372 if (rc) {
5373 ipc_free_security(&sma->sem_perm);
5374 return rc;
5375 }
5376 return 0;
5377}
5378
5379static void selinux_sem_free_security(struct sem_array *sma)
5380{
5381 ipc_free_security(&sma->sem_perm);
5382}
5383
5384static int selinux_sem_associate(struct sem_array *sma, int semflg)
5385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005387 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005388 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005389 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 isec = sma->sem_perm.security;
5392
Thomas Liu2bf49692009-07-14 12:14:09 -04005393 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005394 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 ad.u.ipc_id = sma->sem_perm.key;
5396
David Howells275bb412008-11-14 10:39:19 +11005397 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 SEM__ASSOCIATE, &ad);
5399}
5400
5401/* Note, at this point, sma is locked down */
5402static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5403{
5404 int err;
5405 u32 perms;
5406
Eric Paris828dfe12008-04-17 13:17:49 -04005407 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 case IPC_INFO:
5409 case SEM_INFO:
5410 /* No specific object, just general system-wide information. */
5411 return task_has_system(current, SYSTEM__IPC_INFO);
5412 case GETPID:
5413 case GETNCNT:
5414 case GETZCNT:
5415 perms = SEM__GETATTR;
5416 break;
5417 case GETVAL:
5418 case GETALL:
5419 perms = SEM__READ;
5420 break;
5421 case SETVAL:
5422 case SETALL:
5423 perms = SEM__WRITE;
5424 break;
5425 case IPC_RMID:
5426 perms = SEM__DESTROY;
5427 break;
5428 case IPC_SET:
5429 perms = SEM__SETATTR;
5430 break;
5431 case IPC_STAT:
5432 case SEM_STAT:
5433 perms = SEM__GETATTR | SEM__ASSOCIATE;
5434 break;
5435 default:
5436 return 0;
5437 }
5438
Stephen Smalley6af963f2005-05-01 08:58:39 -07005439 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 return err;
5441}
5442
5443static int selinux_sem_semop(struct sem_array *sma,
5444 struct sembuf *sops, unsigned nsops, int alter)
5445{
5446 u32 perms;
5447
5448 if (alter)
5449 perms = SEM__READ | SEM__WRITE;
5450 else
5451 perms = SEM__READ;
5452
Stephen Smalley6af963f2005-05-01 08:58:39 -07005453 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454}
5455
5456static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 u32 av = 0;
5459
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 av = 0;
5461 if (flag & S_IRUGO)
5462 av |= IPC__UNIX_READ;
5463 if (flag & S_IWUGO)
5464 av |= IPC__UNIX_WRITE;
5465
5466 if (av == 0)
5467 return 0;
5468
Stephen Smalley6af963f2005-05-01 08:58:39 -07005469 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005472static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5473{
5474 struct ipc_security_struct *isec = ipcp->security;
5475 *secid = isec->sid;
5476}
5477
Eric Paris828dfe12008-04-17 13:17:49 -04005478static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
5480 if (inode)
5481 inode_doinit_with_dentry(inode, dentry);
5482}
5483
5484static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005485 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
David Howells275bb412008-11-14 10:39:19 +11005487 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005488 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005490 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
5492 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005493 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 if (error)
5495 return error;
5496 }
5497
David Howells275bb412008-11-14 10:39:19 +11005498 rcu_read_lock();
5499 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
5501 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005502 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005504 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005506 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005508 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005509 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005510 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005511 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005512 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 else
David Howells275bb412008-11-14 10:39:19 +11005514 goto invalid;
5515 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517 if (!sid)
5518 return 0;
5519
Al Viro04ff9702007-03-12 16:17:58 +00005520 error = security_sid_to_context(sid, value, &len);
5521 if (error)
5522 return error;
5523 return len;
David Howells275bb412008-11-14 10:39:19 +11005524
5525invalid:
5526 rcu_read_unlock();
5527 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528}
5529
5530static int selinux_setprocattr(struct task_struct *p,
5531 char *name, void *value, size_t size)
5532{
5533 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005534 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005535 struct cred *new;
5536 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 int error;
5538 char *str = value;
5539
5540 if (current != p) {
5541 /* SELinux only allows a process to change its own
5542 security attributes. */
5543 return -EACCES;
5544 }
5545
5546 /*
5547 * Basic control over ability to set these attributes at all.
5548 * current == p, but we'll pass them separately in case the
5549 * above restriction is ever removed.
5550 */
5551 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005552 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005554 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005555 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005556 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005557 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005558 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005560 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 else
5562 error = -EINVAL;
5563 if (error)
5564 return error;
5565
5566 /* Obtain a SID for the context, if one was specified. */
5567 if (size && str[1] && str[1] != '\n') {
5568 if (str[size-1] == '\n') {
5569 str[size-1] = 0;
5570 size--;
5571 }
5572 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005573 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5574 if (!capable(CAP_MAC_ADMIN))
5575 return error;
5576 error = security_context_to_sid_force(value, size,
5577 &sid);
5578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 if (error)
5580 return error;
5581 }
5582
David Howellsd84f4f92008-11-14 10:39:23 +11005583 new = prepare_creds();
5584 if (!new)
5585 return -ENOMEM;
5586
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 /* Permission checking based on the specified context is
5588 performed during the actual operation (execve,
5589 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005590 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 checks and may_create for the file creation checks. The
5592 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005593 tsec = new->security;
5594 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005596 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005598 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005599 error = may_create_key(sid, p);
5600 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005601 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005602 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005603 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005604 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005605 } else if (!strcmp(name, "current")) {
5606 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005608 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005609
David Howellsd84f4f92008-11-14 10:39:23 +11005610 /* Only allow single threaded processes to change context */
5611 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005612 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005613 error = security_bounded_transition(tsec->sid, sid);
5614 if (error)
5615 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618 /* Check permissions for the transition. */
5619 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005620 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005622 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 /* Check for ptracing, and update the task SID if ok.
5625 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005626 ptsid = 0;
Oleg Nesterov58c23142013-12-23 17:45:01 -05005627 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02005628 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005629 if (tracer)
5630 ptsid = task_sid(tracer);
Oleg Nesterov58c23142013-12-23 17:45:01 -05005631 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
David Howellsd84f4f92008-11-14 10:39:23 +11005633 if (tracer) {
5634 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5635 PROCESS__PTRACE, NULL);
5636 if (error)
5637 goto abort_change;
5638 }
5639
5640 tsec->sid = sid;
5641 } else {
5642 error = -EINVAL;
5643 goto abort_change;
5644 }
5645
5646 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005648
5649abort_change:
5650 abort_creds(new);
5651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005654static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5655{
5656 return security_sid_to_context(secid, secdata, seclen);
5657}
5658
David Howells7bf570d2008-04-29 20:52:51 +01005659static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005660{
5661 return security_context_to_sid(secdata, seclen, secid);
5662}
5663
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005664static void selinux_release_secctx(char *secdata, u32 seclen)
5665{
Paul Moore088999e2007-08-01 11:12:58 -04005666 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005667}
5668
David P. Quigley1ee65e32009-09-03 14:25:57 -04005669/*
5670 * called with inode->i_mutex locked
5671 */
5672static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5673{
5674 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5675}
5676
5677/*
5678 * called with inode->i_mutex locked
5679 */
5680static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5681{
5682 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5683}
5684
5685static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5686{
5687 int len = 0;
5688 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5689 ctx, true);
5690 if (len < 0)
5691 return len;
5692 *ctxlen = len;
5693 return 0;
5694}
Michael LeMayd7200242006-06-22 14:47:17 -07005695#ifdef CONFIG_KEYS
5696
David Howellsd84f4f92008-11-14 10:39:23 +11005697static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005698 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005699{
David Howellsd84f4f92008-11-14 10:39:23 +11005700 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005701 struct key_security_struct *ksec;
5702
5703 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5704 if (!ksec)
5705 return -ENOMEM;
5706
David Howellsd84f4f92008-11-14 10:39:23 +11005707 tsec = cred->security;
5708 if (tsec->keycreate_sid)
5709 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005710 else
David Howellsd84f4f92008-11-14 10:39:23 +11005711 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005712
David Howells275bb412008-11-14 10:39:19 +11005713 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005714 return 0;
5715}
5716
5717static void selinux_key_free(struct key *k)
5718{
5719 struct key_security_struct *ksec = k->security;
5720
5721 k->security = NULL;
5722 kfree(ksec);
5723}
5724
5725static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005726 const struct cred *cred,
5727 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005728{
5729 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005730 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005731 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005732
5733 /* if no specific permissions are requested, we skip the
5734 permission check. No serious, additional covert channels
5735 appear to be created. */
5736 if (perm == 0)
5737 return 0;
5738
David Howellsd84f4f92008-11-14 10:39:23 +11005739 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005740
5741 key = key_ref_to_ptr(key_ref);
5742 ksec = key->security;
5743
5744 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005745}
5746
David Howells70a5bb72008-04-29 01:01:26 -07005747static int selinux_key_getsecurity(struct key *key, char **_buffer)
5748{
5749 struct key_security_struct *ksec = key->security;
5750 char *context = NULL;
5751 unsigned len;
5752 int rc;
5753
5754 rc = security_sid_to_context(ksec->sid, &context, &len);
5755 if (!rc)
5756 rc = len;
5757 *_buffer = context;
5758 return rc;
5759}
5760
Michael LeMayd7200242006-06-22 14:47:17 -07005761#endif
5762
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005764 .name = "selinux",
5765
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05005766 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5767 .binder_transaction = selinux_binder_transaction,
5768 .binder_transfer_binder = selinux_binder_transfer_binder,
5769 .binder_transfer_file = selinux_binder_transfer_file,
5770
Ingo Molnar9e488582009-05-07 19:26:19 +10005771 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005772 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005774 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 .capable = selinux_capable,
5776 .quotactl = selinux_quotactl,
5777 .quota_on = selinux_quota_on,
5778 .syslog = selinux_syslog,
5779 .vm_enough_memory = selinux_vm_enough_memory,
5780
5781 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
David Howellsa6f76f22008-11-14 10:39:24 +11005783 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005784 .bprm_committing_creds = selinux_bprm_committing_creds,
5785 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 .bprm_secureexec = selinux_bprm_secureexec,
5787
5788 .sb_alloc_security = selinux_sb_alloc_security,
5789 .sb_free_security = selinux_sb_free_security,
5790 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005791 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005792 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005793 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 .sb_statfs = selinux_sb_statfs,
5795 .sb_mount = selinux_mount,
5796 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005797 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005798 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005799 .sb_parse_opts_str = selinux_parse_opts_str,
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 .inode_alloc_security = selinux_inode_alloc_security,
5803 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005804 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 .inode_unlink = selinux_inode_unlink,
5808 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 .inode_rmdir = selinux_inode_rmdir,
5811 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 .inode_readlink = selinux_inode_readlink,
5814 .inode_follow_link = selinux_inode_follow_link,
5815 .inode_permission = selinux_inode_permission,
5816 .inode_setattr = selinux_inode_setattr,
5817 .inode_getattr = selinux_inode_getattr,
5818 .inode_setxattr = selinux_inode_setxattr,
5819 .inode_post_setxattr = selinux_inode_post_setxattr,
5820 .inode_getxattr = selinux_inode_getxattr,
5821 .inode_listxattr = selinux_inode_listxattr,
5822 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005823 .inode_getsecurity = selinux_inode_getsecurity,
5824 .inode_setsecurity = selinux_inode_setsecurity,
5825 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005826 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828 .file_permission = selinux_file_permission,
5829 .file_alloc_security = selinux_file_alloc_security,
5830 .file_free_security = selinux_file_free_security,
5831 .file_ioctl = selinux_file_ioctl,
5832 .file_mmap = selinux_file_mmap,
5833 .file_mprotect = selinux_file_mprotect,
5834 .file_lock = selinux_file_lock,
5835 .file_fcntl = selinux_file_fcntl,
5836 .file_set_fowner = selinux_file_set_fowner,
5837 .file_send_sigiotask = selinux_file_send_sigiotask,
5838 .file_receive = selinux_file_receive,
5839
Eric Paris828dfe12008-04-17 13:17:49 -04005840 .dentry_open = selinux_dentry_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005843 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005844 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005845 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005846 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005847 .kernel_act_as = selinux_kernel_act_as,
5848 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005849 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 .task_setpgid = selinux_task_setpgid,
5851 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005852 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005853 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005855 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005856 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 .task_setrlimit = selinux_task_setrlimit,
5858 .task_setscheduler = selinux_task_setscheduler,
5859 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005860 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 .task_kill = selinux_task_kill,
5862 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005863 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005866 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5869 .msg_msg_free_security = selinux_msg_msg_free_security,
5870
5871 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5872 .msg_queue_free_security = selinux_msg_queue_free_security,
5873 .msg_queue_associate = selinux_msg_queue_associate,
5874 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5875 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5876 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5877
5878 .shm_alloc_security = selinux_shm_alloc_security,
5879 .shm_free_security = selinux_shm_free_security,
5880 .shm_associate = selinux_shm_associate,
5881 .shm_shmctl = selinux_shm_shmctl,
5882 .shm_shmat = selinux_shm_shmat,
5883
Eric Paris828dfe12008-04-17 13:17:49 -04005884 .sem_alloc_security = selinux_sem_alloc_security,
5885 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 .sem_associate = selinux_sem_associate,
5887 .sem_semctl = selinux_sem_semctl,
5888 .sem_semop = selinux_sem_semop,
5889
Eric Paris828dfe12008-04-17 13:17:49 -04005890 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Eric Paris828dfe12008-04-17 13:17:49 -04005892 .getprocattr = selinux_getprocattr,
5893 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005895 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005896 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005897 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005898 .inode_notifysecctx = selinux_inode_notifysecctx,
5899 .inode_setsecctx = selinux_inode_setsecctx,
5900 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005901
Eric Paris828dfe12008-04-17 13:17:49 -04005902 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 .unix_may_send = selinux_socket_unix_may_send,
5904
5905 .socket_create = selinux_socket_create,
5906 .socket_post_create = selinux_socket_post_create,
5907 .socket_bind = selinux_socket_bind,
5908 .socket_connect = selinux_socket_connect,
5909 .socket_listen = selinux_socket_listen,
5910 .socket_accept = selinux_socket_accept,
5911 .socket_sendmsg = selinux_socket_sendmsg,
5912 .socket_recvmsg = selinux_socket_recvmsg,
5913 .socket_getsockname = selinux_socket_getsockname,
5914 .socket_getpeername = selinux_socket_getpeername,
5915 .socket_getsockopt = selinux_socket_getsockopt,
5916 .socket_setsockopt = selinux_socket_setsockopt,
5917 .socket_shutdown = selinux_socket_shutdown,
5918 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005919 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5920 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 .sk_alloc_security = selinux_sk_alloc_security,
5922 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005923 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005924 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005925 .sock_graft = selinux_sock_graft,
5926 .inet_conn_request = selinux_inet_conn_request,
5927 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005928 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005929 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5930 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5931 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005932 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005933 .tun_dev_create = selinux_tun_dev_create,
5934 .tun_dev_post_create = selinux_tun_dev_post_create,
5935 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005936
5937#ifdef CONFIG_SECURITY_NETWORK_XFRM
5938 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5939 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5940 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005941 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005942 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5943 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005944 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005945 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005946 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005947 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005949
5950#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005951 .key_alloc = selinux_key_alloc,
5952 .key_free = selinux_key_free,
5953 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005954 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005955#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005956
5957#ifdef CONFIG_AUDIT
5958 .audit_rule_init = selinux_audit_rule_init,
5959 .audit_rule_known = selinux_audit_rule_known,
5960 .audit_rule_match = selinux_audit_rule_match,
5961 .audit_rule_free = selinux_audit_rule_free,
5962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963};
5964
5965static __init int selinux_init(void)
5966{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005967 if (!security_module_enable(&selinux_ops)) {
5968 selinux_enabled = 0;
5969 return 0;
5970 }
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 if (!selinux_enabled) {
5973 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5974 return 0;
5975 }
5976
5977 printk(KERN_INFO "SELinux: Initializing.\n");
5978
5979 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005980 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005982 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5983
James Morris7cae7e22006-03-22 00:09:22 -08005984 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5985 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005986 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 avc_init();
5988
Eric Paris828dfe12008-04-17 13:17:49 -04005989 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 panic("SELinux: Unable to register with kernel.\n");
5991
Eric Paris828dfe12008-04-17 13:17:49 -04005992 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005993 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005994 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005995 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 return 0;
5998}
5999
Al Viroe8c26252010-03-23 06:36:54 -04006000static void delayed_superblock_init(struct super_block *sb, void *unused)
6001{
6002 superblock_doinit(sb, NULL);
6003}
6004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005void selinux_complete_init(void)
6006{
Eric Parisfadcdb42007-02-22 18:11:31 -05006007 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
6009 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05006010 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006011 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012}
6013
6014/* SELinux requires early initialization in order to label
6015 all processes and objects when they are created. */
6016security_initcall(selinux_init);
6017
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006018#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Paul Mooreeffad8d2008-01-29 08:49:27 -05006020static struct nf_hook_ops selinux_ipv4_ops[] = {
6021 {
6022 .hook = selinux_ipv4_postroute,
6023 .owner = THIS_MODULE,
6024 .pf = PF_INET,
6025 .hooknum = NF_INET_POST_ROUTING,
6026 .priority = NF_IP_PRI_SELINUX_LAST,
6027 },
6028 {
6029 .hook = selinux_ipv4_forward,
6030 .owner = THIS_MODULE,
6031 .pf = PF_INET,
6032 .hooknum = NF_INET_FORWARD,
6033 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006034 },
6035 {
6036 .hook = selinux_ipv4_output,
6037 .owner = THIS_MODULE,
6038 .pf = PF_INET,
6039 .hooknum = NF_INET_LOCAL_OUT,
6040 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042};
6043
6044#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6045
Paul Mooreeffad8d2008-01-29 08:49:27 -05006046static struct nf_hook_ops selinux_ipv6_ops[] = {
6047 {
6048 .hook = selinux_ipv6_postroute,
6049 .owner = THIS_MODULE,
6050 .pf = PF_INET6,
6051 .hooknum = NF_INET_POST_ROUTING,
6052 .priority = NF_IP6_PRI_SELINUX_LAST,
6053 },
6054 {
6055 .hook = selinux_ipv6_forward,
6056 .owner = THIS_MODULE,
6057 .pf = PF_INET6,
6058 .hooknum = NF_INET_FORWARD,
6059 .priority = NF_IP6_PRI_SELINUX_FIRST,
6060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061};
6062
6063#endif /* IPV6 */
6064
6065static int __init selinux_nf_ip_init(void)
6066{
6067 int err = 0;
6068
6069 if (!selinux_enabled)
6070 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006071
6072 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6073
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006074 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6075 if (err)
6076 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006079 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6080 if (err)
6081 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084out:
6085 return err;
6086}
6087
6088__initcall(selinux_nf_ip_init);
6089
6090#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6091static void selinux_nf_ip_exit(void)
6092{
Eric Parisfadcdb42007-02-22 18:11:31 -05006093 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006095 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006097 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098#endif /* IPV6 */
6099}
6100#endif
6101
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006102#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
6104#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6105#define selinux_nf_ip_exit()
6106#endif
6107
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006108#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006111static int selinux_disabled;
6112
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113int selinux_disable(void)
6114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 if (ss_initialized) {
6116 /* Not permitted after initial policy load. */
6117 return -EINVAL;
6118 }
6119
6120 if (selinux_disabled) {
6121 /* Only do this once. */
6122 return -EINVAL;
6123 }
6124
6125 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6126
6127 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006128 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006130 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Eric Parisaf8ff042009-09-20 21:23:01 -04006132 /* Try to destroy the avc node cache */
6133 avc_disable();
6134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 /* Unregister netfilter hooks. */
6136 selinux_nf_ip_exit();
6137
6138 /* Unregister selinuxfs. */
6139 exit_sel_fs();
6140
6141 return 0;
6142}
6143#endif