blob: 17fdc17650d3460f9f4a95f794264143425844d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
6 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040023 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050027#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070029#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/sched.h>
32#include <linux/security.h>
33#include <linux/xattr.h>
34#include <linux/capability.h>
35#include <linux/unistd.h>
36#include <linux/mm.h>
37#include <linux/mman.h>
38#include <linux/slab.h>
39#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050040#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050044#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040046#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
48#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/netfilter_ipv4.h>
50#include <linux/netfilter_ipv6.h>
51#include <linux/tty.h>
52#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070053#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore1c5d9d12013-12-04 16:10:45 -050055#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050056#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/ioctls.h>
Arun Sharma60063492011-07-26 16:09:06 -070060#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62#include <linux/interrupt.h>
63#include <linux/netdevice.h> /* for network interface checks */
64#include <linux/netlink.h>
65#include <linux/tcp.h>
66#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080067#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/quota.h>
69#include <linux/un.h> /* for Unix socket types */
70#include <net/af_unix.h> /* for Unix socket types */
71#include <linux/parser.h>
72#include <linux/nfs_mount.h>
73#include <net/ipv6.h>
74#include <linux/hugetlb.h>
75#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070077#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070078#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070079#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070080#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080081#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070082#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040083#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000084#include <linux/msg.h>
85#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include "avc.h"
88#include "objsec.h"
89#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050090#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040091#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080092#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050093#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020094#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100095#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
David P. Quigley11689d42009-01-16 09:22:03 -050097#define NUM_SEL_MNT_OPTS 5
Eric Parisc9180a52007-11-30 13:00:35 -050098
James Morris20510f22007-10-16 23:31:32 -070099extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Paul Moored621d352008-01-29 08:43:36 -0500101/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000102static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400105int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int __init enforcing_setup(char *str)
108{
Eric Parisf5269712008-05-14 11:27:45 -0400109 unsigned long enforcing;
110 if (!strict_strtoul(str, 0, &enforcing))
111 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 1;
113}
114__setup("enforcing=", enforcing_setup);
115#endif
116
117#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
118int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
119
120static int __init selinux_enabled_setup(char *str)
121{
Eric Parisf5269712008-05-14 11:27:45 -0400122 unsigned long enabled;
123 if (!strict_strtoul(str, 0, &enabled))
124 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 1;
126}
127__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400128#else
129int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
131
Christoph Lametere18b8902006-12-06 20:33:20 -0800132static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800133
Paul Moored621d352008-01-29 08:43:36 -0500134/**
135 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
136 *
137 * Description:
138 * This function checks the SECMARK reference counter to see if any SECMARK
139 * targets are currently configured, if the reference counter is greater than
140 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
141 * enabled, false (0) if SECMARK is disabled.
142 *
143 */
144static int selinux_secmark_enabled(void)
145{
146 return (atomic_read(&selinux_secmark_refcount) > 0);
147}
148
David Howellsd84f4f92008-11-14 10:39:23 +1100149/*
150 * initialise the security for the init task
151 */
152static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Howells3b11a1d2008-11-14 10:39:26 +1100154 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct task_security_struct *tsec;
156
James Morris89d155e2005-10-30 14:59:21 -0800157 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100159 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsd84f4f92008-11-14 10:39:23 +1100161 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100162 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
David Howells275bb412008-11-14 10:39:19 +1100165/*
David Howells88e67f32008-11-14 10:39:21 +1100166 * get the security ID of a set of credentials
167 */
168static inline u32 cred_sid(const struct cred *cred)
169{
170 const struct task_security_struct *tsec;
171
172 tsec = cred->security;
173 return tsec->sid;
174}
175
176/*
David Howells3b11a1d2008-11-14 10:39:26 +1100177 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100178 */
179static inline u32 task_sid(const struct task_struct *task)
180{
David Howells275bb412008-11-14 10:39:19 +1100181 u32 sid;
182
183 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100184 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100185 rcu_read_unlock();
186 return sid;
187}
188
189/*
David Howells3b11a1d2008-11-14 10:39:26 +1100190 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100191 */
192static inline u32 current_sid(void)
193{
Paul Moore5fb49872010-04-22 14:46:19 -0400194 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100195
196 return tsec->sid;
197}
198
David Howells88e67f32008-11-14 10:39:21 +1100199/* Allocate and free functions for each kind of security blob. */
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int inode_alloc_security(struct inode *inode)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100204 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Josef Bacika02fe132008-04-04 09:35:05 +1100206 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!isec)
208 return -ENOMEM;
209
Eric Paris23970742006-09-25 23:32:01 -0700210 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 isec->inode = inode;
213 isec->sid = SECINITSID_UNLABELED;
214 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100215 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 inode->i_security = isec;
217
218 return 0;
219}
220
Steven Rostedt9e74d932014-01-09 21:46:34 -0500221static void inode_free_rcu(struct rcu_head *head)
222{
223 struct inode_security_struct *isec;
224
225 isec = container_of(head, struct inode_security_struct, rcu);
226 kmem_cache_free(sel_inode_cache, isec);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void inode_free_security(struct inode *inode)
230{
231 struct inode_security_struct *isec = inode->i_security;
232 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_lock(&sbsec->isec_lock);
235 if (!list_empty(&isec->list))
236 list_del_init(&isec->list);
237 spin_unlock(&sbsec->isec_lock);
238
Steven Rostedt9e74d932014-01-09 21:46:34 -0500239 /*
240 * The inode may still be referenced in a path walk and
241 * a call to selinux_inode_permission() can be made
242 * after inode_free_security() is called. Ideally, the VFS
243 * wouldn't do this, but fixing that is a much harder
244 * job. For now, simply free the i_security via RCU, and
245 * leave the current inode->i_security pointer intact.
246 * The inode will be freed after the RCU grace period too.
247 */
248 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static int file_alloc_security(struct file *file)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800256 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!fsec)
258 return -ENOMEM;
259
David Howells275bb412008-11-14 10:39:19 +1100260 fsec->sid = sid;
261 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 file->f_security = fsec;
263
264 return 0;
265}
266
267static void file_free_security(struct file *file)
268{
269 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 file->f_security = NULL;
271 kfree(fsec);
272}
273
274static int superblock_alloc_security(struct super_block *sb)
275{
276 struct superblock_security_struct *sbsec;
277
James Morris89d155e2005-10-30 14:59:21 -0800278 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!sbsec)
280 return -ENOMEM;
281
Eric Parisbc7e9822006-09-25 23:32:02 -0700282 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 INIT_LIST_HEAD(&sbsec->isec_head);
284 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sbsec->sb = sb;
286 sbsec->sid = SECINITSID_UNLABELED;
287 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700288 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 sb->s_security = sbsec;
290
291 return 0;
292}
293
294static void superblock_free_security(struct super_block *sb)
295{
296 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 sb->s_security = NULL;
298 kfree(sbsec);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* The file system's label must be initialized prior to use. */
302
Stephen Hemminger634a5392010-03-04 21:59:03 -0800303static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "uses xattr",
305 "uses transition SIDs",
306 "uses task SIDs",
307 "uses genfs_contexts",
308 "not configured for labeling",
309 "uses mountpoint labeling",
310};
311
312static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
313
314static inline int inode_doinit(struct inode *inode)
315{
316 return inode_doinit_with_dentry(inode, NULL);
317}
318
319enum {
Eric Paris31e87932007-09-19 17:19:12 -0400320 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 Opt_context = 1,
322 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500323 Opt_defcontext = 3,
324 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500325 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
Steven Whitehousea447c092008-10-13 10:46:57 +0100328static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400329 {Opt_context, CONTEXT_STR "%s"},
330 {Opt_fscontext, FSCONTEXT_STR "%s"},
331 {Opt_defcontext, DEFCONTEXT_STR "%s"},
332 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500333 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400334 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
338
Eric Parisc312feb2006-07-10 04:43:53 -0700339static int may_context_mount_sb_relabel(u32 sid,
340 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100341 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700342{
David Howells275bb412008-11-14 10:39:19 +1100343 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700344 int rc;
345
346 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
347 FILESYSTEM__RELABELFROM, NULL);
348 if (rc)
349 return rc;
350
351 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
352 FILESYSTEM__RELABELTO, NULL);
353 return rc;
354}
355
Eric Paris08089252006-07-10 04:43:55 -0700356static int may_context_mount_inode_relabel(u32 sid,
357 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100358 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700359{
David Howells275bb412008-11-14 10:39:19 +1100360 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700361 int rc;
362 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
363 FILESYSTEM__RELABELFROM, NULL);
364 if (rc)
365 return rc;
366
367 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
368 FILESYSTEM__ASSOCIATE, NULL);
369 return rc;
370}
371
Eric Parisc9180a52007-11-30 13:00:35 -0500372static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct superblock_security_struct *sbsec = sb->s_security;
375 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500376 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int rc = 0;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
380 /* Make sure that the xattr handler exists and that no
381 error other than -ENODATA is returned by getxattr on
382 the root directory. -ENODATA is ok, as this may be
383 the first boot of the SELinux kernel before we have
384 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500385 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
387 "xattr support\n", sb->s_id, sb->s_type->name);
388 rc = -EOPNOTSUPP;
389 goto out;
390 }
Eric Parisc9180a52007-11-30 13:00:35 -0500391 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (rc < 0 && rc != -ENODATA) {
393 if (rc == -EOPNOTSUPP)
394 printk(KERN_WARNING "SELinux: (dev %s, type "
395 "%s) has no security xattr handler\n",
396 sb->s_id, sb->s_type->name);
397 else
398 printk(KERN_WARNING "SELinux: (dev %s, type "
399 "%s) getxattr errno %d\n", sb->s_id,
400 sb->s_type->name, -rc);
401 goto out;
402 }
403 }
404
David P. Quigley11689d42009-01-16 09:22:03 -0500405 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Eric Parisc9180a52007-11-30 13:00:35 -0500407 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500408 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500410 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500411 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sb->s_id, sb->s_type->name,
413 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David P. Quigley11689d42009-01-16 09:22:03 -0500415 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
416 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
417 sbsec->behavior == SECURITY_FS_USE_NONE ||
418 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
419 sbsec->flags &= ~SE_SBLABELSUPP;
420
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400421 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
422 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
423 sbsec->flags |= SE_SBLABELSUPP;
424
Stephen Smalley90078112013-05-10 10:16:19 -0400425 /*
426 * Special handling for rootfs. Is genfs but supports
427 * setting SELinux context on in-core inodes.
428 */
429 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
430 sbsec->flags |= SE_SBLABELSUPP;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500433 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Initialize any other inodes associated with the superblock, e.g.
436 inodes created prior to initial policy load or inodes created
437 during get_sb by a pseudo filesystem that directly
438 populates itself. */
439 spin_lock(&sbsec->isec_lock);
440next_inode:
441 if (!list_empty(&sbsec->isec_head)) {
442 struct inode_security_struct *isec =
443 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500444 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct inode *inode = isec->inode;
Stephen Smalley61dedfa2014-10-06 16:32:52 -0400446 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_unlock(&sbsec->isec_lock);
448 inode = igrab(inode);
449 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500450 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 inode_doinit(inode);
452 iput(inode);
453 }
454 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto next_inode;
456 }
457 spin_unlock(&sbsec->isec_lock);
458out:
Eric Parisc9180a52007-11-30 13:00:35 -0500459 return rc;
460}
461
462/*
463 * This function should allow an FS to ask what it's mount security
464 * options were so it can use those later for submounts, displaying
465 * mount options, or whatever.
466 */
467static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500468 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500469{
470 int rc = 0, i;
471 struct superblock_security_struct *sbsec = sb->s_security;
472 char *context = NULL;
473 u32 len;
474 char tmp;
475
Eric Parise0007522008-03-05 10:31:54 -0500476 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500477
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500478 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500479 return -EINVAL;
480
481 if (!ss_initialized)
482 return -EINVAL;
483
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500484 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500485 /* count the number of mount options for this sb */
486 for (i = 0; i < 8; i++) {
487 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500488 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500489 tmp >>= 1;
490 }
David P. Quigley11689d42009-01-16 09:22:03 -0500491 /* Check if the Label support flag is set */
492 if (sbsec->flags & SE_SBLABELSUPP)
493 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500494
Eric Parise0007522008-03-05 10:31:54 -0500495 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
496 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500497 rc = -ENOMEM;
498 goto out_free;
499 }
500
Eric Parise0007522008-03-05 10:31:54 -0500501 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
502 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500503 rc = -ENOMEM;
504 goto out_free;
505 }
506
507 i = 0;
508 if (sbsec->flags & FSCONTEXT_MNT) {
509 rc = security_sid_to_context(sbsec->sid, &context, &len);
510 if (rc)
511 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500512 opts->mnt_opts[i] = context;
513 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500514 }
515 if (sbsec->flags & CONTEXT_MNT) {
516 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
517 if (rc)
518 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500519 opts->mnt_opts[i] = context;
520 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500521 }
522 if (sbsec->flags & DEFCONTEXT_MNT) {
523 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
524 if (rc)
525 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500526 opts->mnt_opts[i] = context;
527 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500528 }
529 if (sbsec->flags & ROOTCONTEXT_MNT) {
530 struct inode *root = sbsec->sb->s_root->d_inode;
531 struct inode_security_struct *isec = root->i_security;
532
533 rc = security_sid_to_context(isec->sid, &context, &len);
534 if (rc)
535 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500536 opts->mnt_opts[i] = context;
537 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500538 }
David P. Quigley11689d42009-01-16 09:22:03 -0500539 if (sbsec->flags & SE_SBLABELSUPP) {
540 opts->mnt_opts[i] = NULL;
541 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
542 }
Eric Parisc9180a52007-11-30 13:00:35 -0500543
Eric Parise0007522008-03-05 10:31:54 -0500544 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500545
546 return 0;
547
548out_free:
Eric Parise0007522008-03-05 10:31:54 -0500549 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500550 return rc;
551}
552
553static int bad_option(struct superblock_security_struct *sbsec, char flag,
554 u32 old_sid, u32 new_sid)
555{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500556 char mnt_flags = sbsec->flags & SE_MNTMASK;
557
Eric Parisc9180a52007-11-30 13:00:35 -0500558 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500559 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500560 if (!(sbsec->flags & flag) ||
561 (old_sid != new_sid))
562 return 1;
563
564 /* check if we were passed the same options twice,
565 * aka someone passed context=a,context=b
566 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500567 if (!(sbsec->flags & SE_SBINITIALIZED))
568 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500569 return 1;
570 return 0;
571}
Eric Parise0007522008-03-05 10:31:54 -0500572
Eric Parisc9180a52007-11-30 13:00:35 -0500573/*
574 * Allow filesystems with binary mount data to explicitly set mount point
575 * labeling information.
576 */
Eric Parise0007522008-03-05 10:31:54 -0500577static int selinux_set_mnt_opts(struct super_block *sb,
578 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500579{
David Howells275bb412008-11-14 10:39:19 +1100580 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500581 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500582 struct superblock_security_struct *sbsec = sb->s_security;
583 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000584 struct inode *inode = sbsec->sb->s_root->d_inode;
585 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500586 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
587 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500588 char **mount_options = opts->mnt_opts;
589 int *flags = opts->mnt_opts_flags;
590 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500591
592 mutex_lock(&sbsec->lock);
593
594 if (!ss_initialized) {
595 if (!num_opts) {
596 /* Defer initialization until selinux_complete_init,
597 after the initial policy is loaded and the security
598 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500599 goto out;
600 }
601 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400602 printk(KERN_WARNING "SELinux: Unable to set superblock options "
603 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500604 goto out;
605 }
606
607 /*
Eric Parise0007522008-03-05 10:31:54 -0500608 * Binary mount data FS will come through this function twice. Once
609 * from an explicit call and once from the generic calls from the vfs.
610 * Since the generic VFS calls will not contain any security mount data
611 * we need to skip the double mount verification.
612 *
613 * This does open a hole in which we will not notice if the first
614 * mount using this sb set explict options and a second mount using
615 * this sb does not set any security options. (The first options
616 * will be used for both mounts)
617 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500618 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500619 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400620 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500621
622 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500623 * parse the mount options, check if they are valid sids.
624 * also check if someone is trying to mount the same sb more
625 * than once with different security options.
626 */
627 for (i = 0; i < num_opts; i++) {
628 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500629
630 if (flags[i] == SE_SBLABELSUPP)
631 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500632 rc = security_context_to_sid(mount_options[i],
633 strlen(mount_options[i]), &sid);
634 if (rc) {
635 printk(KERN_WARNING "SELinux: security_context_to_sid"
636 "(%s) failed for (dev %s, type %s) errno=%d\n",
637 mount_options[i], sb->s_id, name, rc);
638 goto out;
639 }
640 switch (flags[i]) {
641 case FSCONTEXT_MNT:
642 fscontext_sid = sid;
643
644 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
645 fscontext_sid))
646 goto out_double_mount;
647
648 sbsec->flags |= FSCONTEXT_MNT;
649 break;
650 case CONTEXT_MNT:
651 context_sid = sid;
652
653 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
654 context_sid))
655 goto out_double_mount;
656
657 sbsec->flags |= CONTEXT_MNT;
658 break;
659 case ROOTCONTEXT_MNT:
660 rootcontext_sid = sid;
661
662 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
663 rootcontext_sid))
664 goto out_double_mount;
665
666 sbsec->flags |= ROOTCONTEXT_MNT;
667
668 break;
669 case DEFCONTEXT_MNT:
670 defcontext_sid = sid;
671
672 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
673 defcontext_sid))
674 goto out_double_mount;
675
676 sbsec->flags |= DEFCONTEXT_MNT;
677
678 break;
679 default:
680 rc = -EINVAL;
681 goto out;
682 }
683 }
684
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500685 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500686 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500687 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500688 goto out_double_mount;
689 rc = 0;
690 goto out;
691 }
692
James Morris089be432008-07-15 18:32:49 +1000693 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500694 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500695
696 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500697 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500698 if (rc) {
699 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000700 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500701 goto out;
702 }
703
704 /* sets the context of the superblock for the fs being mounted. */
705 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100706 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500707 if (rc)
708 goto out;
709
710 sbsec->sid = fscontext_sid;
711 }
712
713 /*
714 * Switch to using mount point labeling behavior.
715 * sets the label used on all file below the mountpoint, and will set
716 * the superblock context if not already set.
717 */
718 if (context_sid) {
719 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100720 rc = may_context_mount_sb_relabel(context_sid, sbsec,
721 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500722 if (rc)
723 goto out;
724 sbsec->sid = context_sid;
725 } else {
David Howells275bb412008-11-14 10:39:19 +1100726 rc = may_context_mount_inode_relabel(context_sid, sbsec,
727 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500728 if (rc)
729 goto out;
730 }
731 if (!rootcontext_sid)
732 rootcontext_sid = context_sid;
733
734 sbsec->mntpoint_sid = context_sid;
735 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
736 }
737
738 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100739 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
740 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500741 if (rc)
742 goto out;
743
744 root_isec->sid = rootcontext_sid;
745 root_isec->initialized = 1;
746 }
747
748 if (defcontext_sid) {
749 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
750 rc = -EINVAL;
751 printk(KERN_WARNING "SELinux: defcontext option is "
752 "invalid for this filesystem type\n");
753 goto out;
754 }
755
756 if (defcontext_sid != sbsec->def_sid) {
757 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100758 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500759 if (rc)
760 goto out;
761 }
762
763 sbsec->def_sid = defcontext_sid;
764 }
765
766 rc = sb_finish_set_opts(sb);
767out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700768 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500770out_double_mount:
771 rc = -EINVAL;
772 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
773 "security settings for (dev %s, type %s)\n", sb->s_id, name);
774 goto out;
775}
776
777static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
778 struct super_block *newsb)
779{
780 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
781 struct superblock_security_struct *newsbsec = newsb->s_security;
782
783 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
784 int set_context = (oldsbsec->flags & CONTEXT_MNT);
785 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
786
Eric Paris0f5e6422008-04-21 16:24:11 -0400787 /*
788 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400789 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400790 */
Al Viroe8c26252010-03-23 06:36:54 -0400791 if (!ss_initialized)
Eric Paris0f5e6422008-04-21 16:24:11 -0400792 return;
Eric Parisc9180a52007-11-30 13:00:35 -0500793
Eric Parisc9180a52007-11-30 13:00:35 -0500794 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500795 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500796
Eric Paris5a552612008-04-09 14:08:35 -0400797 /* if fs is reusing a sb, just let its options stand... */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500798 if (newsbsec->flags & SE_SBINITIALIZED)
Eric Paris5a552612008-04-09 14:08:35 -0400799 return;
800
Eric Parisc9180a52007-11-30 13:00:35 -0500801 mutex_lock(&newsbsec->lock);
802
803 newsbsec->flags = oldsbsec->flags;
804
805 newsbsec->sid = oldsbsec->sid;
806 newsbsec->def_sid = oldsbsec->def_sid;
807 newsbsec->behavior = oldsbsec->behavior;
808
809 if (set_context) {
810 u32 sid = oldsbsec->mntpoint_sid;
811
812 if (!set_fscontext)
813 newsbsec->sid = sid;
814 if (!set_rootcontext) {
815 struct inode *newinode = newsb->s_root->d_inode;
816 struct inode_security_struct *newisec = newinode->i_security;
817 newisec->sid = sid;
818 }
819 newsbsec->mntpoint_sid = sid;
820 }
821 if (set_rootcontext) {
822 const struct inode *oldinode = oldsb->s_root->d_inode;
823 const struct inode_security_struct *oldisec = oldinode->i_security;
824 struct inode *newinode = newsb->s_root->d_inode;
825 struct inode_security_struct *newisec = newinode->i_security;
826
827 newisec->sid = oldisec->sid;
828 }
829
830 sb_finish_set_opts(newsb);
831 mutex_unlock(&newsbsec->lock);
832}
833
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200834static int selinux_parse_opts_str(char *options,
835 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500836{
Eric Parise0007522008-03-05 10:31:54 -0500837 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500838 char *context = NULL, *defcontext = NULL;
839 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500840 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500841
Eric Parise0007522008-03-05 10:31:54 -0500842 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500843
844 /* Standard string-based options. */
845 while ((p = strsep(&options, "|")) != NULL) {
846 int token;
847 substring_t args[MAX_OPT_ARGS];
848
849 if (!*p)
850 continue;
851
852 token = match_token(p, tokens, args);
853
854 switch (token) {
855 case Opt_context:
856 if (context || defcontext) {
857 rc = -EINVAL;
858 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
859 goto out_err;
860 }
861 context = match_strdup(&args[0]);
862 if (!context) {
863 rc = -ENOMEM;
864 goto out_err;
865 }
866 break;
867
868 case Opt_fscontext:
869 if (fscontext) {
870 rc = -EINVAL;
871 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
872 goto out_err;
873 }
874 fscontext = match_strdup(&args[0]);
875 if (!fscontext) {
876 rc = -ENOMEM;
877 goto out_err;
878 }
879 break;
880
881 case Opt_rootcontext:
882 if (rootcontext) {
883 rc = -EINVAL;
884 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
885 goto out_err;
886 }
887 rootcontext = match_strdup(&args[0]);
888 if (!rootcontext) {
889 rc = -ENOMEM;
890 goto out_err;
891 }
892 break;
893
894 case Opt_defcontext:
895 if (context || defcontext) {
896 rc = -EINVAL;
897 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
898 goto out_err;
899 }
900 defcontext = match_strdup(&args[0]);
901 if (!defcontext) {
902 rc = -ENOMEM;
903 goto out_err;
904 }
905 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500906 case Opt_labelsupport:
907 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500908 default:
909 rc = -EINVAL;
910 printk(KERN_WARNING "SELinux: unknown mount option\n");
911 goto out_err;
912
913 }
914 }
915
Eric Parise0007522008-03-05 10:31:54 -0500916 rc = -ENOMEM;
917 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
918 if (!opts->mnt_opts)
919 goto out_err;
920
921 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
922 if (!opts->mnt_opts_flags) {
923 kfree(opts->mnt_opts);
924 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500925 }
926
Eric Parise0007522008-03-05 10:31:54 -0500927 if (fscontext) {
928 opts->mnt_opts[num_mnt_opts] = fscontext;
929 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
930 }
931 if (context) {
932 opts->mnt_opts[num_mnt_opts] = context;
933 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
934 }
935 if (rootcontext) {
936 opts->mnt_opts[num_mnt_opts] = rootcontext;
937 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
938 }
939 if (defcontext) {
940 opts->mnt_opts[num_mnt_opts] = defcontext;
941 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
942 }
943
944 opts->num_mnt_opts = num_mnt_opts;
945 return 0;
946
Eric Parisc9180a52007-11-30 13:00:35 -0500947out_err:
948 kfree(context);
949 kfree(defcontext);
950 kfree(fscontext);
951 kfree(rootcontext);
952 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
Eric Parise0007522008-03-05 10:31:54 -0500954/*
955 * string mount options parsing and call set the sbsec
956 */
957static int superblock_doinit(struct super_block *sb, void *data)
958{
959 int rc = 0;
960 char *options = data;
961 struct security_mnt_opts opts;
962
963 security_init_mnt_opts(&opts);
964
965 if (!data)
966 goto out;
967
968 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
969
970 rc = selinux_parse_opts_str(options, &opts);
971 if (rc)
972 goto out_err;
973
974out:
975 rc = selinux_set_mnt_opts(sb, &opts);
976
977out_err:
978 security_free_mnt_opts(&opts);
979 return rc;
980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Adrian Bunk3583a712008-07-22 20:21:23 +0300982static void selinux_write_opts(struct seq_file *m,
983 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000984{
985 int i;
986 char *prefix;
987
988 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500989 char *has_comma;
990
991 if (opts->mnt_opts[i])
992 has_comma = strchr(opts->mnt_opts[i], ',');
993 else
994 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +1000995
996 switch (opts->mnt_opts_flags[i]) {
997 case CONTEXT_MNT:
998 prefix = CONTEXT_STR;
999 break;
1000 case FSCONTEXT_MNT:
1001 prefix = FSCONTEXT_STR;
1002 break;
1003 case ROOTCONTEXT_MNT:
1004 prefix = ROOTCONTEXT_STR;
1005 break;
1006 case DEFCONTEXT_MNT:
1007 prefix = DEFCONTEXT_STR;
1008 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001009 case SE_SBLABELSUPP:
1010 seq_putc(m, ',');
1011 seq_puts(m, LABELSUPP_STR);
1012 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001013 default:
1014 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001015 return;
Eric Paris2069f452008-07-04 09:47:13 +10001016 };
1017 /* we need a comma before each option */
1018 seq_putc(m, ',');
1019 seq_puts(m, prefix);
1020 if (has_comma)
1021 seq_putc(m, '\"');
1022 seq_puts(m, opts->mnt_opts[i]);
1023 if (has_comma)
1024 seq_putc(m, '\"');
1025 }
1026}
1027
1028static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1029{
1030 struct security_mnt_opts opts;
1031 int rc;
1032
1033 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001034 if (rc) {
1035 /* before policy load we may get EINVAL, don't show anything */
1036 if (rc == -EINVAL)
1037 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001038 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001039 }
Eric Paris2069f452008-07-04 09:47:13 +10001040
1041 selinux_write_opts(m, &opts);
1042
1043 security_free_mnt_opts(&opts);
1044
1045 return rc;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048static inline u16 inode_mode_to_security_class(umode_t mode)
1049{
1050 switch (mode & S_IFMT) {
1051 case S_IFSOCK:
1052 return SECCLASS_SOCK_FILE;
1053 case S_IFLNK:
1054 return SECCLASS_LNK_FILE;
1055 case S_IFREG:
1056 return SECCLASS_FILE;
1057 case S_IFBLK:
1058 return SECCLASS_BLK_FILE;
1059 case S_IFDIR:
1060 return SECCLASS_DIR;
1061 case S_IFCHR:
1062 return SECCLASS_CHR_FILE;
1063 case S_IFIFO:
1064 return SECCLASS_FIFO_FILE;
1065
1066 }
1067
1068 return SECCLASS_FILE;
1069}
1070
James Morris13402582005-09-30 14:24:34 -04001071static inline int default_protocol_stream(int protocol)
1072{
1073 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1074}
1075
1076static inline int default_protocol_dgram(int protocol)
1077{
1078 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1082{
1083 switch (family) {
1084 case PF_UNIX:
1085 switch (type) {
1086 case SOCK_STREAM:
1087 case SOCK_SEQPACKET:
1088 return SECCLASS_UNIX_STREAM_SOCKET;
1089 case SOCK_DGRAM:
1090 return SECCLASS_UNIX_DGRAM_SOCKET;
1091 }
1092 break;
1093 case PF_INET:
1094 case PF_INET6:
1095 switch (type) {
1096 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001097 if (default_protocol_stream(protocol))
1098 return SECCLASS_TCP_SOCKET;
1099 else
1100 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001102 if (default_protocol_dgram(protocol))
1103 return SECCLASS_UDP_SOCKET;
1104 else
1105 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001106 case SOCK_DCCP:
1107 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return SECCLASS_RAWIP_SOCKET;
1110 }
1111 break;
1112 case PF_NETLINK:
1113 switch (protocol) {
1114 case NETLINK_ROUTE:
1115 return SECCLASS_NETLINK_ROUTE_SOCKET;
1116 case NETLINK_FIREWALL:
1117 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001118 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1120 case NETLINK_NFLOG:
1121 return SECCLASS_NETLINK_NFLOG_SOCKET;
1122 case NETLINK_XFRM:
1123 return SECCLASS_NETLINK_XFRM_SOCKET;
1124 case NETLINK_SELINUX:
1125 return SECCLASS_NETLINK_SELINUX_SOCKET;
1126 case NETLINK_AUDIT:
1127 return SECCLASS_NETLINK_AUDIT_SOCKET;
1128 case NETLINK_IP6_FW:
1129 return SECCLASS_NETLINK_IP6FW_SOCKET;
1130 case NETLINK_DNRTMSG:
1131 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001132 case NETLINK_KOBJECT_UEVENT:
1133 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 default:
1135 return SECCLASS_NETLINK_SOCKET;
1136 }
1137 case PF_PACKET:
1138 return SECCLASS_PACKET_SOCKET;
1139 case PF_KEY:
1140 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001141 case PF_APPLETALK:
1142 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
1145 return SECCLASS_SOCKET;
1146}
1147
1148#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001149static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 u16 tclass,
1151 u32 *sid)
1152{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001153 int rc;
1154 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Paris828dfe12008-04-17 13:17:49 -04001156 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!buffer)
1158 return -ENOMEM;
1159
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001160 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1161 if (IS_ERR(path))
1162 rc = PTR_ERR(path);
1163 else {
1164 /* each process gets a /proc/PID/ entry. Strip off the
1165 * PID part to get a valid selinux labeling.
1166 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1167 while (path[1] >= '0' && path[1] <= '9') {
1168 path[1] = '/';
1169 path++;
1170 }
1171 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 free_page((unsigned long)buffer);
1174 return rc;
1175}
1176#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001177static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 u16 tclass,
1179 u32 *sid)
1180{
1181 return -EINVAL;
1182}
1183#endif
1184
1185/* The inode's security attributes must be initialized before first use. */
1186static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1187{
1188 struct superblock_security_struct *sbsec = NULL;
1189 struct inode_security_struct *isec = inode->i_security;
1190 u32 sid;
1191 struct dentry *dentry;
1192#define INITCONTEXTLEN 255
1193 char *context = NULL;
1194 unsigned len = 0;
1195 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (isec->initialized)
1198 goto out;
1199
Eric Paris23970742006-09-25 23:32:01 -07001200 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001202 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001205 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Defer initialization until selinux_complete_init,
1207 after the initial policy is loaded and the security
1208 server is ready to handle calls. */
1209 spin_lock(&sbsec->isec_lock);
1210 if (list_empty(&isec->list))
1211 list_add(&isec->list, &sbsec->isec_head);
1212 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001213 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 switch (sbsec->behavior) {
1217 case SECURITY_FS_USE_XATTR:
1218 if (!inode->i_op->getxattr) {
1219 isec->sid = sbsec->def_sid;
1220 break;
1221 }
1222
1223 /* Need a dentry, since the xattr API requires one.
1224 Life would be simpler if we could just pass the inode. */
1225 if (opt_dentry) {
1226 /* Called from d_instantiate or d_splice_alias. */
1227 dentry = dget(opt_dentry);
1228 } else {
1229 /* Called from selinux_complete_init, try to find a dentry. */
1230 dentry = d_find_alias(inode);
1231 }
1232 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001233 /*
1234 * this is can be hit on boot when a file is accessed
1235 * before the policy is loaded. When we load policy we
1236 * may find inodes that have no dentry on the
1237 * sbsec->isec_head list. No reason to complain as these
1238 * will get fixed up the next time we go through
1239 * inode_doinit with a dentry, before these inodes could
1240 * be used again by userspace.
1241 */
Eric Paris23970742006-09-25 23:32:01 -07001242 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001246 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!context) {
1248 rc = -ENOMEM;
1249 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001250 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001252 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1254 context, len);
1255 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001256 kfree(context);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Need a larger buffer. Query for the right size. */
1259 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1260 NULL, 0);
1261 if (rc < 0) {
1262 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001263 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001266 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!context) {
1268 rc = -ENOMEM;
1269 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001270 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001272 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rc = inode->i_op->getxattr(dentry,
1274 XATTR_NAME_SELINUX,
1275 context, len);
1276 }
1277 dput(dentry);
1278 if (rc < 0) {
1279 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001280 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001281 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 -rc, inode->i_sb->s_id, inode->i_ino);
1283 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001284 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 /* Map ENODATA to the default file SID */
1287 sid = sbsec->def_sid;
1288 rc = 0;
1289 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001290 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001291 sbsec->def_sid,
1292 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001294 char *dev = inode->i_sb->s_id;
1295 unsigned long ino = inode->i_ino;
1296
1297 if (rc == -EINVAL) {
1298 if (printk_ratelimit())
1299 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1300 "context=%s. This indicates you may need to relabel the inode or the "
1301 "filesystem in question.\n", ino, dev, context);
1302 } else {
1303 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1304 "returned %d for dev=%s ino=%ld\n",
1305 __func__, context, -rc, dev, ino);
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 kfree(context);
1308 /* Leave with the unlabeled SID */
1309 rc = 0;
1310 break;
1311 }
1312 }
1313 kfree(context);
1314 isec->sid = sid;
1315 break;
1316 case SECURITY_FS_USE_TASK:
1317 isec->sid = isec->task_sid;
1318 break;
1319 case SECURITY_FS_USE_TRANS:
1320 /* Default to the fs SID. */
1321 isec->sid = sbsec->sid;
1322
1323 /* Try to obtain a transition SID. */
1324 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001325 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1326 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001328 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 isec->sid = sid;
1330 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001331 case SECURITY_FS_USE_MNTPOINT:
1332 isec->sid = sbsec->mntpoint_sid;
1333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001335 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 isec->sid = sbsec->sid;
1337
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001338 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Paul Moore23a5a7a2014-03-19 16:46:18 -04001339 /* We must have a dentry to determine the label on
1340 * procfs inodes */
1341 if (opt_dentry)
1342 /* Called from d_instantiate or
1343 * d_splice_alias. */
1344 dentry = dget(opt_dentry);
1345 else
1346 /* Called from selinux_complete_init, try to
1347 * find a dentry. */
1348 dentry = d_find_alias(inode);
1349 /*
1350 * This can be hit on boot when a file is accessed
1351 * before the policy is loaded. When we load policy we
1352 * may find inodes that have no dentry on the
1353 * sbsec->isec_head list. No reason to complain as
1354 * these will get fixed up the next time we go through
1355 * inode_doinit() with a dentry, before these inodes
1356 * could be used again by userspace.
1357 */
1358 if (!dentry)
1359 goto out_unlock;
1360 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1361 rc = selinux_proc_get_sid(dentry, isec->sclass, &sid);
1362 dput(dentry);
1363 if (rc)
1364 goto out_unlock;
1365 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 break;
1368 }
1369
1370 isec->initialized = 1;
1371
Eric Paris23970742006-09-25 23:32:01 -07001372out_unlock:
1373 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374out:
1375 if (isec->sclass == SECCLASS_FILE)
1376 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return rc;
1378}
1379
1380/* Convert a Linux signal to an access vector. */
1381static inline u32 signal_to_av(int sig)
1382{
1383 u32 perm = 0;
1384
1385 switch (sig) {
1386 case SIGCHLD:
1387 /* Commonly granted from child to parent. */
1388 perm = PROCESS__SIGCHLD;
1389 break;
1390 case SIGKILL:
1391 /* Cannot be caught or ignored */
1392 perm = PROCESS__SIGKILL;
1393 break;
1394 case SIGSTOP:
1395 /* Cannot be caught or ignored */
1396 perm = PROCESS__SIGSTOP;
1397 break;
1398 default:
1399 /* All other signals. */
1400 perm = PROCESS__SIGNAL;
1401 break;
1402 }
1403
1404 return perm;
1405}
1406
David Howells275bb412008-11-14 10:39:19 +11001407/*
David Howellsd84f4f92008-11-14 10:39:23 +11001408 * Check permission between a pair of credentials
1409 * fork check, ptrace check, etc.
1410 */
1411static int cred_has_perm(const struct cred *actor,
1412 const struct cred *target,
1413 u32 perms)
1414{
1415 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1416
1417 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1418}
1419
1420/*
David Howells88e67f32008-11-14 10:39:21 +11001421 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001422 * fork check, ptrace check, etc.
1423 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001424 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001425 */
1426static int task_has_perm(const struct task_struct *tsk1,
1427 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 u32 perms)
1429{
David Howells275bb412008-11-14 10:39:19 +11001430 const struct task_security_struct *__tsec1, *__tsec2;
1431 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
David Howells275bb412008-11-14 10:39:19 +11001433 rcu_read_lock();
1434 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1435 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1436 rcu_read_unlock();
1437 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
David Howells3b11a1d2008-11-14 10:39:26 +11001440/*
1441 * Check permission between current and another task, e.g. signal checks,
1442 * fork check, ptrace check, etc.
1443 * current is the actor and tsk2 is the target
1444 * - this uses current's subjective creds
1445 */
1446static int current_has_perm(const struct task_struct *tsk,
1447 u32 perms)
1448{
1449 u32 sid, tsid;
1450
1451 sid = current_sid();
1452 tsid = task_sid(tsk);
1453 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1454}
1455
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001456#if CAP_LAST_CAP > 63
1457#error Fix SELinux to handle capabilities > 63.
1458#endif
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001461static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001462 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Thomas Liu2bf49692009-07-14 12:14:09 -04001464 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001465 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001466 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001467 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001468 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001469 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001470 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Thomas Liu2bf49692009-07-14 12:14:09 -04001472 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001473 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001474 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 ad.u.cap = cap;
1476
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001477 switch (CAP_TO_INDEX(cap)) {
1478 case 0:
1479 sclass = SECCLASS_CAPABILITY;
1480 break;
1481 case 1:
1482 sclass = SECCLASS_CAPABILITY2;
1483 break;
1484 default:
1485 printk(KERN_ERR
1486 "SELinux: out of range capability %d\n", cap);
1487 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001488 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001489 }
Eric Paris06112162008-11-11 22:02:50 +11001490
David Howells275bb412008-11-14 10:39:19 +11001491 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001492 if (audit == SECURITY_CAP_AUDIT) {
1493 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1494 if (rc2)
1495 return rc2;
1496 }
Eric Paris06112162008-11-11 22:02:50 +11001497 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/* Check whether a task is allowed to use a system operation. */
1501static int task_has_system(struct task_struct *tsk,
1502 u32 perms)
1503{
David Howells275bb412008-11-14 10:39:19 +11001504 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
David Howells275bb412008-11-14 10:39:19 +11001506 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 SECCLASS_SYSTEM, perms, NULL);
1508}
1509
1510/* Check whether a task has a particular permission to an inode.
1511 The 'adp' parameter is optional and allows other audit
1512 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001513static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 struct inode *inode,
1515 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001516 struct common_audit_data *adp,
1517 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001520 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
David Howellse0e81732009-09-02 09:13:40 +01001522 validate_creds(cred);
1523
Eric Paris828dfe12008-04-17 13:17:49 -04001524 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001525 return 0;
1526
David Howells88e67f32008-11-14 10:39:21 +11001527 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 isec = inode->i_security;
1529
Eric Paris9ade0cf2011-04-25 16:26:29 -04001530 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001533static int inode_has_perm_noadp(const struct cred *cred,
1534 struct inode *inode,
1535 u32 perms,
1536 unsigned flags)
1537{
1538 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001539 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001540
1541 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1542 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001543 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001544 return inode_has_perm(cred, inode, perms, &ad, flags);
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/* Same as inode_has_perm, but pass explicit audit data containing
1548 the dentry to help the auditing code to more easily generate the
1549 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001550static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct dentry *dentry,
1552 u32 av)
1553{
1554 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001555 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001556 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001557
Eric Paris2875fa02011-04-28 16:04:24 -04001558 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1559 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001560 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001561 return inode_has_perm(cred, inode, av, &ad, 0);
1562}
1563
1564/* Same as inode_has_perm, but pass explicit audit data containing
1565 the path to help the auditing code to more easily generate the
1566 pathname if needed. */
1567static inline int path_has_perm(const struct cred *cred,
1568 struct path *path,
1569 u32 av)
1570{
1571 struct inode *inode = path->dentry->d_inode;
1572 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001573 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001574
Eric Parisf48b7392011-04-25 12:54:27 -04001575 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001576 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001577 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001578 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581/* Check whether a task can use an open file descriptor to
1582 access an inode in a given way. Check access to the
1583 descriptor itself, and then use dentry_has_perm to
1584 check a particular permission to the file.
1585 Access to the descriptor is implicitly granted if it
1586 has the same SID as the process. If av is zero, then
1587 access to the file is not checked, e.g. for cases
1588 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001589static int file_has_perm(const struct cred *cred,
1590 struct file *file,
1591 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001594 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001595 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001596 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001597 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int rc;
1599
Eric Parisf48b7392011-04-25 12:54:27 -04001600 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1601 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001602 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
David Howells275bb412008-11-14 10:39:19 +11001604 if (sid != fsec->sid) {
1605 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 SECCLASS_FD,
1607 FD__USE,
1608 &ad);
1609 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
1613 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001614 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001616 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
David Howells88e67f32008-11-14 10:39:21 +11001618out:
1619 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622/* Check whether a task can create a file. */
1623static int may_create(struct inode *dir,
1624 struct dentry *dentry,
1625 u16 tclass)
1626{
Paul Moore5fb49872010-04-22 14:46:19 -04001627 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct inode_security_struct *dsec;
1629 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001630 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001631 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001632 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int rc;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 dsec = dir->i_security;
1636 sbsec = dir->i_sb->s_security;
1637
David Howells275bb412008-11-14 10:39:19 +11001638 sid = tsec->sid;
1639 newsid = tsec->create_sid;
1640
Eric Parisa2694342011-04-25 13:10:27 -04001641 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1642 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001643 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
David Howells275bb412008-11-14 10:39:19 +11001645 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 DIR__ADD_NAME | DIR__SEARCH,
1647 &ad);
1648 if (rc)
1649 return rc;
1650
David P. Quigleycd895962009-01-16 09:22:04 -05001651 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001652 rc = security_transition_sid(sid, dsec->sid, tclass,
1653 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (rc)
1655 return rc;
1656 }
1657
David Howells275bb412008-11-14 10:39:19 +11001658 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (rc)
1660 return rc;
1661
1662 return avc_has_perm(newsid, sbsec->sid,
1663 SECCLASS_FILESYSTEM,
1664 FILESYSTEM__ASSOCIATE, &ad);
1665}
1666
Michael LeMay4eb582c2006-06-26 00:24:57 -07001667/* Check whether a task can create a key. */
1668static int may_create_key(u32 ksid,
1669 struct task_struct *ctx)
1670{
David Howells275bb412008-11-14 10:39:19 +11001671 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001672
David Howells275bb412008-11-14 10:39:19 +11001673 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001674}
1675
Eric Paris828dfe12008-04-17 13:17:49 -04001676#define MAY_LINK 0
1677#define MAY_UNLINK 1
1678#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680/* Check whether a task can link, unlink, or rmdir a file/directory. */
1681static int may_link(struct inode *dir,
1682 struct dentry *dentry,
1683 int kind)
1684
1685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001687 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001688 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001689 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 u32 av;
1691 int rc;
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 dsec = dir->i_security;
1694 isec = dentry->d_inode->i_security;
1695
Eric Parisa2694342011-04-25 13:10:27 -04001696 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1697 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001698 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 av = DIR__SEARCH;
1701 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001702 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (rc)
1704 return rc;
1705
1706 switch (kind) {
1707 case MAY_LINK:
1708 av = FILE__LINK;
1709 break;
1710 case MAY_UNLINK:
1711 av = FILE__UNLINK;
1712 break;
1713 case MAY_RMDIR:
1714 av = DIR__RMDIR;
1715 break;
1716 default:
Eric Paris744ba352008-04-17 11:52:44 -04001717 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1718 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return 0;
1720 }
1721
David Howells275bb412008-11-14 10:39:19 +11001722 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return rc;
1724}
1725
1726static inline int may_rename(struct inode *old_dir,
1727 struct dentry *old_dentry,
1728 struct inode *new_dir,
1729 struct dentry *new_dentry)
1730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001732 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001733 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001734 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 u32 av;
1736 int old_is_dir, new_is_dir;
1737 int rc;
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 old_dsec = old_dir->i_security;
1740 old_isec = old_dentry->d_inode->i_security;
1741 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1742 new_dsec = new_dir->i_security;
1743
Eric Parisa2694342011-04-25 13:10:27 -04001744 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001745 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Eric Parisa2694342011-04-25 13:10:27 -04001747 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001748 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1750 if (rc)
1751 return rc;
David Howells275bb412008-11-14 10:39:19 +11001752 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 old_isec->sclass, FILE__RENAME, &ad);
1754 if (rc)
1755 return rc;
1756 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001757 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 old_isec->sclass, DIR__REPARENT, &ad);
1759 if (rc)
1760 return rc;
1761 }
1762
Eric Parisa2694342011-04-25 13:10:27 -04001763 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 av = DIR__ADD_NAME | DIR__SEARCH;
1765 if (new_dentry->d_inode)
1766 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001767 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (rc)
1769 return rc;
1770 if (new_dentry->d_inode) {
1771 new_isec = new_dentry->d_inode->i_security;
1772 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001773 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 new_isec->sclass,
1775 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1776 if (rc)
1777 return rc;
1778 }
1779
1780 return 0;
1781}
1782
1783/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001784static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct super_block *sb,
1786 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001787 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001790 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001793 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
1796/* Convert a Linux mode and permission mask to an access vector. */
1797static inline u32 file_mask_to_av(int mode, int mask)
1798{
1799 u32 av = 0;
1800
Al Virodba19c62011-07-25 20:49:29 -04001801 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (mask & MAY_EXEC)
1803 av |= FILE__EXECUTE;
1804 if (mask & MAY_READ)
1805 av |= FILE__READ;
1806
1807 if (mask & MAY_APPEND)
1808 av |= FILE__APPEND;
1809 else if (mask & MAY_WRITE)
1810 av |= FILE__WRITE;
1811
1812 } else {
1813 if (mask & MAY_EXEC)
1814 av |= DIR__SEARCH;
1815 if (mask & MAY_WRITE)
1816 av |= DIR__WRITE;
1817 if (mask & MAY_READ)
1818 av |= DIR__READ;
1819 }
1820
1821 return av;
1822}
1823
1824/* Convert a Linux file to an access vector. */
1825static inline u32 file_to_av(struct file *file)
1826{
1827 u32 av = 0;
1828
1829 if (file->f_mode & FMODE_READ)
1830 av |= FILE__READ;
1831 if (file->f_mode & FMODE_WRITE) {
1832 if (file->f_flags & O_APPEND)
1833 av |= FILE__APPEND;
1834 else
1835 av |= FILE__WRITE;
1836 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001837 if (!av) {
1838 /*
1839 * Special file opened with flags 3 for ioctl-only use.
1840 */
1841 av = FILE__IOCTL;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 return av;
1845}
1846
Eric Paris8b6a5a32008-10-29 17:06:46 -04001847/*
1848 * Convert a file to an access vector and include the correct open
1849 * open permission.
1850 */
1851static inline u32 open_file_to_av(struct file *file)
1852{
1853 u32 av = file_to_av(file);
1854
Eric Paris49b7b8d2010-07-23 11:44:09 -04001855 if (selinux_policycap_openperm)
1856 av |= FILE__OPEN;
1857
Eric Paris8b6a5a32008-10-29 17:06:46 -04001858 return av;
1859}
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/* Hook functions begin here. */
1862
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05001863static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1864{
1865 u32 mysid = current_sid();
1866 u32 mgrsid = task_sid(mgr);
1867
1868 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1869}
1870
1871static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1872{
1873 u32 mysid = current_sid();
1874 u32 fromsid = task_sid(from);
1875 u32 tosid = task_sid(to);
1876 int rc;
1877
1878 if (mysid != fromsid) {
1879 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1880 if (rc)
1881 return rc;
1882 }
1883
1884 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1885}
1886
1887static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1888{
1889 u32 fromsid = task_sid(from);
1890 u32 tosid = task_sid(to);
1891 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1892}
1893
1894static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1895{
1896 u32 sid = task_sid(to);
1897 struct file_security_struct *fsec = file->f_security;
1898 struct inode *inode = file->f_path.dentry->d_inode;
1899 struct inode_security_struct *isec = inode->i_security;
1900 struct common_audit_data ad;
1901 struct selinux_audit_data sad = {0,};
1902 int rc;
1903
1904 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1905 ad.u.path = file->f_path;
1906 ad.selinux_audit_data = &sad;
1907
1908 if (sid != fsec->sid) {
1909 rc = avc_has_perm(sid, fsec->sid,
1910 SECCLASS_FD,
1911 FD__USE,
1912 &ad);
1913 if (rc)
1914 return rc;
1915 }
1916
1917 if (unlikely(IS_PRIVATE(inode)))
1918 return 0;
1919
1920 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1921 &ad);
1922}
1923
Ingo Molnar9e488582009-05-07 19:26:19 +10001924static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001925 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int rc;
1928
Ingo Molnar9e488582009-05-07 19:26:19 +10001929 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (rc)
1931 return rc;
1932
Eric Paris69f594a2012-01-03 12:25:15 -05001933 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001934 u32 sid = current_sid();
1935 u32 csid = task_sid(child);
1936 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001937 }
1938
David Howells3b11a1d2008-11-14 10:39:26 +11001939 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001940}
1941
1942static int selinux_ptrace_traceme(struct task_struct *parent)
1943{
1944 int rc;
1945
Eric Paris200ac532009-02-12 15:01:04 -05001946 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001947 if (rc)
1948 return rc;
1949
1950 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001954 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 int error;
1957
David Howells3b11a1d2008-11-14 10:39:26 +11001958 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (error)
1960 return error;
1961
Eric Paris200ac532009-02-12 15:01:04 -05001962 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
David Howellsd84f4f92008-11-14 10:39:23 +11001965static int selinux_capset(struct cred *new, const struct cred *old,
1966 const kernel_cap_t *effective,
1967 const kernel_cap_t *inheritable,
1968 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 int error;
1971
Eric Paris200ac532009-02-12 15:01:04 -05001972 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001973 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (error)
1975 return error;
1976
David Howellsd84f4f92008-11-14 10:39:23 +11001977 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
James Morris5626d3e2009-01-30 10:05:06 +11001980/*
1981 * (This comment used to live with the selinux_task_setuid hook,
1982 * which was removed).
1983 *
1984 * Since setuid only affects the current process, and since the SELinux
1985 * controls are not based on the Linux identity attributes, SELinux does not
1986 * need to control this operation. However, SELinux does control the use of
1987 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1988 */
1989
Eric Paris6a9de492012-01-03 12:25:14 -05001990static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1991 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 int rc;
1994
Eric Paris6a9de492012-01-03 12:25:14 -05001995 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (rc)
1997 return rc;
1998
Eric Paris6a9de492012-01-03 12:25:14 -05001999 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2003{
David Howells88e67f32008-11-14 10:39:21 +11002004 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int rc = 0;
2006
2007 if (!sb)
2008 return 0;
2009
2010 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002011 case Q_SYNC:
2012 case Q_QUOTAON:
2013 case Q_QUOTAOFF:
2014 case Q_SETINFO:
2015 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002016 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002017 break;
2018 case Q_GETFMT:
2019 case Q_GETINFO:
2020 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002021 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002022 break;
2023 default:
2024 rc = 0; /* let the kernel handle invalid cmds */
2025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 return rc;
2028}
2029
2030static int selinux_quota_on(struct dentry *dentry)
2031{
David Howells88e67f32008-11-14 10:39:21 +11002032 const struct cred *cred = current_cred();
2033
Eric Paris2875fa02011-04-28 16:04:24 -04002034 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Eric Paris12b30522010-11-15 18:36:29 -05002037static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 int rc;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002042 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2043 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002044 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2045 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002046 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2047 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2048 /* Set level of messages printed to console */
2049 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002050 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2051 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002052 case SYSLOG_ACTION_CLOSE: /* Close log */
2053 case SYSLOG_ACTION_OPEN: /* Open log */
2054 case SYSLOG_ACTION_READ: /* Read from log */
2055 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2056 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002057 default:
2058 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 return rc;
2062}
2063
2064/*
2065 * Check that a process has enough memory to allocate a new virtual
2066 * mapping. 0 means there is enough memory for the allocation to
2067 * succeed and -ENOMEM implies there is not.
2068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Do not audit the selinux permission check, as this is applied to all
2070 * processes that allocate mappings.
2071 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002072static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Eric Paris6a9de492012-01-03 12:25:14 -05002076 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002077 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (rc == 0)
2079 cap_sys_admin = 1;
2080
Alan Cox34b4e4a2007-08-22 14:01:28 -07002081 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084/* binprm security operations */
2085
David Howellsa6f76f22008-11-14 10:39:24 +11002086static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
David Howellsa6f76f22008-11-14 10:39:24 +11002088 const struct task_security_struct *old_tsec;
2089 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002091 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002092 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002093 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int rc;
2095
Eric Paris200ac532009-02-12 15:01:04 -05002096 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (rc)
2098 return rc;
2099
David Howellsa6f76f22008-11-14 10:39:24 +11002100 /* SELinux context only depends on initial program or script and not
2101 * the script interpreter */
2102 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
2104
David Howellsa6f76f22008-11-14 10:39:24 +11002105 old_tsec = current_security();
2106 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 isec = inode->i_security;
2108
2109 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002110 new_tsec->sid = old_tsec->sid;
2111 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Michael LeMay28eba5b2006-06-27 02:53:42 -07002113 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002114 new_tsec->create_sid = 0;
2115 new_tsec->keycreate_sid = 0;
2116 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
David Howellsa6f76f22008-11-14 10:39:24 +11002118 if (old_tsec->exec_sid) {
2119 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002121 new_tsec->exec_sid = 0;
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002122
2123 /*
2124 * Minimize confusion: if no_new_privs and a transition is
2125 * explicitly requested, then fail the exec.
2126 */
2127 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2128 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 } else {
2130 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002131 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002132 SECCLASS_PROCESS, NULL,
2133 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (rc)
2135 return rc;
2136 }
2137
Eric Parisf48b7392011-04-25 12:54:27 -04002138 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002139 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002140 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002142 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2143 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002144 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
David Howellsa6f76f22008-11-14 10:39:24 +11002146 if (new_tsec->sid == old_tsec->sid) {
2147 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2149 if (rc)
2150 return rc;
2151 } else {
2152 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002153 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2155 if (rc)
2156 return rc;
2157
David Howellsa6f76f22008-11-14 10:39:24 +11002158 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2160 if (rc)
2161 return rc;
2162
David Howellsa6f76f22008-11-14 10:39:24 +11002163 /* Check for shared state */
2164 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2165 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2166 SECCLASS_PROCESS, PROCESS__SHARE,
2167 NULL);
2168 if (rc)
2169 return -EPERM;
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
David Howellsa6f76f22008-11-14 10:39:24 +11002172 /* Make sure that anyone attempting to ptrace over a task that
2173 * changes its SID has the appropriate permit */
2174 if (bprm->unsafe &
2175 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2176 struct task_struct *tracer;
2177 struct task_security_struct *sec;
2178 u32 ptsid = 0;
2179
2180 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002181 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002182 if (likely(tracer != NULL)) {
2183 sec = __task_cred(tracer)->security;
2184 ptsid = sec->sid;
2185 }
2186 rcu_read_unlock();
2187
2188 if (ptsid != 0) {
2189 rc = avc_has_perm(ptsid, new_tsec->sid,
2190 SECCLASS_PROCESS,
2191 PROCESS__PTRACE, NULL);
2192 if (rc)
2193 return -EPERM;
2194 }
2195 }
2196
2197 /* Clear any possibly unsafe personality bits on exec: */
2198 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
2202}
2203
Eric Paris828dfe12008-04-17 13:17:49 -04002204static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Paul Moore5fb49872010-04-22 14:46:19 -04002206 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002207 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 int atsecure = 0;
2209
David Howells275bb412008-11-14 10:39:19 +11002210 sid = tsec->sid;
2211 osid = tsec->osid;
2212
2213 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* Enable secure mode for SIDs transitions unless
2215 the noatsecure permission is granted between
2216 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002217 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002218 SECCLASS_PROCESS,
2219 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221
Eric Paris200ac532009-02-12 15:01:04 -05002222 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002226static inline void flush_unauthorized_files(const struct cred *cred,
2227 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Thomas Liu2bf49692009-07-14 12:14:09 -04002229 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002230 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002232 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002233 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002235 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002237 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002239 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002240 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002241 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002242 struct inode *inode;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* Revalidate access to controlling tty.
2245 Use inode_has_perm on the tty inode directly rather
2246 than using file_has_perm, as this particular open
2247 file may belong to another process and we are only
2248 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002249 file_priv = list_first_entry(&tty->tty_files,
2250 struct tty_file_private, list);
2251 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002252 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002253 if (inode_has_perm_noadp(cred, inode,
2254 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002255 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002258 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002259 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002261 /* Reset controlling tty. */
2262 if (drop_tty)
2263 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 /* Revalidate access to inherited open files. */
2266
Eric Parisf48b7392011-04-25 12:54:27 -04002267 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002268 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 spin_lock(&files->file_lock);
2271 for (;;) {
2272 unsigned long set, i;
2273 int fd;
2274
2275 j++;
Josh Boyer27cd8f52012-07-25 10:40:34 -04002276 i = j * BITS_PER_LONG;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002277 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002278 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002280 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!set)
2282 continue;
2283 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002284 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (set & 1) {
2286 file = fget(i);
2287 if (!file)
2288 continue;
David Howells88e67f32008-11-14 10:39:21 +11002289 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 file,
2291 file_to_av(file))) {
2292 sys_close(i);
2293 fd = get_unused_fd();
2294 if (fd != i) {
2295 if (fd >= 0)
2296 put_unused_fd(fd);
2297 fput(file);
2298 continue;
2299 }
2300 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002301 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 } else {
David Howells745ca242008-11-14 10:39:22 +11002303 devnull = dentry_open(
2304 dget(selinux_null),
2305 mntget(selinuxfs_mount),
2306 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002307 if (IS_ERR(devnull)) {
2308 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 put_unused_fd(fd);
2310 fput(file);
2311 continue;
2312 }
2313 }
2314 fd_install(fd, devnull);
2315 }
2316 fput(file);
2317 }
2318 }
2319 spin_lock(&files->file_lock);
2320
2321 }
2322 spin_unlock(&files->file_lock);
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/*
David Howellsa6f76f22008-11-14 10:39:24 +11002326 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 */
David Howellsa6f76f22008-11-14 10:39:24 +11002328static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
David Howellsa6f76f22008-11-14 10:39:24 +11002330 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 int rc, i;
2333
David Howellsa6f76f22008-11-14 10:39:24 +11002334 new_tsec = bprm->cred->security;
2335 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return;
2337
2338 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002339 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
David Howellsa6f76f22008-11-14 10:39:24 +11002341 /* Always clear parent death signal on SID transitions. */
2342 current->pdeath_signal = 0;
2343
2344 /* Check whether the new SID can inherit resource limits from the old
2345 * SID. If not, reset all soft limits to the lower of the current
2346 * task's hard limit and the init task's soft limit.
2347 *
2348 * Note that the setting of hard limits (even to lower them) can be
2349 * controlled by the setrlimit check. The inclusion of the init task's
2350 * soft limit into the computation is to avoid resetting soft limits
2351 * higher than the default soft limit for cases where the default is
2352 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2353 */
2354 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2355 PROCESS__RLIMITINH, NULL);
2356 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002357 /* protect against do_prlimit() */
2358 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002359 for (i = 0; i < RLIM_NLIMITS; i++) {
2360 rlim = current->signal->rlim + i;
2361 initrlim = init_task.signal->rlim + i;
2362 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2363 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002364 task_unlock(current);
2365 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002366 }
2367}
2368
2369/*
2370 * Clean up the process immediately after the installation of new credentials
2371 * due to exec
2372 */
2373static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2374{
2375 const struct task_security_struct *tsec = current_security();
2376 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002377 u32 osid, sid;
2378 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002379
David Howellsa6f76f22008-11-14 10:39:24 +11002380 osid = tsec->osid;
2381 sid = tsec->sid;
2382
2383 if (sid == osid)
2384 return;
2385
2386 /* Check whether the new SID can inherit signal state from the old SID.
2387 * If not, clear itimers to avoid subsequent signal generation and
2388 * flush and unblock signals.
2389 *
2390 * This must occur _after_ the task SID has been updated so that any
2391 * kill done after the flush will be checked against the new SID.
2392 */
2393 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (rc) {
2395 memset(&itimer, 0, sizeof itimer);
2396 for (i = 0; i < 3; i++)
2397 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002399 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2400 __flush_signals(current);
2401 flush_signal_handlers(current, 1);
2402 sigemptyset(&current->blocked);
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 spin_unlock_irq(&current->sighand->siglock);
2405 }
2406
David Howellsa6f76f22008-11-14 10:39:24 +11002407 /* Wake up the parent if it is waiting so that it can recheck
2408 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002409 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002410 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002411 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
2414/* superblock security operations */
2415
2416static int selinux_sb_alloc_security(struct super_block *sb)
2417{
2418 return superblock_alloc_security(sb);
2419}
2420
2421static void selinux_sb_free_security(struct super_block *sb)
2422{
2423 superblock_free_security(sb);
2424}
2425
2426static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2427{
2428 if (plen > olen)
2429 return 0;
2430
2431 return !memcmp(prefix, option, plen);
2432}
2433
2434static inline int selinux_option(char *option, int len)
2435{
Eric Paris832cbd92008-04-01 13:24:09 -04002436 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2437 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2438 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002439 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2440 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
2443static inline void take_option(char **to, char *from, int *first, int len)
2444{
2445 if (!*first) {
2446 **to = ',';
2447 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002448 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 *first = 0;
2450 memcpy(*to, from, len);
2451 *to += len;
2452}
2453
Eric Paris828dfe12008-04-17 13:17:49 -04002454static inline void take_selinux_option(char **to, char *from, int *first,
2455 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002456{
2457 int current_size = 0;
2458
2459 if (!*first) {
2460 **to = '|';
2461 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002462 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002463 *first = 0;
2464
2465 while (current_size < len) {
2466 if (*from != '"') {
2467 **to = *from;
2468 *to += 1;
2469 }
2470 from += 1;
2471 current_size += 1;
2472 }
2473}
2474
Eric Parise0007522008-03-05 10:31:54 -05002475static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 int fnosec, fsec, rc = 0;
2478 char *in_save, *in_curr, *in_end;
2479 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002480 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 in_curr = orig;
2483 sec_curr = copy;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2486 if (!nosec) {
2487 rc = -ENOMEM;
2488 goto out;
2489 }
2490
2491 nosec_save = nosec;
2492 fnosec = fsec = 1;
2493 in_save = in_end = orig;
2494
2495 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002496 if (*in_end == '"')
2497 open_quote = !open_quote;
2498 if ((*in_end == ',' && open_quote == 0) ||
2499 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 int len = in_end - in_curr;
2501
2502 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002503 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 else
2505 take_option(&nosec, in_curr, &fnosec, len);
2506
2507 in_curr = in_end + 1;
2508 }
2509 } while (*in_end++);
2510
Eric Paris6931dfc2005-06-30 02:58:51 -07002511 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002512 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513out:
2514 return rc;
2515}
2516
Eric Paris026eb162011-03-03 16:09:14 -05002517static int selinux_sb_remount(struct super_block *sb, void *data)
2518{
2519 int rc, i, *flags;
2520 struct security_mnt_opts opts;
2521 char *secdata, **mount_options;
2522 struct superblock_security_struct *sbsec = sb->s_security;
2523
2524 if (!(sbsec->flags & SE_SBINITIALIZED))
2525 return 0;
2526
2527 if (!data)
2528 return 0;
2529
2530 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2531 return 0;
2532
2533 security_init_mnt_opts(&opts);
2534 secdata = alloc_secdata();
2535 if (!secdata)
2536 return -ENOMEM;
2537 rc = selinux_sb_copy_data(data, secdata);
2538 if (rc)
2539 goto out_free_secdata;
2540
2541 rc = selinux_parse_opts_str(secdata, &opts);
2542 if (rc)
2543 goto out_free_secdata;
2544
2545 mount_options = opts.mnt_opts;
2546 flags = opts.mnt_opts_flags;
2547
2548 for (i = 0; i < opts.num_mnt_opts; i++) {
2549 u32 sid;
2550 size_t len;
2551
2552 if (flags[i] == SE_SBLABELSUPP)
2553 continue;
2554 len = strlen(mount_options[i]);
2555 rc = security_context_to_sid(mount_options[i], len, &sid);
2556 if (rc) {
2557 printk(KERN_WARNING "SELinux: security_context_to_sid"
2558 "(%s) failed for (dev %s, type %s) errno=%d\n",
2559 mount_options[i], sb->s_id, sb->s_type->name, rc);
2560 goto out_free_opts;
2561 }
2562 rc = -EINVAL;
2563 switch (flags[i]) {
2564 case FSCONTEXT_MNT:
2565 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2566 goto out_bad_option;
2567 break;
2568 case CONTEXT_MNT:
2569 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2570 goto out_bad_option;
2571 break;
2572 case ROOTCONTEXT_MNT: {
2573 struct inode_security_struct *root_isec;
2574 root_isec = sb->s_root->d_inode->i_security;
2575
2576 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2577 goto out_bad_option;
2578 break;
2579 }
2580 case DEFCONTEXT_MNT:
2581 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2582 goto out_bad_option;
2583 break;
2584 default:
2585 goto out_free_opts;
2586 }
2587 }
2588
2589 rc = 0;
2590out_free_opts:
2591 security_free_mnt_opts(&opts);
2592out_free_secdata:
2593 free_secdata(secdata);
2594 return rc;
2595out_bad_option:
2596 printk(KERN_WARNING "SELinux: unable to change security options "
2597 "during remount (dev %s, type=%s)\n", sb->s_id,
2598 sb->s_type->name);
2599 goto out_free_opts;
2600}
2601
James Morris12204e22008-12-19 10:44:42 +11002602static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
David Howells88e67f32008-11-14 10:39:21 +11002604 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002605 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002606 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 int rc;
2608
2609 rc = superblock_doinit(sb, data);
2610 if (rc)
2611 return rc;
2612
James Morris74192242008-12-19 11:41:10 +11002613 /* Allow all mounts performed by the kernel */
2614 if (flags & MS_KERNMOUNT)
2615 return 0;
2616
Eric Parisa2694342011-04-25 13:10:27 -04002617 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002618 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002619 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002620 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
David Howells726c3342006-06-23 02:02:58 -07002623static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
David Howells88e67f32008-11-14 10:39:21 +11002625 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002626 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002627 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Eric Parisa2694342011-04-25 13:10:27 -04002629 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002630 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002631 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002632 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Eric Paris828dfe12008-04-17 13:17:49 -04002635static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002636 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002637 char *type,
2638 unsigned long flags,
2639 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
David Howells88e67f32008-11-14 10:39:21 +11002641 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002644 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002645 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 else
Eric Paris2875fa02011-04-28 16:04:24 -04002647 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650static int selinux_umount(struct vfsmount *mnt, int flags)
2651{
David Howells88e67f32008-11-14 10:39:21 +11002652 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
David Howells88e67f32008-11-14 10:39:21 +11002654 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002655 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/* inode security operations */
2659
2660static int selinux_inode_alloc_security(struct inode *inode)
2661{
2662 return inode_alloc_security(inode);
2663}
2664
2665static void selinux_inode_free_security(struct inode *inode)
2666{
2667 inode_free_security(inode);
2668}
2669
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002670static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002671 const struct qstr *qstr, char **name,
2672 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002673{
Paul Moore5fb49872010-04-22 14:46:19 -04002674 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002675 struct inode_security_struct *dsec;
2676 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002677 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002678 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002679 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002680
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002681 dsec = dir->i_security;
2682 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002683
David Howells275bb412008-11-14 10:39:19 +11002684 sid = tsec->sid;
2685 newsid = tsec->create_sid;
2686
Eric Paris415103f2010-12-02 16:13:40 -05002687 if ((sbsec->flags & SE_SBINITIALIZED) &&
2688 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2689 newsid = sbsec->mntpoint_sid;
2690 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002691 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002692 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002693 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002694 if (rc) {
2695 printk(KERN_WARNING "%s: "
2696 "security_transition_sid failed, rc=%d (dev=%s "
2697 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002698 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002699 -rc, inode->i_sb->s_id, inode->i_ino);
2700 return rc;
2701 }
2702 }
2703
Eric Paris296fddf2006-09-25 23:32:00 -07002704 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002705 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002706 struct inode_security_struct *isec = inode->i_security;
2707 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2708 isec->sid = newsid;
2709 isec->initialized = 1;
2710 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002711
David P. Quigleycd895962009-01-16 09:22:04 -05002712 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002713 return -EOPNOTSUPP;
2714
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002715 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002716 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002717 if (!namep)
2718 return -ENOMEM;
2719 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002720 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002721
2722 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002723 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002724 if (rc) {
2725 kfree(namep);
2726 return rc;
2727 }
2728 *value = context;
2729 *len = clen;
2730 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002731
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002732 return 0;
2733}
2734
Al Viro4acdaf22011-07-26 01:42:34 -04002735static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 return may_create(dir, dentry, SECCLASS_FILE);
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return may_link(dir, old_dentry, MAY_LINK);
2743}
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return may_link(dir, dentry, MAY_UNLINK);
2748}
2749
2750static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2751{
2752 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2753}
2754
Al Viro18bb1db2011-07-26 01:41:39 -04002755static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 return may_create(dir, dentry, SECCLASS_DIR);
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2761{
2762 return may_link(dir, dentry, MAY_RMDIR);
2763}
2764
Al Viro1a67aaf2011-07-26 01:52:52 -04002765static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002771 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776static int selinux_inode_readlink(struct dentry *dentry)
2777{
David Howells88e67f32008-11-14 10:39:21 +11002778 const struct cred *cred = current_cred();
2779
Eric Paris2875fa02011-04-28 16:04:24 -04002780 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2784{
David Howells88e67f32008-11-14 10:39:21 +11002785 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Eric Paris2875fa02011-04-28 16:04:24 -04002787 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
Al Viroe74f71e2011-06-20 19:38:15 -04002790static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
David Howells88e67f32008-11-14 10:39:21 +11002792 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002793 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002794 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002795 u32 perms;
2796 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002797 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Eric Parisb782e0a2010-07-23 11:44:03 -04002799 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002800 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2801
Eric Parisb782e0a2010-07-23 11:44:03 -04002802 /* No permission to check. Existence test. */
2803 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Eric Parisf48b7392011-04-25 12:54:27 -04002806 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002807 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002808 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002809
2810 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002811 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002812
2813 perms = file_mask_to_av(inode->i_mode, mask);
2814
Eric Paris9ade0cf2011-04-25 16:26:29 -04002815 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2819{
David Howells88e67f32008-11-14 10:39:21 +11002820 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002821 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002823 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2824 if (ia_valid & ATTR_FORCE) {
2825 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2826 ATTR_FORCE);
2827 if (!ia_valid)
2828 return 0;
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002831 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2832 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002833 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Eric Paris2875fa02011-04-28 16:04:24 -04002835 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2839{
David Howells88e67f32008-11-14 10:39:21 +11002840 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002841 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002842
Eric Paris2875fa02011-04-28 16:04:24 -04002843 path.dentry = dentry;
2844 path.mnt = mnt;
2845
2846 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
David Howells8f0cfa52008-04-29 00:59:41 -07002849static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002850{
David Howells88e67f32008-11-14 10:39:21 +11002851 const struct cred *cred = current_cred();
2852
Serge E. Hallynb5376772007-10-16 23:31:36 -07002853 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2854 sizeof XATTR_SECURITY_PREFIX - 1)) {
2855 if (!strcmp(name, XATTR_NAME_CAPS)) {
2856 if (!capable(CAP_SETFCAP))
2857 return -EPERM;
2858 } else if (!capable(CAP_SYS_ADMIN)) {
2859 /* A different attribute in the security namespace.
2860 Restrict to administrator. */
2861 return -EPERM;
2862 }
2863 }
2864
2865 /* Not an attribute we recognize, so just check the
2866 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002867 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002868}
2869
David Howells8f0cfa52008-04-29 00:59:41 -07002870static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2871 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct inode *inode = dentry->d_inode;
2874 struct inode_security_struct *isec = inode->i_security;
2875 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002876 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002877 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002878 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 int rc = 0;
2880
Serge E. Hallynb5376772007-10-16 23:31:36 -07002881 if (strcmp(name, XATTR_NAME_SELINUX))
2882 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002885 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -EOPNOTSUPP;
2887
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002888 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return -EPERM;
2890
Eric Parisa2694342011-04-25 13:10:27 -04002891 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002892 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002893 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
David Howells275bb412008-11-14 10:39:19 +11002895 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 FILE__RELABELFROM, &ad);
2897 if (rc)
2898 return rc;
2899
2900 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002901 if (rc == -EINVAL) {
2902 if (!capable(CAP_MAC_ADMIN))
2903 return rc;
2904 rc = security_context_to_sid_force(value, size, &newsid);
2905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (rc)
2907 return rc;
2908
David Howells275bb412008-11-14 10:39:19 +11002909 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 FILE__RELABELTO, &ad);
2911 if (rc)
2912 return rc;
2913
David Howells275bb412008-11-14 10:39:19 +11002914 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002915 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (rc)
2917 return rc;
2918
2919 return avc_has_perm(newsid,
2920 sbsec->sid,
2921 SECCLASS_FILESYSTEM,
2922 FILESYSTEM__ASSOCIATE,
2923 &ad);
2924}
2925
David Howells8f0cfa52008-04-29 00:59:41 -07002926static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002927 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002928 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 struct inode *inode = dentry->d_inode;
2931 struct inode_security_struct *isec = inode->i_security;
2932 u32 newsid;
2933 int rc;
2934
2935 if (strcmp(name, XATTR_NAME_SELINUX)) {
2936 /* Not an attribute we recognize, so nothing to do. */
2937 return;
2938 }
2939
Stephen Smalley12b29f32008-05-07 13:03:20 -04002940 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002942 printk(KERN_ERR "SELinux: unable to map context to SID"
2943 "for (%s, %lu), rc=%d\n",
2944 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return;
2946 }
2947
2948 isec->sid = newsid;
2949 return;
2950}
2951
David Howells8f0cfa52008-04-29 00:59:41 -07002952static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
David Howells88e67f32008-11-14 10:39:21 +11002954 const struct cred *cred = current_cred();
2955
Eric Paris2875fa02011-04-28 16:04:24 -04002956 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
Eric Paris828dfe12008-04-17 13:17:49 -04002959static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
David Howells88e67f32008-11-14 10:39:21 +11002961 const struct cred *cred = current_cred();
2962
Eric Paris2875fa02011-04-28 16:04:24 -04002963 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
David Howells8f0cfa52008-04-29 00:59:41 -07002966static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002968 if (strcmp(name, XATTR_NAME_SELINUX))
2969 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 /* No one is allowed to remove a SELinux security label.
2972 You can change the label, but all data must be labeled. */
2973 return -EACCES;
2974}
2975
James Morrisd381d8a2005-10-30 14:59:22 -08002976/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002977 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002978 *
2979 * Permission check is handled by selinux_inode_getxattr hook.
2980 */
David P. Quigley42492592008-02-04 22:29:39 -08002981static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
David P. Quigley42492592008-02-04 22:29:39 -08002983 u32 size;
2984 int error;
2985 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002988 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2989 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002991 /*
2992 * If the caller has CAP_MAC_ADMIN, then get the raw context
2993 * value even if it is not defined by current policy; otherwise,
2994 * use the in-core value under current policy.
2995 * Use the non-auditing forms of the permission checks since
2996 * getxattr may be called by unprivileged processes commonly
2997 * and lack of permission just means that we fall back to the
2998 * in-core context value, not a denial.
2999 */
Eric Paris6a9de492012-01-03 12:25:14 -05003000 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00003001 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003002 if (!error)
3003 error = security_sid_to_context_force(isec->sid, &context,
3004 &size);
3005 else
3006 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003007 if (error)
3008 return error;
3009 error = size;
3010 if (alloc) {
3011 *buffer = context;
3012 goto out_nofree;
3013 }
3014 kfree(context);
3015out_nofree:
3016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
3019static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003020 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 struct inode_security_struct *isec = inode->i_security;
3023 u32 newsid;
3024 int rc;
3025
3026 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3027 return -EOPNOTSUPP;
3028
3029 if (!value || !size)
3030 return -EACCES;
3031
Eric Paris828dfe12008-04-17 13:17:49 -04003032 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (rc)
3034 return rc;
3035
3036 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003037 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
3039}
3040
3041static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3042{
3043 const int len = sizeof(XATTR_NAME_SELINUX);
3044 if (buffer && len <= buffer_size)
3045 memcpy(buffer, XATTR_NAME_SELINUX, len);
3046 return len;
3047}
3048
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003049static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3050{
3051 struct inode_security_struct *isec = inode->i_security;
3052 *secid = isec->sid;
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055/* file security operations */
3056
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003057static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
David Howells88e67f32008-11-14 10:39:21 +11003059 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003060 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3063 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3064 mask |= MAY_APPEND;
3065
Paul Moore389fb802009-03-27 17:10:34 -04003066 return file_has_perm(cred, file,
3067 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003070static int selinux_file_permission(struct file *file, int mask)
3071{
Stephen Smalley20dda182009-06-22 14:54:53 -04003072 struct inode *inode = file->f_path.dentry->d_inode;
3073 struct file_security_struct *fsec = file->f_security;
3074 struct inode_security_struct *isec = inode->i_security;
3075 u32 sid = current_sid();
3076
Paul Moore389fb802009-03-27 17:10:34 -04003077 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003078 /* No permission to check. Existence test. */
3079 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003080
Stephen Smalley20dda182009-06-22 14:54:53 -04003081 if (sid == fsec->sid && fsec->isid == isec->sid &&
3082 fsec->pseqno == avc_policy_seqno())
3083 /* No change since dentry_open check. */
3084 return 0;
3085
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003086 return selinux_revalidate_file_permission(file, mask);
3087}
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089static int selinux_file_alloc_security(struct file *file)
3090{
3091 return file_alloc_security(file);
3092}
3093
3094static void selinux_file_free_security(struct file *file)
3095{
3096 file_free_security(file);
3097}
3098
3099static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3100 unsigned long arg)
3101{
David Howells88e67f32008-11-14 10:39:21 +11003102 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003103 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Eric Paris0b24dcb2011-02-25 15:39:20 -05003105 switch (cmd) {
3106 case FIONREAD:
3107 /* fall through */
3108 case FIBMAP:
3109 /* fall through */
3110 case FIGETBSZ:
3111 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003112 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003113 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003114 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003115 error = file_has_perm(cred, file, FILE__GETATTR);
3116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Al Viro2f99c362012-03-23 16:04:05 -04003118 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003119 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003120 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003121 error = file_has_perm(cred, file, FILE__SETATTR);
3122 break;
3123
3124 /* sys_ioctl() checks */
3125 case FIONBIO:
3126 /* fall through */
3127 case FIOASYNC:
3128 error = file_has_perm(cred, file, 0);
3129 break;
3130
3131 case KDSKBENT:
3132 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003133 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3134 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003135 break;
3136
3137 /* default case assumes that the command will go
3138 * to the file's ioctl() function.
3139 */
3140 default:
3141 error = file_has_perm(cred, file, FILE__IOCTL);
3142 }
3143 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003146static int default_noexec;
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3149{
David Howells88e67f32008-11-14 10:39:21 +11003150 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003151 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003152
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003153 if (default_noexec &&
3154 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /*
3156 * We are making executable an anonymous mapping or a
3157 * private file mapping that will also be writable.
3158 * This has an additional check.
3159 */
David Howellsd84f4f92008-11-14 10:39:23 +11003160 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003162 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 if (file) {
3166 /* read access is always possible with a mapping */
3167 u32 av = FILE__READ;
3168
3169 /* write access only matters if the mapping is shared */
3170 if (shared && (prot & PROT_WRITE))
3171 av |= FILE__WRITE;
3172
3173 if (prot & PROT_EXEC)
3174 av |= FILE__EXECUTE;
3175
David Howells88e67f32008-11-14 10:39:21 +11003176 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
David Howellsd84f4f92008-11-14 10:39:23 +11003178
3179error:
3180 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
3183static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003184 unsigned long prot, unsigned long flags,
3185 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Eric Parised032182007-06-28 15:55:21 -04003187 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003188 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Eric Paris84336d1a2009-07-31 12:54:05 -04003190 /*
3191 * notice that we are intentionally putting the SELinux check before
3192 * the secondary cap_file_mmap check. This is such a likely attempt
3193 * at bad behaviour/exploit that we always want to get the AVC, even
3194 * if DAC would have also denied the operation.
3195 */
Eric Parisa2551df2009-07-31 12:54:11 -04003196 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003197 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3198 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003199 if (rc)
3200 return rc;
3201 }
3202
3203 /* do DAC check on address space usage */
3204 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003205 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return rc;
3207
3208 if (selinux_checkreqprot)
3209 prot = reqprot;
3210
3211 return file_map_prot_check(file, prot,
3212 (flags & MAP_TYPE) == MAP_SHARED);
3213}
3214
3215static int selinux_file_mprotect(struct vm_area_struct *vma,
3216 unsigned long reqprot,
3217 unsigned long prot)
3218{
David Howells88e67f32008-11-14 10:39:21 +11003219 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 if (selinux_checkreqprot)
3222 prot = reqprot;
3223
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003224 if (default_noexec &&
3225 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003226 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003227 if (vma->vm_start >= vma->vm_mm->start_brk &&
3228 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003229 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003230 } else if (!vma->vm_file &&
3231 vma->vm_start <= vma->vm_mm->start_stack &&
3232 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003233 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003234 } else if (vma->vm_file && vma->anon_vma) {
3235 /*
3236 * We are making executable a file mapping that has
3237 * had some COW done. Since pages might have been
3238 * written, check ability to execute the possibly
3239 * modified content. This typically should only
3240 * occur for text relocations.
3241 */
David Howellsd84f4f92008-11-14 10:39:23 +11003242 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003243 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003244 if (rc)
3245 return rc;
3246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3249}
3250
3251static int selinux_file_lock(struct file *file, unsigned int cmd)
3252{
David Howells88e67f32008-11-14 10:39:21 +11003253 const struct cred *cred = current_cred();
3254
3255 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3259 unsigned long arg)
3260{
David Howells88e67f32008-11-14 10:39:21 +11003261 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 int err = 0;
3263
3264 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003265 case F_SETFL:
3266 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3267 err = -EINVAL;
3268 break;
3269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Eric Paris828dfe12008-04-17 13:17:49 -04003271 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003272 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003274 }
3275 /* fall through */
3276 case F_SETOWN:
3277 case F_SETSIG:
3278 case F_GETFL:
3279 case F_GETOWN:
3280 case F_GETSIG:
3281 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003282 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003283 break;
3284 case F_GETLK:
3285 case F_SETLK:
3286 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003288 case F_GETLK64:
3289 case F_SETLK64:
3290 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003292 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3293 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003295 }
David Howells88e67f32008-11-14 10:39:21 +11003296 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299
3300 return err;
3301}
3302
3303static int selinux_file_set_fowner(struct file *file)
3304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 struct file_security_struct *fsec;
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003308 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310 return 0;
3311}
3312
3313static int selinux_file_send_sigiotask(struct task_struct *tsk,
3314 struct fown_struct *fown, int signum)
3315{
Eric Paris828dfe12008-04-17 13:17:49 -04003316 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003317 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 struct file_security_struct *fsec;
3320
3321 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003322 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 fsec = file->f_security;
3325
3326 if (!signum)
3327 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3328 else
3329 perm = signal_to_av(signum);
3330
David Howells275bb412008-11-14 10:39:19 +11003331 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 SECCLASS_PROCESS, perm, NULL);
3333}
3334
3335static int selinux_file_receive(struct file *file)
3336{
David Howells88e67f32008-11-14 10:39:21 +11003337 const struct cred *cred = current_cred();
3338
3339 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340}
3341
David Howells745ca242008-11-14 10:39:22 +11003342static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003343{
3344 struct file_security_struct *fsec;
3345 struct inode *inode;
3346 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003347
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003348 inode = file->f_path.dentry->d_inode;
3349 fsec = file->f_security;
3350 isec = inode->i_security;
3351 /*
3352 * Save inode label and policy sequence number
3353 * at open-time so that selinux_file_permission
3354 * can determine whether revalidation is necessary.
3355 * Task label is already saved in the file security
3356 * struct as its SID.
3357 */
3358 fsec->isid = isec->sid;
3359 fsec->pseqno = avc_policy_seqno();
3360 /*
3361 * Since the inode label or policy seqno may have changed
3362 * between the selinux_inode_permission check and the saving
3363 * of state above, recheck that access is still permitted.
3364 * Otherwise, access might never be revalidated against the
3365 * new inode label or new policy.
3366 * This check is not redundant - do not remove.
3367 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003368 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003369}
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371/* task security operations */
3372
3373static int selinux_task_create(unsigned long clone_flags)
3374{
David Howells3b11a1d2008-11-14 10:39:26 +11003375 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
David Howellsf1752ee2008-11-14 10:39:17 +11003378/*
David Howellsee18d642009-09-02 09:14:21 +01003379 * allocate the SELinux part of blank credentials
3380 */
3381static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3382{
3383 struct task_security_struct *tsec;
3384
3385 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3386 if (!tsec)
3387 return -ENOMEM;
3388
3389 cred->security = tsec;
3390 return 0;
3391}
3392
3393/*
David Howellsf1752ee2008-11-14 10:39:17 +11003394 * detach and free the LSM part of a set of credentials
3395 */
3396static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
David Howellsf1752ee2008-11-14 10:39:17 +11003398 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003399
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003400 /*
3401 * cred->security == NULL if security_cred_alloc_blank() or
3402 * security_prepare_creds() returned an error.
3403 */
3404 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003405 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003406 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
David Howellsd84f4f92008-11-14 10:39:23 +11003409/*
3410 * prepare a new set of credentials for modification
3411 */
3412static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3413 gfp_t gfp)
3414{
3415 const struct task_security_struct *old_tsec;
3416 struct task_security_struct *tsec;
3417
3418 old_tsec = old->security;
3419
3420 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3421 if (!tsec)
3422 return -ENOMEM;
3423
3424 new->security = tsec;
3425 return 0;
3426}
3427
3428/*
David Howellsee18d642009-09-02 09:14:21 +01003429 * transfer the SELinux data to a blank set of creds
3430 */
3431static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3432{
3433 const struct task_security_struct *old_tsec = old->security;
3434 struct task_security_struct *tsec = new->security;
3435
3436 *tsec = *old_tsec;
3437}
3438
3439/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003440 * set the security data for a kernel service
3441 * - all the creation contexts are set to unlabelled
3442 */
3443static int selinux_kernel_act_as(struct cred *new, u32 secid)
3444{
3445 struct task_security_struct *tsec = new->security;
3446 u32 sid = current_sid();
3447 int ret;
3448
3449 ret = avc_has_perm(sid, secid,
3450 SECCLASS_KERNEL_SERVICE,
3451 KERNEL_SERVICE__USE_AS_OVERRIDE,
3452 NULL);
3453 if (ret == 0) {
3454 tsec->sid = secid;
3455 tsec->create_sid = 0;
3456 tsec->keycreate_sid = 0;
3457 tsec->sockcreate_sid = 0;
3458 }
3459 return ret;
3460}
3461
3462/*
3463 * set the file creation context in a security record to the same as the
3464 * objective context of the specified inode
3465 */
3466static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3467{
3468 struct inode_security_struct *isec = inode->i_security;
3469 struct task_security_struct *tsec = new->security;
3470 u32 sid = current_sid();
3471 int ret;
3472
3473 ret = avc_has_perm(sid, isec->sid,
3474 SECCLASS_KERNEL_SERVICE,
3475 KERNEL_SERVICE__CREATE_FILES_AS,
3476 NULL);
3477
3478 if (ret == 0)
3479 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003480 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003481}
3482
Eric Parisdd8dbf22009-11-03 16:35:32 +11003483static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003484{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003485 u32 sid;
3486 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003487 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003488
3489 sid = task_sid(current);
3490
3491 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003492 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003493 ad.u.kmod_name = kmod_name;
3494
3495 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3496 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003497}
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3500{
David Howells3b11a1d2008-11-14 10:39:26 +11003501 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504static int selinux_task_getpgid(struct task_struct *p)
3505{
David Howells3b11a1d2008-11-14 10:39:26 +11003506 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
3509static int selinux_task_getsid(struct task_struct *p)
3510{
David Howells3b11a1d2008-11-14 10:39:26 +11003511 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
David Quigleyf9008e42006-06-30 01:55:46 -07003514static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3515{
David Howells275bb412008-11-14 10:39:19 +11003516 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003517}
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519static int selinux_task_setnice(struct task_struct *p, int nice)
3520{
3521 int rc;
3522
Eric Paris200ac532009-02-12 15:01:04 -05003523 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 if (rc)
3525 return rc;
3526
David Howells3b11a1d2008-11-14 10:39:26 +11003527 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
James Morris03e68062006-06-23 02:03:58 -07003530static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3531{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003532 int rc;
3533
Eric Paris200ac532009-02-12 15:01:04 -05003534 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003535 if (rc)
3536 return rc;
3537
David Howells3b11a1d2008-11-14 10:39:26 +11003538 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003539}
3540
David Quigleya1836a42006-06-30 01:55:49 -07003541static int selinux_task_getioprio(struct task_struct *p)
3542{
David Howells3b11a1d2008-11-14 10:39:26 +11003543 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003544}
3545
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003546static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3547 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003549 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 /* Control the ability to change the hard limit (whether
3552 lowering or raising it), so that the hard limit can
3553 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003554 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003556 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 return 0;
3559}
3560
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003561static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003563 int rc;
3564
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003565 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003566 if (rc)
3567 return rc;
3568
David Howells3b11a1d2008-11-14 10:39:26 +11003569 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
3572static int selinux_task_getscheduler(struct task_struct *p)
3573{
David Howells3b11a1d2008-11-14 10:39:26 +11003574 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
David Quigley35601542006-06-23 02:04:01 -07003577static int selinux_task_movememory(struct task_struct *p)
3578{
David Howells3b11a1d2008-11-14 10:39:26 +11003579 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003580}
3581
David Quigleyf9008e42006-06-30 01:55:46 -07003582static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3583 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 u32 perm;
3586 int rc;
3587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (!sig)
3589 perm = PROCESS__SIGNULL; /* null signal; existence test */
3590 else
3591 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003592 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003593 rc = avc_has_perm(secid, task_sid(p),
3594 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003595 else
David Howells3b11a1d2008-11-14 10:39:26 +11003596 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003597 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600static int selinux_task_wait(struct task_struct *p)
3601{
Eric Paris8a535142007-10-22 16:10:31 -04003602 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605static void selinux_task_to_inode(struct task_struct *p,
3606 struct inode *inode)
3607{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003609 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
David Howells275bb412008-11-14 10:39:19 +11003611 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003616static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003617 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 int offset, ihlen, ret = -EINVAL;
3620 struct iphdr _iph, *ih;
3621
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003622 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3624 if (ih == NULL)
3625 goto out;
3626
3627 ihlen = ih->ihl * 4;
3628 if (ihlen < sizeof(_iph))
3629 goto out;
3630
Eric Paris48c62af2012-04-02 13:15:44 -04003631 ad->u.net->v4info.saddr = ih->saddr;
3632 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ret = 0;
3634
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003635 if (proto)
3636 *proto = ih->protocol;
3637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003639 case IPPROTO_TCP: {
3640 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Eric Paris828dfe12008-04-17 13:17:49 -04003642 if (ntohs(ih->frag_off) & IP_OFFSET)
3643 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 offset += ihlen;
3646 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3647 if (th == NULL)
3648 break;
3649
Eric Paris48c62af2012-04-02 13:15:44 -04003650 ad->u.net->sport = th->source;
3651 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Eric Paris828dfe12008-04-17 13:17:49 -04003655 case IPPROTO_UDP: {
3656 struct udphdr _udph, *uh;
3657
3658 if (ntohs(ih->frag_off) & IP_OFFSET)
3659 break;
3660
3661 offset += ihlen;
3662 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3663 if (uh == NULL)
3664 break;
3665
Eric Paris48c62af2012-04-02 13:15:44 -04003666 ad->u.net->sport = uh->source;
3667 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003668 break;
3669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
James Morris2ee92d42006-11-13 16:09:01 -08003671 case IPPROTO_DCCP: {
3672 struct dccp_hdr _dccph, *dh;
3673
3674 if (ntohs(ih->frag_off) & IP_OFFSET)
3675 break;
3676
3677 offset += ihlen;
3678 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3679 if (dh == NULL)
3680 break;
3681
Eric Paris48c62af2012-04-02 13:15:44 -04003682 ad->u.net->sport = dh->dccph_sport;
3683 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003684 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003685 }
James Morris2ee92d42006-11-13 16:09:01 -08003686
Eric Paris828dfe12008-04-17 13:17:49 -04003687 default:
3688 break;
3689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690out:
3691 return ret;
3692}
3693
3694#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3695
3696/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003697static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003698 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 u8 nexthdr;
3701 int ret = -EINVAL, offset;
3702 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003703 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003705 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3707 if (ip6 == NULL)
3708 goto out;
3709
Eric Paris48c62af2012-04-02 13:15:44 -04003710 ad->u.net->v6info.saddr = ip6->saddr;
3711 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 ret = 0;
3713
3714 nexthdr = ip6->nexthdr;
3715 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003716 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 if (offset < 0)
3718 goto out;
3719
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003720 if (proto)
3721 *proto = nexthdr;
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 switch (nexthdr) {
3724 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003725 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
3727 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3728 if (th == NULL)
3729 break;
3730
Eric Paris48c62af2012-04-02 13:15:44 -04003731 ad->u.net->sport = th->source;
3732 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 break;
3734 }
3735
3736 case IPPROTO_UDP: {
3737 struct udphdr _udph, *uh;
3738
3739 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3740 if (uh == NULL)
3741 break;
3742
Eric Paris48c62af2012-04-02 13:15:44 -04003743 ad->u.net->sport = uh->source;
3744 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 break;
3746 }
3747
James Morris2ee92d42006-11-13 16:09:01 -08003748 case IPPROTO_DCCP: {
3749 struct dccp_hdr _dccph, *dh;
3750
3751 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3752 if (dh == NULL)
3753 break;
3754
Eric Paris48c62af2012-04-02 13:15:44 -04003755 ad->u.net->sport = dh->dccph_sport;
3756 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003757 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003758 }
James Morris2ee92d42006-11-13 16:09:01 -08003759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 /* includes fragments */
3761 default:
3762 break;
3763 }
3764out:
3765 return ret;
3766}
3767
3768#endif /* IPV6 */
3769
Thomas Liu2bf49692009-07-14 12:14:09 -04003770static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003771 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
David Howellscf9481e2008-07-27 21:31:07 +10003773 char *addrp;
3774 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Eric Paris48c62af2012-04-02 13:15:44 -04003776 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003778 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003779 if (ret)
3780 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003781 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3782 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003783 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
3785#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3786 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003787 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003788 if (ret)
3789 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003790 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3791 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003792 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793#endif /* IPV6 */
3794 default:
David Howellscf9481e2008-07-27 21:31:07 +10003795 addrp = NULL;
3796 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
3798
David Howellscf9481e2008-07-27 21:31:07 +10003799parse_error:
3800 printk(KERN_WARNING
3801 "SELinux: failure in selinux_parse_skb(),"
3802 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003804
3805okay:
3806 if (_addrp)
3807 *_addrp = addrp;
3808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
Paul Moore4f6a9932007-03-01 14:35:22 -05003811/**
Paul Moore220deb92008-01-29 08:38:23 -05003812 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003813 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003814 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003815 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003816 *
3817 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003818 * Check the various different forms of network peer labeling and determine
3819 * the peer label/SID for the packet; most of the magic actually occurs in
3820 * the security server function security_net_peersid_cmp(). The function
3821 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3822 * or -EACCES if @sid is invalid due to inconsistencies with the different
3823 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003824 *
3825 */
Paul Moore220deb92008-01-29 08:38:23 -05003826static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003827{
Paul Moore71f1cb02008-01-29 08:51:16 -05003828 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003829 u32 xfrm_sid;
3830 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003831 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003832
Paul Moore73ec9552013-12-10 14:57:54 -05003833 selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003834 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003835
Paul Moore71f1cb02008-01-29 08:51:16 -05003836 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3837 if (unlikely(err)) {
3838 printk(KERN_WARNING
3839 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3840 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003841 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003842 }
Paul Moore220deb92008-01-29 08:38:23 -05003843
3844 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003845}
3846
Paul Moore2ea04e52013-12-04 16:10:51 -05003847/**
3848 * selinux_conn_sid - Determine the child socket label for a connection
3849 * @sk_sid: the parent socket's SID
3850 * @skb_sid: the packet's SID
3851 * @conn_sid: the resulting connection SID
3852 *
3853 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3854 * combined with the MLS information from @skb_sid in order to create
3855 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3856 * of @sk_sid. Returns zero on success, negative values on failure.
3857 *
3858 */
3859static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3860{
3861 int err = 0;
3862
3863 if (skb_sid != SECSID_NULL)
3864 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3865 else
3866 *conn_sid = sk_sid;
3867
3868 return err;
3869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003872
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003873static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3874 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003875{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003876 if (tsec->sockcreate_sid > SECSID_NULL) {
3877 *socksid = tsec->sockcreate_sid;
3878 return 0;
3879 }
3880
3881 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3882 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003883}
3884
Paul Moore253bfae2010-04-22 14:46:19 -04003885static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Paul Moore253bfae2010-04-22 14:46:19 -04003887 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003888 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003889 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003890 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003891 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Paul Moore253bfae2010-04-22 14:46:19 -04003893 if (sksec->sid == SECINITSID_KERNEL)
3894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Thomas Liu2bf49692009-07-14 12:14:09 -04003896 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003897 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003898 ad.u.net = &net;
3899 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Paul Moore253bfae2010-04-22 14:46:19 -04003901 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
3904static int selinux_socket_create(int family, int type,
3905 int protocol, int kern)
3906{
Paul Moore5fb49872010-04-22 14:46:19 -04003907 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003908 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003909 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003910 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
3912 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
David Howells275bb412008-11-14 10:39:19 +11003915 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003916 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3917 if (rc)
3918 return rc;
3919
Paul Moored4f2d972010-04-22 14:46:18 -04003920 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921}
3922
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003923static int selinux_socket_post_create(struct socket *sock, int family,
3924 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Paul Moore5fb49872010-04-22 14:46:19 -04003926 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003927 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003928 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003929 int err = 0;
3930
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003931 isec->sclass = socket_type_to_security_class(family, type, protocol);
3932
David Howells275bb412008-11-14 10:39:19 +11003933 if (kern)
3934 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003935 else {
3936 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3937 if (err)
3938 return err;
3939 }
David Howells275bb412008-11-14 10:39:19 +11003940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 isec->initialized = 1;
3942
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003943 if (sock->sk) {
3944 sksec = sock->sk->sk_security;
3945 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003946 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003947 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003948 }
3949
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951}
3952
3953/* Range of port numbers used to automatically bind.
3954 Need to determine whether we should perform a name_bind
3955 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
3957static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3958{
Paul Moore253bfae2010-04-22 14:46:19 -04003959 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 u16 family;
3961 int err;
3962
Paul Moore253bfae2010-04-22 14:46:19 -04003963 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 if (err)
3965 goto out;
3966
3967 /*
3968 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003969 * Multiple address binding for SCTP is not supported yet: we just
3970 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 */
Paul Moore253bfae2010-04-22 14:46:19 -04003972 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (family == PF_INET || family == PF_INET6) {
3974 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003975 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003976 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003977 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003978 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 struct sockaddr_in *addr4 = NULL;
3980 struct sockaddr_in6 *addr6 = NULL;
3981 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003982 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 if (family == PF_INET) {
3985 addr4 = (struct sockaddr_in *)address;
3986 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 addrp = (char *)&addr4->sin_addr.s_addr;
3988 } else {
3989 addr6 = (struct sockaddr_in6 *)address;
3990 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 addrp = (char *)&addr6->sin6_addr.s6_addr;
3992 }
3993
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003994 if (snum) {
3995 int low, high;
3996
3997 inet_get_local_port_range(&low, &high);
3998
3999 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004000 err = sel_netport_sid(sk->sk_protocol,
4001 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004002 if (err)
4003 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04004004 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004005 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004006 ad.u.net = &net;
4007 ad.u.net->sport = htons(snum);
4008 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004009 err = avc_has_perm(sksec->sid, sid,
4010 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004011 SOCKET__NAME_BIND, &ad);
4012 if (err)
4013 goto out;
4014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
Eric Paris828dfe12008-04-17 13:17:49 -04004016
Paul Moore253bfae2010-04-22 14:46:19 -04004017 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004018 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 node_perm = TCP_SOCKET__NODE_BIND;
4020 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004021
James Morris13402582005-09-30 14:24:34 -04004022 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 node_perm = UDP_SOCKET__NODE_BIND;
4024 break;
James Morris2ee92d42006-11-13 16:09:01 -08004025
4026 case SECCLASS_DCCP_SOCKET:
4027 node_perm = DCCP_SOCKET__NODE_BIND;
4028 break;
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 default:
4031 node_perm = RAWIP_SOCKET__NODE_BIND;
4032 break;
4033 }
Eric Paris828dfe12008-04-17 13:17:49 -04004034
Paul Moore224dfbd2008-01-29 08:38:13 -05004035 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (err)
4037 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004038
Thomas Liu2bf49692009-07-14 12:14:09 -04004039 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004040 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004041 ad.u.net = &net;
4042 ad.u.net->sport = htons(snum);
4043 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004046 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 else
Eric Paris48c62af2012-04-02 13:15:44 -04004048 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Paul Moore253bfae2010-04-22 14:46:19 -04004050 err = avc_has_perm(sksec->sid, sid,
4051 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (err)
4053 goto out;
4054 }
4055out:
4056 return err;
4057}
4058
4059static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4060{
Paul Moore014ab192008-10-10 10:16:33 -04004061 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004062 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 int err;
4064
Paul Moore253bfae2010-04-22 14:46:19 -04004065 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 if (err)
4067 return err;
4068
4069 /*
James Morris2ee92d42006-11-13 16:09:01 -08004070 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 */
Paul Moore253bfae2010-04-22 14:46:19 -04004072 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4073 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004074 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004075 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004076 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 struct sockaddr_in *addr4 = NULL;
4078 struct sockaddr_in6 *addr6 = NULL;
4079 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004080 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
4082 if (sk->sk_family == PF_INET) {
4083 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004084 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return -EINVAL;
4086 snum = ntohs(addr4->sin_port);
4087 } else {
4088 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004089 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 return -EINVAL;
4091 snum = ntohs(addr6->sin6_port);
4092 }
4093
Paul Moore3e112172008-04-10 10:48:14 -04004094 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (err)
4096 goto out;
4097
Paul Moore253bfae2010-04-22 14:46:19 -04004098 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004099 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4100
Thomas Liu2bf49692009-07-14 12:14:09 -04004101 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004102 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004103 ad.u.net = &net;
4104 ad.u.net->dport = htons(snum);
4105 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004106 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 if (err)
4108 goto out;
4109 }
4110
Paul Moore014ab192008-10-10 10:16:33 -04004111 err = selinux_netlbl_socket_connect(sk, address);
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113out:
4114 return err;
4115}
4116
4117static int selinux_socket_listen(struct socket *sock, int backlog)
4118{
Paul Moore253bfae2010-04-22 14:46:19 -04004119 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
4122static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4123{
4124 int err;
4125 struct inode_security_struct *isec;
4126 struct inode_security_struct *newisec;
4127
Paul Moore253bfae2010-04-22 14:46:19 -04004128 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (err)
4130 return err;
4131
4132 newisec = SOCK_INODE(newsock)->i_security;
4133
4134 isec = SOCK_INODE(sock)->i_security;
4135 newisec->sclass = isec->sclass;
4136 newisec->sid = isec->sid;
4137 newisec->initialized = 1;
4138
4139 return 0;
4140}
4141
4142static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004143 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Paul Moore253bfae2010-04-22 14:46:19 -04004145 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
4148static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4149 int size, int flags)
4150{
Paul Moore253bfae2010-04-22 14:46:19 -04004151 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
4153
4154static int selinux_socket_getsockname(struct socket *sock)
4155{
Paul Moore253bfae2010-04-22 14:46:19 -04004156 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
4159static int selinux_socket_getpeername(struct socket *sock)
4160{
Paul Moore253bfae2010-04-22 14:46:19 -04004161 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162}
4163
Eric Paris828dfe12008-04-17 13:17:49 -04004164static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Paul Mooref8687af2006-10-30 15:22:15 -08004166 int err;
4167
Paul Moore253bfae2010-04-22 14:46:19 -04004168 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004169 if (err)
4170 return err;
4171
4172 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
4175static int selinux_socket_getsockopt(struct socket *sock, int level,
4176 int optname)
4177{
Paul Moore253bfae2010-04-22 14:46:19 -04004178 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179}
4180
4181static int selinux_socket_shutdown(struct socket *sock, int how)
4182{
Paul Moore253bfae2010-04-22 14:46:19 -04004183 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
David S. Miller3610cda2011-01-05 15:38:53 -08004186static int selinux_socket_unix_stream_connect(struct sock *sock,
4187 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 struct sock *newsk)
4189{
David S. Miller3610cda2011-01-05 15:38:53 -08004190 struct sk_security_struct *sksec_sock = sock->sk_security;
4191 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004192 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004193 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004194 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004195 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 int err;
4197
Thomas Liu2bf49692009-07-14 12:14:09 -04004198 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004199 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004200 ad.u.net = &net;
4201 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Paul Moore4d1e2452010-04-22 14:46:18 -04004203 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4204 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4206 if (err)
4207 return err;
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004210 sksec_new->peer_sid = sksec_sock->sid;
4211 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4212 &sksec_new->sid);
4213 if (err)
4214 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004215
Paul Moore4d1e2452010-04-22 14:46:18 -04004216 /* connecting socket */
4217 sksec_sock->peer_sid = sksec_new->sid;
4218
4219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
4222static int selinux_socket_unix_may_send(struct socket *sock,
4223 struct socket *other)
4224{
Paul Moore253bfae2010-04-22 14:46:19 -04004225 struct sk_security_struct *ssec = sock->sk->sk_security;
4226 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004227 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004228 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004229 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Thomas Liu2bf49692009-07-14 12:14:09 -04004231 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004232 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004233 ad.u.net = &net;
4234 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Paul Moore253bfae2010-04-22 14:46:19 -04004236 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4237 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Paul Mooreeffad8d2008-01-29 08:49:27 -05004240static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4241 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004242 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004243{
4244 int err;
4245 u32 if_sid;
4246 u32 node_sid;
4247
4248 err = sel_netif_sid(ifindex, &if_sid);
4249 if (err)
4250 return err;
4251 err = avc_has_perm(peer_sid, if_sid,
4252 SECCLASS_NETIF, NETIF__INGRESS, ad);
4253 if (err)
4254 return err;
4255
4256 err = sel_netnode_sid(addrp, family, &node_sid);
4257 if (err)
4258 return err;
4259 return avc_has_perm(peer_sid, node_sid,
4260 SECCLASS_NODE, NODE__RECVFROM, ad);
4261}
4262
Paul Moore220deb92008-01-29 08:38:23 -05004263static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004264 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004265{
Paul Moore277d3422008-12-31 12:54:11 -05004266 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004267 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004268 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004269 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004270 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004271 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004272 char *addrp;
4273
Thomas Liu2bf49692009-07-14 12:14:09 -04004274 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004275 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004276 ad.u.net = &net;
4277 ad.u.net->netif = skb->skb_iif;
4278 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004279 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4280 if (err)
4281 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004282
Paul Moore58bfbb52009-03-27 17:10:41 -04004283 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004284 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004285 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004286 if (err)
4287 return err;
4288 }
Paul Moore220deb92008-01-29 08:38:23 -05004289
Steffen Klassertb9679a72011-02-23 12:55:21 +01004290 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4291 if (err)
4292 return err;
4293 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004294
James Morris4e5ab4c2006-06-09 00:33:33 -07004295 return err;
4296}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004297
James Morris4e5ab4c2006-06-09 00:33:33 -07004298static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4299{
Paul Moore220deb92008-01-29 08:38:23 -05004300 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004301 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004302 u16 family = sk->sk_family;
4303 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004304 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004305 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004306 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004307 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004308 u8 secmark_active;
4309 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004310
James Morris4e5ab4c2006-06-09 00:33:33 -07004311 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004312 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004313
4314 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004315 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004316 family = PF_INET;
4317
Paul Moored8395c82008-10-10 10:16:30 -04004318 /* If any sort of compatibility mode is enabled then handoff processing
4319 * to the selinux_sock_rcv_skb_compat() function to deal with the
4320 * special handling. We do this in an attempt to keep this function
4321 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004322 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004323 return selinux_sock_rcv_skb_compat(sk, skb, family);
4324
4325 secmark_active = selinux_secmark_enabled();
4326 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4327 if (!secmark_active && !peerlbl_active)
4328 return 0;
4329
Thomas Liu2bf49692009-07-14 12:14:09 -04004330 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004331 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004332 ad.u.net = &net;
4333 ad.u.net->netif = skb->skb_iif;
4334 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004335 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004336 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004337 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004338
Paul Moored8395c82008-10-10 10:16:30 -04004339 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004340 u32 peer_sid;
4341
4342 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4343 if (err)
4344 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004345 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004346 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004347 if (err) {
4348 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004349 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004350 }
Paul Moored621d352008-01-29 08:43:36 -05004351 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4352 PEER__RECV, &ad);
Chad Hanson351381d2013-12-23 17:45:01 -05004353 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004354 selinux_netlbl_err(skb, err, 0);
Chad Hanson351381d2013-12-23 17:45:01 -05004355 return err;
4356 }
Paul Moored621d352008-01-29 08:43:36 -05004357 }
4358
Paul Moored8395c82008-10-10 10:16:30 -04004359 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004360 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4361 PACKET__RECV, &ad);
4362 if (err)
4363 return err;
4364 }
4365
Paul Moored621d352008-01-29 08:43:36 -05004366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367}
4368
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004369static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4370 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
4372 int err = 0;
4373 char *scontext;
4374 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004375 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004376 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Paul Moore253bfae2010-04-22 14:46:19 -04004378 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4379 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004380 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004381 if (peer_sid == SECSID_NULL)
4382 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004384 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 if (scontext_len > len) {
4389 err = -ERANGE;
4390 goto out_len;
4391 }
4392
4393 if (copy_to_user(optval, scontext, scontext_len))
4394 err = -EFAULT;
4395
4396out_len:
4397 if (put_user(scontext_len, optlen))
4398 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return err;
4401}
4402
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004403static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004404{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004405 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004406 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004407
Paul Mooreaa862902008-10-10 10:16:29 -04004408 if (skb && skb->protocol == htons(ETH_P_IP))
4409 family = PF_INET;
4410 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4411 family = PF_INET6;
4412 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004413 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004414 else
4415 goto out;
4416
4417 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004418 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004419 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004420 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004421
Paul Moore75e22912008-01-29 08:38:04 -05004422out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004423 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004424 if (peer_secid == SECSID_NULL)
4425 return -EINVAL;
4426 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004427}
4428
Al Viro7d877f32005-10-21 03:20:43 -04004429static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Paul Moore84914b72010-04-22 14:46:18 -04004431 struct sk_security_struct *sksec;
4432
4433 sksec = kzalloc(sizeof(*sksec), priority);
4434 if (!sksec)
4435 return -ENOMEM;
4436
4437 sksec->peer_sid = SECINITSID_UNLABELED;
4438 sksec->sid = SECINITSID_UNLABELED;
4439 selinux_netlbl_sk_security_reset(sksec);
4440 sk->sk_security = sksec;
4441
4442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
4445static void selinux_sk_free_security(struct sock *sk)
4446{
Paul Moore84914b72010-04-22 14:46:18 -04004447 struct sk_security_struct *sksec = sk->sk_security;
4448
4449 sk->sk_security = NULL;
4450 selinux_netlbl_sk_security_free(sksec);
4451 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004454static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4455{
Eric Parisdd3e7832010-04-07 15:08:46 -04004456 struct sk_security_struct *sksec = sk->sk_security;
4457 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004458
Eric Parisdd3e7832010-04-07 15:08:46 -04004459 newsksec->sid = sksec->sid;
4460 newsksec->peer_sid = sksec->peer_sid;
4461 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004462
Eric Parisdd3e7832010-04-07 15:08:46 -04004463 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004464}
4465
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004466static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004467{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004468 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004469 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004470 else {
4471 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004472
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004473 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004474 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004475}
4476
Eric Paris828dfe12008-04-17 13:17:49 -04004477static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004478{
4479 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4480 struct sk_security_struct *sksec = sk->sk_security;
4481
David Woodhouse2148ccc2006-09-29 15:50:25 -07004482 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4483 sk->sk_family == PF_UNIX)
4484 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004485 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004486}
4487
Adrian Bunk9a673e52006-08-15 00:03:53 -07004488static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4489 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004490{
4491 struct sk_security_struct *sksec = sk->sk_security;
4492 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004493 u16 family = sk->sk_family;
Paul Moore2ea04e52013-12-04 16:10:51 -05004494 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004495 u32 peersid;
4496
Paul Mooreaa862902008-10-10 10:16:29 -04004497 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4498 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4499 family = PF_INET;
4500
4501 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004502 if (err)
4503 return err;
Paul Moore2ea04e52013-12-04 16:10:51 -05004504 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4505 if (err)
4506 return err;
4507 req->secid = connsid;
4508 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004509
Paul Moore389fb802009-03-27 17:10:34 -04004510 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004511}
4512
Adrian Bunk9a673e52006-08-15 00:03:53 -07004513static void selinux_inet_csk_clone(struct sock *newsk,
4514 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004515{
4516 struct sk_security_struct *newsksec = newsk->sk_security;
4517
4518 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004519 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004520 /* NOTE: Ideally, we should also get the isec->sid for the
4521 new socket in sync, but we don't have the isec available yet.
4522 So we will wait until sock_graft to do it, by which
4523 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004524
Paul Moore9f2ad662006-11-17 17:38:53 -05004525 /* We don't need to take any sort of lock here as we are the only
4526 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004527 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004528}
4529
Paul Moore014ab192008-10-10 10:16:33 -04004530static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004531{
Paul Mooreaa862902008-10-10 10:16:29 -04004532 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004533 struct sk_security_struct *sksec = sk->sk_security;
4534
Paul Mooreaa862902008-10-10 10:16:29 -04004535 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4536 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4537 family = PF_INET;
4538
4539 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004540}
4541
Eric Paris2606fd12010-10-13 16:24:41 -04004542static int selinux_secmark_relabel_packet(u32 sid)
4543{
4544 const struct task_security_struct *__tsec;
4545 u32 tsid;
4546
4547 __tsec = current_security();
4548 tsid = __tsec->sid;
4549
4550 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4551}
4552
4553static void selinux_secmark_refcount_inc(void)
4554{
4555 atomic_inc(&selinux_secmark_refcount);
4556}
4557
4558static void selinux_secmark_refcount_dec(void)
4559{
4560 atomic_dec(&selinux_secmark_refcount);
4561}
4562
Adrian Bunk9a673e52006-08-15 00:03:53 -07004563static void selinux_req_classify_flow(const struct request_sock *req,
4564 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004565{
David S. Miller1d28f422011-03-12 00:29:39 -05004566 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004567}
4568
Paul Mooreed6d76e2009-08-28 18:12:49 -04004569static int selinux_tun_dev_create(void)
4570{
4571 u32 sid = current_sid();
4572
4573 /* we aren't taking into account the "sockcreate" SID since the socket
4574 * that is being created here is not a socket in the traditional sense,
4575 * instead it is a private sock, accessible only to the kernel, and
4576 * representing a wide range of network traffic spanning multiple
4577 * connections unlike traditional sockets - check the TUN driver to
4578 * get a better understanding of why this socket is special */
4579
4580 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4581 NULL);
4582}
4583
4584static void selinux_tun_dev_post_create(struct sock *sk)
4585{
4586 struct sk_security_struct *sksec = sk->sk_security;
4587
4588 /* we don't currently perform any NetLabel based labeling here and it
4589 * isn't clear that we would want to do so anyway; while we could apply
4590 * labeling without the support of the TUN user the resulting labeled
4591 * traffic from the other end of the connection would almost certainly
4592 * cause confusion to the TUN user that had no idea network labeling
4593 * protocols were being used */
4594
4595 /* see the comments in selinux_tun_dev_create() about why we don't use
4596 * the sockcreate SID here */
4597
4598 sksec->sid = current_sid();
4599 sksec->sclass = SECCLASS_TUN_SOCKET;
4600}
4601
4602static int selinux_tun_dev_attach(struct sock *sk)
4603{
4604 struct sk_security_struct *sksec = sk->sk_security;
4605 u32 sid = current_sid();
4606 int err;
4607
4608 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4609 TUN_SOCKET__RELABELFROM, NULL);
4610 if (err)
4611 return err;
4612 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4613 TUN_SOCKET__RELABELTO, NULL);
4614 if (err)
4615 return err;
4616
4617 sksec->sid = sid;
4618
4619 return 0;
4620}
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4623{
4624 int err = 0;
4625 u32 perm;
4626 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004627 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 if (skb->len < NLMSG_SPACE(0)) {
4630 err = -EINVAL;
4631 goto out;
4632 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004633 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004634
Paul Moore253bfae2010-04-22 14:46:19 -04004635 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (err) {
4637 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004638 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 "SELinux: unrecognized netlink message"
4640 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004641 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004642 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 err = 0;
4644 }
4645
4646 /* Ignore */
4647 if (err == -ENOENT)
4648 err = 0;
4649 goto out;
4650 }
4651
Paul Moore253bfae2010-04-22 14:46:19 -04004652 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653out:
4654 return err;
4655}
4656
4657#ifdef CONFIG_NETFILTER
4658
Paul Mooreeffad8d2008-01-29 08:49:27 -05004659static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4660 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
Paul Mooredfaebe92008-10-10 10:16:31 -04004662 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004663 char *addrp;
4664 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004665 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004666 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004667 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004668 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004669 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004670 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004671
Paul Mooreeffad8d2008-01-29 08:49:27 -05004672 if (!selinux_policycap_netpeer)
4673 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004674
Paul Mooreeffad8d2008-01-29 08:49:27 -05004675 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004676 netlbl_active = netlbl_enabled();
4677 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004678 if (!secmark_active && !peerlbl_active)
4679 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004680
Paul Moored8395c82008-10-10 10:16:30 -04004681 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4682 return NF_DROP;
4683
Thomas Liu2bf49692009-07-14 12:14:09 -04004684 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004685 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004686 ad.u.net = &net;
4687 ad.u.net->netif = ifindex;
4688 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004689 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4690 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Paul Mooredfaebe92008-10-10 10:16:31 -04004692 if (peerlbl_active) {
4693 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4694 peer_sid, &ad);
4695 if (err) {
4696 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004697 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004698 }
4699 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004700
4701 if (secmark_active)
4702 if (avc_has_perm(peer_sid, skb->secmark,
4703 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4704 return NF_DROP;
4705
Paul Moore948bf852008-10-10 10:16:32 -04004706 if (netlbl_active)
4707 /* we do this in the FORWARD path and not the POST_ROUTING
4708 * path because we want to make sure we apply the necessary
4709 * labeling before IPsec is applied so we can leverage AH
4710 * protection */
4711 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4712 return NF_DROP;
4713
Paul Mooreeffad8d2008-01-29 08:49:27 -05004714 return NF_ACCEPT;
4715}
4716
4717static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4718 struct sk_buff *skb,
4719 const struct net_device *in,
4720 const struct net_device *out,
4721 int (*okfn)(struct sk_buff *))
4722{
4723 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4724}
4725
4726#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4727static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4728 struct sk_buff *skb,
4729 const struct net_device *in,
4730 const struct net_device *out,
4731 int (*okfn)(struct sk_buff *))
4732{
4733 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4734}
4735#endif /* IPV6 */
4736
Paul Moore948bf852008-10-10 10:16:32 -04004737static unsigned int selinux_ip_output(struct sk_buff *skb,
4738 u16 family)
4739{
Paul Moore1c5d9d12013-12-04 16:10:45 -05004740 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004741 u32 sid;
4742
4743 if (!netlbl_enabled())
4744 return NF_ACCEPT;
4745
4746 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4747 * because we want to make sure we apply the necessary labeling
4748 * before IPsec is applied so we can leverage AH protection */
Paul Moore1c5d9d12013-12-04 16:10:45 -05004749 sk = skb->sk;
4750 if (sk) {
4751 struct sk_security_struct *sksec;
4752
4753 if (sk->sk_state == TCP_LISTEN)
4754 /* if the socket is the listening state then this
4755 * packet is a SYN-ACK packet which means it needs to
4756 * be labeled based on the connection/request_sock and
4757 * not the parent socket. unfortunately, we can't
4758 * lookup the request_sock yet as it isn't queued on
4759 * the parent socket until after the SYN-ACK is sent.
4760 * the "solution" is to simply pass the packet as-is
4761 * as any IP option based labeling should be copied
4762 * from the initial connection request (in the IP
4763 * layer). it is far from ideal, but until we get a
4764 * security label in the packet itself this is the
4765 * best we can do. */
4766 return NF_ACCEPT;
4767
4768 /* standard practice, label using the parent socket */
4769 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004770 sid = sksec->sid;
4771 } else
4772 sid = SECINITSID_KERNEL;
4773 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4774 return NF_DROP;
4775
4776 return NF_ACCEPT;
4777}
4778
4779static unsigned int selinux_ipv4_output(unsigned int hooknum,
4780 struct sk_buff *skb,
4781 const struct net_device *in,
4782 const struct net_device *out,
4783 int (*okfn)(struct sk_buff *))
4784{
4785 return selinux_ip_output(skb, PF_INET);
4786}
4787
Paul Mooreeffad8d2008-01-29 08:49:27 -05004788static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4789 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004790 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004791{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004792 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004793 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004794 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004795 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004796 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004797 char *addrp;
4798 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004799
Paul Mooreeffad8d2008-01-29 08:49:27 -05004800 if (sk == NULL)
4801 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004802 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004803
Thomas Liu2bf49692009-07-14 12:14:09 -04004804 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004805 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004806 ad.u.net = &net;
4807 ad.u.net->netif = ifindex;
4808 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004809 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4810 return NF_DROP;
4811
Paul Moore58bfbb52009-03-27 17:10:41 -04004812 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004813 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004814 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004815 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004816
Steffen Klassertb9679a72011-02-23 12:55:21 +01004817 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4818 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004819
Paul Mooreeffad8d2008-01-29 08:49:27 -05004820 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Paul Mooreeffad8d2008-01-29 08:49:27 -05004823static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4824 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004826 u32 secmark_perm;
4827 u32 peer_sid;
4828 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004829 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004830 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004831 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004832 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004833 u8 secmark_active;
4834 u8 peerlbl_active;
4835
Paul Mooreeffad8d2008-01-29 08:49:27 -05004836 /* If any sort of compatibility mode is enabled then handoff processing
4837 * to the selinux_ip_postroute_compat() function to deal with the
4838 * special handling. We do this in an attempt to keep this function
4839 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004840 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004841 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moore420cc6d2013-12-10 14:58:01 -05004842
Paul Mooreeffad8d2008-01-29 08:49:27 -05004843 secmark_active = selinux_secmark_enabled();
4844 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4845 if (!secmark_active && !peerlbl_active)
4846 return NF_ACCEPT;
4847
Paul Mooreeffad8d2008-01-29 08:49:27 -05004848 sk = skb->sk;
Paul Moore420cc6d2013-12-10 14:58:01 -05004849
4850#ifdef CONFIG_XFRM
4851 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4852 * packet transformation so allow the packet to pass without any checks
4853 * since we'll have another chance to perform access control checks
4854 * when the packet is on it's final way out.
4855 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4856 * is NULL, in this case go ahead and apply access control.
4857 * is NULL, in this case go ahead and apply access control.
4858 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4859 * TCP listening state we cannot wait until the XFRM processing
4860 * is done as we will miss out on the SA label if we do;
4861 * unfortunately, this means more work, but it is only once per
4862 * connection. */
4863 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4864 !(sk != NULL && sk->sk_state == TCP_LISTEN))
4865 return NF_ACCEPT;
4866#endif
4867
Paul Moored8395c82008-10-10 10:16:30 -04004868 if (sk == NULL) {
Paul Moore2ea04e52013-12-04 16:10:51 -05004869 /* Without an associated socket the packet is either coming
4870 * from the kernel or it is being forwarded; check the packet
4871 * to determine which and if the packet is being forwarded
4872 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004873 if (skb->skb_iif) {
4874 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004875 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004876 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004877 } else {
4878 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004879 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004880 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004881 } else if (sk->sk_state == TCP_LISTEN) {
4882 /* Locally generated packet but the associated socket is in the
4883 * listening state which means this is a SYN-ACK packet. In
4884 * this particular case the correct security label is assigned
4885 * to the connection/request_sock but unfortunately we can't
4886 * query the request_sock as it isn't queued on the parent
4887 * socket until after the SYN-ACK packet is sent; the only
4888 * viable choice is to regenerate the label like we do in
4889 * selinux_inet_conn_request(). See also selinux_ip_output()
4890 * for similar problems. */
4891 u32 skb_sid;
4892 struct sk_security_struct *sksec = sk->sk_security;
4893 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4894 return NF_DROP;
Paul Moore420cc6d2013-12-10 14:58:01 -05004895 /* At this point, if the returned skb peerlbl is SECSID_NULL
4896 * and the packet has been through at least one XFRM
4897 * transformation then we must be dealing with the "final"
4898 * form of labeled IPsec packet; since we've already applied
4899 * all of our access controls on this packet we can safely
4900 * pass the packet. */
4901 if (skb_sid == SECSID_NULL) {
4902 switch (family) {
4903 case PF_INET:
4904 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4905 return NF_ACCEPT;
4906 break;
4907 case PF_INET6:
4908 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4909 return NF_ACCEPT;
4910 default:
4911 return NF_DROP_ERR(-ECONNREFUSED);
4912 }
4913 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004914 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4915 return NF_DROP;
4916 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004917 } else {
Paul Moore2ea04e52013-12-04 16:10:51 -05004918 /* Locally generated packet, fetch the security label from the
4919 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004920 struct sk_security_struct *sksec = sk->sk_security;
4921 peer_sid = sksec->sid;
4922 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004923 }
4924
Thomas Liu2bf49692009-07-14 12:14:09 -04004925 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004926 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004927 ad.u.net = &net;
4928 ad.u.net->netif = ifindex;
4929 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004930 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004931 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004932
Paul Mooreeffad8d2008-01-29 08:49:27 -05004933 if (secmark_active)
4934 if (avc_has_perm(peer_sid, skb->secmark,
4935 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004936 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004937
4938 if (peerlbl_active) {
4939 u32 if_sid;
4940 u32 node_sid;
4941
4942 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004943 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004944 if (avc_has_perm(peer_sid, if_sid,
4945 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004946 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004947
4948 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004949 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004950 if (avc_has_perm(peer_sid, node_sid,
4951 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004952 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004953 }
4954
4955 return NF_ACCEPT;
4956}
4957
4958static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4959 struct sk_buff *skb,
4960 const struct net_device *in,
4961 const struct net_device *out,
4962 int (*okfn)(struct sk_buff *))
4963{
4964 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
4967#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004968static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4969 struct sk_buff *skb,
4970 const struct net_device *in,
4971 const struct net_device *out,
4972 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004974 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976#endif /* IPV6 */
4977
4978#endif /* CONFIG_NETFILTER */
4979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 int err;
4983
Eric Paris200ac532009-02-12 15:01:04 -05004984 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 if (err)
4986 return err;
4987
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004988 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989}
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991static int ipc_alloc_security(struct task_struct *task,
4992 struct kern_ipc_perm *perm,
4993 u16 sclass)
4994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004996 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
James Morris89d155e2005-10-30 14:59:21 -08004998 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 if (!isec)
5000 return -ENOMEM;
5001
David Howells275bb412008-11-14 10:39:19 +11005002 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11005004 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 perm->security = isec;
5006
5007 return 0;
5008}
5009
5010static void ipc_free_security(struct kern_ipc_perm *perm)
5011{
5012 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 perm->security = NULL;
5014 kfree(isec);
5015}
5016
5017static int msg_msg_alloc_security(struct msg_msg *msg)
5018{
5019 struct msg_security_struct *msec;
5020
James Morris89d155e2005-10-30 14:59:21 -08005021 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 if (!msec)
5023 return -ENOMEM;
5024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 msec->sid = SECINITSID_UNLABELED;
5026 msg->security = msec;
5027
5028 return 0;
5029}
5030
5031static void msg_msg_free_security(struct msg_msg *msg)
5032{
5033 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
5035 msg->security = NULL;
5036 kfree(msec);
5037}
5038
5039static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005040 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005043 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005044 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005045 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 isec = ipc_perms->security;
5048
Thomas Liu2bf49692009-07-14 12:14:09 -04005049 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005050 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 ad.u.ipc_id = ipc_perms->key;
5052
David Howells275bb412008-11-14 10:39:19 +11005053 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
5056static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5057{
5058 return msg_msg_alloc_security(msg);
5059}
5060
5061static void selinux_msg_msg_free_security(struct msg_msg *msg)
5062{
5063 msg_msg_free_security(msg);
5064}
5065
5066/* message queue security operations */
5067static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005070 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005071 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005072 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 int rc;
5074
5075 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5076 if (rc)
5077 return rc;
5078
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 isec = msq->q_perm.security;
5080
Thomas Liu2bf49692009-07-14 12:14:09 -04005081 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005082 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005083 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
David Howells275bb412008-11-14 10:39:19 +11005085 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 MSGQ__CREATE, &ad);
5087 if (rc) {
5088 ipc_free_security(&msq->q_perm);
5089 return rc;
5090 }
5091 return 0;
5092}
5093
5094static void selinux_msg_queue_free_security(struct msg_queue *msq)
5095{
5096 ipc_free_security(&msq->q_perm);
5097}
5098
5099static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005102 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005103 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005104 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 isec = msq->q_perm.security;
5107
Thomas Liu2bf49692009-07-14 12:14:09 -04005108 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005109 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 ad.u.ipc_id = msq->q_perm.key;
5111
David Howells275bb412008-11-14 10:39:19 +11005112 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 MSGQ__ASSOCIATE, &ad);
5114}
5115
5116static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5117{
5118 int err;
5119 int perms;
5120
Eric Paris828dfe12008-04-17 13:17:49 -04005121 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 case IPC_INFO:
5123 case MSG_INFO:
5124 /* No specific object, just general system-wide information. */
5125 return task_has_system(current, SYSTEM__IPC_INFO);
5126 case IPC_STAT:
5127 case MSG_STAT:
5128 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5129 break;
5130 case IPC_SET:
5131 perms = MSGQ__SETATTR;
5132 break;
5133 case IPC_RMID:
5134 perms = MSGQ__DESTROY;
5135 break;
5136 default:
5137 return 0;
5138 }
5139
Stephen Smalley6af963f2005-05-01 08:58:39 -07005140 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 return err;
5142}
5143
5144static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 struct ipc_security_struct *isec;
5147 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005148 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005149 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005150 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 int rc;
5152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 isec = msq->q_perm.security;
5154 msec = msg->security;
5155
5156 /*
5157 * First time through, need to assign label to the message
5158 */
5159 if (msec->sid == SECINITSID_UNLABELED) {
5160 /*
5161 * Compute new sid based on current process and
5162 * message queue this message will be stored in
5163 */
David Howells275bb412008-11-14 10:39:19 +11005164 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005165 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (rc)
5167 return rc;
5168 }
5169
Thomas Liu2bf49692009-07-14 12:14:09 -04005170 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005171 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 ad.u.ipc_id = msq->q_perm.key;
5173
5174 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005175 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 MSGQ__WRITE, &ad);
5177 if (!rc)
5178 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005179 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5180 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 if (!rc)
5182 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005183 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5184 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
5186 return rc;
5187}
5188
5189static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5190 struct task_struct *target,
5191 long type, int mode)
5192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 struct ipc_security_struct *isec;
5194 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005195 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005196 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005197 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 int rc;
5199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 isec = msq->q_perm.security;
5201 msec = msg->security;
5202
Thomas Liu2bf49692009-07-14 12:14:09 -04005203 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005204 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005205 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
David Howells275bb412008-11-14 10:39:19 +11005207 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 SECCLASS_MSGQ, MSGQ__READ, &ad);
5209 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005210 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 SECCLASS_MSG, MSG__RECEIVE, &ad);
5212 return rc;
5213}
5214
5215/* Shared Memory security operations */
5216static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005219 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005220 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005221 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 int rc;
5223
5224 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5225 if (rc)
5226 return rc;
5227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 isec = shp->shm_perm.security;
5229
Thomas Liu2bf49692009-07-14 12:14:09 -04005230 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005231 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005232 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
David Howells275bb412008-11-14 10:39:19 +11005234 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 SHM__CREATE, &ad);
5236 if (rc) {
5237 ipc_free_security(&shp->shm_perm);
5238 return rc;
5239 }
5240 return 0;
5241}
5242
5243static void selinux_shm_free_security(struct shmid_kernel *shp)
5244{
5245 ipc_free_security(&shp->shm_perm);
5246}
5247
5248static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005251 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005252 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005253 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 isec = shp->shm_perm.security;
5256
Thomas Liu2bf49692009-07-14 12:14:09 -04005257 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005258 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 ad.u.ipc_id = shp->shm_perm.key;
5260
David Howells275bb412008-11-14 10:39:19 +11005261 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 SHM__ASSOCIATE, &ad);
5263}
5264
5265/* Note, at this point, shp is locked down */
5266static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5267{
5268 int perms;
5269 int err;
5270
Eric Paris828dfe12008-04-17 13:17:49 -04005271 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 case IPC_INFO:
5273 case SHM_INFO:
5274 /* No specific object, just general system-wide information. */
5275 return task_has_system(current, SYSTEM__IPC_INFO);
5276 case IPC_STAT:
5277 case SHM_STAT:
5278 perms = SHM__GETATTR | SHM__ASSOCIATE;
5279 break;
5280 case IPC_SET:
5281 perms = SHM__SETATTR;
5282 break;
5283 case SHM_LOCK:
5284 case SHM_UNLOCK:
5285 perms = SHM__LOCK;
5286 break;
5287 case IPC_RMID:
5288 perms = SHM__DESTROY;
5289 break;
5290 default:
5291 return 0;
5292 }
5293
Stephen Smalley6af963f2005-05-01 08:58:39 -07005294 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 return err;
5296}
5297
5298static int selinux_shm_shmat(struct shmid_kernel *shp,
5299 char __user *shmaddr, int shmflg)
5300{
5301 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303 if (shmflg & SHM_RDONLY)
5304 perms = SHM__READ;
5305 else
5306 perms = SHM__READ | SHM__WRITE;
5307
Stephen Smalley6af963f2005-05-01 08:58:39 -07005308 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309}
5310
5311/* Semaphore security operations */
5312static int selinux_sem_alloc_security(struct sem_array *sma)
5313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005315 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005316 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005317 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 int rc;
5319
5320 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5321 if (rc)
5322 return rc;
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 isec = sma->sem_perm.security;
5325
Thomas Liu2bf49692009-07-14 12:14:09 -04005326 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005327 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005328 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
David Howells275bb412008-11-14 10:39:19 +11005330 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 SEM__CREATE, &ad);
5332 if (rc) {
5333 ipc_free_security(&sma->sem_perm);
5334 return rc;
5335 }
5336 return 0;
5337}
5338
5339static void selinux_sem_free_security(struct sem_array *sma)
5340{
5341 ipc_free_security(&sma->sem_perm);
5342}
5343
5344static int selinux_sem_associate(struct sem_array *sma, int semflg)
5345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005347 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005348 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005349 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 isec = sma->sem_perm.security;
5352
Thomas Liu2bf49692009-07-14 12:14:09 -04005353 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005354 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 ad.u.ipc_id = sma->sem_perm.key;
5356
David Howells275bb412008-11-14 10:39:19 +11005357 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 SEM__ASSOCIATE, &ad);
5359}
5360
5361/* Note, at this point, sma is locked down */
5362static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5363{
5364 int err;
5365 u32 perms;
5366
Eric Paris828dfe12008-04-17 13:17:49 -04005367 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 case IPC_INFO:
5369 case SEM_INFO:
5370 /* No specific object, just general system-wide information. */
5371 return task_has_system(current, SYSTEM__IPC_INFO);
5372 case GETPID:
5373 case GETNCNT:
5374 case GETZCNT:
5375 perms = SEM__GETATTR;
5376 break;
5377 case GETVAL:
5378 case GETALL:
5379 perms = SEM__READ;
5380 break;
5381 case SETVAL:
5382 case SETALL:
5383 perms = SEM__WRITE;
5384 break;
5385 case IPC_RMID:
5386 perms = SEM__DESTROY;
5387 break;
5388 case IPC_SET:
5389 perms = SEM__SETATTR;
5390 break;
5391 case IPC_STAT:
5392 case SEM_STAT:
5393 perms = SEM__GETATTR | SEM__ASSOCIATE;
5394 break;
5395 default:
5396 return 0;
5397 }
5398
Stephen Smalley6af963f2005-05-01 08:58:39 -07005399 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 return err;
5401}
5402
5403static int selinux_sem_semop(struct sem_array *sma,
5404 struct sembuf *sops, unsigned nsops, int alter)
5405{
5406 u32 perms;
5407
5408 if (alter)
5409 perms = SEM__READ | SEM__WRITE;
5410 else
5411 perms = SEM__READ;
5412
Stephen Smalley6af963f2005-05-01 08:58:39 -07005413 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414}
5415
5416static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 u32 av = 0;
5419
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 av = 0;
5421 if (flag & S_IRUGO)
5422 av |= IPC__UNIX_READ;
5423 if (flag & S_IWUGO)
5424 av |= IPC__UNIX_WRITE;
5425
5426 if (av == 0)
5427 return 0;
5428
Stephen Smalley6af963f2005-05-01 08:58:39 -07005429 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430}
5431
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005432static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5433{
5434 struct ipc_security_struct *isec = ipcp->security;
5435 *secid = isec->sid;
5436}
5437
Eric Paris828dfe12008-04-17 13:17:49 -04005438static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
5440 if (inode)
5441 inode_doinit_with_dentry(inode, dentry);
5442}
5443
5444static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005445 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
David Howells275bb412008-11-14 10:39:19 +11005447 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005448 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005450 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
5452 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005453 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 if (error)
5455 return error;
5456 }
5457
David Howells275bb412008-11-14 10:39:19 +11005458 rcu_read_lock();
5459 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
5461 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005462 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005464 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005466 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005468 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005469 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005470 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005471 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005472 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 else
David Howells275bb412008-11-14 10:39:19 +11005474 goto invalid;
5475 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477 if (!sid)
5478 return 0;
5479
Al Viro04ff9702007-03-12 16:17:58 +00005480 error = security_sid_to_context(sid, value, &len);
5481 if (error)
5482 return error;
5483 return len;
David Howells275bb412008-11-14 10:39:19 +11005484
5485invalid:
5486 rcu_read_unlock();
5487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488}
5489
5490static int selinux_setprocattr(struct task_struct *p,
5491 char *name, void *value, size_t size)
5492{
5493 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005494 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005495 struct cred *new;
5496 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 int error;
5498 char *str = value;
5499
5500 if (current != p) {
5501 /* SELinux only allows a process to change its own
5502 security attributes. */
5503 return -EACCES;
5504 }
5505
5506 /*
5507 * Basic control over ability to set these attributes at all.
5508 * current == p, but we'll pass them separately in case the
5509 * above restriction is ever removed.
5510 */
5511 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005512 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005514 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005515 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005516 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005517 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005518 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005520 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 else
5522 error = -EINVAL;
5523 if (error)
5524 return error;
5525
5526 /* Obtain a SID for the context, if one was specified. */
5527 if (size && str[1] && str[1] != '\n') {
5528 if (str[size-1] == '\n') {
5529 str[size-1] = 0;
5530 size--;
5531 }
5532 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005533 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5534 if (!capable(CAP_MAC_ADMIN))
5535 return error;
5536 error = security_context_to_sid_force(value, size,
5537 &sid);
5538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 if (error)
5540 return error;
5541 }
5542
David Howellsd84f4f92008-11-14 10:39:23 +11005543 new = prepare_creds();
5544 if (!new)
5545 return -ENOMEM;
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 /* Permission checking based on the specified context is
5548 performed during the actual operation (execve,
5549 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005550 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 checks and may_create for the file creation checks. The
5552 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005553 tsec = new->security;
5554 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005556 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005558 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005559 error = may_create_key(sid, p);
5560 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005561 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005562 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005563 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005564 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005565 } else if (!strcmp(name, "current")) {
5566 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005568 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005569
David Howellsd84f4f92008-11-14 10:39:23 +11005570 /* Only allow single threaded processes to change context */
5571 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005572 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005573 error = security_bounded_transition(tsec->sid, sid);
5574 if (error)
5575 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
5578 /* Check permissions for the transition. */
5579 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005580 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005582 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
5584 /* Check for ptracing, and update the task SID if ok.
5585 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005586 ptsid = 0;
Oleg Nesterov58c23142013-12-23 17:45:01 -05005587 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02005588 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005589 if (tracer)
5590 ptsid = task_sid(tracer);
Oleg Nesterov58c23142013-12-23 17:45:01 -05005591 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
David Howellsd84f4f92008-11-14 10:39:23 +11005593 if (tracer) {
5594 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5595 PROCESS__PTRACE, NULL);
5596 if (error)
5597 goto abort_change;
5598 }
5599
5600 tsec->sid = sid;
5601 } else {
5602 error = -EINVAL;
5603 goto abort_change;
5604 }
5605
5606 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005608
5609abort_change:
5610 abort_creds(new);
5611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612}
5613
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005614static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5615{
5616 return security_sid_to_context(secid, secdata, seclen);
5617}
5618
David Howells7bf570d2008-04-29 20:52:51 +01005619static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005620{
5621 return security_context_to_sid(secdata, seclen, secid);
5622}
5623
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005624static void selinux_release_secctx(char *secdata, u32 seclen)
5625{
Paul Moore088999e2007-08-01 11:12:58 -04005626 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005627}
5628
David P. Quigley1ee65e32009-09-03 14:25:57 -04005629/*
5630 * called with inode->i_mutex locked
5631 */
5632static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5633{
5634 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5635}
5636
5637/*
5638 * called with inode->i_mutex locked
5639 */
5640static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5641{
5642 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5643}
5644
5645static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5646{
5647 int len = 0;
5648 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5649 ctx, true);
5650 if (len < 0)
5651 return len;
5652 *ctxlen = len;
5653 return 0;
5654}
Michael LeMayd7200242006-06-22 14:47:17 -07005655#ifdef CONFIG_KEYS
5656
David Howellsd84f4f92008-11-14 10:39:23 +11005657static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005658 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005659{
David Howellsd84f4f92008-11-14 10:39:23 +11005660 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005661 struct key_security_struct *ksec;
5662
5663 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5664 if (!ksec)
5665 return -ENOMEM;
5666
David Howellsd84f4f92008-11-14 10:39:23 +11005667 tsec = cred->security;
5668 if (tsec->keycreate_sid)
5669 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005670 else
David Howellsd84f4f92008-11-14 10:39:23 +11005671 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005672
David Howells275bb412008-11-14 10:39:19 +11005673 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005674 return 0;
5675}
5676
5677static void selinux_key_free(struct key *k)
5678{
5679 struct key_security_struct *ksec = k->security;
5680
5681 k->security = NULL;
5682 kfree(ksec);
5683}
5684
5685static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005686 const struct cred *cred,
5687 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005688{
5689 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005690 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005691 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005692
5693 /* if no specific permissions are requested, we skip the
5694 permission check. No serious, additional covert channels
5695 appear to be created. */
5696 if (perm == 0)
5697 return 0;
5698
David Howellsd84f4f92008-11-14 10:39:23 +11005699 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005700
5701 key = key_ref_to_ptr(key_ref);
5702 ksec = key->security;
5703
5704 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005705}
5706
David Howells70a5bb72008-04-29 01:01:26 -07005707static int selinux_key_getsecurity(struct key *key, char **_buffer)
5708{
5709 struct key_security_struct *ksec = key->security;
5710 char *context = NULL;
5711 unsigned len;
5712 int rc;
5713
5714 rc = security_sid_to_context(ksec->sid, &context, &len);
5715 if (!rc)
5716 rc = len;
5717 *_buffer = context;
5718 return rc;
5719}
5720
Michael LeMayd7200242006-06-22 14:47:17 -07005721#endif
5722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005724 .name = "selinux",
5725
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05005726 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5727 .binder_transaction = selinux_binder_transaction,
5728 .binder_transfer_binder = selinux_binder_transfer_binder,
5729 .binder_transfer_file = selinux_binder_transfer_file,
5730
Ingo Molnar9e488582009-05-07 19:26:19 +10005731 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005732 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005734 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 .capable = selinux_capable,
5736 .quotactl = selinux_quotactl,
5737 .quota_on = selinux_quota_on,
5738 .syslog = selinux_syslog,
5739 .vm_enough_memory = selinux_vm_enough_memory,
5740
5741 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
David Howellsa6f76f22008-11-14 10:39:24 +11005743 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005744 .bprm_committing_creds = selinux_bprm_committing_creds,
5745 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 .bprm_secureexec = selinux_bprm_secureexec,
5747
5748 .sb_alloc_security = selinux_sb_alloc_security,
5749 .sb_free_security = selinux_sb_free_security,
5750 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005751 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005752 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005753 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 .sb_statfs = selinux_sb_statfs,
5755 .sb_mount = selinux_mount,
5756 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005757 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005758 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005759 .sb_parse_opts_str = selinux_parse_opts_str,
5760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 .inode_alloc_security = selinux_inode_alloc_security,
5763 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005764 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 .inode_unlink = selinux_inode_unlink,
5768 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 .inode_rmdir = selinux_inode_rmdir,
5771 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 .inode_readlink = selinux_inode_readlink,
5774 .inode_follow_link = selinux_inode_follow_link,
5775 .inode_permission = selinux_inode_permission,
5776 .inode_setattr = selinux_inode_setattr,
5777 .inode_getattr = selinux_inode_getattr,
5778 .inode_setxattr = selinux_inode_setxattr,
5779 .inode_post_setxattr = selinux_inode_post_setxattr,
5780 .inode_getxattr = selinux_inode_getxattr,
5781 .inode_listxattr = selinux_inode_listxattr,
5782 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005783 .inode_getsecurity = selinux_inode_getsecurity,
5784 .inode_setsecurity = selinux_inode_setsecurity,
5785 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005786 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
5788 .file_permission = selinux_file_permission,
5789 .file_alloc_security = selinux_file_alloc_security,
5790 .file_free_security = selinux_file_free_security,
5791 .file_ioctl = selinux_file_ioctl,
5792 .file_mmap = selinux_file_mmap,
5793 .file_mprotect = selinux_file_mprotect,
5794 .file_lock = selinux_file_lock,
5795 .file_fcntl = selinux_file_fcntl,
5796 .file_set_fowner = selinux_file_set_fowner,
5797 .file_send_sigiotask = selinux_file_send_sigiotask,
5798 .file_receive = selinux_file_receive,
5799
Eric Paris828dfe12008-04-17 13:17:49 -04005800 .dentry_open = selinux_dentry_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005803 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005804 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005805 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005806 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005807 .kernel_act_as = selinux_kernel_act_as,
5808 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005809 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 .task_setpgid = selinux_task_setpgid,
5811 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005812 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005813 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005815 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005816 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 .task_setrlimit = selinux_task_setrlimit,
5818 .task_setscheduler = selinux_task_setscheduler,
5819 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005820 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 .task_kill = selinux_task_kill,
5822 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005823 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
5825 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005826 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5829 .msg_msg_free_security = selinux_msg_msg_free_security,
5830
5831 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5832 .msg_queue_free_security = selinux_msg_queue_free_security,
5833 .msg_queue_associate = selinux_msg_queue_associate,
5834 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5835 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5836 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5837
5838 .shm_alloc_security = selinux_shm_alloc_security,
5839 .shm_free_security = selinux_shm_free_security,
5840 .shm_associate = selinux_shm_associate,
5841 .shm_shmctl = selinux_shm_shmctl,
5842 .shm_shmat = selinux_shm_shmat,
5843
Eric Paris828dfe12008-04-17 13:17:49 -04005844 .sem_alloc_security = selinux_sem_alloc_security,
5845 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 .sem_associate = selinux_sem_associate,
5847 .sem_semctl = selinux_sem_semctl,
5848 .sem_semop = selinux_sem_semop,
5849
Eric Paris828dfe12008-04-17 13:17:49 -04005850 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Eric Paris828dfe12008-04-17 13:17:49 -04005852 .getprocattr = selinux_getprocattr,
5853 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005855 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005856 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005857 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005858 .inode_notifysecctx = selinux_inode_notifysecctx,
5859 .inode_setsecctx = selinux_inode_setsecctx,
5860 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005861
Eric Paris828dfe12008-04-17 13:17:49 -04005862 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 .unix_may_send = selinux_socket_unix_may_send,
5864
5865 .socket_create = selinux_socket_create,
5866 .socket_post_create = selinux_socket_post_create,
5867 .socket_bind = selinux_socket_bind,
5868 .socket_connect = selinux_socket_connect,
5869 .socket_listen = selinux_socket_listen,
5870 .socket_accept = selinux_socket_accept,
5871 .socket_sendmsg = selinux_socket_sendmsg,
5872 .socket_recvmsg = selinux_socket_recvmsg,
5873 .socket_getsockname = selinux_socket_getsockname,
5874 .socket_getpeername = selinux_socket_getpeername,
5875 .socket_getsockopt = selinux_socket_getsockopt,
5876 .socket_setsockopt = selinux_socket_setsockopt,
5877 .socket_shutdown = selinux_socket_shutdown,
5878 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005879 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5880 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 .sk_alloc_security = selinux_sk_alloc_security,
5882 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005883 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005884 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005885 .sock_graft = selinux_sock_graft,
5886 .inet_conn_request = selinux_inet_conn_request,
5887 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005888 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005889 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5890 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5891 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005892 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005893 .tun_dev_create = selinux_tun_dev_create,
5894 .tun_dev_post_create = selinux_tun_dev_post_create,
5895 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005896
5897#ifdef CONFIG_SECURITY_NETWORK_XFRM
5898 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5899 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5900 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005901 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005902 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5903 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005904 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005905 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005906 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005907 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005909
5910#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005911 .key_alloc = selinux_key_alloc,
5912 .key_free = selinux_key_free,
5913 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005914 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005915#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005916
5917#ifdef CONFIG_AUDIT
5918 .audit_rule_init = selinux_audit_rule_init,
5919 .audit_rule_known = selinux_audit_rule_known,
5920 .audit_rule_match = selinux_audit_rule_match,
5921 .audit_rule_free = selinux_audit_rule_free,
5922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923};
5924
5925static __init int selinux_init(void)
5926{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005927 if (!security_module_enable(&selinux_ops)) {
5928 selinux_enabled = 0;
5929 return 0;
5930 }
5931
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 if (!selinux_enabled) {
5933 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5934 return 0;
5935 }
5936
5937 printk(KERN_INFO "SELinux: Initializing.\n");
5938
5939 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005940 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005942 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5943
James Morris7cae7e22006-03-22 00:09:22 -08005944 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5945 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005946 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 avc_init();
5948
Eric Paris828dfe12008-04-17 13:17:49 -04005949 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 panic("SELinux: Unable to register with kernel.\n");
5951
Eric Paris828dfe12008-04-17 13:17:49 -04005952 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005953 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005954 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005955 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return 0;
5958}
5959
Al Viroe8c26252010-03-23 06:36:54 -04005960static void delayed_superblock_init(struct super_block *sb, void *unused)
5961{
5962 superblock_doinit(sb, NULL);
5963}
5964
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965void selinux_complete_init(void)
5966{
Eric Parisfadcdb42007-02-22 18:11:31 -05005967 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
5969 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005970 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005971 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972}
5973
5974/* SELinux requires early initialization in order to label
5975 all processes and objects when they are created. */
5976security_initcall(selinux_init);
5977
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005978#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
Paul Mooreeffad8d2008-01-29 08:49:27 -05005980static struct nf_hook_ops selinux_ipv4_ops[] = {
5981 {
5982 .hook = selinux_ipv4_postroute,
5983 .owner = THIS_MODULE,
5984 .pf = PF_INET,
5985 .hooknum = NF_INET_POST_ROUTING,
5986 .priority = NF_IP_PRI_SELINUX_LAST,
5987 },
5988 {
5989 .hook = selinux_ipv4_forward,
5990 .owner = THIS_MODULE,
5991 .pf = PF_INET,
5992 .hooknum = NF_INET_FORWARD,
5993 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005994 },
5995 {
5996 .hook = selinux_ipv4_output,
5997 .owner = THIS_MODULE,
5998 .pf = PF_INET,
5999 .hooknum = NF_INET_LOCAL_OUT,
6000 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002};
6003
6004#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6005
Paul Mooreeffad8d2008-01-29 08:49:27 -05006006static struct nf_hook_ops selinux_ipv6_ops[] = {
6007 {
6008 .hook = selinux_ipv6_postroute,
6009 .owner = THIS_MODULE,
6010 .pf = PF_INET6,
6011 .hooknum = NF_INET_POST_ROUTING,
6012 .priority = NF_IP6_PRI_SELINUX_LAST,
6013 },
6014 {
6015 .hook = selinux_ipv6_forward,
6016 .owner = THIS_MODULE,
6017 .pf = PF_INET6,
6018 .hooknum = NF_INET_FORWARD,
6019 .priority = NF_IP6_PRI_SELINUX_FIRST,
6020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021};
6022
6023#endif /* IPV6 */
6024
6025static int __init selinux_nf_ip_init(void)
6026{
6027 int err = 0;
6028
6029 if (!selinux_enabled)
6030 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006031
6032 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6033
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006034 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6035 if (err)
6036 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
6038#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006039 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6040 if (err)
6041 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044out:
6045 return err;
6046}
6047
6048__initcall(selinux_nf_ip_init);
6049
6050#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6051static void selinux_nf_ip_exit(void)
6052{
Eric Parisfadcdb42007-02-22 18:11:31 -05006053 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006055 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006057 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058#endif /* IPV6 */
6059}
6060#endif
6061
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006062#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
6064#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6065#define selinux_nf_ip_exit()
6066#endif
6067
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006068#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
6070#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006071static int selinux_disabled;
6072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073int selinux_disable(void)
6074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 if (ss_initialized) {
6076 /* Not permitted after initial policy load. */
6077 return -EINVAL;
6078 }
6079
6080 if (selinux_disabled) {
6081 /* Only do this once. */
6082 return -EINVAL;
6083 }
6084
6085 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6086
6087 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006088 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006090 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
Eric Parisaf8ff042009-09-20 21:23:01 -04006092 /* Try to destroy the avc node cache */
6093 avc_disable();
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 /* Unregister netfilter hooks. */
6096 selinux_nf_ip_exit();
6097
6098 /* Unregister selinuxfs. */
6099 exit_sel_fs();
6100
6101 return 0;
6102}
6103#endif