blob: 84ebb24651bbd8dc37258132c95909d6c9ea5685 [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
Mark Salyzyn472d3c62015-01-07 09:27:15 -0800421 /* Special handling. Is genfs but also has in-core setxattr handler*/
422 if (!strcmp(sb->s_type->name, "sysfs") ||
423 !strcmp(sb->s_type->name, "pstore") ||
424 !strcmp(sb->s_type->name, "debugfs") ||
425 !strcmp(sb->s_type->name, "rootfs"))
Stephen Smalley90078112013-05-10 10:16:19 -0400426 sbsec->flags |= SE_SBLABELSUPP;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500429 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Initialize any other inodes associated with the superblock, e.g.
432 inodes created prior to initial policy load or inodes created
433 during get_sb by a pseudo filesystem that directly
434 populates itself. */
435 spin_lock(&sbsec->isec_lock);
436next_inode:
437 if (!list_empty(&sbsec->isec_head)) {
438 struct inode_security_struct *isec =
439 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500440 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct inode *inode = isec->inode;
Stephen Smalley61dedfa2014-10-06 16:32:52 -0400442 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_unlock(&sbsec->isec_lock);
444 inode = igrab(inode);
445 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500446 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 inode_doinit(inode);
448 iput(inode);
449 }
450 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto next_inode;
452 }
453 spin_unlock(&sbsec->isec_lock);
454out:
Eric Parisc9180a52007-11-30 13:00:35 -0500455 return rc;
456}
457
458/*
459 * This function should allow an FS to ask what it's mount security
460 * options were so it can use those later for submounts, displaying
461 * mount options, or whatever.
462 */
463static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500464 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500465{
466 int rc = 0, i;
467 struct superblock_security_struct *sbsec = sb->s_security;
468 char *context = NULL;
469 u32 len;
470 char tmp;
471
Eric Parise0007522008-03-05 10:31:54 -0500472 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500473
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500474 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500475 return -EINVAL;
476
477 if (!ss_initialized)
478 return -EINVAL;
479
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500480 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500481 /* count the number of mount options for this sb */
482 for (i = 0; i < 8; i++) {
483 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500484 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500485 tmp >>= 1;
486 }
David P. Quigley11689d42009-01-16 09:22:03 -0500487 /* Check if the Label support flag is set */
488 if (sbsec->flags & SE_SBLABELSUPP)
489 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500490
Eric Parise0007522008-03-05 10:31:54 -0500491 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
492 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500493 rc = -ENOMEM;
494 goto out_free;
495 }
496
Eric Parise0007522008-03-05 10:31:54 -0500497 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
498 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500499 rc = -ENOMEM;
500 goto out_free;
501 }
502
503 i = 0;
504 if (sbsec->flags & FSCONTEXT_MNT) {
505 rc = security_sid_to_context(sbsec->sid, &context, &len);
506 if (rc)
507 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500508 opts->mnt_opts[i] = context;
509 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500510 }
511 if (sbsec->flags & CONTEXT_MNT) {
512 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
513 if (rc)
514 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500515 opts->mnt_opts[i] = context;
516 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500517 }
518 if (sbsec->flags & DEFCONTEXT_MNT) {
519 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
520 if (rc)
521 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500522 opts->mnt_opts[i] = context;
523 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500524 }
525 if (sbsec->flags & ROOTCONTEXT_MNT) {
526 struct inode *root = sbsec->sb->s_root->d_inode;
527 struct inode_security_struct *isec = root->i_security;
528
529 rc = security_sid_to_context(isec->sid, &context, &len);
530 if (rc)
531 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500532 opts->mnt_opts[i] = context;
533 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500534 }
David P. Quigley11689d42009-01-16 09:22:03 -0500535 if (sbsec->flags & SE_SBLABELSUPP) {
536 opts->mnt_opts[i] = NULL;
537 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
538 }
Eric Parisc9180a52007-11-30 13:00:35 -0500539
Eric Parise0007522008-03-05 10:31:54 -0500540 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500541
542 return 0;
543
544out_free:
Eric Parise0007522008-03-05 10:31:54 -0500545 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500546 return rc;
547}
548
549static int bad_option(struct superblock_security_struct *sbsec, char flag,
550 u32 old_sid, u32 new_sid)
551{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500552 char mnt_flags = sbsec->flags & SE_MNTMASK;
553
Eric Parisc9180a52007-11-30 13:00:35 -0500554 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500555 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500556 if (!(sbsec->flags & flag) ||
557 (old_sid != new_sid))
558 return 1;
559
560 /* check if we were passed the same options twice,
561 * aka someone passed context=a,context=b
562 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500563 if (!(sbsec->flags & SE_SBINITIALIZED))
564 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500565 return 1;
566 return 0;
567}
Eric Parise0007522008-03-05 10:31:54 -0500568
Eric Parisc9180a52007-11-30 13:00:35 -0500569/*
570 * Allow filesystems with binary mount data to explicitly set mount point
571 * labeling information.
572 */
Eric Parise0007522008-03-05 10:31:54 -0500573static int selinux_set_mnt_opts(struct super_block *sb,
574 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500575{
David Howells275bb412008-11-14 10:39:19 +1100576 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500577 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500578 struct superblock_security_struct *sbsec = sb->s_security;
579 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000580 struct inode *inode = sbsec->sb->s_root->d_inode;
581 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500582 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
583 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500584 char **mount_options = opts->mnt_opts;
585 int *flags = opts->mnt_opts_flags;
586 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500587
588 mutex_lock(&sbsec->lock);
589
590 if (!ss_initialized) {
591 if (!num_opts) {
592 /* Defer initialization until selinux_complete_init,
593 after the initial policy is loaded and the security
594 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500595 goto out;
596 }
597 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400598 printk(KERN_WARNING "SELinux: Unable to set superblock options "
599 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500600 goto out;
601 }
602
603 /*
Eric Parise0007522008-03-05 10:31:54 -0500604 * Binary mount data FS will come through this function twice. Once
605 * from an explicit call and once from the generic calls from the vfs.
606 * Since the generic VFS calls will not contain any security mount data
607 * we need to skip the double mount verification.
608 *
609 * This does open a hole in which we will not notice if the first
610 * mount using this sb set explict options and a second mount using
611 * this sb does not set any security options. (The first options
612 * will be used for both mounts)
613 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500614 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500615 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400616 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500617
618 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500619 * parse the mount options, check if they are valid sids.
620 * also check if someone is trying to mount the same sb more
621 * than once with different security options.
622 */
623 for (i = 0; i < num_opts; i++) {
624 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500625
626 if (flags[i] == SE_SBLABELSUPP)
627 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500628 rc = security_context_to_sid(mount_options[i],
629 strlen(mount_options[i]), &sid);
630 if (rc) {
631 printk(KERN_WARNING "SELinux: security_context_to_sid"
632 "(%s) failed for (dev %s, type %s) errno=%d\n",
633 mount_options[i], sb->s_id, name, rc);
634 goto out;
635 }
636 switch (flags[i]) {
637 case FSCONTEXT_MNT:
638 fscontext_sid = sid;
639
640 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
641 fscontext_sid))
642 goto out_double_mount;
643
644 sbsec->flags |= FSCONTEXT_MNT;
645 break;
646 case CONTEXT_MNT:
647 context_sid = sid;
648
649 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
650 context_sid))
651 goto out_double_mount;
652
653 sbsec->flags |= CONTEXT_MNT;
654 break;
655 case ROOTCONTEXT_MNT:
656 rootcontext_sid = sid;
657
658 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
659 rootcontext_sid))
660 goto out_double_mount;
661
662 sbsec->flags |= ROOTCONTEXT_MNT;
663
664 break;
665 case DEFCONTEXT_MNT:
666 defcontext_sid = sid;
667
668 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
669 defcontext_sid))
670 goto out_double_mount;
671
672 sbsec->flags |= DEFCONTEXT_MNT;
673
674 break;
675 default:
676 rc = -EINVAL;
677 goto out;
678 }
679 }
680
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500681 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500682 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500683 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500684 goto out_double_mount;
685 rc = 0;
686 goto out;
687 }
688
James Morris089be432008-07-15 18:32:49 +1000689 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500690 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500691
692 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500693 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500694 if (rc) {
695 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000696 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500697 goto out;
698 }
699
700 /* sets the context of the superblock for the fs being mounted. */
701 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100702 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500703 if (rc)
704 goto out;
705
706 sbsec->sid = fscontext_sid;
707 }
708
709 /*
710 * Switch to using mount point labeling behavior.
711 * sets the label used on all file below the mountpoint, and will set
712 * the superblock context if not already set.
713 */
714 if (context_sid) {
715 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100716 rc = may_context_mount_sb_relabel(context_sid, sbsec,
717 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500718 if (rc)
719 goto out;
720 sbsec->sid = context_sid;
721 } else {
David Howells275bb412008-11-14 10:39:19 +1100722 rc = may_context_mount_inode_relabel(context_sid, sbsec,
723 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500724 if (rc)
725 goto out;
726 }
727 if (!rootcontext_sid)
728 rootcontext_sid = context_sid;
729
730 sbsec->mntpoint_sid = context_sid;
731 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
732 }
733
734 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100735 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
736 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500737 if (rc)
738 goto out;
739
740 root_isec->sid = rootcontext_sid;
741 root_isec->initialized = 1;
742 }
743
744 if (defcontext_sid) {
745 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
746 rc = -EINVAL;
747 printk(KERN_WARNING "SELinux: defcontext option is "
748 "invalid for this filesystem type\n");
749 goto out;
750 }
751
752 if (defcontext_sid != sbsec->def_sid) {
753 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100754 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500755 if (rc)
756 goto out;
757 }
758
759 sbsec->def_sid = defcontext_sid;
760 }
761
762 rc = sb_finish_set_opts(sb);
763out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700764 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500766out_double_mount:
767 rc = -EINVAL;
768 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
769 "security settings for (dev %s, type %s)\n", sb->s_id, name);
770 goto out;
771}
772
773static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
774 struct super_block *newsb)
775{
776 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
777 struct superblock_security_struct *newsbsec = newsb->s_security;
778
779 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
780 int set_context = (oldsbsec->flags & CONTEXT_MNT);
781 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
782
Eric Paris0f5e6422008-04-21 16:24:11 -0400783 /*
784 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400785 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400786 */
Al Viroe8c26252010-03-23 06:36:54 -0400787 if (!ss_initialized)
Eric Paris0f5e6422008-04-21 16:24:11 -0400788 return;
Eric Parisc9180a52007-11-30 13:00:35 -0500789
Eric Parisc9180a52007-11-30 13:00:35 -0500790 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500791 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500792
Eric Paris5a552612008-04-09 14:08:35 -0400793 /* if fs is reusing a sb, just let its options stand... */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500794 if (newsbsec->flags & SE_SBINITIALIZED)
Eric Paris5a552612008-04-09 14:08:35 -0400795 return;
796
Eric Parisc9180a52007-11-30 13:00:35 -0500797 mutex_lock(&newsbsec->lock);
798
799 newsbsec->flags = oldsbsec->flags;
800
801 newsbsec->sid = oldsbsec->sid;
802 newsbsec->def_sid = oldsbsec->def_sid;
803 newsbsec->behavior = oldsbsec->behavior;
804
805 if (set_context) {
806 u32 sid = oldsbsec->mntpoint_sid;
807
808 if (!set_fscontext)
809 newsbsec->sid = sid;
810 if (!set_rootcontext) {
811 struct inode *newinode = newsb->s_root->d_inode;
812 struct inode_security_struct *newisec = newinode->i_security;
813 newisec->sid = sid;
814 }
815 newsbsec->mntpoint_sid = sid;
816 }
817 if (set_rootcontext) {
818 const struct inode *oldinode = oldsb->s_root->d_inode;
819 const struct inode_security_struct *oldisec = oldinode->i_security;
820 struct inode *newinode = newsb->s_root->d_inode;
821 struct inode_security_struct *newisec = newinode->i_security;
822
823 newisec->sid = oldisec->sid;
824 }
825
826 sb_finish_set_opts(newsb);
827 mutex_unlock(&newsbsec->lock);
828}
829
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200830static int selinux_parse_opts_str(char *options,
831 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500832{
Eric Parise0007522008-03-05 10:31:54 -0500833 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500834 char *context = NULL, *defcontext = NULL;
835 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500836 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500837
Eric Parise0007522008-03-05 10:31:54 -0500838 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500839
840 /* Standard string-based options. */
841 while ((p = strsep(&options, "|")) != NULL) {
842 int token;
843 substring_t args[MAX_OPT_ARGS];
844
845 if (!*p)
846 continue;
847
848 token = match_token(p, tokens, args);
849
850 switch (token) {
851 case Opt_context:
852 if (context || defcontext) {
853 rc = -EINVAL;
854 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
855 goto out_err;
856 }
857 context = match_strdup(&args[0]);
858 if (!context) {
859 rc = -ENOMEM;
860 goto out_err;
861 }
862 break;
863
864 case Opt_fscontext:
865 if (fscontext) {
866 rc = -EINVAL;
867 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
868 goto out_err;
869 }
870 fscontext = match_strdup(&args[0]);
871 if (!fscontext) {
872 rc = -ENOMEM;
873 goto out_err;
874 }
875 break;
876
877 case Opt_rootcontext:
878 if (rootcontext) {
879 rc = -EINVAL;
880 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
881 goto out_err;
882 }
883 rootcontext = match_strdup(&args[0]);
884 if (!rootcontext) {
885 rc = -ENOMEM;
886 goto out_err;
887 }
888 break;
889
890 case Opt_defcontext:
891 if (context || defcontext) {
892 rc = -EINVAL;
893 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
894 goto out_err;
895 }
896 defcontext = match_strdup(&args[0]);
897 if (!defcontext) {
898 rc = -ENOMEM;
899 goto out_err;
900 }
901 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500902 case Opt_labelsupport:
903 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500904 default:
905 rc = -EINVAL;
906 printk(KERN_WARNING "SELinux: unknown mount option\n");
907 goto out_err;
908
909 }
910 }
911
Eric Parise0007522008-03-05 10:31:54 -0500912 rc = -ENOMEM;
913 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
914 if (!opts->mnt_opts)
915 goto out_err;
916
917 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
918 if (!opts->mnt_opts_flags) {
919 kfree(opts->mnt_opts);
920 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500921 }
922
Eric Parise0007522008-03-05 10:31:54 -0500923 if (fscontext) {
924 opts->mnt_opts[num_mnt_opts] = fscontext;
925 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
926 }
927 if (context) {
928 opts->mnt_opts[num_mnt_opts] = context;
929 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
930 }
931 if (rootcontext) {
932 opts->mnt_opts[num_mnt_opts] = rootcontext;
933 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
934 }
935 if (defcontext) {
936 opts->mnt_opts[num_mnt_opts] = defcontext;
937 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
938 }
939
940 opts->num_mnt_opts = num_mnt_opts;
941 return 0;
942
Eric Parisc9180a52007-11-30 13:00:35 -0500943out_err:
944 kfree(context);
945 kfree(defcontext);
946 kfree(fscontext);
947 kfree(rootcontext);
948 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Eric Parise0007522008-03-05 10:31:54 -0500950/*
951 * string mount options parsing and call set the sbsec
952 */
953static int superblock_doinit(struct super_block *sb, void *data)
954{
955 int rc = 0;
956 char *options = data;
957 struct security_mnt_opts opts;
958
959 security_init_mnt_opts(&opts);
960
961 if (!data)
962 goto out;
963
964 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
965
966 rc = selinux_parse_opts_str(options, &opts);
967 if (rc)
968 goto out_err;
969
970out:
971 rc = selinux_set_mnt_opts(sb, &opts);
972
973out_err:
974 security_free_mnt_opts(&opts);
975 return rc;
976}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Adrian Bunk3583a712008-07-22 20:21:23 +0300978static void selinux_write_opts(struct seq_file *m,
979 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000980{
981 int i;
982 char *prefix;
983
984 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500985 char *has_comma;
986
987 if (opts->mnt_opts[i])
988 has_comma = strchr(opts->mnt_opts[i], ',');
989 else
990 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +1000991
992 switch (opts->mnt_opts_flags[i]) {
993 case CONTEXT_MNT:
994 prefix = CONTEXT_STR;
995 break;
996 case FSCONTEXT_MNT:
997 prefix = FSCONTEXT_STR;
998 break;
999 case ROOTCONTEXT_MNT:
1000 prefix = ROOTCONTEXT_STR;
1001 break;
1002 case DEFCONTEXT_MNT:
1003 prefix = DEFCONTEXT_STR;
1004 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001005 case SE_SBLABELSUPP:
1006 seq_putc(m, ',');
1007 seq_puts(m, LABELSUPP_STR);
1008 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001009 default:
1010 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001011 return;
Eric Paris2069f452008-07-04 09:47:13 +10001012 };
1013 /* we need a comma before each option */
1014 seq_putc(m, ',');
1015 seq_puts(m, prefix);
1016 if (has_comma)
1017 seq_putc(m, '\"');
1018 seq_puts(m, opts->mnt_opts[i]);
1019 if (has_comma)
1020 seq_putc(m, '\"');
1021 }
1022}
1023
1024static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1025{
1026 struct security_mnt_opts opts;
1027 int rc;
1028
1029 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001030 if (rc) {
1031 /* before policy load we may get EINVAL, don't show anything */
1032 if (rc == -EINVAL)
1033 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001034 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001035 }
Eric Paris2069f452008-07-04 09:47:13 +10001036
1037 selinux_write_opts(m, &opts);
1038
1039 security_free_mnt_opts(&opts);
1040
1041 return rc;
1042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static inline u16 inode_mode_to_security_class(umode_t mode)
1045{
1046 switch (mode & S_IFMT) {
1047 case S_IFSOCK:
1048 return SECCLASS_SOCK_FILE;
1049 case S_IFLNK:
1050 return SECCLASS_LNK_FILE;
1051 case S_IFREG:
1052 return SECCLASS_FILE;
1053 case S_IFBLK:
1054 return SECCLASS_BLK_FILE;
1055 case S_IFDIR:
1056 return SECCLASS_DIR;
1057 case S_IFCHR:
1058 return SECCLASS_CHR_FILE;
1059 case S_IFIFO:
1060 return SECCLASS_FIFO_FILE;
1061
1062 }
1063
1064 return SECCLASS_FILE;
1065}
1066
James Morris13402582005-09-30 14:24:34 -04001067static inline int default_protocol_stream(int protocol)
1068{
1069 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1070}
1071
1072static inline int default_protocol_dgram(int protocol)
1073{
1074 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1078{
1079 switch (family) {
1080 case PF_UNIX:
1081 switch (type) {
1082 case SOCK_STREAM:
1083 case SOCK_SEQPACKET:
1084 return SECCLASS_UNIX_STREAM_SOCKET;
1085 case SOCK_DGRAM:
1086 return SECCLASS_UNIX_DGRAM_SOCKET;
1087 }
1088 break;
1089 case PF_INET:
1090 case PF_INET6:
1091 switch (type) {
1092 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001093 if (default_protocol_stream(protocol))
1094 return SECCLASS_TCP_SOCKET;
1095 else
1096 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001098 if (default_protocol_dgram(protocol))
1099 return SECCLASS_UDP_SOCKET;
1100 else
1101 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001102 case SOCK_DCCP:
1103 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001104 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return SECCLASS_RAWIP_SOCKET;
1106 }
1107 break;
1108 case PF_NETLINK:
1109 switch (protocol) {
1110 case NETLINK_ROUTE:
1111 return SECCLASS_NETLINK_ROUTE_SOCKET;
1112 case NETLINK_FIREWALL:
1113 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001114 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1116 case NETLINK_NFLOG:
1117 return SECCLASS_NETLINK_NFLOG_SOCKET;
1118 case NETLINK_XFRM:
1119 return SECCLASS_NETLINK_XFRM_SOCKET;
1120 case NETLINK_SELINUX:
1121 return SECCLASS_NETLINK_SELINUX_SOCKET;
1122 case NETLINK_AUDIT:
1123 return SECCLASS_NETLINK_AUDIT_SOCKET;
1124 case NETLINK_IP6_FW:
1125 return SECCLASS_NETLINK_IP6FW_SOCKET;
1126 case NETLINK_DNRTMSG:
1127 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001128 case NETLINK_KOBJECT_UEVENT:
1129 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 default:
1131 return SECCLASS_NETLINK_SOCKET;
1132 }
1133 case PF_PACKET:
1134 return SECCLASS_PACKET_SOCKET;
1135 case PF_KEY:
1136 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001137 case PF_APPLETALK:
1138 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 return SECCLASS_SOCKET;
1142}
1143
1144#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001145static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 u16 tclass,
1147 u32 *sid)
1148{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001149 int rc;
1150 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Eric Paris828dfe12008-04-17 13:17:49 -04001152 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!buffer)
1154 return -ENOMEM;
1155
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001156 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1157 if (IS_ERR(path))
1158 rc = PTR_ERR(path);
1159 else {
1160 /* each process gets a /proc/PID/ entry. Strip off the
1161 * PID part to get a valid selinux labeling.
1162 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1163 while (path[1] >= '0' && path[1] <= '9') {
1164 path[1] = '/';
1165 path++;
1166 }
1167 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 free_page((unsigned long)buffer);
1170 return rc;
1171}
1172#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001173static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 u16 tclass,
1175 u32 *sid)
1176{
1177 return -EINVAL;
1178}
1179#endif
1180
1181/* The inode's security attributes must be initialized before first use. */
1182static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1183{
1184 struct superblock_security_struct *sbsec = NULL;
1185 struct inode_security_struct *isec = inode->i_security;
1186 u32 sid;
1187 struct dentry *dentry;
1188#define INITCONTEXTLEN 255
1189 char *context = NULL;
1190 unsigned len = 0;
1191 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (isec->initialized)
1194 goto out;
1195
Eric Paris23970742006-09-25 23:32:01 -07001196 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001198 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001201 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* Defer initialization until selinux_complete_init,
1203 after the initial policy is loaded and the security
1204 server is ready to handle calls. */
1205 spin_lock(&sbsec->isec_lock);
1206 if (list_empty(&isec->list))
1207 list_add(&isec->list, &sbsec->isec_head);
1208 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001209 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
1212 switch (sbsec->behavior) {
1213 case SECURITY_FS_USE_XATTR:
1214 if (!inode->i_op->getxattr) {
1215 isec->sid = sbsec->def_sid;
1216 break;
1217 }
1218
1219 /* Need a dentry, since the xattr API requires one.
1220 Life would be simpler if we could just pass the inode. */
1221 if (opt_dentry) {
1222 /* Called from d_instantiate or d_splice_alias. */
1223 dentry = dget(opt_dentry);
1224 } else {
1225 /* Called from selinux_complete_init, try to find a dentry. */
1226 dentry = d_find_alias(inode);
1227 }
1228 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001229 /*
1230 * this is can be hit on boot when a file is accessed
1231 * before the policy is loaded. When we load policy we
1232 * may find inodes that have no dentry on the
1233 * sbsec->isec_head list. No reason to complain as these
1234 * will get fixed up the next time we go through
1235 * inode_doinit with a dentry, before these inodes could
1236 * be used again by userspace.
1237 */
Eric Paris23970742006-09-25 23:32:01 -07001238 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
1241 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001242 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (!context) {
1244 rc = -ENOMEM;
1245 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001246 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001248 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1250 context, len);
1251 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001252 kfree(context);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Need a larger buffer. Query for the right size. */
1255 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1256 NULL, 0);
1257 if (rc < 0) {
1258 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001259 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001262 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (!context) {
1264 rc = -ENOMEM;
1265 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001266 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001268 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 rc = inode->i_op->getxattr(dentry,
1270 XATTR_NAME_SELINUX,
1271 context, len);
1272 }
1273 dput(dentry);
1274 if (rc < 0) {
1275 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001276 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001277 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 -rc, inode->i_sb->s_id, inode->i_ino);
1279 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001280 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 /* Map ENODATA to the default file SID */
1283 sid = sbsec->def_sid;
1284 rc = 0;
1285 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001286 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001287 sbsec->def_sid,
1288 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001290 char *dev = inode->i_sb->s_id;
1291 unsigned long ino = inode->i_ino;
1292
1293 if (rc == -EINVAL) {
1294 if (printk_ratelimit())
1295 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1296 "context=%s. This indicates you may need to relabel the inode or the "
1297 "filesystem in question.\n", ino, dev, context);
1298 } else {
1299 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1300 "returned %d for dev=%s ino=%ld\n",
1301 __func__, context, -rc, dev, ino);
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 kfree(context);
1304 /* Leave with the unlabeled SID */
1305 rc = 0;
1306 break;
1307 }
1308 }
1309 kfree(context);
1310 isec->sid = sid;
1311 break;
1312 case SECURITY_FS_USE_TASK:
1313 isec->sid = isec->task_sid;
1314 break;
1315 case SECURITY_FS_USE_TRANS:
1316 /* Default to the fs SID. */
1317 isec->sid = sbsec->sid;
1318
1319 /* Try to obtain a transition SID. */
1320 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001321 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1322 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001324 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 isec->sid = sid;
1326 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001327 case SECURITY_FS_USE_MNTPOINT:
1328 isec->sid = sbsec->mntpoint_sid;
1329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001331 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 isec->sid = sbsec->sid;
1333
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001334 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Paul Moore23a5a7a2014-03-19 16:46:18 -04001335 /* We must have a dentry to determine the label on
1336 * procfs inodes */
1337 if (opt_dentry)
1338 /* Called from d_instantiate or
1339 * d_splice_alias. */
1340 dentry = dget(opt_dentry);
1341 else
1342 /* Called from selinux_complete_init, try to
1343 * find a dentry. */
1344 dentry = d_find_alias(inode);
1345 /*
1346 * This can be hit on boot when a file is accessed
1347 * before the policy is loaded. When we load policy we
1348 * may find inodes that have no dentry on the
1349 * sbsec->isec_head list. No reason to complain as
1350 * these will get fixed up the next time we go through
1351 * inode_doinit() with a dentry, before these inodes
1352 * could be used again by userspace.
1353 */
1354 if (!dentry)
1355 goto out_unlock;
1356 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1357 rc = selinux_proc_get_sid(dentry, isec->sclass, &sid);
1358 dput(dentry);
1359 if (rc)
1360 goto out_unlock;
1361 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 break;
1364 }
1365
1366 isec->initialized = 1;
1367
Eric Paris23970742006-09-25 23:32:01 -07001368out_unlock:
1369 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370out:
1371 if (isec->sclass == SECCLASS_FILE)
1372 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return rc;
1374}
1375
1376/* Convert a Linux signal to an access vector. */
1377static inline u32 signal_to_av(int sig)
1378{
1379 u32 perm = 0;
1380
1381 switch (sig) {
1382 case SIGCHLD:
1383 /* Commonly granted from child to parent. */
1384 perm = PROCESS__SIGCHLD;
1385 break;
1386 case SIGKILL:
1387 /* Cannot be caught or ignored */
1388 perm = PROCESS__SIGKILL;
1389 break;
1390 case SIGSTOP:
1391 /* Cannot be caught or ignored */
1392 perm = PROCESS__SIGSTOP;
1393 break;
1394 default:
1395 /* All other signals. */
1396 perm = PROCESS__SIGNAL;
1397 break;
1398 }
1399
1400 return perm;
1401}
1402
David Howells275bb412008-11-14 10:39:19 +11001403/*
David Howellsd84f4f92008-11-14 10:39:23 +11001404 * Check permission between a pair of credentials
1405 * fork check, ptrace check, etc.
1406 */
1407static int cred_has_perm(const struct cred *actor,
1408 const struct cred *target,
1409 u32 perms)
1410{
1411 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1412
1413 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1414}
1415
1416/*
David Howells88e67f32008-11-14 10:39:21 +11001417 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001418 * fork check, ptrace check, etc.
1419 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001420 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001421 */
1422static int task_has_perm(const struct task_struct *tsk1,
1423 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 u32 perms)
1425{
David Howells275bb412008-11-14 10:39:19 +11001426 const struct task_security_struct *__tsec1, *__tsec2;
1427 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
David Howells275bb412008-11-14 10:39:19 +11001429 rcu_read_lock();
1430 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1431 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1432 rcu_read_unlock();
1433 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
David Howells3b11a1d2008-11-14 10:39:26 +11001436/*
1437 * Check permission between current and another task, e.g. signal checks,
1438 * fork check, ptrace check, etc.
1439 * current is the actor and tsk2 is the target
1440 * - this uses current's subjective creds
1441 */
1442static int current_has_perm(const struct task_struct *tsk,
1443 u32 perms)
1444{
1445 u32 sid, tsid;
1446
1447 sid = current_sid();
1448 tsid = task_sid(tsk);
1449 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1450}
1451
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001452#if CAP_LAST_CAP > 63
1453#error Fix SELinux to handle capabilities > 63.
1454#endif
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001457static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001458 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Thomas Liu2bf49692009-07-14 12:14:09 -04001460 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001461 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001462 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001463 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001464 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001465 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001466 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Thomas Liu2bf49692009-07-14 12:14:09 -04001468 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001469 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001470 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ad.u.cap = cap;
1472
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001473 switch (CAP_TO_INDEX(cap)) {
1474 case 0:
1475 sclass = SECCLASS_CAPABILITY;
1476 break;
1477 case 1:
1478 sclass = SECCLASS_CAPABILITY2;
1479 break;
1480 default:
1481 printk(KERN_ERR
1482 "SELinux: out of range capability %d\n", cap);
1483 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001484 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001485 }
Eric Paris06112162008-11-11 22:02:50 +11001486
David Howells275bb412008-11-14 10:39:19 +11001487 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001488 if (audit == SECURITY_CAP_AUDIT) {
1489 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1490 if (rc2)
1491 return rc2;
1492 }
Eric Paris06112162008-11-11 22:02:50 +11001493 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496/* Check whether a task is allowed to use a system operation. */
1497static int task_has_system(struct task_struct *tsk,
1498 u32 perms)
1499{
David Howells275bb412008-11-14 10:39:19 +11001500 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David Howells275bb412008-11-14 10:39:19 +11001502 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 SECCLASS_SYSTEM, perms, NULL);
1504}
1505
1506/* Check whether a task has a particular permission to an inode.
1507 The 'adp' parameter is optional and allows other audit
1508 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001509static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 struct inode *inode,
1511 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001512 struct common_audit_data *adp,
1513 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001516 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
David Howellse0e81732009-09-02 09:13:40 +01001518 validate_creds(cred);
1519
Eric Paris828dfe12008-04-17 13:17:49 -04001520 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001521 return 0;
1522
David Howells88e67f32008-11-14 10:39:21 +11001523 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 isec = inode->i_security;
1525
Eric Paris9ade0cf2011-04-25 16:26:29 -04001526 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001529static int inode_has_perm_noadp(const struct cred *cred,
1530 struct inode *inode,
1531 u32 perms,
1532 unsigned flags)
1533{
1534 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001535 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001536
1537 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1538 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001539 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001540 return inode_has_perm(cred, inode, perms, &ad, flags);
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/* Same as inode_has_perm, but pass explicit audit data containing
1544 the dentry to help the auditing code to more easily generate the
1545 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001546static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct dentry *dentry,
1548 u32 av)
1549{
1550 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001551 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001552 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001553
Eric Paris2875fa02011-04-28 16:04:24 -04001554 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1555 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001556 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001557 return inode_has_perm(cred, inode, av, &ad, 0);
1558}
1559
1560/* Same as inode_has_perm, but pass explicit audit data containing
1561 the path to help the auditing code to more easily generate the
1562 pathname if needed. */
1563static inline int path_has_perm(const struct cred *cred,
1564 struct path *path,
1565 u32 av)
1566{
1567 struct inode *inode = path->dentry->d_inode;
1568 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001569 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001570
Eric Parisf48b7392011-04-25 12:54:27 -04001571 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001572 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001573 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001574 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/* Check whether a task can use an open file descriptor to
1578 access an inode in a given way. Check access to the
1579 descriptor itself, and then use dentry_has_perm to
1580 check a particular permission to the file.
1581 Access to the descriptor is implicitly granted if it
1582 has the same SID as the process. If av is zero, then
1583 access to the file is not checked, e.g. for cases
1584 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001585static int file_has_perm(const struct cred *cred,
1586 struct file *file,
1587 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001590 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001591 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001592 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001593 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int rc;
1595
Eric Parisf48b7392011-04-25 12:54:27 -04001596 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1597 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001598 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David Howells275bb412008-11-14 10:39:19 +11001600 if (sid != fsec->sid) {
1601 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 SECCLASS_FD,
1603 FD__USE,
1604 &ad);
1605 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001610 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001612 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
David Howells88e67f32008-11-14 10:39:21 +11001614out:
1615 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618/* Check whether a task can create a file. */
1619static int may_create(struct inode *dir,
1620 struct dentry *dentry,
1621 u16 tclass)
1622{
Paul Moore5fb49872010-04-22 14:46:19 -04001623 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct inode_security_struct *dsec;
1625 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001626 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001627 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001628 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int rc;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 dsec = dir->i_security;
1632 sbsec = dir->i_sb->s_security;
1633
David Howells275bb412008-11-14 10:39:19 +11001634 sid = tsec->sid;
1635 newsid = tsec->create_sid;
1636
Eric Parisa2694342011-04-25 13:10:27 -04001637 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1638 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001639 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David Howells275bb412008-11-14 10:39:19 +11001641 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 DIR__ADD_NAME | DIR__SEARCH,
1643 &ad);
1644 if (rc)
1645 return rc;
1646
David P. Quigleycd895962009-01-16 09:22:04 -05001647 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001648 rc = security_transition_sid(sid, dsec->sid, tclass,
1649 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (rc)
1651 return rc;
1652 }
1653
David Howells275bb412008-11-14 10:39:19 +11001654 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (rc)
1656 return rc;
1657
1658 return avc_has_perm(newsid, sbsec->sid,
1659 SECCLASS_FILESYSTEM,
1660 FILESYSTEM__ASSOCIATE, &ad);
1661}
1662
Michael LeMay4eb582c2006-06-26 00:24:57 -07001663/* Check whether a task can create a key. */
1664static int may_create_key(u32 ksid,
1665 struct task_struct *ctx)
1666{
David Howells275bb412008-11-14 10:39:19 +11001667 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001668
David Howells275bb412008-11-14 10:39:19 +11001669 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001670}
1671
Eric Paris828dfe12008-04-17 13:17:49 -04001672#define MAY_LINK 0
1673#define MAY_UNLINK 1
1674#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676/* Check whether a task can link, unlink, or rmdir a file/directory. */
1677static int may_link(struct inode *dir,
1678 struct dentry *dentry,
1679 int kind)
1680
1681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001683 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001684 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001685 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 u32 av;
1687 int rc;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 dsec = dir->i_security;
1690 isec = dentry->d_inode->i_security;
1691
Eric Parisa2694342011-04-25 13:10:27 -04001692 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1693 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001694 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 av = DIR__SEARCH;
1697 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001698 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (rc)
1700 return rc;
1701
1702 switch (kind) {
1703 case MAY_LINK:
1704 av = FILE__LINK;
1705 break;
1706 case MAY_UNLINK:
1707 av = FILE__UNLINK;
1708 break;
1709 case MAY_RMDIR:
1710 av = DIR__RMDIR;
1711 break;
1712 default:
Eric Paris744ba352008-04-17 11:52:44 -04001713 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1714 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return 0;
1716 }
1717
David Howells275bb412008-11-14 10:39:19 +11001718 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return rc;
1720}
1721
1722static inline int may_rename(struct inode *old_dir,
1723 struct dentry *old_dentry,
1724 struct inode *new_dir,
1725 struct dentry *new_dentry)
1726{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001728 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001729 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001730 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 u32 av;
1732 int old_is_dir, new_is_dir;
1733 int rc;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 old_dsec = old_dir->i_security;
1736 old_isec = old_dentry->d_inode->i_security;
1737 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1738 new_dsec = new_dir->i_security;
1739
Eric Parisa2694342011-04-25 13:10:27 -04001740 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001741 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Eric Parisa2694342011-04-25 13:10:27 -04001743 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001744 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1746 if (rc)
1747 return rc;
David Howells275bb412008-11-14 10:39:19 +11001748 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 old_isec->sclass, FILE__RENAME, &ad);
1750 if (rc)
1751 return rc;
1752 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001753 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 old_isec->sclass, DIR__REPARENT, &ad);
1755 if (rc)
1756 return rc;
1757 }
1758
Eric Parisa2694342011-04-25 13:10:27 -04001759 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 av = DIR__ADD_NAME | DIR__SEARCH;
1761 if (new_dentry->d_inode)
1762 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001763 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (rc)
1765 return rc;
1766 if (new_dentry->d_inode) {
1767 new_isec = new_dentry->d_inode->i_security;
1768 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001769 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 new_isec->sclass,
1771 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1772 if (rc)
1773 return rc;
1774 }
1775
1776 return 0;
1777}
1778
1779/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001780static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct super_block *sb,
1782 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001783 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001786 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001789 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/* Convert a Linux mode and permission mask to an access vector. */
1793static inline u32 file_mask_to_av(int mode, int mask)
1794{
1795 u32 av = 0;
1796
Al Virodba19c62011-07-25 20:49:29 -04001797 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (mask & MAY_EXEC)
1799 av |= FILE__EXECUTE;
1800 if (mask & MAY_READ)
1801 av |= FILE__READ;
1802
1803 if (mask & MAY_APPEND)
1804 av |= FILE__APPEND;
1805 else if (mask & MAY_WRITE)
1806 av |= FILE__WRITE;
1807
1808 } else {
1809 if (mask & MAY_EXEC)
1810 av |= DIR__SEARCH;
1811 if (mask & MAY_WRITE)
1812 av |= DIR__WRITE;
1813 if (mask & MAY_READ)
1814 av |= DIR__READ;
1815 }
1816
1817 return av;
1818}
1819
1820/* Convert a Linux file to an access vector. */
1821static inline u32 file_to_av(struct file *file)
1822{
1823 u32 av = 0;
1824
1825 if (file->f_mode & FMODE_READ)
1826 av |= FILE__READ;
1827 if (file->f_mode & FMODE_WRITE) {
1828 if (file->f_flags & O_APPEND)
1829 av |= FILE__APPEND;
1830 else
1831 av |= FILE__WRITE;
1832 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001833 if (!av) {
1834 /*
1835 * Special file opened with flags 3 for ioctl-only use.
1836 */
1837 av = FILE__IOCTL;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 return av;
1841}
1842
Eric Paris8b6a5a32008-10-29 17:06:46 -04001843/*
1844 * Convert a file to an access vector and include the correct open
1845 * open permission.
1846 */
1847static inline u32 open_file_to_av(struct file *file)
1848{
1849 u32 av = file_to_av(file);
1850
Eric Paris49b7b8d2010-07-23 11:44:09 -04001851 if (selinux_policycap_openperm)
1852 av |= FILE__OPEN;
1853
Eric Paris8b6a5a32008-10-29 17:06:46 -04001854 return av;
1855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857/* Hook functions begin here. */
1858
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05001859static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1860{
1861 u32 mysid = current_sid();
1862 u32 mgrsid = task_sid(mgr);
1863
1864 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1865}
1866
1867static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1868{
1869 u32 mysid = current_sid();
1870 u32 fromsid = task_sid(from);
1871 u32 tosid = task_sid(to);
1872 int rc;
1873
1874 if (mysid != fromsid) {
1875 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1876 if (rc)
1877 return rc;
1878 }
1879
1880 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1881}
1882
1883static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1884{
1885 u32 fromsid = task_sid(from);
1886 u32 tosid = task_sid(to);
1887 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1888}
1889
1890static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1891{
1892 u32 sid = task_sid(to);
1893 struct file_security_struct *fsec = file->f_security;
1894 struct inode *inode = file->f_path.dentry->d_inode;
1895 struct inode_security_struct *isec = inode->i_security;
1896 struct common_audit_data ad;
1897 struct selinux_audit_data sad = {0,};
1898 int rc;
1899
1900 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1901 ad.u.path = file->f_path;
1902 ad.selinux_audit_data = &sad;
1903
1904 if (sid != fsec->sid) {
1905 rc = avc_has_perm(sid, fsec->sid,
1906 SECCLASS_FD,
1907 FD__USE,
1908 &ad);
1909 if (rc)
1910 return rc;
1911 }
1912
1913 if (unlikely(IS_PRIVATE(inode)))
1914 return 0;
1915
1916 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1917 &ad);
1918}
1919
Ingo Molnar9e488582009-05-07 19:26:19 +10001920static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001921 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int rc;
1924
Ingo Molnar9e488582009-05-07 19:26:19 +10001925 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (rc)
1927 return rc;
1928
Eric Paris69f594a2012-01-03 12:25:15 -05001929 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001930 u32 sid = current_sid();
1931 u32 csid = task_sid(child);
1932 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001933 }
1934
David Howells3b11a1d2008-11-14 10:39:26 +11001935 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001936}
1937
1938static int selinux_ptrace_traceme(struct task_struct *parent)
1939{
1940 int rc;
1941
Eric Paris200ac532009-02-12 15:01:04 -05001942 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001943 if (rc)
1944 return rc;
1945
1946 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
1949static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001950 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 int error;
1953
David Howells3b11a1d2008-11-14 10:39:26 +11001954 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (error)
1956 return error;
1957
Eric Paris200ac532009-02-12 15:01:04 -05001958 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
David Howellsd84f4f92008-11-14 10:39:23 +11001961static int selinux_capset(struct cred *new, const struct cred *old,
1962 const kernel_cap_t *effective,
1963 const kernel_cap_t *inheritable,
1964 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 int error;
1967
Eric Paris200ac532009-02-12 15:01:04 -05001968 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001969 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (error)
1971 return error;
1972
David Howellsd84f4f92008-11-14 10:39:23 +11001973 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
James Morris5626d3e2009-01-30 10:05:06 +11001976/*
1977 * (This comment used to live with the selinux_task_setuid hook,
1978 * which was removed).
1979 *
1980 * Since setuid only affects the current process, and since the SELinux
1981 * controls are not based on the Linux identity attributes, SELinux does not
1982 * need to control this operation. However, SELinux does control the use of
1983 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1984 */
1985
Eric Paris6a9de492012-01-03 12:25:14 -05001986static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1987 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int rc;
1990
Eric Paris6a9de492012-01-03 12:25:14 -05001991 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (rc)
1993 return rc;
1994
Eric Paris6a9de492012-01-03 12:25:14 -05001995 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1999{
David Howells88e67f32008-11-14 10:39:21 +11002000 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int rc = 0;
2002
2003 if (!sb)
2004 return 0;
2005
2006 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002007 case Q_SYNC:
2008 case Q_QUOTAON:
2009 case Q_QUOTAOFF:
2010 case Q_SETINFO:
2011 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002012 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002013 break;
2014 case Q_GETFMT:
2015 case Q_GETINFO:
2016 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002017 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002018 break;
2019 default:
2020 rc = 0; /* let the kernel handle invalid cmds */
2021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023 return rc;
2024}
2025
2026static int selinux_quota_on(struct dentry *dentry)
2027{
David Howells88e67f32008-11-14 10:39:21 +11002028 const struct cred *cred = current_cred();
2029
Eric Paris2875fa02011-04-28 16:04:24 -04002030 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Eric Paris12b30522010-11-15 18:36:29 -05002033static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 int rc;
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002038 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2039 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002040 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2041 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002042 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2043 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2044 /* Set level of messages printed to console */
2045 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002046 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2047 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002048 case SYSLOG_ACTION_CLOSE: /* Close log */
2049 case SYSLOG_ACTION_OPEN: /* Open log */
2050 case SYSLOG_ACTION_READ: /* Read from log */
2051 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2052 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002053 default:
2054 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057 return rc;
2058}
2059
2060/*
2061 * Check that a process has enough memory to allocate a new virtual
2062 * mapping. 0 means there is enough memory for the allocation to
2063 * succeed and -ENOMEM implies there is not.
2064 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * Do not audit the selinux permission check, as this is applied to all
2066 * processes that allocate mappings.
2067 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002068static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Eric Paris6a9de492012-01-03 12:25:14 -05002072 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002073 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (rc == 0)
2075 cap_sys_admin = 1;
2076
Alan Cox34b4e4a2007-08-22 14:01:28 -07002077 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080/* binprm security operations */
2081
David Howellsa6f76f22008-11-14 10:39:24 +11002082static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
David Howellsa6f76f22008-11-14 10:39:24 +11002084 const struct task_security_struct *old_tsec;
2085 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002087 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002088 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002089 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 int rc;
2091
Eric Paris200ac532009-02-12 15:01:04 -05002092 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (rc)
2094 return rc;
2095
David Howellsa6f76f22008-11-14 10:39:24 +11002096 /* SELinux context only depends on initial program or script and not
2097 * the script interpreter */
2098 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100
David Howellsa6f76f22008-11-14 10:39:24 +11002101 old_tsec = current_security();
2102 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 isec = inode->i_security;
2104
2105 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002106 new_tsec->sid = old_tsec->sid;
2107 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Michael LeMay28eba5b2006-06-27 02:53:42 -07002109 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002110 new_tsec->create_sid = 0;
2111 new_tsec->keycreate_sid = 0;
2112 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
David Howellsa6f76f22008-11-14 10:39:24 +11002114 if (old_tsec->exec_sid) {
2115 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002117 new_tsec->exec_sid = 0;
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002118
2119 /*
2120 * Minimize confusion: if no_new_privs and a transition is
2121 * explicitly requested, then fail the exec.
2122 */
2123 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2124 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 } else {
2126 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002127 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002128 SECCLASS_PROCESS, NULL,
2129 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (rc)
2131 return rc;
2132 }
2133
Eric Parisf48b7392011-04-25 12:54:27 -04002134 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002135 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002136 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002138 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2139 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002140 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
David Howellsa6f76f22008-11-14 10:39:24 +11002142 if (new_tsec->sid == old_tsec->sid) {
2143 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2145 if (rc)
2146 return rc;
2147 } else {
2148 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002149 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2151 if (rc)
2152 return rc;
2153
David Howellsa6f76f22008-11-14 10:39:24 +11002154 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2156 if (rc)
2157 return rc;
2158
David Howellsa6f76f22008-11-14 10:39:24 +11002159 /* Check for shared state */
2160 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2161 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2162 SECCLASS_PROCESS, PROCESS__SHARE,
2163 NULL);
2164 if (rc)
2165 return -EPERM;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
David Howellsa6f76f22008-11-14 10:39:24 +11002168 /* Make sure that anyone attempting to ptrace over a task that
2169 * changes its SID has the appropriate permit */
2170 if (bprm->unsafe &
2171 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2172 struct task_struct *tracer;
2173 struct task_security_struct *sec;
2174 u32 ptsid = 0;
2175
2176 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002177 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002178 if (likely(tracer != NULL)) {
2179 sec = __task_cred(tracer)->security;
2180 ptsid = sec->sid;
2181 }
2182 rcu_read_unlock();
2183
2184 if (ptsid != 0) {
2185 rc = avc_has_perm(ptsid, new_tsec->sid,
2186 SECCLASS_PROCESS,
2187 PROCESS__PTRACE, NULL);
2188 if (rc)
2189 return -EPERM;
2190 }
2191 }
2192
2193 /* Clear any possibly unsafe personality bits on exec: */
2194 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198}
2199
Eric Paris828dfe12008-04-17 13:17:49 -04002200static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Paul Moore5fb49872010-04-22 14:46:19 -04002202 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002203 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int atsecure = 0;
2205
David Howells275bb412008-11-14 10:39:19 +11002206 sid = tsec->sid;
2207 osid = tsec->osid;
2208
2209 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* Enable secure mode for SIDs transitions unless
2211 the noatsecure permission is granted between
2212 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002213 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002214 SECCLASS_PROCESS,
2215 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217
Eric Paris200ac532009-02-12 15:01:04 -05002218 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002222static inline void flush_unauthorized_files(const struct cred *cred,
2223 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Thomas Liu2bf49692009-07-14 12:14:09 -04002225 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002226 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002228 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002229 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002231 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002233 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002235 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002236 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002237 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002238 struct inode *inode;
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /* Revalidate access to controlling tty.
2241 Use inode_has_perm on the tty inode directly rather
2242 than using file_has_perm, as this particular open
2243 file may belong to another process and we are only
2244 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002245 file_priv = list_first_entry(&tty->tty_files,
2246 struct tty_file_private, list);
2247 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002248 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002249 if (inode_has_perm_noadp(cred, inode,
2250 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002251 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002254 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002255 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002257 /* Reset controlling tty. */
2258 if (drop_tty)
2259 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Revalidate access to inherited open files. */
2262
Eric Parisf48b7392011-04-25 12:54:27 -04002263 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002264 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 spin_lock(&files->file_lock);
2267 for (;;) {
2268 unsigned long set, i;
2269 int fd;
2270
2271 j++;
Josh Boyer27cd8f52012-07-25 10:40:34 -04002272 i = j * BITS_PER_LONG;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002273 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002274 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002276 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (!set)
2278 continue;
2279 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002280 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (set & 1) {
2282 file = fget(i);
2283 if (!file)
2284 continue;
David Howells88e67f32008-11-14 10:39:21 +11002285 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 file,
2287 file_to_av(file))) {
2288 sys_close(i);
2289 fd = get_unused_fd();
2290 if (fd != i) {
2291 if (fd >= 0)
2292 put_unused_fd(fd);
2293 fput(file);
2294 continue;
2295 }
2296 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002297 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 } else {
David Howells745ca242008-11-14 10:39:22 +11002299 devnull = dentry_open(
2300 dget(selinux_null),
2301 mntget(selinuxfs_mount),
2302 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002303 if (IS_ERR(devnull)) {
2304 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 put_unused_fd(fd);
2306 fput(file);
2307 continue;
2308 }
2309 }
2310 fd_install(fd, devnull);
2311 }
2312 fput(file);
2313 }
2314 }
2315 spin_lock(&files->file_lock);
2316
2317 }
2318 spin_unlock(&files->file_lock);
2319}
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321/*
David Howellsa6f76f22008-11-14 10:39:24 +11002322 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 */
David Howellsa6f76f22008-11-14 10:39:24 +11002324static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
David Howellsa6f76f22008-11-14 10:39:24 +11002326 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 int rc, i;
2329
David Howellsa6f76f22008-11-14 10:39:24 +11002330 new_tsec = bprm->cred->security;
2331 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return;
2333
2334 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002335 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
David Howellsa6f76f22008-11-14 10:39:24 +11002337 /* Always clear parent death signal on SID transitions. */
2338 current->pdeath_signal = 0;
2339
2340 /* Check whether the new SID can inherit resource limits from the old
2341 * SID. If not, reset all soft limits to the lower of the current
2342 * task's hard limit and the init task's soft limit.
2343 *
2344 * Note that the setting of hard limits (even to lower them) can be
2345 * controlled by the setrlimit check. The inclusion of the init task's
2346 * soft limit into the computation is to avoid resetting soft limits
2347 * higher than the default soft limit for cases where the default is
2348 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2349 */
2350 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2351 PROCESS__RLIMITINH, NULL);
2352 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002353 /* protect against do_prlimit() */
2354 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002355 for (i = 0; i < RLIM_NLIMITS; i++) {
2356 rlim = current->signal->rlim + i;
2357 initrlim = init_task.signal->rlim + i;
2358 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2359 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002360 task_unlock(current);
2361 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002362 }
2363}
2364
2365/*
2366 * Clean up the process immediately after the installation of new credentials
2367 * due to exec
2368 */
2369static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2370{
2371 const struct task_security_struct *tsec = current_security();
2372 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002373 u32 osid, sid;
2374 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002375
David Howellsa6f76f22008-11-14 10:39:24 +11002376 osid = tsec->osid;
2377 sid = tsec->sid;
2378
2379 if (sid == osid)
2380 return;
2381
2382 /* Check whether the new SID can inherit signal state from the old SID.
2383 * If not, clear itimers to avoid subsequent signal generation and
2384 * flush and unblock signals.
2385 *
2386 * This must occur _after_ the task SID has been updated so that any
2387 * kill done after the flush will be checked against the new SID.
2388 */
2389 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (rc) {
2391 memset(&itimer, 0, sizeof itimer);
2392 for (i = 0; i < 3; i++)
2393 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002395 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2396 __flush_signals(current);
2397 flush_signal_handlers(current, 1);
2398 sigemptyset(&current->blocked);
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 spin_unlock_irq(&current->sighand->siglock);
2401 }
2402
David Howellsa6f76f22008-11-14 10:39:24 +11002403 /* Wake up the parent if it is waiting so that it can recheck
2404 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002405 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002406 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002407 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410/* superblock security operations */
2411
2412static int selinux_sb_alloc_security(struct super_block *sb)
2413{
2414 return superblock_alloc_security(sb);
2415}
2416
2417static void selinux_sb_free_security(struct super_block *sb)
2418{
2419 superblock_free_security(sb);
2420}
2421
2422static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2423{
2424 if (plen > olen)
2425 return 0;
2426
2427 return !memcmp(prefix, option, plen);
2428}
2429
2430static inline int selinux_option(char *option, int len)
2431{
Eric Paris832cbd92008-04-01 13:24:09 -04002432 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2433 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2434 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002435 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2436 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
2439static inline void take_option(char **to, char *from, int *first, int len)
2440{
2441 if (!*first) {
2442 **to = ',';
2443 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002444 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 *first = 0;
2446 memcpy(*to, from, len);
2447 *to += len;
2448}
2449
Eric Paris828dfe12008-04-17 13:17:49 -04002450static inline void take_selinux_option(char **to, char *from, int *first,
2451 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002452{
2453 int current_size = 0;
2454
2455 if (!*first) {
2456 **to = '|';
2457 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002458 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002459 *first = 0;
2460
2461 while (current_size < len) {
2462 if (*from != '"') {
2463 **to = *from;
2464 *to += 1;
2465 }
2466 from += 1;
2467 current_size += 1;
2468 }
2469}
2470
Eric Parise0007522008-03-05 10:31:54 -05002471static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 int fnosec, fsec, rc = 0;
2474 char *in_save, *in_curr, *in_end;
2475 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002476 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 in_curr = orig;
2479 sec_curr = copy;
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2482 if (!nosec) {
2483 rc = -ENOMEM;
2484 goto out;
2485 }
2486
2487 nosec_save = nosec;
2488 fnosec = fsec = 1;
2489 in_save = in_end = orig;
2490
2491 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002492 if (*in_end == '"')
2493 open_quote = !open_quote;
2494 if ((*in_end == ',' && open_quote == 0) ||
2495 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 int len = in_end - in_curr;
2497
2498 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002499 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 else
2501 take_option(&nosec, in_curr, &fnosec, len);
2502
2503 in_curr = in_end + 1;
2504 }
2505 } while (*in_end++);
2506
Eric Paris6931dfc2005-06-30 02:58:51 -07002507 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002508 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509out:
2510 return rc;
2511}
2512
Eric Paris026eb162011-03-03 16:09:14 -05002513static int selinux_sb_remount(struct super_block *sb, void *data)
2514{
2515 int rc, i, *flags;
2516 struct security_mnt_opts opts;
2517 char *secdata, **mount_options;
2518 struct superblock_security_struct *sbsec = sb->s_security;
2519
2520 if (!(sbsec->flags & SE_SBINITIALIZED))
2521 return 0;
2522
2523 if (!data)
2524 return 0;
2525
2526 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2527 return 0;
2528
2529 security_init_mnt_opts(&opts);
2530 secdata = alloc_secdata();
2531 if (!secdata)
2532 return -ENOMEM;
2533 rc = selinux_sb_copy_data(data, secdata);
2534 if (rc)
2535 goto out_free_secdata;
2536
2537 rc = selinux_parse_opts_str(secdata, &opts);
2538 if (rc)
2539 goto out_free_secdata;
2540
2541 mount_options = opts.mnt_opts;
2542 flags = opts.mnt_opts_flags;
2543
2544 for (i = 0; i < opts.num_mnt_opts; i++) {
2545 u32 sid;
2546 size_t len;
2547
2548 if (flags[i] == SE_SBLABELSUPP)
2549 continue;
2550 len = strlen(mount_options[i]);
2551 rc = security_context_to_sid(mount_options[i], len, &sid);
2552 if (rc) {
2553 printk(KERN_WARNING "SELinux: security_context_to_sid"
2554 "(%s) failed for (dev %s, type %s) errno=%d\n",
2555 mount_options[i], sb->s_id, sb->s_type->name, rc);
2556 goto out_free_opts;
2557 }
2558 rc = -EINVAL;
2559 switch (flags[i]) {
2560 case FSCONTEXT_MNT:
2561 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2562 goto out_bad_option;
2563 break;
2564 case CONTEXT_MNT:
2565 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2566 goto out_bad_option;
2567 break;
2568 case ROOTCONTEXT_MNT: {
2569 struct inode_security_struct *root_isec;
2570 root_isec = sb->s_root->d_inode->i_security;
2571
2572 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2573 goto out_bad_option;
2574 break;
2575 }
2576 case DEFCONTEXT_MNT:
2577 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2578 goto out_bad_option;
2579 break;
2580 default:
2581 goto out_free_opts;
2582 }
2583 }
2584
2585 rc = 0;
2586out_free_opts:
2587 security_free_mnt_opts(&opts);
2588out_free_secdata:
2589 free_secdata(secdata);
2590 return rc;
2591out_bad_option:
2592 printk(KERN_WARNING "SELinux: unable to change security options "
2593 "during remount (dev %s, type=%s)\n", sb->s_id,
2594 sb->s_type->name);
2595 goto out_free_opts;
2596}
2597
James Morris12204e22008-12-19 10:44:42 +11002598static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
David Howells88e67f32008-11-14 10:39:21 +11002600 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002601 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002602 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 int rc;
2604
2605 rc = superblock_doinit(sb, data);
2606 if (rc)
2607 return rc;
2608
James Morris74192242008-12-19 11:41:10 +11002609 /* Allow all mounts performed by the kernel */
2610 if (flags & MS_KERNMOUNT)
2611 return 0;
2612
Eric Parisa2694342011-04-25 13:10:27 -04002613 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002614 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002615 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002616 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
David Howells726c3342006-06-23 02:02:58 -07002619static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
David Howells88e67f32008-11-14 10:39:21 +11002621 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002622 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002623 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Eric Parisa2694342011-04-25 13:10:27 -04002625 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002626 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002627 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002628 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
Eric Paris828dfe12008-04-17 13:17:49 -04002631static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002632 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002633 char *type,
2634 unsigned long flags,
2635 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
David Howells88e67f32008-11-14 10:39:21 +11002637 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002640 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002641 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 else
Eric Paris2875fa02011-04-28 16:04:24 -04002643 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646static int selinux_umount(struct vfsmount *mnt, int flags)
2647{
David Howells88e67f32008-11-14 10:39:21 +11002648 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
David Howells88e67f32008-11-14 10:39:21 +11002650 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002651 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
2654/* inode security operations */
2655
2656static int selinux_inode_alloc_security(struct inode *inode)
2657{
2658 return inode_alloc_security(inode);
2659}
2660
2661static void selinux_inode_free_security(struct inode *inode)
2662{
2663 inode_free_security(inode);
2664}
2665
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002666static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002667 const struct qstr *qstr, char **name,
2668 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002669{
Paul Moore5fb49872010-04-22 14:46:19 -04002670 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002671 struct inode_security_struct *dsec;
2672 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002673 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002674 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002675 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002676
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002677 dsec = dir->i_security;
2678 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002679
David Howells275bb412008-11-14 10:39:19 +11002680 sid = tsec->sid;
2681 newsid = tsec->create_sid;
2682
Eric Paris415103f2010-12-02 16:13:40 -05002683 if ((sbsec->flags & SE_SBINITIALIZED) &&
2684 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2685 newsid = sbsec->mntpoint_sid;
2686 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002687 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002688 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002689 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002690 if (rc) {
2691 printk(KERN_WARNING "%s: "
2692 "security_transition_sid failed, rc=%d (dev=%s "
2693 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002694 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002695 -rc, inode->i_sb->s_id, inode->i_ino);
2696 return rc;
2697 }
2698 }
2699
Eric Paris296fddf2006-09-25 23:32:00 -07002700 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002701 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002702 struct inode_security_struct *isec = inode->i_security;
2703 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2704 isec->sid = newsid;
2705 isec->initialized = 1;
2706 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002707
David P. Quigleycd895962009-01-16 09:22:04 -05002708 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002709 return -EOPNOTSUPP;
2710
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002711 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002712 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002713 if (!namep)
2714 return -ENOMEM;
2715 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002716 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002717
2718 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002719 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002720 if (rc) {
2721 kfree(namep);
2722 return rc;
2723 }
2724 *value = context;
2725 *len = clen;
2726 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002727
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002728 return 0;
2729}
2730
Al Viro4acdaf22011-07-26 01:42:34 -04002731static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 return may_create(dir, dentry, SECCLASS_FILE);
2734}
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return may_link(dir, old_dentry, MAY_LINK);
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return may_link(dir, dentry, MAY_UNLINK);
2744}
2745
2746static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2747{
2748 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2749}
2750
Al Viro18bb1db2011-07-26 01:41:39 -04002751static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 return may_create(dir, dentry, SECCLASS_DIR);
2754}
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2757{
2758 return may_link(dir, dentry, MAY_RMDIR);
2759}
2760
Al Viro1a67aaf2011-07-26 01:52:52 -04002761static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2764}
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002767 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772static int selinux_inode_readlink(struct dentry *dentry)
2773{
David Howells88e67f32008-11-14 10:39:21 +11002774 const struct cred *cred = current_cred();
2775
Eric Paris2875fa02011-04-28 16:04:24 -04002776 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
2779static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2780{
David Howells88e67f32008-11-14 10:39:21 +11002781 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Eric Paris2875fa02011-04-28 16:04:24 -04002783 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Al Viroe74f71e2011-06-20 19:38:15 -04002786static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
David Howells88e67f32008-11-14 10:39:21 +11002788 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002789 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002790 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002791 u32 perms;
2792 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002793 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Eric Parisb782e0a2010-07-23 11:44:03 -04002795 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002796 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2797
Eric Parisb782e0a2010-07-23 11:44:03 -04002798 /* No permission to check. Existence test. */
2799 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Eric Parisf48b7392011-04-25 12:54:27 -04002802 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002803 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002804 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002805
2806 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002807 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002808
2809 perms = file_mask_to_av(inode->i_mode, mask);
2810
Eric Paris9ade0cf2011-04-25 16:26:29 -04002811 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2815{
David Howells88e67f32008-11-14 10:39:21 +11002816 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002817 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002819 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2820 if (ia_valid & ATTR_FORCE) {
2821 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2822 ATTR_FORCE);
2823 if (!ia_valid)
2824 return 0;
2825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002827 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2828 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002829 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Eric Paris2875fa02011-04-28 16:04:24 -04002831 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
2834static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2835{
David Howells88e67f32008-11-14 10:39:21 +11002836 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002837 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002838
Eric Paris2875fa02011-04-28 16:04:24 -04002839 path.dentry = dentry;
2840 path.mnt = mnt;
2841
2842 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
David Howells8f0cfa52008-04-29 00:59:41 -07002845static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002846{
David Howells88e67f32008-11-14 10:39:21 +11002847 const struct cred *cred = current_cred();
2848
Serge E. Hallynb5376772007-10-16 23:31:36 -07002849 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2850 sizeof XATTR_SECURITY_PREFIX - 1)) {
2851 if (!strcmp(name, XATTR_NAME_CAPS)) {
2852 if (!capable(CAP_SETFCAP))
2853 return -EPERM;
2854 } else if (!capable(CAP_SYS_ADMIN)) {
2855 /* A different attribute in the security namespace.
2856 Restrict to administrator. */
2857 return -EPERM;
2858 }
2859 }
2860
2861 /* Not an attribute we recognize, so just check the
2862 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002863 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002864}
2865
David Howells8f0cfa52008-04-29 00:59:41 -07002866static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2867 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 struct inode *inode = dentry->d_inode;
2870 struct inode_security_struct *isec = inode->i_security;
2871 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002872 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002873 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002874 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 int rc = 0;
2876
Serge E. Hallynb5376772007-10-16 23:31:36 -07002877 if (strcmp(name, XATTR_NAME_SELINUX))
2878 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002881 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return -EOPNOTSUPP;
2883
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002884 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return -EPERM;
2886
Eric Parisa2694342011-04-25 13:10:27 -04002887 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002888 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002889 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
David Howells275bb412008-11-14 10:39:19 +11002891 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 FILE__RELABELFROM, &ad);
2893 if (rc)
2894 return rc;
2895
2896 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002897 if (rc == -EINVAL) {
2898 if (!capable(CAP_MAC_ADMIN))
2899 return rc;
2900 rc = security_context_to_sid_force(value, size, &newsid);
2901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (rc)
2903 return rc;
2904
David Howells275bb412008-11-14 10:39:19 +11002905 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 FILE__RELABELTO, &ad);
2907 if (rc)
2908 return rc;
2909
David Howells275bb412008-11-14 10:39:19 +11002910 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002911 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (rc)
2913 return rc;
2914
2915 return avc_has_perm(newsid,
2916 sbsec->sid,
2917 SECCLASS_FILESYSTEM,
2918 FILESYSTEM__ASSOCIATE,
2919 &ad);
2920}
2921
David Howells8f0cfa52008-04-29 00:59:41 -07002922static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002923 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002924 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
2926 struct inode *inode = dentry->d_inode;
2927 struct inode_security_struct *isec = inode->i_security;
2928 u32 newsid;
2929 int rc;
2930
2931 if (strcmp(name, XATTR_NAME_SELINUX)) {
2932 /* Not an attribute we recognize, so nothing to do. */
2933 return;
2934 }
2935
Stephen Smalley12b29f32008-05-07 13:03:20 -04002936 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002938 printk(KERN_ERR "SELinux: unable to map context to SID"
2939 "for (%s, %lu), rc=%d\n",
2940 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return;
2942 }
2943
2944 isec->sid = newsid;
2945 return;
2946}
2947
David Howells8f0cfa52008-04-29 00:59:41 -07002948static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
David Howells88e67f32008-11-14 10:39:21 +11002950 const struct cred *cred = current_cred();
2951
Eric Paris2875fa02011-04-28 16:04:24 -04002952 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
Eric Paris828dfe12008-04-17 13:17:49 -04002955static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
David Howells88e67f32008-11-14 10:39:21 +11002957 const struct cred *cred = current_cred();
2958
Eric Paris2875fa02011-04-28 16:04:24 -04002959 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960}
2961
David Howells8f0cfa52008-04-29 00:59:41 -07002962static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002964 if (strcmp(name, XATTR_NAME_SELINUX))
2965 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 /* No one is allowed to remove a SELinux security label.
2968 You can change the label, but all data must be labeled. */
2969 return -EACCES;
2970}
2971
James Morrisd381d8a2005-10-30 14:59:22 -08002972/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002973 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002974 *
2975 * Permission check is handled by selinux_inode_getxattr hook.
2976 */
David P. Quigley42492592008-02-04 22:29:39 -08002977static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
David P. Quigley42492592008-02-04 22:29:39 -08002979 u32 size;
2980 int error;
2981 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002984 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2985 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002987 /*
2988 * If the caller has CAP_MAC_ADMIN, then get the raw context
2989 * value even if it is not defined by current policy; otherwise,
2990 * use the in-core value under current policy.
2991 * Use the non-auditing forms of the permission checks since
2992 * getxattr may be called by unprivileged processes commonly
2993 * and lack of permission just means that we fall back to the
2994 * in-core context value, not a denial.
2995 */
Eric Paris6a9de492012-01-03 12:25:14 -05002996 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002997 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002998 if (!error)
2999 error = security_sid_to_context_force(isec->sid, &context,
3000 &size);
3001 else
3002 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003003 if (error)
3004 return error;
3005 error = size;
3006 if (alloc) {
3007 *buffer = context;
3008 goto out_nofree;
3009 }
3010 kfree(context);
3011out_nofree:
3012 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
3015static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003016 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018 struct inode_security_struct *isec = inode->i_security;
3019 u32 newsid;
3020 int rc;
3021
3022 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3023 return -EOPNOTSUPP;
3024
3025 if (!value || !size)
3026 return -EACCES;
3027
Eric Paris828dfe12008-04-17 13:17:49 -04003028 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (rc)
3030 return rc;
3031
3032 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003033 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return 0;
3035}
3036
3037static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3038{
3039 const int len = sizeof(XATTR_NAME_SELINUX);
3040 if (buffer && len <= buffer_size)
3041 memcpy(buffer, XATTR_NAME_SELINUX, len);
3042 return len;
3043}
3044
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003045static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3046{
3047 struct inode_security_struct *isec = inode->i_security;
3048 *secid = isec->sid;
3049}
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051/* file security operations */
3052
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003053static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
David Howells88e67f32008-11-14 10:39:21 +11003055 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003056 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3059 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3060 mask |= MAY_APPEND;
3061
Paul Moore389fb802009-03-27 17:10:34 -04003062 return file_has_perm(cred, file,
3063 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003066static int selinux_file_permission(struct file *file, int mask)
3067{
Stephen Smalley20dda182009-06-22 14:54:53 -04003068 struct inode *inode = file->f_path.dentry->d_inode;
3069 struct file_security_struct *fsec = file->f_security;
3070 struct inode_security_struct *isec = inode->i_security;
3071 u32 sid = current_sid();
3072
Paul Moore389fb802009-03-27 17:10:34 -04003073 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003074 /* No permission to check. Existence test. */
3075 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003076
Stephen Smalley20dda182009-06-22 14:54:53 -04003077 if (sid == fsec->sid && fsec->isid == isec->sid &&
3078 fsec->pseqno == avc_policy_seqno())
3079 /* No change since dentry_open check. */
3080 return 0;
3081
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003082 return selinux_revalidate_file_permission(file, mask);
3083}
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085static int selinux_file_alloc_security(struct file *file)
3086{
3087 return file_alloc_security(file);
3088}
3089
3090static void selinux_file_free_security(struct file *file)
3091{
3092 file_free_security(file);
3093}
3094
Jeff Vander Stoepf3e85342015-04-08 11:27:46 -07003095/*
3096 * Check whether a task has the ioctl permission and cmd
3097 * operation to an inode.
3098 */
3099int ioctl_has_perm(const struct cred *cred, struct file *file,
3100 u32 requested, u16 cmd)
3101{
3102 struct common_audit_data ad;
3103 struct file_security_struct *fsec = file->f_security;
3104 struct inode *inode = file->f_path.dentry->d_inode;
3105 struct inode_security_struct *isec = inode->i_security;
3106 struct lsm_ioctlop_audit ioctl;
3107 u32 ssid = cred_sid(cred);
3108 struct selinux_audit_data sad = {0,};
3109 int rc;
3110
3111 COMMON_AUDIT_DATA_INIT(&ad, IOCTL_OP);
3112 ad.u.op = &ioctl;
3113 ad.u.op->cmd = cmd;
3114 ad.u.op->path = file->f_path;
3115 ad.selinux_audit_data = &sad;
3116
3117 if (ssid != fsec->sid) {
3118 rc = avc_has_perm(ssid, fsec->sid,
3119 SECCLASS_FD,
3120 FD__USE,
3121 &ad);
3122 if (rc)
3123 goto out;
3124 }
3125
3126 if (unlikely(IS_PRIVATE(inode)))
3127 return 0;
3128
3129 rc = avc_has_operation(ssid, isec->sid, isec->sclass,
3130 requested, cmd, &ad);
3131out:
3132 return rc;
3133}
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3136 unsigned long arg)
3137{
David Howells88e67f32008-11-14 10:39:21 +11003138 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003139 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Eric Paris0b24dcb2011-02-25 15:39:20 -05003141 switch (cmd) {
3142 case FIONREAD:
3143 /* fall through */
3144 case FIBMAP:
3145 /* fall through */
3146 case FIGETBSZ:
3147 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003148 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003149 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003150 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003151 error = file_has_perm(cred, file, FILE__GETATTR);
3152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Al Viro2f99c362012-03-23 16:04:05 -04003154 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003155 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003156 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003157 error = file_has_perm(cred, file, FILE__SETATTR);
3158 break;
3159
3160 /* sys_ioctl() checks */
3161 case FIONBIO:
3162 /* fall through */
3163 case FIOASYNC:
3164 error = file_has_perm(cred, file, 0);
3165 break;
3166
3167 case KDSKBENT:
3168 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003169 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3170 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003171 break;
3172
3173 /* default case assumes that the command will go
3174 * to the file's ioctl() function.
3175 */
3176 default:
Jeff Vander Stoepf3e85342015-04-08 11:27:46 -07003177 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003178 }
3179 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003182static int default_noexec;
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3185{
David Howells88e67f32008-11-14 10:39:21 +11003186 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003187 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003188
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003189 if (default_noexec &&
3190 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /*
3192 * We are making executable an anonymous mapping or a
3193 * private file mapping that will also be writable.
3194 * This has an additional check.
3195 */
David Howellsd84f4f92008-11-14 10:39:23 +11003196 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003198 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 if (file) {
3202 /* read access is always possible with a mapping */
3203 u32 av = FILE__READ;
3204
3205 /* write access only matters if the mapping is shared */
3206 if (shared && (prot & PROT_WRITE))
3207 av |= FILE__WRITE;
3208
3209 if (prot & PROT_EXEC)
3210 av |= FILE__EXECUTE;
3211
David Howells88e67f32008-11-14 10:39:21 +11003212 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
David Howellsd84f4f92008-11-14 10:39:23 +11003214
3215error:
3216 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
3219static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003220 unsigned long prot, unsigned long flags,
3221 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Eric Parised032182007-06-28 15:55:21 -04003223 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003224 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Eric Paris84336d1a2009-07-31 12:54:05 -04003226 /*
3227 * notice that we are intentionally putting the SELinux check before
3228 * the secondary cap_file_mmap check. This is such a likely attempt
3229 * at bad behaviour/exploit that we always want to get the AVC, even
3230 * if DAC would have also denied the operation.
3231 */
Eric Parisa2551df2009-07-31 12:54:11 -04003232 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003233 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3234 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003235 if (rc)
3236 return rc;
3237 }
3238
3239 /* do DAC check on address space usage */
3240 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003241 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return rc;
3243
3244 if (selinux_checkreqprot)
3245 prot = reqprot;
3246
3247 return file_map_prot_check(file, prot,
3248 (flags & MAP_TYPE) == MAP_SHARED);
3249}
3250
3251static int selinux_file_mprotect(struct vm_area_struct *vma,
3252 unsigned long reqprot,
3253 unsigned long prot)
3254{
David Howells88e67f32008-11-14 10:39:21 +11003255 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 if (selinux_checkreqprot)
3258 prot = reqprot;
3259
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003260 if (default_noexec &&
3261 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003262 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003263 if (vma->vm_start >= vma->vm_mm->start_brk &&
3264 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003265 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003266 } else if (!vma->vm_file &&
3267 vma->vm_start <= vma->vm_mm->start_stack &&
3268 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003269 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003270 } else if (vma->vm_file && vma->anon_vma) {
3271 /*
3272 * We are making executable a file mapping that has
3273 * had some COW done. Since pages might have been
3274 * written, check ability to execute the possibly
3275 * modified content. This typically should only
3276 * occur for text relocations.
3277 */
David Howellsd84f4f92008-11-14 10:39:23 +11003278 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003279 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003280 if (rc)
3281 return rc;
3282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3285}
3286
3287static int selinux_file_lock(struct file *file, unsigned int cmd)
3288{
David Howells88e67f32008-11-14 10:39:21 +11003289 const struct cred *cred = current_cred();
3290
3291 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3295 unsigned long arg)
3296{
David Howells88e67f32008-11-14 10:39:21 +11003297 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 int err = 0;
3299
3300 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003301 case F_SETFL:
3302 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3303 err = -EINVAL;
3304 break;
3305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Eric Paris828dfe12008-04-17 13:17:49 -04003307 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003308 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003310 }
3311 /* fall through */
3312 case F_SETOWN:
3313 case F_SETSIG:
3314 case F_GETFL:
3315 case F_GETOWN:
3316 case F_GETSIG:
3317 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003318 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003319 break;
3320 case F_GETLK:
3321 case F_SETLK:
3322 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003324 case F_GETLK64:
3325 case F_SETLK64:
3326 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003328 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3329 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003331 }
David Howells88e67f32008-11-14 10:39:21 +11003332 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335
3336 return err;
3337}
3338
3339static int selinux_file_set_fowner(struct file *file)
3340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 struct file_security_struct *fsec;
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003344 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 return 0;
3347}
3348
3349static int selinux_file_send_sigiotask(struct task_struct *tsk,
3350 struct fown_struct *fown, int signum)
3351{
Eric Paris828dfe12008-04-17 13:17:49 -04003352 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003353 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 struct file_security_struct *fsec;
3356
3357 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003358 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 fsec = file->f_security;
3361
3362 if (!signum)
3363 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3364 else
3365 perm = signal_to_av(signum);
3366
David Howells275bb412008-11-14 10:39:19 +11003367 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 SECCLASS_PROCESS, perm, NULL);
3369}
3370
3371static int selinux_file_receive(struct file *file)
3372{
David Howells88e67f32008-11-14 10:39:21 +11003373 const struct cred *cred = current_cred();
3374
3375 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
David Howells745ca242008-11-14 10:39:22 +11003378static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003379{
3380 struct file_security_struct *fsec;
3381 struct inode *inode;
3382 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003383
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003384 inode = file->f_path.dentry->d_inode;
3385 fsec = file->f_security;
3386 isec = inode->i_security;
3387 /*
3388 * Save inode label and policy sequence number
3389 * at open-time so that selinux_file_permission
3390 * can determine whether revalidation is necessary.
3391 * Task label is already saved in the file security
3392 * struct as its SID.
3393 */
3394 fsec->isid = isec->sid;
3395 fsec->pseqno = avc_policy_seqno();
3396 /*
3397 * Since the inode label or policy seqno may have changed
3398 * between the selinux_inode_permission check and the saving
3399 * of state above, recheck that access is still permitted.
3400 * Otherwise, access might never be revalidated against the
3401 * new inode label or new policy.
3402 * This check is not redundant - do not remove.
3403 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003404 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003405}
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407/* task security operations */
3408
3409static int selinux_task_create(unsigned long clone_flags)
3410{
David Howells3b11a1d2008-11-14 10:39:26 +11003411 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
David Howellsf1752ee2008-11-14 10:39:17 +11003414/*
David Howellsee18d642009-09-02 09:14:21 +01003415 * allocate the SELinux part of blank credentials
3416 */
3417static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3418{
3419 struct task_security_struct *tsec;
3420
3421 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3422 if (!tsec)
3423 return -ENOMEM;
3424
3425 cred->security = tsec;
3426 return 0;
3427}
3428
3429/*
David Howellsf1752ee2008-11-14 10:39:17 +11003430 * detach and free the LSM part of a set of credentials
3431 */
3432static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
David Howellsf1752ee2008-11-14 10:39:17 +11003434 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003435
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003436 /*
3437 * cred->security == NULL if security_cred_alloc_blank() or
3438 * security_prepare_creds() returned an error.
3439 */
3440 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003441 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003442 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
David Howellsd84f4f92008-11-14 10:39:23 +11003445/*
3446 * prepare a new set of credentials for modification
3447 */
3448static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3449 gfp_t gfp)
3450{
3451 const struct task_security_struct *old_tsec;
3452 struct task_security_struct *tsec;
3453
3454 old_tsec = old->security;
3455
3456 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3457 if (!tsec)
3458 return -ENOMEM;
3459
3460 new->security = tsec;
3461 return 0;
3462}
3463
3464/*
David Howellsee18d642009-09-02 09:14:21 +01003465 * transfer the SELinux data to a blank set of creds
3466 */
3467static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3468{
3469 const struct task_security_struct *old_tsec = old->security;
3470 struct task_security_struct *tsec = new->security;
3471
3472 *tsec = *old_tsec;
3473}
3474
3475/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003476 * set the security data for a kernel service
3477 * - all the creation contexts are set to unlabelled
3478 */
3479static int selinux_kernel_act_as(struct cred *new, u32 secid)
3480{
3481 struct task_security_struct *tsec = new->security;
3482 u32 sid = current_sid();
3483 int ret;
3484
3485 ret = avc_has_perm(sid, secid,
3486 SECCLASS_KERNEL_SERVICE,
3487 KERNEL_SERVICE__USE_AS_OVERRIDE,
3488 NULL);
3489 if (ret == 0) {
3490 tsec->sid = secid;
3491 tsec->create_sid = 0;
3492 tsec->keycreate_sid = 0;
3493 tsec->sockcreate_sid = 0;
3494 }
3495 return ret;
3496}
3497
3498/*
3499 * set the file creation context in a security record to the same as the
3500 * objective context of the specified inode
3501 */
3502static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3503{
3504 struct inode_security_struct *isec = inode->i_security;
3505 struct task_security_struct *tsec = new->security;
3506 u32 sid = current_sid();
3507 int ret;
3508
3509 ret = avc_has_perm(sid, isec->sid,
3510 SECCLASS_KERNEL_SERVICE,
3511 KERNEL_SERVICE__CREATE_FILES_AS,
3512 NULL);
3513
3514 if (ret == 0)
3515 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003516 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003517}
3518
Eric Parisdd8dbf22009-11-03 16:35:32 +11003519static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003520{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003521 u32 sid;
3522 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003523 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003524
3525 sid = task_sid(current);
3526
3527 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003528 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003529 ad.u.kmod_name = kmod_name;
3530
3531 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3532 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003533}
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3536{
David Howells3b11a1d2008-11-14 10:39:26 +11003537 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
3540static int selinux_task_getpgid(struct task_struct *p)
3541{
David Howells3b11a1d2008-11-14 10:39:26 +11003542 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
3545static int selinux_task_getsid(struct task_struct *p)
3546{
David Howells3b11a1d2008-11-14 10:39:26 +11003547 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
David Quigleyf9008e42006-06-30 01:55:46 -07003550static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3551{
David Howells275bb412008-11-14 10:39:19 +11003552 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003553}
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555static int selinux_task_setnice(struct task_struct *p, int nice)
3556{
3557 int rc;
3558
Eric Paris200ac532009-02-12 15:01:04 -05003559 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 if (rc)
3561 return rc;
3562
David Howells3b11a1d2008-11-14 10:39:26 +11003563 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
James Morris03e68062006-06-23 02:03:58 -07003566static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3567{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003568 int rc;
3569
Eric Paris200ac532009-02-12 15:01:04 -05003570 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003571 if (rc)
3572 return rc;
3573
David Howells3b11a1d2008-11-14 10:39:26 +11003574 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003575}
3576
David Quigleya1836a42006-06-30 01:55:49 -07003577static int selinux_task_getioprio(struct task_struct *p)
3578{
David Howells3b11a1d2008-11-14 10:39:26 +11003579 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003580}
3581
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003582static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3583 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003585 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 /* Control the ability to change the hard limit (whether
3588 lowering or raising it), so that the hard limit can
3589 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003590 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003592 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594 return 0;
3595}
3596
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003597static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003599 int rc;
3600
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003601 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003602 if (rc)
3603 return rc;
3604
David Howells3b11a1d2008-11-14 10:39:26 +11003605 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
3608static int selinux_task_getscheduler(struct task_struct *p)
3609{
David Howells3b11a1d2008-11-14 10:39:26 +11003610 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
David Quigley35601542006-06-23 02:04:01 -07003613static int selinux_task_movememory(struct task_struct *p)
3614{
David Howells3b11a1d2008-11-14 10:39:26 +11003615 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003616}
3617
David Quigleyf9008e42006-06-30 01:55:46 -07003618static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3619 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 u32 perm;
3622 int rc;
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 if (!sig)
3625 perm = PROCESS__SIGNULL; /* null signal; existence test */
3626 else
3627 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003628 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003629 rc = avc_has_perm(secid, task_sid(p),
3630 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003631 else
David Howells3b11a1d2008-11-14 10:39:26 +11003632 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003633 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636static int selinux_task_wait(struct task_struct *p)
3637{
Eric Paris8a535142007-10-22 16:10:31 -04003638 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641static void selinux_task_to_inode(struct task_struct *p,
3642 struct inode *inode)
3643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003645 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
David Howells275bb412008-11-14 10:39:19 +11003647 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003652static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003653 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
3655 int offset, ihlen, ret = -EINVAL;
3656 struct iphdr _iph, *ih;
3657
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003658 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3660 if (ih == NULL)
3661 goto out;
3662
3663 ihlen = ih->ihl * 4;
3664 if (ihlen < sizeof(_iph))
3665 goto out;
3666
Eric Paris48c62af2012-04-02 13:15:44 -04003667 ad->u.net->v4info.saddr = ih->saddr;
3668 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 ret = 0;
3670
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003671 if (proto)
3672 *proto = ih->protocol;
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003675 case IPPROTO_TCP: {
3676 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Eric Paris828dfe12008-04-17 13:17:49 -04003678 if (ntohs(ih->frag_off) & IP_OFFSET)
3679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 offset += ihlen;
3682 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3683 if (th == NULL)
3684 break;
3685
Eric Paris48c62af2012-04-02 13:15:44 -04003686 ad->u.net->sport = th->source;
3687 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Eric Paris828dfe12008-04-17 13:17:49 -04003691 case IPPROTO_UDP: {
3692 struct udphdr _udph, *uh;
3693
3694 if (ntohs(ih->frag_off) & IP_OFFSET)
3695 break;
3696
3697 offset += ihlen;
3698 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3699 if (uh == NULL)
3700 break;
3701
Eric Paris48c62af2012-04-02 13:15:44 -04003702 ad->u.net->sport = uh->source;
3703 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003704 break;
3705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
James Morris2ee92d42006-11-13 16:09:01 -08003707 case IPPROTO_DCCP: {
3708 struct dccp_hdr _dccph, *dh;
3709
3710 if (ntohs(ih->frag_off) & IP_OFFSET)
3711 break;
3712
3713 offset += ihlen;
3714 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3715 if (dh == NULL)
3716 break;
3717
Eric Paris48c62af2012-04-02 13:15:44 -04003718 ad->u.net->sport = dh->dccph_sport;
3719 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003720 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003721 }
James Morris2ee92d42006-11-13 16:09:01 -08003722
Eric Paris828dfe12008-04-17 13:17:49 -04003723 default:
3724 break;
3725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726out:
3727 return ret;
3728}
3729
3730#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3731
3732/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003733static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003734 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 u8 nexthdr;
3737 int ret = -EINVAL, offset;
3738 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003739 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003741 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3743 if (ip6 == NULL)
3744 goto out;
3745
Eric Paris48c62af2012-04-02 13:15:44 -04003746 ad->u.net->v6info.saddr = ip6->saddr;
3747 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 ret = 0;
3749
3750 nexthdr = ip6->nexthdr;
3751 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003752 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if (offset < 0)
3754 goto out;
3755
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003756 if (proto)
3757 *proto = nexthdr;
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 switch (nexthdr) {
3760 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003761 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3764 if (th == NULL)
3765 break;
3766
Eric Paris48c62af2012-04-02 13:15:44 -04003767 ad->u.net->sport = th->source;
3768 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 break;
3770 }
3771
3772 case IPPROTO_UDP: {
3773 struct udphdr _udph, *uh;
3774
3775 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3776 if (uh == NULL)
3777 break;
3778
Eric Paris48c62af2012-04-02 13:15:44 -04003779 ad->u.net->sport = uh->source;
3780 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 break;
3782 }
3783
James Morris2ee92d42006-11-13 16:09:01 -08003784 case IPPROTO_DCCP: {
3785 struct dccp_hdr _dccph, *dh;
3786
3787 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3788 if (dh == NULL)
3789 break;
3790
Eric Paris48c62af2012-04-02 13:15:44 -04003791 ad->u.net->sport = dh->dccph_sport;
3792 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003793 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003794 }
James Morris2ee92d42006-11-13 16:09:01 -08003795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 /* includes fragments */
3797 default:
3798 break;
3799 }
3800out:
3801 return ret;
3802}
3803
3804#endif /* IPV6 */
3805
Thomas Liu2bf49692009-07-14 12:14:09 -04003806static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003807 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
David Howellscf9481e2008-07-27 21:31:07 +10003809 char *addrp;
3810 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Eric Paris48c62af2012-04-02 13:15:44 -04003812 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003814 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003815 if (ret)
3816 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003817 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3818 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003819 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3822 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003823 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003824 if (ret)
3825 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003826 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3827 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003828 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829#endif /* IPV6 */
3830 default:
David Howellscf9481e2008-07-27 21:31:07 +10003831 addrp = NULL;
3832 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
3834
David Howellscf9481e2008-07-27 21:31:07 +10003835parse_error:
3836 printk(KERN_WARNING
3837 "SELinux: failure in selinux_parse_skb(),"
3838 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003840
3841okay:
3842 if (_addrp)
3843 *_addrp = addrp;
3844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
Paul Moore4f6a9932007-03-01 14:35:22 -05003847/**
Paul Moore220deb92008-01-29 08:38:23 -05003848 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003849 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003850 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003851 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003852 *
3853 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003854 * Check the various different forms of network peer labeling and determine
3855 * the peer label/SID for the packet; most of the magic actually occurs in
3856 * the security server function security_net_peersid_cmp(). The function
3857 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3858 * or -EACCES if @sid is invalid due to inconsistencies with the different
3859 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003860 *
3861 */
Paul Moore220deb92008-01-29 08:38:23 -05003862static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003863{
Paul Moore71f1cb02008-01-29 08:51:16 -05003864 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003865 u32 xfrm_sid;
3866 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003867 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003868
Paul Moore73ec9552013-12-10 14:57:54 -05003869 selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003870 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003871
Paul Moore71f1cb02008-01-29 08:51:16 -05003872 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3873 if (unlikely(err)) {
3874 printk(KERN_WARNING
3875 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3876 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003877 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003878 }
Paul Moore220deb92008-01-29 08:38:23 -05003879
3880 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003881}
3882
Paul Moore2ea04e52013-12-04 16:10:51 -05003883/**
3884 * selinux_conn_sid - Determine the child socket label for a connection
3885 * @sk_sid: the parent socket's SID
3886 * @skb_sid: the packet's SID
3887 * @conn_sid: the resulting connection SID
3888 *
3889 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3890 * combined with the MLS information from @skb_sid in order to create
3891 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3892 * of @sk_sid. Returns zero on success, negative values on failure.
3893 *
3894 */
3895static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3896{
3897 int err = 0;
3898
3899 if (skb_sid != SECSID_NULL)
3900 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3901 else
3902 *conn_sid = sk_sid;
3903
3904 return err;
3905}
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003908
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003909static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3910 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003911{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003912 if (tsec->sockcreate_sid > SECSID_NULL) {
3913 *socksid = tsec->sockcreate_sid;
3914 return 0;
3915 }
3916
3917 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3918 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003919}
3920
Paul Moore253bfae2010-04-22 14:46:19 -04003921static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Paul Moore253bfae2010-04-22 14:46:19 -04003923 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003924 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003925 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003926 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003927 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Paul Moore253bfae2010-04-22 14:46:19 -04003929 if (sksec->sid == SECINITSID_KERNEL)
3930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Thomas Liu2bf49692009-07-14 12:14:09 -04003932 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003933 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003934 ad.u.net = &net;
3935 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Paul Moore253bfae2010-04-22 14:46:19 -04003937 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
3940static int selinux_socket_create(int family, int type,
3941 int protocol, int kern)
3942{
Paul Moore5fb49872010-04-22 14:46:19 -04003943 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003944 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003945 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003946 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
David Howells275bb412008-11-14 10:39:19 +11003951 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003952 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3953 if (rc)
3954 return rc;
3955
Paul Moored4f2d972010-04-22 14:46:18 -04003956 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
3958
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003959static int selinux_socket_post_create(struct socket *sock, int family,
3960 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Paul Moore5fb49872010-04-22 14:46:19 -04003962 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003963 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003964 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003965 int err = 0;
3966
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003967 isec->sclass = socket_type_to_security_class(family, type, protocol);
3968
David Howells275bb412008-11-14 10:39:19 +11003969 if (kern)
3970 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003971 else {
3972 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3973 if (err)
3974 return err;
3975 }
David Howells275bb412008-11-14 10:39:19 +11003976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 isec->initialized = 1;
3978
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003979 if (sock->sk) {
3980 sksec = sock->sk->sk_security;
3981 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003982 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003983 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003984 }
3985
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003986 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987}
3988
3989/* Range of port numbers used to automatically bind.
3990 Need to determine whether we should perform a name_bind
3991 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3994{
Paul Moore253bfae2010-04-22 14:46:19 -04003995 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 u16 family;
3997 int err;
3998
Paul Moore253bfae2010-04-22 14:46:19 -04003999 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (err)
4001 goto out;
4002
4003 /*
4004 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04004005 * Multiple address binding for SCTP is not supported yet: we just
4006 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
Paul Moore253bfae2010-04-22 14:46:19 -04004008 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 if (family == PF_INET || family == PF_INET6) {
4010 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04004011 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004012 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004013 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004014 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 struct sockaddr_in *addr4 = NULL;
4016 struct sockaddr_in6 *addr6 = NULL;
4017 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004018 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (family == PF_INET) {
4021 addr4 = (struct sockaddr_in *)address;
4022 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 addrp = (char *)&addr4->sin_addr.s_addr;
4024 } else {
4025 addr6 = (struct sockaddr_in6 *)address;
4026 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 addrp = (char *)&addr6->sin6_addr.s6_addr;
4028 }
4029
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004030 if (snum) {
4031 int low, high;
4032
4033 inet_get_local_port_range(&low, &high);
4034
4035 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004036 err = sel_netport_sid(sk->sk_protocol,
4037 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004038 if (err)
4039 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04004040 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004041 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004042 ad.u.net = &net;
4043 ad.u.net->sport = htons(snum);
4044 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004045 err = avc_has_perm(sksec->sid, sid,
4046 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004047 SOCKET__NAME_BIND, &ad);
4048 if (err)
4049 goto out;
4050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
Eric Paris828dfe12008-04-17 13:17:49 -04004052
Paul Moore253bfae2010-04-22 14:46:19 -04004053 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004054 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 node_perm = TCP_SOCKET__NODE_BIND;
4056 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004057
James Morris13402582005-09-30 14:24:34 -04004058 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 node_perm = UDP_SOCKET__NODE_BIND;
4060 break;
James Morris2ee92d42006-11-13 16:09:01 -08004061
4062 case SECCLASS_DCCP_SOCKET:
4063 node_perm = DCCP_SOCKET__NODE_BIND;
4064 break;
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 default:
4067 node_perm = RAWIP_SOCKET__NODE_BIND;
4068 break;
4069 }
Eric Paris828dfe12008-04-17 13:17:49 -04004070
Paul Moore224dfbd2008-01-29 08:38:13 -05004071 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 if (err)
4073 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004074
Thomas Liu2bf49692009-07-14 12:14:09 -04004075 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004076 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004077 ad.u.net = &net;
4078 ad.u.net->sport = htons(snum);
4079 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004082 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 else
Eric Paris48c62af2012-04-02 13:15:44 -04004084 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Paul Moore253bfae2010-04-22 14:46:19 -04004086 err = avc_has_perm(sksec->sid, sid,
4087 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 if (err)
4089 goto out;
4090 }
4091out:
4092 return err;
4093}
4094
4095static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4096{
Paul Moore014ab192008-10-10 10:16:33 -04004097 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004098 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 int err;
4100
Paul Moore253bfae2010-04-22 14:46:19 -04004101 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 if (err)
4103 return err;
4104
4105 /*
James Morris2ee92d42006-11-13 16:09:01 -08004106 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 */
Paul Moore253bfae2010-04-22 14:46:19 -04004108 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4109 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004110 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004111 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004112 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 struct sockaddr_in *addr4 = NULL;
4114 struct sockaddr_in6 *addr6 = NULL;
4115 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004116 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 if (sk->sk_family == PF_INET) {
4119 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004120 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return -EINVAL;
4122 snum = ntohs(addr4->sin_port);
4123 } else {
4124 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004125 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return -EINVAL;
4127 snum = ntohs(addr6->sin6_port);
4128 }
4129
Paul Moore3e112172008-04-10 10:48:14 -04004130 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (err)
4132 goto out;
4133
Paul Moore253bfae2010-04-22 14:46:19 -04004134 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004135 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4136
Thomas Liu2bf49692009-07-14 12:14:09 -04004137 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004138 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004139 ad.u.net = &net;
4140 ad.u.net->dport = htons(snum);
4141 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004142 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (err)
4144 goto out;
4145 }
4146
Paul Moore014ab192008-10-10 10:16:33 -04004147 err = selinux_netlbl_socket_connect(sk, address);
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149out:
4150 return err;
4151}
4152
4153static int selinux_socket_listen(struct socket *sock, int backlog)
4154{
Paul Moore253bfae2010-04-22 14:46:19 -04004155 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
4158static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4159{
4160 int err;
4161 struct inode_security_struct *isec;
4162 struct inode_security_struct *newisec;
4163
Paul Moore253bfae2010-04-22 14:46:19 -04004164 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 if (err)
4166 return err;
4167
4168 newisec = SOCK_INODE(newsock)->i_security;
4169
4170 isec = SOCK_INODE(sock)->i_security;
4171 newisec->sclass = isec->sclass;
4172 newisec->sid = isec->sid;
4173 newisec->initialized = 1;
4174
4175 return 0;
4176}
4177
4178static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004179 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Paul Moore253bfae2010-04-22 14:46:19 -04004181 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
4184static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4185 int size, int flags)
4186{
Paul Moore253bfae2010-04-22 14:46:19 -04004187 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
4190static int selinux_socket_getsockname(struct socket *sock)
4191{
Paul Moore253bfae2010-04-22 14:46:19 -04004192 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
4195static int selinux_socket_getpeername(struct socket *sock)
4196{
Paul Moore253bfae2010-04-22 14:46:19 -04004197 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198}
4199
Eric Paris828dfe12008-04-17 13:17:49 -04004200static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Paul Mooref8687af2006-10-30 15:22:15 -08004202 int err;
4203
Paul Moore253bfae2010-04-22 14:46:19 -04004204 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004205 if (err)
4206 return err;
4207
4208 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
4211static int selinux_socket_getsockopt(struct socket *sock, int level,
4212 int optname)
4213{
Paul Moore253bfae2010-04-22 14:46:19 -04004214 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
4217static int selinux_socket_shutdown(struct socket *sock, int how)
4218{
Paul Moore253bfae2010-04-22 14:46:19 -04004219 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
David S. Miller3610cda2011-01-05 15:38:53 -08004222static int selinux_socket_unix_stream_connect(struct sock *sock,
4223 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 struct sock *newsk)
4225{
David S. Miller3610cda2011-01-05 15:38:53 -08004226 struct sk_security_struct *sksec_sock = sock->sk_security;
4227 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004228 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004229 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004230 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004231 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 int err;
4233
Thomas Liu2bf49692009-07-14 12:14:09 -04004234 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004235 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004236 ad.u.net = &net;
4237 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Paul Moore4d1e2452010-04-22 14:46:18 -04004239 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4240 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4242 if (err)
4243 return err;
4244
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004246 sksec_new->peer_sid = sksec_sock->sid;
4247 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4248 &sksec_new->sid);
4249 if (err)
4250 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004251
Paul Moore4d1e2452010-04-22 14:46:18 -04004252 /* connecting socket */
4253 sksec_sock->peer_sid = sksec_new->sid;
4254
4255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256}
4257
4258static int selinux_socket_unix_may_send(struct socket *sock,
4259 struct socket *other)
4260{
Paul Moore253bfae2010-04-22 14:46:19 -04004261 struct sk_security_struct *ssec = sock->sk->sk_security;
4262 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004263 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004264 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004265 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Thomas Liu2bf49692009-07-14 12:14:09 -04004267 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004268 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004269 ad.u.net = &net;
4270 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Paul Moore253bfae2010-04-22 14:46:19 -04004272 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4273 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
Paul Mooreeffad8d2008-01-29 08:49:27 -05004276static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4277 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004278 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004279{
4280 int err;
4281 u32 if_sid;
4282 u32 node_sid;
4283
4284 err = sel_netif_sid(ifindex, &if_sid);
4285 if (err)
4286 return err;
4287 err = avc_has_perm(peer_sid, if_sid,
4288 SECCLASS_NETIF, NETIF__INGRESS, ad);
4289 if (err)
4290 return err;
4291
4292 err = sel_netnode_sid(addrp, family, &node_sid);
4293 if (err)
4294 return err;
4295 return avc_has_perm(peer_sid, node_sid,
4296 SECCLASS_NODE, NODE__RECVFROM, ad);
4297}
4298
Paul Moore220deb92008-01-29 08:38:23 -05004299static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004300 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004301{
Paul Moore277d3422008-12-31 12:54:11 -05004302 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004303 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004304 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004305 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004306 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004307 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004308 char *addrp;
4309
Thomas Liu2bf49692009-07-14 12:14:09 -04004310 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004311 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004312 ad.u.net = &net;
4313 ad.u.net->netif = skb->skb_iif;
4314 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004315 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4316 if (err)
4317 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004318
Paul Moore58bfbb52009-03-27 17:10:41 -04004319 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004320 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004321 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004322 if (err)
4323 return err;
4324 }
Paul Moore220deb92008-01-29 08:38:23 -05004325
Steffen Klassertb9679a72011-02-23 12:55:21 +01004326 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4327 if (err)
4328 return err;
4329 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004330
James Morris4e5ab4c2006-06-09 00:33:33 -07004331 return err;
4332}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004333
James Morris4e5ab4c2006-06-09 00:33:33 -07004334static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4335{
Paul Moore220deb92008-01-29 08:38:23 -05004336 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004337 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004338 u16 family = sk->sk_family;
4339 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004340 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004341 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004342 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004343 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004344 u8 secmark_active;
4345 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004346
James Morris4e5ab4c2006-06-09 00:33:33 -07004347 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004348 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004349
4350 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004351 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004352 family = PF_INET;
4353
Paul Moored8395c82008-10-10 10:16:30 -04004354 /* If any sort of compatibility mode is enabled then handoff processing
4355 * to the selinux_sock_rcv_skb_compat() function to deal with the
4356 * special handling. We do this in an attempt to keep this function
4357 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004358 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004359 return selinux_sock_rcv_skb_compat(sk, skb, family);
4360
4361 secmark_active = selinux_secmark_enabled();
4362 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4363 if (!secmark_active && !peerlbl_active)
4364 return 0;
4365
Thomas Liu2bf49692009-07-14 12:14:09 -04004366 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004367 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004368 ad.u.net = &net;
4369 ad.u.net->netif = skb->skb_iif;
4370 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004371 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004372 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004373 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004374
Paul Moored8395c82008-10-10 10:16:30 -04004375 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004376 u32 peer_sid;
4377
4378 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4379 if (err)
4380 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004381 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004382 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004383 if (err) {
4384 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004385 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004386 }
Paul Moored621d352008-01-29 08:43:36 -05004387 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4388 PEER__RECV, &ad);
Chad Hanson351381d2013-12-23 17:45:01 -05004389 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004390 selinux_netlbl_err(skb, err, 0);
Chad Hanson351381d2013-12-23 17:45:01 -05004391 return err;
4392 }
Paul Moored621d352008-01-29 08:43:36 -05004393 }
4394
Paul Moored8395c82008-10-10 10:16:30 -04004395 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004396 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4397 PACKET__RECV, &ad);
4398 if (err)
4399 return err;
4400 }
4401
Paul Moored621d352008-01-29 08:43:36 -05004402 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403}
4404
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004405static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4406 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
4408 int err = 0;
4409 char *scontext;
4410 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004411 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004412 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Paul Moore253bfae2010-04-22 14:46:19 -04004414 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4415 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004416 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004417 if (peer_sid == SECSID_NULL)
4418 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004420 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
4424 if (scontext_len > len) {
4425 err = -ERANGE;
4426 goto out_len;
4427 }
4428
4429 if (copy_to_user(optval, scontext, scontext_len))
4430 err = -EFAULT;
4431
4432out_len:
4433 if (put_user(scontext_len, optlen))
4434 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return err;
4437}
4438
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004439static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004440{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004441 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004442 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004443
Paul Mooreaa862902008-10-10 10:16:29 -04004444 if (skb && skb->protocol == htons(ETH_P_IP))
4445 family = PF_INET;
4446 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4447 family = PF_INET6;
4448 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004449 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004450 else
4451 goto out;
4452
4453 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004454 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004455 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004456 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004457
Paul Moore75e22912008-01-29 08:38:04 -05004458out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004459 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004460 if (peer_secid == SECSID_NULL)
4461 return -EINVAL;
4462 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004463}
4464
Al Viro7d877f32005-10-21 03:20:43 -04004465static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
Paul Moore84914b72010-04-22 14:46:18 -04004467 struct sk_security_struct *sksec;
4468
4469 sksec = kzalloc(sizeof(*sksec), priority);
4470 if (!sksec)
4471 return -ENOMEM;
4472
4473 sksec->peer_sid = SECINITSID_UNLABELED;
4474 sksec->sid = SECINITSID_UNLABELED;
4475 selinux_netlbl_sk_security_reset(sksec);
4476 sk->sk_security = sksec;
4477
4478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
4481static void selinux_sk_free_security(struct sock *sk)
4482{
Paul Moore84914b72010-04-22 14:46:18 -04004483 struct sk_security_struct *sksec = sk->sk_security;
4484
4485 sk->sk_security = NULL;
4486 selinux_netlbl_sk_security_free(sksec);
4487 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004490static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4491{
Eric Parisdd3e7832010-04-07 15:08:46 -04004492 struct sk_security_struct *sksec = sk->sk_security;
4493 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004494
Eric Parisdd3e7832010-04-07 15:08:46 -04004495 newsksec->sid = sksec->sid;
4496 newsksec->peer_sid = sksec->peer_sid;
4497 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004498
Eric Parisdd3e7832010-04-07 15:08:46 -04004499 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004500}
4501
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004502static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004503{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004504 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004505 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004506 else {
4507 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004508
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004509 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004510 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004511}
4512
Eric Paris828dfe12008-04-17 13:17:49 -04004513static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004514{
4515 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4516 struct sk_security_struct *sksec = sk->sk_security;
4517
David Woodhouse2148ccc2006-09-29 15:50:25 -07004518 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4519 sk->sk_family == PF_UNIX)
4520 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004521 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004522}
4523
Adrian Bunk9a673e52006-08-15 00:03:53 -07004524static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4525 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004526{
4527 struct sk_security_struct *sksec = sk->sk_security;
4528 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004529 u16 family = sk->sk_family;
Paul Moore2ea04e52013-12-04 16:10:51 -05004530 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004531 u32 peersid;
4532
Paul Mooreaa862902008-10-10 10:16:29 -04004533 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4534 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4535 family = PF_INET;
4536
4537 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004538 if (err)
4539 return err;
Paul Moore2ea04e52013-12-04 16:10:51 -05004540 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4541 if (err)
4542 return err;
4543 req->secid = connsid;
4544 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004545
Paul Moore389fb802009-03-27 17:10:34 -04004546 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004547}
4548
Adrian Bunk9a673e52006-08-15 00:03:53 -07004549static void selinux_inet_csk_clone(struct sock *newsk,
4550 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004551{
4552 struct sk_security_struct *newsksec = newsk->sk_security;
4553
4554 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004555 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004556 /* NOTE: Ideally, we should also get the isec->sid for the
4557 new socket in sync, but we don't have the isec available yet.
4558 So we will wait until sock_graft to do it, by which
4559 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004560
Paul Moore9f2ad662006-11-17 17:38:53 -05004561 /* We don't need to take any sort of lock here as we are the only
4562 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004563 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004564}
4565
Paul Moore014ab192008-10-10 10:16:33 -04004566static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004567{
Paul Mooreaa862902008-10-10 10:16:29 -04004568 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004569 struct sk_security_struct *sksec = sk->sk_security;
4570
Paul Mooreaa862902008-10-10 10:16:29 -04004571 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4572 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4573 family = PF_INET;
4574
4575 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004576}
4577
Eric Paris2606fd12010-10-13 16:24:41 -04004578static int selinux_secmark_relabel_packet(u32 sid)
4579{
4580 const struct task_security_struct *__tsec;
4581 u32 tsid;
4582
4583 __tsec = current_security();
4584 tsid = __tsec->sid;
4585
4586 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4587}
4588
4589static void selinux_secmark_refcount_inc(void)
4590{
4591 atomic_inc(&selinux_secmark_refcount);
4592}
4593
4594static void selinux_secmark_refcount_dec(void)
4595{
4596 atomic_dec(&selinux_secmark_refcount);
4597}
4598
Adrian Bunk9a673e52006-08-15 00:03:53 -07004599static void selinux_req_classify_flow(const struct request_sock *req,
4600 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004601{
David S. Miller1d28f422011-03-12 00:29:39 -05004602 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004603}
4604
Paul Mooreed6d76e2009-08-28 18:12:49 -04004605static int selinux_tun_dev_create(void)
4606{
4607 u32 sid = current_sid();
4608
4609 /* we aren't taking into account the "sockcreate" SID since the socket
4610 * that is being created here is not a socket in the traditional sense,
4611 * instead it is a private sock, accessible only to the kernel, and
4612 * representing a wide range of network traffic spanning multiple
4613 * connections unlike traditional sockets - check the TUN driver to
4614 * get a better understanding of why this socket is special */
4615
4616 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4617 NULL);
4618}
4619
4620static void selinux_tun_dev_post_create(struct sock *sk)
4621{
4622 struct sk_security_struct *sksec = sk->sk_security;
4623
4624 /* we don't currently perform any NetLabel based labeling here and it
4625 * isn't clear that we would want to do so anyway; while we could apply
4626 * labeling without the support of the TUN user the resulting labeled
4627 * traffic from the other end of the connection would almost certainly
4628 * cause confusion to the TUN user that had no idea network labeling
4629 * protocols were being used */
4630
4631 /* see the comments in selinux_tun_dev_create() about why we don't use
4632 * the sockcreate SID here */
4633
4634 sksec->sid = current_sid();
4635 sksec->sclass = SECCLASS_TUN_SOCKET;
4636}
4637
4638static int selinux_tun_dev_attach(struct sock *sk)
4639{
4640 struct sk_security_struct *sksec = sk->sk_security;
4641 u32 sid = current_sid();
4642 int err;
4643
4644 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4645 TUN_SOCKET__RELABELFROM, NULL);
4646 if (err)
4647 return err;
4648 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4649 TUN_SOCKET__RELABELTO, NULL);
4650 if (err)
4651 return err;
4652
4653 sksec->sid = sid;
4654
4655 return 0;
4656}
4657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4659{
4660 int err = 0;
4661 u32 perm;
4662 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004663 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 if (skb->len < NLMSG_SPACE(0)) {
4666 err = -EINVAL;
4667 goto out;
4668 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004669 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004670
Paul Moore253bfae2010-04-22 14:46:19 -04004671 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 if (err) {
4673 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004674 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 "SELinux: unrecognized netlink message"
4676 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004677 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004678 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 err = 0;
4680 }
4681
4682 /* Ignore */
4683 if (err == -ENOENT)
4684 err = 0;
4685 goto out;
4686 }
4687
Paul Moore253bfae2010-04-22 14:46:19 -04004688 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689out:
4690 return err;
4691}
4692
4693#ifdef CONFIG_NETFILTER
4694
Paul Mooreeffad8d2008-01-29 08:49:27 -05004695static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4696 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Paul Mooredfaebe92008-10-10 10:16:31 -04004698 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004699 char *addrp;
4700 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004701 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004702 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004703 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004704 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004705 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004706 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004707
Paul Mooreeffad8d2008-01-29 08:49:27 -05004708 if (!selinux_policycap_netpeer)
4709 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004710
Paul Mooreeffad8d2008-01-29 08:49:27 -05004711 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004712 netlbl_active = netlbl_enabled();
4713 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004714 if (!secmark_active && !peerlbl_active)
4715 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004716
Paul Moored8395c82008-10-10 10:16:30 -04004717 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4718 return NF_DROP;
4719
Thomas Liu2bf49692009-07-14 12:14:09 -04004720 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004721 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004722 ad.u.net = &net;
4723 ad.u.net->netif = ifindex;
4724 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004725 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4726 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Paul Mooredfaebe92008-10-10 10:16:31 -04004728 if (peerlbl_active) {
4729 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4730 peer_sid, &ad);
4731 if (err) {
4732 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004733 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004734 }
4735 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004736
4737 if (secmark_active)
4738 if (avc_has_perm(peer_sid, skb->secmark,
4739 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4740 return NF_DROP;
4741
Paul Moore948bf852008-10-10 10:16:32 -04004742 if (netlbl_active)
4743 /* we do this in the FORWARD path and not the POST_ROUTING
4744 * path because we want to make sure we apply the necessary
4745 * labeling before IPsec is applied so we can leverage AH
4746 * protection */
4747 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4748 return NF_DROP;
4749
Paul Mooreeffad8d2008-01-29 08:49:27 -05004750 return NF_ACCEPT;
4751}
4752
4753static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4754 struct sk_buff *skb,
4755 const struct net_device *in,
4756 const struct net_device *out,
4757 int (*okfn)(struct sk_buff *))
4758{
4759 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4760}
4761
4762#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4763static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4764 struct sk_buff *skb,
4765 const struct net_device *in,
4766 const struct net_device *out,
4767 int (*okfn)(struct sk_buff *))
4768{
4769 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4770}
4771#endif /* IPV6 */
4772
Paul Moore948bf852008-10-10 10:16:32 -04004773static unsigned int selinux_ip_output(struct sk_buff *skb,
4774 u16 family)
4775{
Paul Moore1c5d9d12013-12-04 16:10:45 -05004776 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004777 u32 sid;
4778
4779 if (!netlbl_enabled())
4780 return NF_ACCEPT;
4781
4782 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4783 * because we want to make sure we apply the necessary labeling
4784 * before IPsec is applied so we can leverage AH protection */
Paul Moore1c5d9d12013-12-04 16:10:45 -05004785 sk = skb->sk;
4786 if (sk) {
4787 struct sk_security_struct *sksec;
4788
4789 if (sk->sk_state == TCP_LISTEN)
4790 /* if the socket is the listening state then this
4791 * packet is a SYN-ACK packet which means it needs to
4792 * be labeled based on the connection/request_sock and
4793 * not the parent socket. unfortunately, we can't
4794 * lookup the request_sock yet as it isn't queued on
4795 * the parent socket until after the SYN-ACK is sent.
4796 * the "solution" is to simply pass the packet as-is
4797 * as any IP option based labeling should be copied
4798 * from the initial connection request (in the IP
4799 * layer). it is far from ideal, but until we get a
4800 * security label in the packet itself this is the
4801 * best we can do. */
4802 return NF_ACCEPT;
4803
4804 /* standard practice, label using the parent socket */
4805 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004806 sid = sksec->sid;
4807 } else
4808 sid = SECINITSID_KERNEL;
4809 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4810 return NF_DROP;
4811
4812 return NF_ACCEPT;
4813}
4814
4815static unsigned int selinux_ipv4_output(unsigned int hooknum,
4816 struct sk_buff *skb,
4817 const struct net_device *in,
4818 const struct net_device *out,
4819 int (*okfn)(struct sk_buff *))
4820{
4821 return selinux_ip_output(skb, PF_INET);
4822}
4823
Paul Mooreeffad8d2008-01-29 08:49:27 -05004824static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4825 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004826 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004827{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004828 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004829 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004830 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004831 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004832 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004833 char *addrp;
4834 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004835
Paul Mooreeffad8d2008-01-29 08:49:27 -05004836 if (sk == NULL)
4837 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004838 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004839
Thomas Liu2bf49692009-07-14 12:14:09 -04004840 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004841 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004842 ad.u.net = &net;
4843 ad.u.net->netif = ifindex;
4844 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004845 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4846 return NF_DROP;
4847
Paul Moore58bfbb52009-03-27 17:10:41 -04004848 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004849 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004850 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004851 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004852
Steffen Klassertb9679a72011-02-23 12:55:21 +01004853 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4854 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004855
Paul Mooreeffad8d2008-01-29 08:49:27 -05004856 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857}
4858
Paul Mooreeffad8d2008-01-29 08:49:27 -05004859static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4860 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004862 u32 secmark_perm;
4863 u32 peer_sid;
4864 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004865 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004866 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004867 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004868 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004869 u8 secmark_active;
4870 u8 peerlbl_active;
4871
Paul Mooreeffad8d2008-01-29 08:49:27 -05004872 /* If any sort of compatibility mode is enabled then handoff processing
4873 * to the selinux_ip_postroute_compat() function to deal with the
4874 * special handling. We do this in an attempt to keep this function
4875 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004876 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004877 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moore420cc6d2013-12-10 14:58:01 -05004878
Paul Mooreeffad8d2008-01-29 08:49:27 -05004879 secmark_active = selinux_secmark_enabled();
4880 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4881 if (!secmark_active && !peerlbl_active)
4882 return NF_ACCEPT;
4883
Paul Mooreeffad8d2008-01-29 08:49:27 -05004884 sk = skb->sk;
Paul Moore420cc6d2013-12-10 14:58:01 -05004885
4886#ifdef CONFIG_XFRM
4887 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4888 * packet transformation so allow the packet to pass without any checks
4889 * since we'll have another chance to perform access control checks
4890 * when the packet is on it's final way out.
4891 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4892 * is NULL, in this case go ahead and apply access control.
4893 * is NULL, in this case go ahead and apply access control.
4894 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4895 * TCP listening state we cannot wait until the XFRM processing
4896 * is done as we will miss out on the SA label if we do;
4897 * unfortunately, this means more work, but it is only once per
4898 * connection. */
4899 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4900 !(sk != NULL && sk->sk_state == TCP_LISTEN))
4901 return NF_ACCEPT;
4902#endif
4903
Paul Moored8395c82008-10-10 10:16:30 -04004904 if (sk == NULL) {
Paul Moore2ea04e52013-12-04 16:10:51 -05004905 /* Without an associated socket the packet is either coming
4906 * from the kernel or it is being forwarded; check the packet
4907 * to determine which and if the packet is being forwarded
4908 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004909 if (skb->skb_iif) {
4910 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004911 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004912 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004913 } else {
4914 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004915 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004916 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004917 } else if (sk->sk_state == TCP_LISTEN) {
4918 /* Locally generated packet but the associated socket is in the
4919 * listening state which means this is a SYN-ACK packet. In
4920 * this particular case the correct security label is assigned
4921 * to the connection/request_sock but unfortunately we can't
4922 * query the request_sock as it isn't queued on the parent
4923 * socket until after the SYN-ACK packet is sent; the only
4924 * viable choice is to regenerate the label like we do in
4925 * selinux_inet_conn_request(). See also selinux_ip_output()
4926 * for similar problems. */
4927 u32 skb_sid;
4928 struct sk_security_struct *sksec = sk->sk_security;
4929 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4930 return NF_DROP;
Paul Moore420cc6d2013-12-10 14:58:01 -05004931 /* At this point, if the returned skb peerlbl is SECSID_NULL
4932 * and the packet has been through at least one XFRM
4933 * transformation then we must be dealing with the "final"
4934 * form of labeled IPsec packet; since we've already applied
4935 * all of our access controls on this packet we can safely
4936 * pass the packet. */
4937 if (skb_sid == SECSID_NULL) {
4938 switch (family) {
4939 case PF_INET:
4940 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4941 return NF_ACCEPT;
4942 break;
4943 case PF_INET6:
4944 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4945 return NF_ACCEPT;
4946 default:
4947 return NF_DROP_ERR(-ECONNREFUSED);
4948 }
4949 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004950 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4951 return NF_DROP;
4952 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004953 } else {
Paul Moore2ea04e52013-12-04 16:10:51 -05004954 /* Locally generated packet, fetch the security label from the
4955 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004956 struct sk_security_struct *sksec = sk->sk_security;
4957 peer_sid = sksec->sid;
4958 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004959 }
4960
Thomas Liu2bf49692009-07-14 12:14:09 -04004961 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004962 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004963 ad.u.net = &net;
4964 ad.u.net->netif = ifindex;
4965 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004966 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004967 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004968
Paul Mooreeffad8d2008-01-29 08:49:27 -05004969 if (secmark_active)
4970 if (avc_has_perm(peer_sid, skb->secmark,
4971 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004972 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004973
4974 if (peerlbl_active) {
4975 u32 if_sid;
4976 u32 node_sid;
4977
4978 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004979 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004980 if (avc_has_perm(peer_sid, if_sid,
4981 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004982 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004983
4984 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004985 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004986 if (avc_has_perm(peer_sid, node_sid,
4987 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004988 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004989 }
4990
4991 return NF_ACCEPT;
4992}
4993
4994static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4995 struct sk_buff *skb,
4996 const struct net_device *in,
4997 const struct net_device *out,
4998 int (*okfn)(struct sk_buff *))
4999{
5000 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
5003#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005004static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
5005 struct sk_buff *skb,
5006 const struct net_device *in,
5007 const struct net_device *out,
5008 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005010 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012#endif /* IPV6 */
5013
5014#endif /* CONFIG_NETFILTER */
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5017{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 int err;
5019
Eric Paris200ac532009-02-12 15:01:04 -05005020 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 if (err)
5022 return err;
5023
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005024 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025}
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027static int ipc_alloc_security(struct task_struct *task,
5028 struct kern_ipc_perm *perm,
5029 u16 sclass)
5030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11005032 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
James Morris89d155e2005-10-30 14:59:21 -08005034 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 if (!isec)
5036 return -ENOMEM;
5037
David Howells275bb412008-11-14 10:39:19 +11005038 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11005040 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 perm->security = isec;
5042
5043 return 0;
5044}
5045
5046static void ipc_free_security(struct kern_ipc_perm *perm)
5047{
5048 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 perm->security = NULL;
5050 kfree(isec);
5051}
5052
5053static int msg_msg_alloc_security(struct msg_msg *msg)
5054{
5055 struct msg_security_struct *msec;
5056
James Morris89d155e2005-10-30 14:59:21 -08005057 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 if (!msec)
5059 return -ENOMEM;
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 msec->sid = SECINITSID_UNLABELED;
5062 msg->security = msec;
5063
5064 return 0;
5065}
5066
5067static void msg_msg_free_security(struct msg_msg *msg)
5068{
5069 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
5071 msg->security = NULL;
5072 kfree(msec);
5073}
5074
5075static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005076 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005079 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005080 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005081 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 isec = ipc_perms->security;
5084
Thomas Liu2bf49692009-07-14 12:14:09 -04005085 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005086 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 ad.u.ipc_id = ipc_perms->key;
5088
David Howells275bb412008-11-14 10:39:19 +11005089 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090}
5091
5092static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5093{
5094 return msg_msg_alloc_security(msg);
5095}
5096
5097static void selinux_msg_msg_free_security(struct msg_msg *msg)
5098{
5099 msg_msg_free_security(msg);
5100}
5101
5102/* message queue security operations */
5103static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005106 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005107 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005108 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 int rc;
5110
5111 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5112 if (rc)
5113 return rc;
5114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 isec = msq->q_perm.security;
5116
Thomas Liu2bf49692009-07-14 12:14:09 -04005117 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005118 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005119 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
David Howells275bb412008-11-14 10:39:19 +11005121 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 MSGQ__CREATE, &ad);
5123 if (rc) {
5124 ipc_free_security(&msq->q_perm);
5125 return rc;
5126 }
5127 return 0;
5128}
5129
5130static void selinux_msg_queue_free_security(struct msg_queue *msq)
5131{
5132 ipc_free_security(&msq->q_perm);
5133}
5134
5135static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005138 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005139 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005140 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 isec = msq->q_perm.security;
5143
Thomas Liu2bf49692009-07-14 12:14:09 -04005144 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005145 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 ad.u.ipc_id = msq->q_perm.key;
5147
David Howells275bb412008-11-14 10:39:19 +11005148 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 MSGQ__ASSOCIATE, &ad);
5150}
5151
5152static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5153{
5154 int err;
5155 int perms;
5156
Eric Paris828dfe12008-04-17 13:17:49 -04005157 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 case IPC_INFO:
5159 case MSG_INFO:
5160 /* No specific object, just general system-wide information. */
5161 return task_has_system(current, SYSTEM__IPC_INFO);
5162 case IPC_STAT:
5163 case MSG_STAT:
5164 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5165 break;
5166 case IPC_SET:
5167 perms = MSGQ__SETATTR;
5168 break;
5169 case IPC_RMID:
5170 perms = MSGQ__DESTROY;
5171 break;
5172 default:
5173 return 0;
5174 }
5175
Stephen Smalley6af963f2005-05-01 08:58:39 -07005176 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return err;
5178}
5179
5180static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 struct ipc_security_struct *isec;
5183 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005184 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005185 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005186 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 int rc;
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 isec = msq->q_perm.security;
5190 msec = msg->security;
5191
5192 /*
5193 * First time through, need to assign label to the message
5194 */
5195 if (msec->sid == SECINITSID_UNLABELED) {
5196 /*
5197 * Compute new sid based on current process and
5198 * message queue this message will be stored in
5199 */
David Howells275bb412008-11-14 10:39:19 +11005200 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005201 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if (rc)
5203 return rc;
5204 }
5205
Thomas Liu2bf49692009-07-14 12:14:09 -04005206 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005207 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 ad.u.ipc_id = msq->q_perm.key;
5209
5210 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005211 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 MSGQ__WRITE, &ad);
5213 if (!rc)
5214 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005215 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5216 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 if (!rc)
5218 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005219 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5220 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
5222 return rc;
5223}
5224
5225static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5226 struct task_struct *target,
5227 long type, int mode)
5228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 struct ipc_security_struct *isec;
5230 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005231 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005232 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005233 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 int rc;
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 isec = msq->q_perm.security;
5237 msec = msg->security;
5238
Thomas Liu2bf49692009-07-14 12:14:09 -04005239 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005240 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005241 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
David Howells275bb412008-11-14 10:39:19 +11005243 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 SECCLASS_MSGQ, MSGQ__READ, &ad);
5245 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005246 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 SECCLASS_MSG, MSG__RECEIVE, &ad);
5248 return rc;
5249}
5250
5251/* Shared Memory security operations */
5252static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005255 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005256 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005257 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 int rc;
5259
5260 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5261 if (rc)
5262 return rc;
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 isec = shp->shm_perm.security;
5265
Thomas Liu2bf49692009-07-14 12:14:09 -04005266 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005267 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005268 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
David Howells275bb412008-11-14 10:39:19 +11005270 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 SHM__CREATE, &ad);
5272 if (rc) {
5273 ipc_free_security(&shp->shm_perm);
5274 return rc;
5275 }
5276 return 0;
5277}
5278
5279static void selinux_shm_free_security(struct shmid_kernel *shp)
5280{
5281 ipc_free_security(&shp->shm_perm);
5282}
5283
5284static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5285{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005287 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005288 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005289 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 isec = shp->shm_perm.security;
5292
Thomas Liu2bf49692009-07-14 12:14:09 -04005293 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005294 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 ad.u.ipc_id = shp->shm_perm.key;
5296
David Howells275bb412008-11-14 10:39:19 +11005297 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 SHM__ASSOCIATE, &ad);
5299}
5300
5301/* Note, at this point, shp is locked down */
5302static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5303{
5304 int perms;
5305 int err;
5306
Eric Paris828dfe12008-04-17 13:17:49 -04005307 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 case IPC_INFO:
5309 case SHM_INFO:
5310 /* No specific object, just general system-wide information. */
5311 return task_has_system(current, SYSTEM__IPC_INFO);
5312 case IPC_STAT:
5313 case SHM_STAT:
5314 perms = SHM__GETATTR | SHM__ASSOCIATE;
5315 break;
5316 case IPC_SET:
5317 perms = SHM__SETATTR;
5318 break;
5319 case SHM_LOCK:
5320 case SHM_UNLOCK:
5321 perms = SHM__LOCK;
5322 break;
5323 case IPC_RMID:
5324 perms = SHM__DESTROY;
5325 break;
5326 default:
5327 return 0;
5328 }
5329
Stephen Smalley6af963f2005-05-01 08:58:39 -07005330 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return err;
5332}
5333
5334static int selinux_shm_shmat(struct shmid_kernel *shp,
5335 char __user *shmaddr, int shmflg)
5336{
5337 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 if (shmflg & SHM_RDONLY)
5340 perms = SHM__READ;
5341 else
5342 perms = SHM__READ | SHM__WRITE;
5343
Stephen Smalley6af963f2005-05-01 08:58:39 -07005344 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345}
5346
5347/* Semaphore security operations */
5348static int selinux_sem_alloc_security(struct sem_array *sma)
5349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005351 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005352 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005353 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 int rc;
5355
5356 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5357 if (rc)
5358 return rc;
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 isec = sma->sem_perm.security;
5361
Thomas Liu2bf49692009-07-14 12:14:09 -04005362 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005363 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005364 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
David Howells275bb412008-11-14 10:39:19 +11005366 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 SEM__CREATE, &ad);
5368 if (rc) {
5369 ipc_free_security(&sma->sem_perm);
5370 return rc;
5371 }
5372 return 0;
5373}
5374
5375static void selinux_sem_free_security(struct sem_array *sma)
5376{
5377 ipc_free_security(&sma->sem_perm);
5378}
5379
5380static int selinux_sem_associate(struct sem_array *sma, int semflg)
5381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005383 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005384 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005385 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 isec = sma->sem_perm.security;
5388
Thomas Liu2bf49692009-07-14 12:14:09 -04005389 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005390 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 ad.u.ipc_id = sma->sem_perm.key;
5392
David Howells275bb412008-11-14 10:39:19 +11005393 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 SEM__ASSOCIATE, &ad);
5395}
5396
5397/* Note, at this point, sma is locked down */
5398static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5399{
5400 int err;
5401 u32 perms;
5402
Eric Paris828dfe12008-04-17 13:17:49 -04005403 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 case IPC_INFO:
5405 case SEM_INFO:
5406 /* No specific object, just general system-wide information. */
5407 return task_has_system(current, SYSTEM__IPC_INFO);
5408 case GETPID:
5409 case GETNCNT:
5410 case GETZCNT:
5411 perms = SEM__GETATTR;
5412 break;
5413 case GETVAL:
5414 case GETALL:
5415 perms = SEM__READ;
5416 break;
5417 case SETVAL:
5418 case SETALL:
5419 perms = SEM__WRITE;
5420 break;
5421 case IPC_RMID:
5422 perms = SEM__DESTROY;
5423 break;
5424 case IPC_SET:
5425 perms = SEM__SETATTR;
5426 break;
5427 case IPC_STAT:
5428 case SEM_STAT:
5429 perms = SEM__GETATTR | SEM__ASSOCIATE;
5430 break;
5431 default:
5432 return 0;
5433 }
5434
Stephen Smalley6af963f2005-05-01 08:58:39 -07005435 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 return err;
5437}
5438
5439static int selinux_sem_semop(struct sem_array *sma,
5440 struct sembuf *sops, unsigned nsops, int alter)
5441{
5442 u32 perms;
5443
5444 if (alter)
5445 perms = SEM__READ | SEM__WRITE;
5446 else
5447 perms = SEM__READ;
5448
Stephen Smalley6af963f2005-05-01 08:58:39 -07005449 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450}
5451
5452static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 u32 av = 0;
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 av = 0;
5457 if (flag & S_IRUGO)
5458 av |= IPC__UNIX_READ;
5459 if (flag & S_IWUGO)
5460 av |= IPC__UNIX_WRITE;
5461
5462 if (av == 0)
5463 return 0;
5464
Stephen Smalley6af963f2005-05-01 08:58:39 -07005465 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
5467
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005468static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5469{
5470 struct ipc_security_struct *isec = ipcp->security;
5471 *secid = isec->sid;
5472}
5473
Eric Paris828dfe12008-04-17 13:17:49 -04005474static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
5476 if (inode)
5477 inode_doinit_with_dentry(inode, dentry);
5478}
5479
5480static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005481 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
David Howells275bb412008-11-14 10:39:19 +11005483 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005484 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005486 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005489 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 if (error)
5491 return error;
5492 }
5493
David Howells275bb412008-11-14 10:39:19 +11005494 rcu_read_lock();
5495 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
5497 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005498 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005500 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005502 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005504 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005505 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005506 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005507 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005508 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 else
David Howells275bb412008-11-14 10:39:19 +11005510 goto invalid;
5511 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
5513 if (!sid)
5514 return 0;
5515
Al Viro04ff9702007-03-12 16:17:58 +00005516 error = security_sid_to_context(sid, value, &len);
5517 if (error)
5518 return error;
5519 return len;
David Howells275bb412008-11-14 10:39:19 +11005520
5521invalid:
5522 rcu_read_unlock();
5523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
5526static int selinux_setprocattr(struct task_struct *p,
5527 char *name, void *value, size_t size)
5528{
5529 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005530 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005531 struct cred *new;
5532 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 int error;
5534 char *str = value;
5535
5536 if (current != p) {
5537 /* SELinux only allows a process to change its own
5538 security attributes. */
5539 return -EACCES;
5540 }
5541
5542 /*
5543 * Basic control over ability to set these attributes at all.
5544 * current == p, but we'll pass them separately in case the
5545 * above restriction is ever removed.
5546 */
5547 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005548 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005550 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005551 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005552 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005553 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005554 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005556 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 else
5558 error = -EINVAL;
5559 if (error)
5560 return error;
5561
5562 /* Obtain a SID for the context, if one was specified. */
5563 if (size && str[1] && str[1] != '\n') {
5564 if (str[size-1] == '\n') {
5565 str[size-1] = 0;
5566 size--;
5567 }
5568 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005569 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5570 if (!capable(CAP_MAC_ADMIN))
5571 return error;
5572 error = security_context_to_sid_force(value, size,
5573 &sid);
5574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 if (error)
5576 return error;
5577 }
5578
David Howellsd84f4f92008-11-14 10:39:23 +11005579 new = prepare_creds();
5580 if (!new)
5581 return -ENOMEM;
5582
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 /* Permission checking based on the specified context is
5584 performed during the actual operation (execve,
5585 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005586 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 checks and may_create for the file creation checks. The
5588 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005589 tsec = new->security;
5590 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005592 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005594 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005595 error = may_create_key(sid, p);
5596 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005597 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005598 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005599 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005600 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005601 } else if (!strcmp(name, "current")) {
5602 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005604 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005605
David Howellsd84f4f92008-11-14 10:39:23 +11005606 /* Only allow single threaded processes to change context */
5607 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005608 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005609 error = security_bounded_transition(tsec->sid, sid);
5610 if (error)
5611 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
5614 /* Check permissions for the transition. */
5615 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005616 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005618 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 /* Check for ptracing, and update the task SID if ok.
5621 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005622 ptsid = 0;
Oleg Nesterov58c23142013-12-23 17:45:01 -05005623 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02005624 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005625 if (tracer)
5626 ptsid = task_sid(tracer);
Oleg Nesterov58c23142013-12-23 17:45:01 -05005627 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
David Howellsd84f4f92008-11-14 10:39:23 +11005629 if (tracer) {
5630 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5631 PROCESS__PTRACE, NULL);
5632 if (error)
5633 goto abort_change;
5634 }
5635
5636 tsec->sid = sid;
5637 } else {
5638 error = -EINVAL;
5639 goto abort_change;
5640 }
5641
5642 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005644
5645abort_change:
5646 abort_creds(new);
5647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648}
5649
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005650static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5651{
5652 return security_sid_to_context(secid, secdata, seclen);
5653}
5654
David Howells7bf570d2008-04-29 20:52:51 +01005655static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005656{
5657 return security_context_to_sid(secdata, seclen, secid);
5658}
5659
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005660static void selinux_release_secctx(char *secdata, u32 seclen)
5661{
Paul Moore088999e2007-08-01 11:12:58 -04005662 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005663}
5664
David P. Quigley1ee65e32009-09-03 14:25:57 -04005665/*
5666 * called with inode->i_mutex locked
5667 */
5668static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5669{
5670 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5671}
5672
5673/*
5674 * called with inode->i_mutex locked
5675 */
5676static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5677{
5678 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5679}
5680
5681static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5682{
5683 int len = 0;
5684 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5685 ctx, true);
5686 if (len < 0)
5687 return len;
5688 *ctxlen = len;
5689 return 0;
5690}
Michael LeMayd7200242006-06-22 14:47:17 -07005691#ifdef CONFIG_KEYS
5692
David Howellsd84f4f92008-11-14 10:39:23 +11005693static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005694 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005695{
David Howellsd84f4f92008-11-14 10:39:23 +11005696 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005697 struct key_security_struct *ksec;
5698
5699 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5700 if (!ksec)
5701 return -ENOMEM;
5702
David Howellsd84f4f92008-11-14 10:39:23 +11005703 tsec = cred->security;
5704 if (tsec->keycreate_sid)
5705 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005706 else
David Howellsd84f4f92008-11-14 10:39:23 +11005707 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005708
David Howells275bb412008-11-14 10:39:19 +11005709 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005710 return 0;
5711}
5712
5713static void selinux_key_free(struct key *k)
5714{
5715 struct key_security_struct *ksec = k->security;
5716
5717 k->security = NULL;
5718 kfree(ksec);
5719}
5720
5721static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005722 const struct cred *cred,
5723 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005724{
5725 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005726 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005727 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005728
5729 /* if no specific permissions are requested, we skip the
5730 permission check. No serious, additional covert channels
5731 appear to be created. */
5732 if (perm == 0)
5733 return 0;
5734
David Howellsd84f4f92008-11-14 10:39:23 +11005735 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005736
5737 key = key_ref_to_ptr(key_ref);
5738 ksec = key->security;
5739
5740 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005741}
5742
David Howells70a5bb72008-04-29 01:01:26 -07005743static int selinux_key_getsecurity(struct key *key, char **_buffer)
5744{
5745 struct key_security_struct *ksec = key->security;
5746 char *context = NULL;
5747 unsigned len;
5748 int rc;
5749
5750 rc = security_sid_to_context(ksec->sid, &context, &len);
5751 if (!rc)
5752 rc = len;
5753 *_buffer = context;
5754 return rc;
5755}
5756
Michael LeMayd7200242006-06-22 14:47:17 -07005757#endif
5758
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005760 .name = "selinux",
5761
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05005762 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5763 .binder_transaction = selinux_binder_transaction,
5764 .binder_transfer_binder = selinux_binder_transfer_binder,
5765 .binder_transfer_file = selinux_binder_transfer_file,
5766
Ingo Molnar9e488582009-05-07 19:26:19 +10005767 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005768 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005770 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 .capable = selinux_capable,
5772 .quotactl = selinux_quotactl,
5773 .quota_on = selinux_quota_on,
5774 .syslog = selinux_syslog,
5775 .vm_enough_memory = selinux_vm_enough_memory,
5776
5777 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
David Howellsa6f76f22008-11-14 10:39:24 +11005779 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005780 .bprm_committing_creds = selinux_bprm_committing_creds,
5781 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 .bprm_secureexec = selinux_bprm_secureexec,
5783
5784 .sb_alloc_security = selinux_sb_alloc_security,
5785 .sb_free_security = selinux_sb_free_security,
5786 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005787 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005788 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005789 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 .sb_statfs = selinux_sb_statfs,
5791 .sb_mount = selinux_mount,
5792 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005793 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005794 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005795 .sb_parse_opts_str = selinux_parse_opts_str,
5796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
5798 .inode_alloc_security = selinux_inode_alloc_security,
5799 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005800 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 .inode_unlink = selinux_inode_unlink,
5804 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 .inode_rmdir = selinux_inode_rmdir,
5807 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 .inode_readlink = selinux_inode_readlink,
5810 .inode_follow_link = selinux_inode_follow_link,
5811 .inode_permission = selinux_inode_permission,
5812 .inode_setattr = selinux_inode_setattr,
5813 .inode_getattr = selinux_inode_getattr,
5814 .inode_setxattr = selinux_inode_setxattr,
5815 .inode_post_setxattr = selinux_inode_post_setxattr,
5816 .inode_getxattr = selinux_inode_getxattr,
5817 .inode_listxattr = selinux_inode_listxattr,
5818 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005819 .inode_getsecurity = selinux_inode_getsecurity,
5820 .inode_setsecurity = selinux_inode_setsecurity,
5821 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005822 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
5824 .file_permission = selinux_file_permission,
5825 .file_alloc_security = selinux_file_alloc_security,
5826 .file_free_security = selinux_file_free_security,
5827 .file_ioctl = selinux_file_ioctl,
5828 .file_mmap = selinux_file_mmap,
5829 .file_mprotect = selinux_file_mprotect,
5830 .file_lock = selinux_file_lock,
5831 .file_fcntl = selinux_file_fcntl,
5832 .file_set_fowner = selinux_file_set_fowner,
5833 .file_send_sigiotask = selinux_file_send_sigiotask,
5834 .file_receive = selinux_file_receive,
5835
Eric Paris828dfe12008-04-17 13:17:49 -04005836 .dentry_open = selinux_dentry_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005839 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005840 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005841 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005842 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005843 .kernel_act_as = selinux_kernel_act_as,
5844 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005845 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 .task_setpgid = selinux_task_setpgid,
5847 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005848 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005849 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005851 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005852 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 .task_setrlimit = selinux_task_setrlimit,
5854 .task_setscheduler = selinux_task_setscheduler,
5855 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005856 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 .task_kill = selinux_task_kill,
5858 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005859 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
5861 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005862 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
5864 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5865 .msg_msg_free_security = selinux_msg_msg_free_security,
5866
5867 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5868 .msg_queue_free_security = selinux_msg_queue_free_security,
5869 .msg_queue_associate = selinux_msg_queue_associate,
5870 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5871 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5872 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5873
5874 .shm_alloc_security = selinux_shm_alloc_security,
5875 .shm_free_security = selinux_shm_free_security,
5876 .shm_associate = selinux_shm_associate,
5877 .shm_shmctl = selinux_shm_shmctl,
5878 .shm_shmat = selinux_shm_shmat,
5879
Eric Paris828dfe12008-04-17 13:17:49 -04005880 .sem_alloc_security = selinux_sem_alloc_security,
5881 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 .sem_associate = selinux_sem_associate,
5883 .sem_semctl = selinux_sem_semctl,
5884 .sem_semop = selinux_sem_semop,
5885
Eric Paris828dfe12008-04-17 13:17:49 -04005886 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
Eric Paris828dfe12008-04-17 13:17:49 -04005888 .getprocattr = selinux_getprocattr,
5889 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005891 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005892 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005893 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005894 .inode_notifysecctx = selinux_inode_notifysecctx,
5895 .inode_setsecctx = selinux_inode_setsecctx,
5896 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005897
Eric Paris828dfe12008-04-17 13:17:49 -04005898 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 .unix_may_send = selinux_socket_unix_may_send,
5900
5901 .socket_create = selinux_socket_create,
5902 .socket_post_create = selinux_socket_post_create,
5903 .socket_bind = selinux_socket_bind,
5904 .socket_connect = selinux_socket_connect,
5905 .socket_listen = selinux_socket_listen,
5906 .socket_accept = selinux_socket_accept,
5907 .socket_sendmsg = selinux_socket_sendmsg,
5908 .socket_recvmsg = selinux_socket_recvmsg,
5909 .socket_getsockname = selinux_socket_getsockname,
5910 .socket_getpeername = selinux_socket_getpeername,
5911 .socket_getsockopt = selinux_socket_getsockopt,
5912 .socket_setsockopt = selinux_socket_setsockopt,
5913 .socket_shutdown = selinux_socket_shutdown,
5914 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005915 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5916 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 .sk_alloc_security = selinux_sk_alloc_security,
5918 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005919 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005920 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005921 .sock_graft = selinux_sock_graft,
5922 .inet_conn_request = selinux_inet_conn_request,
5923 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005924 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005925 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5926 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5927 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005928 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005929 .tun_dev_create = selinux_tun_dev_create,
5930 .tun_dev_post_create = selinux_tun_dev_post_create,
5931 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005932
5933#ifdef CONFIG_SECURITY_NETWORK_XFRM
5934 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5935 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5936 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005937 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005938 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5939 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005940 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005941 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005942 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005943 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005945
5946#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005947 .key_alloc = selinux_key_alloc,
5948 .key_free = selinux_key_free,
5949 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005950 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005951#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005952
5953#ifdef CONFIG_AUDIT
5954 .audit_rule_init = selinux_audit_rule_init,
5955 .audit_rule_known = selinux_audit_rule_known,
5956 .audit_rule_match = selinux_audit_rule_match,
5957 .audit_rule_free = selinux_audit_rule_free,
5958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959};
5960
5961static __init int selinux_init(void)
5962{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005963 if (!security_module_enable(&selinux_ops)) {
5964 selinux_enabled = 0;
5965 return 0;
5966 }
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 if (!selinux_enabled) {
5969 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5970 return 0;
5971 }
5972
5973 printk(KERN_INFO "SELinux: Initializing.\n");
5974
5975 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005976 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005978 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5979
James Morris7cae7e22006-03-22 00:09:22 -08005980 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5981 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005982 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 avc_init();
5984
Eric Paris828dfe12008-04-17 13:17:49 -04005985 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 panic("SELinux: Unable to register with kernel.\n");
5987
Eric Paris828dfe12008-04-17 13:17:49 -04005988 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005989 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005990 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005991 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 return 0;
5994}
5995
Al Viroe8c26252010-03-23 06:36:54 -04005996static void delayed_superblock_init(struct super_block *sb, void *unused)
5997{
5998 superblock_doinit(sb, NULL);
5999}
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001void selinux_complete_init(void)
6002{
Eric Parisfadcdb42007-02-22 18:11:31 -05006003 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
6005 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05006006 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006007 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008}
6009
6010/* SELinux requires early initialization in order to label
6011 all processes and objects when they are created. */
6012security_initcall(selinux_init);
6013
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006014#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Paul Mooreeffad8d2008-01-29 08:49:27 -05006016static struct nf_hook_ops selinux_ipv4_ops[] = {
6017 {
6018 .hook = selinux_ipv4_postroute,
6019 .owner = THIS_MODULE,
6020 .pf = PF_INET,
6021 .hooknum = NF_INET_POST_ROUTING,
6022 .priority = NF_IP_PRI_SELINUX_LAST,
6023 },
6024 {
6025 .hook = selinux_ipv4_forward,
6026 .owner = THIS_MODULE,
6027 .pf = PF_INET,
6028 .hooknum = NF_INET_FORWARD,
6029 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006030 },
6031 {
6032 .hook = selinux_ipv4_output,
6033 .owner = THIS_MODULE,
6034 .pf = PF_INET,
6035 .hooknum = NF_INET_LOCAL_OUT,
6036 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038};
6039
6040#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6041
Paul Mooreeffad8d2008-01-29 08:49:27 -05006042static struct nf_hook_ops selinux_ipv6_ops[] = {
6043 {
6044 .hook = selinux_ipv6_postroute,
6045 .owner = THIS_MODULE,
6046 .pf = PF_INET6,
6047 .hooknum = NF_INET_POST_ROUTING,
6048 .priority = NF_IP6_PRI_SELINUX_LAST,
6049 },
6050 {
6051 .hook = selinux_ipv6_forward,
6052 .owner = THIS_MODULE,
6053 .pf = PF_INET6,
6054 .hooknum = NF_INET_FORWARD,
6055 .priority = NF_IP6_PRI_SELINUX_FIRST,
6056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057};
6058
6059#endif /* IPV6 */
6060
6061static int __init selinux_nf_ip_init(void)
6062{
6063 int err = 0;
6064
6065 if (!selinux_enabled)
6066 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006067
6068 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6069
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006070 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6071 if (err)
6072 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
6074#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006075 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6076 if (err)
6077 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080out:
6081 return err;
6082}
6083
6084__initcall(selinux_nf_ip_init);
6085
6086#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6087static void selinux_nf_ip_exit(void)
6088{
Eric Parisfadcdb42007-02-22 18:11:31 -05006089 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006091 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006093 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094#endif /* IPV6 */
6095}
6096#endif
6097
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006098#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6101#define selinux_nf_ip_exit()
6102#endif
6103
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006104#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
6106#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006107static int selinux_disabled;
6108
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109int selinux_disable(void)
6110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 if (ss_initialized) {
6112 /* Not permitted after initial policy load. */
6113 return -EINVAL;
6114 }
6115
6116 if (selinux_disabled) {
6117 /* Only do this once. */
6118 return -EINVAL;
6119 }
6120
6121 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6122
6123 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006124 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006126 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Eric Parisaf8ff042009-09-20 21:23:01 -04006128 /* Try to destroy the avc node cache */
6129 avc_disable();
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 /* Unregister netfilter hooks. */
6132 selinux_nf_ip_exit();
6133
6134 /* Unregister selinuxfs. */
6135 exit_sel_fs();
6136
6137 return 0;
6138}
6139#endif